#!/bin/sh # the next line restarts using wish \ exec wish "$0" "$@" # Written 2005 Markus Triska triska@gmx.at # Public domain code. set boardsize 9 if {$argc == 1} { set boardsize [lindex $argv 0] } set linedist 28 set pipe [open "| gnugo --mode gtp --boardsize $boardsize" w+] fconfigure $pipe -buffering none proc x_to_letter {p} { return [lindex "A B C D E F G H J K L M N O P Q R S T" $p] } proc read_pipe {} { global pipe set reply [split [string trim [gets $pipe]]] set ignore [gets $pipe] return $reply } proc draw_pieces {} { global goban pipe puts $pipe "list_stones black\n" set blacks [read_pipe] puts $pipe "list_stones white\n" set whites [read_pipe] $goban delete pieces foreach b [lrange $blacks 1 end] { set cs [$goban coords $b] $goban create oval $cs -tags pieces -fill black } foreach w [lrange $whites 1 end] { set cs [$goban coords $w] $goban create oval $cs -tags pieces -fill white } } proc clicked_field {name} { global pipe puts $pipe "play black $name" set reply [read_pipe] if [string equal [lindex $reply 0] "?"] return puts $pipe "genmove white" set reply [read_pipe] puts "white: [lindex $reply 1]" draw_pieces } proc score {} { global pipe puts $pipe "final_score" puts "Score: [gets $pipe]" } proc draw_board {board} { global boardsize linedist set max [expr $boardsize * $linedist] for {set i 1} {$i <= $boardsize} {incr i} { set start [expr $linedist*$i] $board create line $linedist $start $max $start $board create line $start $linedist $start $max } for {set i 0} {$i < $boardsize} {incr i} { for {set j 0} {$j < $boardsize} {incr j} { set x1 [expr $linedist*$i + ($linedist/2)] set y1 [expr $linedist*$j + ($linedist/2)] set x2 [expr $x1 + $linedist] set y2 [expr $y1 + $linedist] set fieldname [join [list [x_to_letter $i] [expr $boardsize - $j]] ""] $board create rect $x1 $y1 $x2 $y2 -tags $fieldname -outline "" $board bind $fieldname "clicked_field $fieldname" } } } set f1 [frame .f1] set passb [button $f1.pass -command {clicked_field pass} -text Pass] set scoreb [button $f1.score -command score -text Score] set undob [button $f1.undo -command {puts $pipe undo;readpipe; draw_pieces} -text Undo] pack $passb -padx 5 -pady 5 -side left pack $scoreb -padx 5 -pady 5 -side left pack $undob -padx 5 -pady 5 -side left wm title . "GO considered" set gobansize [expr $linedist*($boardsize + 1)] set goban [canvas .goban -height $gobansize -width $gobansize] pack $goban -expand yes pack $f1 -expand yes draw_board $goban