home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / ObjectTcl-1.1 / examples / TicTacToe / Game.tcl next >
Encoding:
Text File  |  1995-06-30  |  3.9 KB  |  167 lines

  1. otclInterface Game {
  2.    constructor {mark player}
  3.  
  4.    method setOpponent {opponent}
  5.    method opponentRegistered {opponent}
  6.  
  7.    method iveMoved {row col}
  8.    method opponentMoved {row col}
  9.  
  10.    method getName {}
  11.  
  12.    method quit {}
  13.    method opponentQuit {}
  14. }
  15.  
  16. otclImplementation Game {
  17.    constructor {m p} {} {
  18.       set myMark $m
  19.       set player $p
  20.       if {$m == "X"} {
  21.          set opponentMark "O"
  22.       } else {
  23.          set opponentMark "X"
  24.       }
  25.  
  26.       set toplevel [toplevel ".a$nextToplevel"]
  27.       incr nextToplevel
  28.  
  29.       foreach row {0 1 2} {
  30.          pack append $toplevel [frame $toplevel.$row] {left fillx filly}
  31.          foreach col {0 1 2} {
  32.             button $toplevel.$row.$col -width 2
  33.             pack append $toplevel.$row $toplevel.$row.$col {top fillx}
  34.          }
  35.       }
  36.  
  37.       wm protocol $toplevel WM_DELETE_WINDOW "$this quit"
  38.  
  39.    }
  40.  
  41.    destructor {
  42.       destroy $toplevel
  43.    }
  44.  
  45.    method setOpponent {o} {
  46.       set opponent $o
  47.       $o opponentRegistered $this
  48.       wm title $toplevel "[$opponent getName]"
  49.       $this myGo
  50.    }
  51.  
  52.    method opponentRegistered {o} {
  53.       set opponent $o
  54.       wm title $toplevel "[$opponent getName]"
  55.       $this theirGo
  56.    }
  57.  
  58.    method iveMoved {row col} {
  59.       $toplevel.$row.$col configure -text $myMark
  60.       $toplevel.$row.$col configure -state disabled
  61.       $opponent opponentMoved $row $col
  62.       if {![$this hasPlayerOne $myMark]} {
  63.          $this theirGo
  64.       } else {
  65.          $this gameFinished
  66.       }
  67.    }
  68.  
  69.    method opponentMoved {row col} {
  70.       $toplevel.$row.$col configure -text $opponentMark
  71.       $toplevel.$row.$col configure -state disabled
  72.       if {![$this hasPlayerOne $opponentMark]} {
  73.          $this myGo
  74.       } else {
  75.          $this gameFinished
  76.       }
  77.    }
  78.  
  79.    method theirGo {} {
  80.       # set all inactive
  81.       foreach r {0 1 2} {
  82.          foreach c {0 1 2} {
  83.             $toplevel.$r.$c configure -command ""
  84.          }
  85.       }
  86.       $toplevel configure -cursor watch
  87.    }
  88.  
  89.    method myGo {} {
  90.       foreach r {0 1 2} {
  91.          foreach c {0 1 2} {
  92.             $toplevel.$r.$c configure -command "$this iveMoved $r $c"
  93.          }
  94.       }
  95.       $toplevel configure -cursor hand1
  96.    }
  97.  
  98.    method gameFinished {} {
  99.       # set all inactive
  100.       foreach r {0 1 2} {
  101.          foreach c {0 1 2} {
  102.             $toplevel.$r.$c configure -state disabled
  103.          }
  104.       }
  105.       $toplevel configure -cursor star
  106.    }
  107.  
  108.    # Checks to see if the player with mark "mark" has one
  109.    method hasPlayerOne {mark} {
  110.  
  111.       # Check columns
  112.       foreach c {0 1 2} {
  113.          if {([lindex [$toplevel.0.$c configure -text] 4] == $mark) && \
  114.              ([lindex [$toplevel.1.$c configure -text] 4] == $mark) && \
  115.              ([lindex [$toplevel.2.$c configure -text] 4] == $mark)} {
  116.             return 1
  117.          }
  118.       }
  119.  
  120.       # Check the rows
  121.       foreach r {0 1 2} {
  122.          if {([lindex [$toplevel.$r.0 configure -text] 4] == $mark) && \
  123.              ([lindex [$toplevel.$r.1 configure -text] 4] == $mark) && \
  124.              ([lindex [$toplevel.$r.2 configure -text] 4] == $mark)} {
  125.             return 1
  126.          }
  127.       }
  128.  
  129.       # Check to two diagnals
  130.       if {([lindex [$toplevel.0.0 configure -text] 4] == $mark) && \
  131.           ([lindex [$toplevel.1.1 configure -text] 4] == $mark) && \
  132.           ([lindex [$toplevel.2.2 configure -text] 4] == $mark)} {
  133.          return 1
  134.       }
  135.  
  136.       if {([lindex [$toplevel.0.2 configure -text] 4] == $mark) && \
  137.           ([lindex [$toplevel.1.1 configure -text] 4] == $mark) && \
  138.           ([lindex [$toplevel.2.0 configure -text] 4] == $mark)} {
  139.          return 1
  140.       }
  141.  
  142.       return 0
  143.    }
  144.  
  145.    method getName {} {
  146.       return [$player getName]
  147.    }
  148.  
  149.    method quit {} {
  150.       $opponent opponentQuit
  151.       otclDelete $this
  152.    }
  153.  
  154.    method opponentQuit {} {
  155.       otclDelete $this
  156.    }
  157.  
  158.    attribute toplevel
  159.    attribute myMark
  160.    attribute opponentMark
  161.    attribute opponent
  162.    attribute player
  163.  
  164.    classAttribute nextToplevel 0
  165. }
  166.  
  167.