Class: Othello

Inherits:
Object show all
Defined in:
sample/tcltklib/sample2.rb

Defined Under Namespace

Classes: Board, BoardView

Constant Summary collapse

EMPTY =
0
BLACK =
1
WHITE =
- BLACK

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOthello

———————-> class BoardView ends here



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File '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 Attribute Details

#game_overObject (readonly)

Returns the value of attribute game_over



32
33
34
# File 'sample/tcltklib/sample2.rb', line 32

def game_over
  @game_over
end

#in_com_turnObject (readonly)

Returns the value of attribute in_com_turn



31
32
33
# File 'sample/tcltklib/sample2.rb', line 31

def in_com_turn
  @in_com_turn
end

Instance Method Details

#com_turnObject



408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# File '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_gameObject



397
398
399
400
401
402
403
404
405
406
# File '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_pointObject



441
442
443
444
445
446
# File '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_sideObject



388
389
390
391
392
393
394
395
# File '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