Namespace
Methods
C
N
R
S
Constants
EMPTY = 0
 
BLACK = 1
 
WHITE = - BLACK
 
Attributes
[R] game_over
[R] in_com_turn
Class Public methods
new()
# File ext/tk/sample/tcltklib/sample2.rb, line 339
   def initialize
      @msg_label = TclTkWidget.new($ip, $root, $label)
      $pack.e(@msg_label)

      @board = Board.new(self)
      @board_view = BoardView.new(self, @board)
      #### added by Y. Shigehiro
      ## board_view の大きさを設定する.
      x1, y1, x2, y2 = @board_view.e("bbox all").split(/ /).collect{|i| i.to_f}
      @board_view.e("configure -width", x2 - x1)
      @board_view.e("configure -height", y2 - y1)
      ## scrollregion を設定する.
      @board_view.e("configure -scrollregion {", @board_view.e("bbox all"),
         "}")
      #### ここまで
      $pack.e(@board_view, "-fill both -expand true")

      panel = TclTkWidget.new($ip, $root, $frame)

      @play_black = TclTkWidget.new($ip, panel, $checkbutton,
        "-text {com is black} -command", TclTkCallback.new($ip, proc{
         switch_side
      }))
      $pack.e(@play_black, "-side left")

      quit = TclTkWidget.new($ip, panel, $button, "-text Quit -command",
         TclTkCallback.new($ip, proc{
         exit
      }))
      $pack.e(quit, "-side right -fill x")

      reset = TclTkWidget.new($ip, panel, $button, "-text Reset -command",
         TclTkCallback.new($ip, proc{
         reset_game
      }))
      $pack.e(reset, "-side right -fill x")

      $pack.e(panel, "-side bottom -fill x")

#      root = Tk.root
      $wm.e("title", $root, "Othello")
      $wm.e("iconname", $root, "Othello")

      @board.com_disk = WHITE
      @game_over = FALSE

      TclTk.mainloop
   end
Instance Public methods
com_turn()
# File ext/tk/sample/tcltklib/sample2.rb, line 408
def com_turn
   @in_com_turn = TRUE
   $update.e()
   sleep(0.5)
   begin
      com_disk = @board.count_disk(@board.com_disk)
      man_disk = @board.count_disk(@board.man_disk)
      if @board.count_disk(EMPTY) == 0
         if man_disk == com_disk
            $wm.e("title", $root, "{Othello - Draw!}")
         elsif man_disk > com_disk
            $wm.e("title", $root, "{Othello - You Win!}")
         else
            $wm.e("title", $root, "{Othello - You Loose!}")
         end
         @game_over = TRUE
         break
      elsif com_disk == 0
         $wm.e("title", $root, "{Othello - You Win!}")
         @game_over = TRUE
         break
      elsif man_disk == 0
         $wm.e("title", $root, "{Othello - You Loose!}")
         @game_over = TRUE
         break
      end
      row, col = @board.search(@board.com_disk)
      break if row == nil || col == nil
      @board.put_disk(row, col, @board.com_disk)
   end while @board.search(@board.man_disk) == [nil, nil]
   @in_com_turn = FALSE
end
reset_game()
# File ext/tk/sample/tcltklib/sample2.rb, line 397
def reset_game
   if @board.com_disk == BLACK
      @board.com_disk = WHITE
      @play_black.e("toggle")
   end
   @board_view.clear
   @board.reset
   $wm.e("title", $root, "Othello")
   @game_over = FALSE
end
show_point()
# File ext/tk/sample/tcltklib/sample2.rb, line 441
def show_point
   black = @board.count_disk(BLACK)
   white = @board.count_disk(WHITE)
   @msg_label.e("configure -text",
      %Q{#{format("BLACK: %.2d    WHITE: %.2d", black, white)}}/)
end
switch_side()
# File ext/tk/sample/tcltklib/sample2.rb, line 388
def switch_side
   if @in_com_turn
      @play_black.e("toggle")
   else
      @board.com_disk = @board.man_disk
      com_turn unless @game_over
   end
end