home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / tcl / 1836 next >
Encoding:
Internet Message Format  |  1992-11-15  |  9.8 KB

  1. Path: sparky!uunet!know!cass.ma02.bull.com!think.com!rpi!zaphod.mps.ohio-state.edu!malgudi.oar.net!news.ans.net!newsgate.watson.ibm.com!yktnews!admin!ghostwind!shmdgljd
  2. From: shmdgljd@rchland.vnet.ibm.com (Jay Schmidgall)
  3. Newsgroups: comp.lang.tcl
  4. Subject: Re: Announcing tkreversi!!!
  5. Keywords: game reversi
  6. Message-ID: <1992Nov15.205146.25235@rchland.ibm.com>
  7. Date: 15 Nov 92 20:51:46 GMT
  8. References: <1e1jvlINNd8a@agate.berkeley.edu>
  9. Sender: news@rchland.ibm.com
  10. Reply-To: jay@vnet.ibm.com
  11. Organization: IBM Rochester, MN
  12. Lines: 300
  13. Disclaimer: This posting represents the poster's views, not necessarily those of IBM
  14. Nntp-Posting-Host: ghostwind.rchland.ibm.com
  15.  
  16. As it happens, I was mucking with a tk reversi as well as an exercise in
  17. learning how canvas did things.  It handles resizing and provides a nice
  18. flash for illegal moves and hints, plus no reliance on bitmaps.
  19.  
  20. Anyways, it was relatively straightforward to plug my canvas code into
  21. Joel's reversi code, so here's a patch that'll let you check it out.
  22.  
  23. *** reversi.tcl.old    Sun Nov 15 12:45:10 1992
  24. --- reversi.tcl    Sun Nov 15 14:49:24 1992
  25. ***************
  26. *** 1,10 ****
  27.   proc makeBoard {f width height} {
  28.     global $f.white.IsRobot $f.black.IsRobot
  29.     frame $f.menu
  30.     frame $f.msg
  31.     frame $f.board
  32.     frame $f.stat -bd 10
  33. !   pack append $f $f.menu {top frame w} $f.msg top $f.stat top $f.board top
  34.   
  35.     menubutton $f.menu.mb -text "Options" -menu $f.menu.m -relief raised
  36.     pack append $f.menu $f.menu.mb left
  37. --- 1,12 ----
  38.   proc makeBoard {f width height} {
  39.     global $f.white.IsRobot $f.black.IsRobot
  40. +   wm maxsize $f 1280 1024
  41.     frame $f.menu
  42.     frame $f.msg
  43.     frame $f.board
  44.     frame $f.stat -bd 10
  45. !   pack append $f $f.menu {top frame w} $f.msg top $f.stat top \
  46. !       $f.board { top expand fill }
  47.   
  48.     menubutton $f.menu.mb -text "Options" -menu $f.menu.m -relief raised
  49.     pack append $f.menu $f.menu.mb left
  50. ***************
  51. *** 18,28 ****
  52.     $f.menu.m add command -label "Quit..." -command "quit Reversi"
  53.   
  54.     label $f.msg.l1
  55. !   label $f.msg.l2
  56.     label $f.stat.l1
  57.     label $f.stat.l2
  58.     label $f.stat.l3
  59. !   pack append $f.msg $f.msg.l1 left $f.msg.l2 left
  60.     pack append $f.stat $f.stat.l1 top $f.stat.l2 top $f.stat.l3 top
  61.     makeButtons $f $width $height
  62.     set $f.white.IsRobot 0
  63. --- 20,30 ----
  64.     $f.menu.m add command -label "Quit..." -command "quit Reversi"
  65.   
  66.     label $f.msg.l1
  67. !   canvas $f.msg.c -width 43 -height 43
  68.     label $f.stat.l1
  69.     label $f.stat.l2
  70.     label $f.stat.l3
  71. !   pack append $f.msg $f.msg.l1 left $f.msg.c left
  72.     pack append $f.stat $f.stat.l1 top $f.stat.l2 top $f.stat.l3 top
  73.     makeButtons $f $width $height
  74.     set $f.white.IsRobot 0
  75. ***************
  76. *** 31,47 ****
  77.   }
  78.   
  79.   proc makeButtons {f width height} {
  80. !   global $f.width $f.height $f.maxMoves $f.moveCount
  81.     set $f.maxMoves [expr {$width * $height}]
  82.     set $f.moveCount 0
  83.     set $f.width $width
  84.     set $f.height $height
  85.     for {set i 0} {$i < $height} {incr i} {
  86. -     frame $f.board.$i
  87. -     pack append $f.board $f.board.$i top
  88.       for {set j 0} {$j < $width} {incr j} {
  89. !       button $f.board.$i.$j -command "after 10 move $f $i $j"
  90. !       pack append $f.board.$i $f.board.$i.$j left
  91.         global $f.$i.$j.white.nbr
  92.         set $f.$i.$j.white.nbr 0
  93.         global $f.$i.$j.black.nbr
  94. --- 33,63 ----
  95.   }
  96.   
  97.   proc makeButtons {f width height} {
  98. !   global $f.width $f.height $f.maxMoves $f.moveCount board
  99.     set $f.maxMoves [expr {$width * $height}]
  100.     set $f.moveCount 0
  101.     set $f.width $width
  102.     set $f.height $height
  103. +   set board(height) [expr 328+($height-8)*40]
  104. +   set board(width)  [expr 328+($width-8)*40]
  105. +   canvas $f.board.c -background {black} \
  106. +     -confine {true} \
  107. +     -width $board(width) \
  108. +     -height $board(height)
  109. +   bind $f.board.c <Configure> {
  110. +     set scalex [expr { "%w.0" / "$board(width).0" }]
  111. +     set scaley [expr { "%h.0" / "$board(height).0" }]
  112. +     %W scale all 0 0 $scalex $scaley
  113. +     set board(width) %w
  114. +     set board(height) %h
  115. +   }
  116. +   pack append $f.board $f.board.c { top expand fillx filly }
  117.     for {set i 0} {$i < $height} {incr i} {
  118.       for {set j 0} {$j < $width} {incr j} {
  119. !       $f.board.c create rect [expr $j*40+4] [expr $i*40+4] \
  120. !     [expr $j*40+43] [expr $i*40+43] -tags "$i,$j" -fill limegreen
  121. !       $f.board.c bind $i,$j <1> " move $f $i $j "
  122.         global $f.$i.$j.white.nbr
  123.         set $f.$i.$j.white.nbr 0
  124.         global $f.$i.$j.black.nbr
  125. ***************
  126. *** 101,118 ****
  127.     setBoard $f
  128.   }
  129.   
  130. ! proc flashSquare {f row col} {
  131. !   #set orig [get $f $row $col]
  132. !   #makeBlack $f $row $col; update
  133. !   #makeWhite $f $row $col; update
  134. !   #makeBlack $f $row $col; update
  135. !   #makeWhite $f $row $col; update
  136. !   #makeColor $f $row $col $orig; update
  137. !   #$f.board.$row.$col config -rel sunken
  138. !   #after 1000 $f.board.$row.$col config -rel raised
  139. !   $f.board.$row.$col flash
  140. !   $f.board.$row.$col activate
  141. !   after 4000 $f.board.$row.$col deactivate
  142.   }
  143.   
  144.   proc setBoard {f} {
  145. --- 117,133 ----
  146.     setBoard $f
  147.   }
  148.   
  149. ! proc flashSquare { {f} {row} {col} {color red}} {
  150. !   set bound [$f.board.c bbox $row,$col]
  151. !   set x1 [expr [lindex $bound 0]+2]
  152. !   set y1 [expr [lindex $bound 1]+2]
  153. !   set x2 [expr [lindex $bound 2]-2]
  154. !   set y2 [expr [lindex $bound 3]-2]
  155. !     
  156. !   set warn1 [$f.board.c create rect $x1 $y1 $x2 $y2 \
  157. !      -fill $color -width 3 -tag warning]
  158. !   update
  159. !   after 100 $f.board.c delete $warn1
  160.   }
  161.   
  162.   proc setBoard {f} {
  163. ***************
  164. *** 124,129 ****
  165. --- 139,145 ----
  166.     set centerY [expr {$width / 2}]
  167.     set $f.white.count 0
  168.     set $f.black.count 0
  169. +   $f.board.c delete piece
  170.     makeBlack $f [expr {$centerX - 1}] $centerY
  171.     makeBlack $f $centerX [expr {$centerY - 1}]
  172.     makeWhite $f [expr {$centerX - 1}] [expr {$centerY - 1}]
  173. ***************
  174. *** 162,167 ****
  175. --- 178,196 ----
  176.     globIncr $f.[expr {$row - 1}].[expr {$col + 1}].$color.nbr $inc
  177.   }
  178.   
  179. + proc drawPiece { {f} {color} {row} {col} } {
  180. +   set bound [$f.board.c coords $row,$col]
  181. +   set x1 [expr [lindex $bound 0]+3]
  182. +   set y1 [expr [lindex $bound 1]+3]
  183. +   set x2 [expr [lindex $bound 2]-3]
  184. +   set y2 [expr [lindex $bound 3]-3]
  185. +   $f.board.c delete "o,$row,$col"
  186. +   $f.board.c create oval $x1 $y1 $x2 $y2 -tags "piece o,$row,$col" \
  187. +     -fill $color
  188. +   $f.board.c bind <1> "o,$row,$col" "flashSquare $f $row $col"  
  189. +   update  
  190. + }
  191.   proc makeWhite {f row col} {
  192.     global sourceDir
  193.     global $f.white.count $f.black.count
  194. ***************
  195. *** 169,175 ****
  196.     if {[get $f $row $col] == "black"} {doIncr $f $row $col black -1; doIncr $f $row $col white}
  197.     if {[get $f $row $col] == "empty"} {doIncr $f $row $col white}
  198.     set $f.board.$row.$col.color white
  199. !   $f.board.$row.$col config -bitmap "@$sourceDir/bitmaps/white.bm"
  200.   }
  201.   
  202.   proc makeBlack {f row col} {
  203. --- 198,204 ----
  204.     if {[get $f $row $col] == "black"} {doIncr $f $row $col black -1; doIncr $f $row $col white}
  205.     if {[get $f $row $col] == "empty"} {doIncr $f $row $col white}
  206.     set $f.board.$row.$col.color white
  207. !   drawPiece $f white $row $col
  208.   }
  209.   
  210.   proc makeBlack {f row col} {
  211. ***************
  212. *** 179,185 ****
  213.     if {[get $f $row $col] == "white"} {doIncr $f $row $col white -1; doIncr $f $row $col black}
  214.     if {[get $f $row $col] == "empty"} {doIncr $f $row $col black}
  215.     set $f.board.$row.$col.color black
  216. !   $f.board.$row.$col config -bitmap "@$sourceDir/bitmaps/black.bm"
  217.   }
  218.   
  219.   proc makeEmpty {f row col} {
  220. --- 208,214 ----
  221.     if {[get $f $row $col] == "white"} {doIncr $f $row $col white -1; doIncr $f $row $col black}
  222.     if {[get $f $row $col] == "empty"} {doIncr $f $row $col black}
  223.     set $f.board.$row.$col.color black
  224. !   drawPiece $f black $row $col
  225.   }
  226.   
  227.   proc makeEmpty {f row col} {
  228. ***************
  229. *** 189,195 ****
  230.     if {[get $f $row $col] == "black"} {incr $f.black.count -1}
  231.     if {[get $f $row $col] == "white"} {incr $f.white.count -1}
  232.     set $f.board.$row.$col.color empty
  233. -   $f.board.$row.$col config -bitmap "@$sourceDir/bitmaps/empty.bm"
  234.   }
  235.   
  236.   proc makeColor {f row col color} {
  237. --- 218,223 ----
  238. ***************
  239. *** 388,394 ****
  240.       $f.stat.l1 config -text "No moves available!"
  241.       return
  242.     }
  243. !   flashSquare $f [lindex $move 0] [lindex $move 1]
  244.   }
  245.   
  246.   proc distToEdge {f row col} {
  247. --- 416,422 ----
  248.       $f.stat.l1 config -text "No moves available!"
  249.       return
  250.     }
  251. !   flashSquare $f [lindex $move 0] [lindex $move 1] yellow
  252.   }
  253.   
  254.   proc distToEdge {f row col} {
  255. ***************
  256. *** 475,481 ****
  257.       } else {
  258.         makeBlack $f $row $col
  259.       }
  260. !     flashSquare $f $row $col
  261.       flipList $f $flips
  262.       lappend $f.moveList [list $row $col] $flips $turn
  263.       #echo [set $f.moveList]
  264. --- 503,509 ----
  265.       } else {
  266.         makeBlack $f $row $col
  267.       }
  268. !     flashSquare $f $row $col yellow
  269.       flipList $f $flips
  270.       lappend $f.moveList [list $row $col] $flips $turn
  271.       #echo [set $f.moveList]
  272. ***************
  273. *** 483,490 ****
  274.       set turn [opposite $turn]
  275.       set $f.turn $turn
  276.       $f.stat.l1 config -text ""
  277. !   } else {
  278.       $f.stat.l1 config -text "Illegal move!"
  279.       return
  280.     }
  281.     setStatLines $f
  282. --- 511,519 ----
  283.       set turn [opposite $turn]
  284.       set $f.turn $turn
  285.       $f.stat.l1 config -text ""
  286. !   } else {  
  287.       $f.stat.l1 config -text "Illegal move!"
  288. +     flashSquare $f $row $col
  289.       return
  290.     }
  291.     setStatLines $f
  292. ***************
  293. *** 528,534 ****
  294.     set turn [set $f.turn]
  295.     $f.stat.l2 config -text "White: [set $f.white.count]"
  296.     $f.stat.l3 config -text "Black: [set $f.black.count]"
  297. !   $f.msg.l2 config -bitmap "@$sourceDir/bitmaps/$turn.bm"
  298.     $f.msg.l1 config -text "$turn's turn"
  299.     update
  300.   }
  301. --- 557,564 ----
  302.     set turn [set $f.turn]
  303.     $f.stat.l2 config -text "White: [set $f.white.count]"
  304.     $f.stat.l3 config -text "Black: [set $f.black.count]"
  305. !   $f.msg.c dtag all
  306. !   $f.msg.c create oval 3 3 40 40 -fill $turn -tag ind
  307.     $f.msg.l1 config -text "$turn's turn"
  308.     update
  309.   }
  310.  
  311. -- 
  312. : jay          jay@vnet.ibm.com    My opinions and ideas, not my employer's.
  313. : shmdgljd@rchland.vnet.ibm.com    (c) Copyright 1992.  All rights reserved.
  314.