1998-01-16 07:19:09 -05:00
|
|
|
|
#!/usr/local/bin/ruby
|
|
|
|
|
#----------------------> pretty simple othello game <-----------------------
|
|
|
|
|
# othello.rb
|
|
|
|
|
#
|
|
|
|
|
# version 0.3
|
|
|
|
|
# maeda shugo (shuto@po.aianet.ne.jp)
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
2004-10-11 00:51:21 -04:00
|
|
|
|
# Sep. 17, 1997 modified by Y. Shigehiro for tcltk library
|
|
|
|
|
# maeda shugo (shugo@po.aianet.ne.jp) <20><><EFBFBD>ˤ<EFBFBD><CBA4><EFBFBD>
|
1998-01-16 07:19:09 -05:00
|
|
|
|
# (ruby/tk <20>ǽ<C7BD><F1A4ABA4>Ƥ<EFBFBD><C6A4><EFBFBD>) ruby <20>Υ<EFBFBD><CEA5><EFBFBD><EFBFBD>ץ<EFBFBD><D7A5>ץ<EFBFBD><D7A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2004-10-11 00:51:21 -04:00
|
|
|
|
# http://www.aianet.or.jp/~shugo/ruby/othello.rb.gz
|
|
|
|
|
# <20><> tcltk <20>饤<EFBFBD>֥<EFBFBD><D6A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ȥ<EFBFBD><C8A4>褦<EFBFBD><E8A4A6>, <20><><EFBFBD><EFBFBD>Ū<EFBFBD><C5AA><EFBFBD>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD>Ƥߤޤ<DFA4><DEA4><EFBFBD>.
|
1998-01-16 07:19:09 -05:00
|
|
|
|
#
|
2004-10-11 00:51:21 -04:00
|
|
|
|
# <20>ʤ<EFBFBD><CAA4>٤<EFBFBD><D9A4><EFBFBD><EFBFBD>ꥸ<EFBFBD>ʥ<EFBFBD><CAA5><EFBFBD>Ʊ<EFBFBD><C6B1><EFBFBD>ˤʤ<CBA4><CAA4>褦<EFBFBD>ˤ<EFBFBD><CBA4>Ƥ<EFBFBD><C6A4><EFBFBD><EFBFBD>ޤ<EFBFBD>.
|
1998-01-16 07:19:09 -05:00
|
|
|
|
|
|
|
|
|
require "observer"
|
|
|
|
|
require "tcltk"
|
|
|
|
|
$ip = TclTkInterpreter.new()
|
|
|
|
|
$root = $ip.rootwidget()
|
|
|
|
|
$button, $canvas, $checkbutton, $frame, $label, $pack, $update, $wm =
|
2005-01-25 09:31:45 -05:00
|
|
|
|
$ip.commands().values_at(
|
1998-01-16 07:19:09 -05:00
|
|
|
|
"button", "canvas", "checkbutton", "frame", "label", "pack", "update", "wm")
|
|
|
|
|
|
|
|
|
|
class Othello
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
EMPTY = 0
|
|
|
|
|
BLACK = 1
|
|
|
|
|
WHITE = - BLACK
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
attr :in_com_turn
|
|
|
|
|
attr :game_over
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
class Board
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
include Observable
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
DIRECTIONS = [
|
2004-10-11 00:51:21 -04:00
|
|
|
|
[-1, -1], [-1, 0], [-1, 1],
|
|
|
|
|
[ 0, -1], [ 0, 1],
|
|
|
|
|
[ 1, -1], [ 1, 0], [ 1, 1]
|
1998-01-16 07:19:09 -05:00
|
|
|
|
]
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
2006-07-20 13:36:36 -04:00
|
|
|
|
attr_accessor :com_disk
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
def initialize(othello)
|
2004-10-11 00:51:21 -04:00
|
|
|
|
@othello = othello
|
|
|
|
|
reset
|
1998-01-16 07:19:09 -05:00
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
def notify_observers(*arg)
|
2004-10-11 00:51:21 -04:00
|
|
|
|
if @observer_peers != nil
|
|
|
|
|
super(*arg)
|
|
|
|
|
end
|
1998-01-16 07:19:09 -05:00
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
def reset
|
2004-10-11 00:51:21 -04:00
|
|
|
|
@data = [
|
|
|
|
|
[EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY],
|
|
|
|
|
[EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY],
|
|
|
|
|
[EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY],
|
|
|
|
|
[EMPTY, EMPTY, EMPTY, WHITE, BLACK, EMPTY, EMPTY, EMPTY],
|
|
|
|
|
[EMPTY, EMPTY, EMPTY, BLACK, WHITE, EMPTY, EMPTY, EMPTY],
|
|
|
|
|
[EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY],
|
|
|
|
|
[EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY],
|
|
|
|
|
[EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY]
|
|
|
|
|
]
|
|
|
|
|
changed
|
|
|
|
|
notify_observers
|
1998-01-16 07:19:09 -05:00
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
def man_disk
|
2004-10-11 00:51:21 -04:00
|
|
|
|
return - @com_disk
|
1998-01-16 07:19:09 -05:00
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
def other_disk(disk)
|
2004-10-11 00:51:21 -04:00
|
|
|
|
return - disk
|
1998-01-16 07:19:09 -05:00
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
def get_disk(row, col)
|
2004-10-11 00:51:21 -04:00
|
|
|
|
return @data[row][col]
|
1998-01-16 07:19:09 -05:00
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
def reverse_to(row, col, my_disk, dir_y, dir_x)
|
2004-10-11 00:51:21 -04:00
|
|
|
|
y = row
|
|
|
|
|
x = col
|
|
|
|
|
begin
|
|
|
|
|
y += dir_y
|
|
|
|
|
x += dir_x
|
|
|
|
|
if y < 0 || x < 0 || y > 7 || x > 7 ||
|
|
|
|
|
@data[y][x] == EMPTY
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
end until @data[y][x] == my_disk
|
|
|
|
|
begin
|
|
|
|
|
@data[y][x] = my_disk
|
|
|
|
|
changed
|
|
|
|
|
notify_observers(y, x)
|
|
|
|
|
y -= dir_y
|
|
|
|
|
x -= dir_x
|
|
|
|
|
end until y == row && x == col
|
1998-01-16 07:19:09 -05:00
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
def put_disk(row, col, disk)
|
2004-10-11 00:51:21 -04:00
|
|
|
|
@data[row][col] = disk
|
|
|
|
|
changed
|
|
|
|
|
notify_observers(row, col)
|
|
|
|
|
DIRECTIONS.each do |dir|
|
|
|
|
|
reverse_to(row, col, disk, *dir)
|
|
|
|
|
end
|
1998-01-16 07:19:09 -05:00
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
def count_disk(disk)
|
2004-10-11 00:51:21 -04:00
|
|
|
|
num = 0
|
|
|
|
|
@data.each do |rows|
|
|
|
|
|
rows.each do |d|
|
|
|
|
|
if d == disk
|
|
|
|
|
num += 1
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
return num
|
1998-01-16 07:19:09 -05:00
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
def count_point_to(row, col, my_disk, dir_y, dir_x)
|
2004-10-11 00:51:21 -04:00
|
|
|
|
return 0 if @data[row][col] != EMPTY
|
|
|
|
|
count = 0
|
|
|
|
|
loop do
|
|
|
|
|
row += dir_y
|
|
|
|
|
col += dir_x
|
|
|
|
|
break if row < 0 || col < 0 || row > 7 || col > 7
|
|
|
|
|
case @data[row][col]
|
|
|
|
|
when my_disk
|
|
|
|
|
return count
|
|
|
|
|
when other_disk(my_disk)
|
|
|
|
|
count += 1
|
|
|
|
|
when EMPTY
|
|
|
|
|
break
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
return 0
|
1998-01-16 07:19:09 -05:00
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
def count_point(row, col, my_disk)
|
2004-10-11 00:51:21 -04:00
|
|
|
|
count = 0
|
|
|
|
|
DIRECTIONS.each do |dir|
|
|
|
|
|
count += count_point_to(row, col, my_disk, *dir)
|
|
|
|
|
end
|
|
|
|
|
return count
|
1998-01-16 07:19:09 -05:00
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
def corner?(row, col)
|
2004-10-11 00:51:21 -04:00
|
|
|
|
return (row == 0 && col == 0) ||
|
|
|
|
|
(row == 0 && col == 7) ||
|
|
|
|
|
(row == 7 && col == 0) ||
|
|
|
|
|
(row == 7 && col == 7)
|
1998-01-16 07:19:09 -05:00
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
def search(my_disk)
|
2004-10-11 00:51:21 -04:00
|
|
|
|
max = 0
|
|
|
|
|
max_row = nil
|
|
|
|
|
max_col = nil
|
|
|
|
|
for row in 0 .. 7
|
|
|
|
|
for col in 0 .. 7
|
|
|
|
|
buf = count_point(row, col, my_disk)
|
|
|
|
|
if (corner?(row, col) && buf > 0) || max < buf
|
|
|
|
|
max = buf
|
|
|
|
|
max_row = row
|
|
|
|
|
max_col = col
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
return max_row, max_col
|
1998-01-16 07:19:09 -05:00
|
|
|
|
end
|
|
|
|
|
end #--------------------------> class Board ends here
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
class BoardView < TclTkWidget
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
BACK_GROUND_COLOR = "DarkGreen"
|
|
|
|
|
HILIT_BG_COLOR = "green"
|
|
|
|
|
BORDER_COLOR = "black"
|
|
|
|
|
BLACK_COLOR = "black"
|
|
|
|
|
WHITE_COLOR = "white"
|
|
|
|
|
STOP_COLOR = "red"
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
attr :left
|
|
|
|
|
attr :top
|
|
|
|
|
attr :right
|
|
|
|
|
attr :bottom
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
class Square
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
2004-10-11 00:51:21 -04:00
|
|
|
|
attr :oval, TRUE
|
|
|
|
|
attr :row
|
|
|
|
|
attr :col
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
2004-10-11 00:51:21 -04:00
|
|
|
|
def initialize(view, row, col)
|
|
|
|
|
@view = view
|
2009-03-05 22:56:38 -05:00
|
|
|
|
@id = @view.e("create rectangle",
|
2005-01-25 09:31:45 -05:00
|
|
|
|
*(view.tk_rect(view.left + col,
|
|
|
|
|
view.top + row,
|
|
|
|
|
view.left + col + 1,
|
|
|
|
|
view.top + row + 1) \
|
|
|
|
|
<< "-fill #{BACK_GROUND_COLOR}") )
|
2004-10-11 00:51:21 -04:00
|
|
|
|
@row = row
|
|
|
|
|
@col = col
|
|
|
|
|
@view.e("itemconfigure", @id,
|
|
|
|
|
"-width 0.5m -outline #{BORDER_COLOR}")
|
|
|
|
|
@view.e("bind", @id, "<Any-Enter>", TclTkCallback.new($ip, proc{
|
|
|
|
|
if @oval == nil
|
|
|
|
|
view.e("itemconfigure", @id, "-fill #{HILIT_BG_COLOR}")
|
|
|
|
|
end
|
|
|
|
|
}))
|
|
|
|
|
@view.e("bind", @id, "<Any-Leave>", TclTkCallback.new($ip, proc{
|
|
|
|
|
view.e("itemconfigure", @id, "-fill #{BACK_GROUND_COLOR}")
|
|
|
|
|
}))
|
|
|
|
|
@view.e("bind", @id, "<ButtonRelease-1>", TclTkCallback.new($ip,
|
|
|
|
|
proc{
|
|
|
|
|
view.click_square(self)
|
|
|
|
|
}))
|
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
2004-10-11 00:51:21 -04:00
|
|
|
|
def blink(color)
|
|
|
|
|
@view.e("itemconfigure", @id, "-fill #{color}")
|
|
|
|
|
$update.e()
|
|
|
|
|
sleep(0.1)
|
|
|
|
|
@view.e("itemconfigure", @id, "-fill #{BACK_GROUND_COLOR}")
|
|
|
|
|
end
|
1998-01-16 07:19:09 -05:00
|
|
|
|
end #-----------------------> class Square ends here
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
def initialize(othello, board)
|
2004-10-11 00:51:21 -04:00
|
|
|
|
super($ip, $root, $canvas)
|
|
|
|
|
@othello = othello
|
|
|
|
|
@board = board
|
|
|
|
|
@board.add_observer(self)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
2004-10-11 00:51:21 -04:00
|
|
|
|
@squares = Array.new(8)
|
|
|
|
|
for i in 0 .. 7
|
|
|
|
|
@squares[i] = Array.new(8)
|
|
|
|
|
end
|
|
|
|
|
@left = 1
|
|
|
|
|
@top = 0.5
|
|
|
|
|
@right = @left + 8
|
|
|
|
|
@bottom = @top + 8
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
2004-10-11 00:51:21 -04:00
|
|
|
|
i = self.e("create rectangle", *tk_rect(@left, @top, @right, @bottom))
|
|
|
|
|
self.e("itemconfigure", i,
|
|
|
|
|
"-width 1m -outline #{BORDER_COLOR} -fill #{BACK_GROUND_COLOR}")
|
1998-01-16 07:19:09 -05:00
|
|
|
|
|
2004-10-11 00:51:21 -04:00
|
|
|
|
for row in 0 .. 7
|
|
|
|
|
for col in 0 .. 7
|
|
|
|
|
@squares[row][col] = Square.new(self, row, col)
|
|
|
|
|
end
|
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
2004-10-11 00:51:21 -04:00
|
|
|
|
update
|
1998-01-16 07:19:09 -05:00
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
def tk_rect(left, top, right, bottom)
|
2004-10-11 00:51:21 -04:00
|
|
|
|
return left.to_s + "c", top.to_s + "c",
|
|
|
|
|
right.to_s + "c", bottom.to_s + "c"
|
1998-01-16 07:19:09 -05:00
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
def clear
|
2004-10-11 00:51:21 -04:00
|
|
|
|
each_square do |square|
|
|
|
|
|
if square.oval != nil
|
|
|
|
|
self.e("delete", square.oval)
|
|
|
|
|
square.oval = nil
|
|
|
|
|
end
|
|
|
|
|
end
|
1998-01-16 07:19:09 -05:00
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
def draw_disk(row, col, disk)
|
2004-10-11 00:51:21 -04:00
|
|
|
|
if disk == EMPTY
|
|
|
|
|
if @squares[row][col].oval != nil
|
|
|
|
|
self.e("delete", @squares[row][col].oval)
|
|
|
|
|
@squares[row][col].oval = nil
|
|
|
|
|
end
|
|
|
|
|
return
|
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
2004-10-11 00:51:21 -04:00
|
|
|
|
$update.e()
|
|
|
|
|
sleep(0.05)
|
|
|
|
|
oval = @squares[row][col].oval
|
|
|
|
|
if oval == nil
|
|
|
|
|
oval = self.e("create oval", *tk_rect(@left + col + 0.2,
|
|
|
|
|
@top + row + 0.2,
|
|
|
|
|
@left + col + 0.8,
|
|
|
|
|
@top + row + 0.8))
|
|
|
|
|
@squares[row][col].oval = oval
|
|
|
|
|
end
|
|
|
|
|
case disk
|
|
|
|
|
when BLACK
|
|
|
|
|
color = BLACK_COLOR
|
|
|
|
|
when WHITE
|
|
|
|
|
color = WHITE_COLOR
|
|
|
|
|
else
|
|
|
|
|
fail format("Unknown disk type: %d", disk)
|
|
|
|
|
end
|
|
|
|
|
self.e("itemconfigure", oval, "-outline #{color} -fill #{color}")
|
1998-01-16 07:19:09 -05:00
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
def update(row = nil, col = nil)
|
2004-10-11 00:51:21 -04:00
|
|
|
|
if row && col
|
|
|
|
|
draw_disk(row, col, @board.get_disk(row, col))
|
|
|
|
|
else
|
|
|
|
|
each_square do |square|
|
|
|
|
|
draw_disk(square.row, square.col,
|
|
|
|
|
@board.get_disk(square.row, square.col))
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
@othello.show_point
|
1998-01-16 07:19:09 -05:00
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
def each_square
|
2004-10-11 00:51:21 -04:00
|
|
|
|
@squares.each do |rows|
|
|
|
|
|
rows.each do |square|
|
|
|
|
|
yield(square)
|
|
|
|
|
end
|
|
|
|
|
end
|
1998-01-16 07:19:09 -05:00
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
def click_square(square)
|
2004-10-11 00:51:21 -04:00
|
|
|
|
if @othello.in_com_turn || @othello.game_over ||
|
|
|
|
|
@board.count_point(square.row,
|
|
|
|
|
square.col,
|
|
|
|
|
@board.man_disk) == 0
|
|
|
|
|
square.blink(STOP_COLOR)
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
@board.put_disk(square.row, square.col, @board.man_disk)
|
|
|
|
|
@othello.com_turn
|
1998-01-16 07:19:09 -05:00
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
private :draw_disk
|
|
|
|
|
public :update
|
|
|
|
|
end #----------------------> class BoardView ends here
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
def initialize
|
|
|
|
|
@msg_label = TclTkWidget.new($ip, $root, $label)
|
|
|
|
|
$pack.e(@msg_label)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
@board = Board.new(self)
|
|
|
|
|
@board_view = BoardView.new(self, @board)
|
|
|
|
|
#### added by Y. Shigehiro
|
|
|
|
|
## board_view <20><><EFBFBD>礭<EFBFBD><E7A4AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ꤹ<EFBFBD><EAA4B9>.
|
|
|
|
|
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 <20><><EFBFBD><EFBFBD><EFBFBD>ꤹ<EFBFBD><EAA4B9>.
|
|
|
|
|
@board_view.e("configure -scrollregion {", @board_view.e("bbox all"),
|
2004-10-11 00:51:21 -04:00
|
|
|
|
"}")
|
1998-01-16 07:19:09 -05:00
|
|
|
|
#### <20><><EFBFBD><EFBFBD><EFBFBD>ޤ<EFBFBD>
|
|
|
|
|
$pack.e(@board_view, "-fill both -expand true")
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
panel = TclTkWidget.new($ip, $root, $frame)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
@play_black = TclTkWidget.new($ip, panel, $checkbutton,
|
|
|
|
|
"-text {com is black} -command", TclTkCallback.new($ip, proc{
|
2004-10-11 00:51:21 -04:00
|
|
|
|
switch_side
|
1998-01-16 07:19:09 -05:00
|
|
|
|
}))
|
|
|
|
|
$pack.e(@play_black, "-side left")
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
quit = TclTkWidget.new($ip, panel, $button, "-text Quit -command",
|
2004-10-11 00:51:21 -04:00
|
|
|
|
TclTkCallback.new($ip, proc{
|
|
|
|
|
exit
|
1998-01-16 07:19:09 -05:00
|
|
|
|
}))
|
|
|
|
|
$pack.e(quit, "-side right -fill x")
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
reset = TclTkWidget.new($ip, panel, $button, "-text Reset -command",
|
2004-10-11 00:51:21 -04:00
|
|
|
|
TclTkCallback.new($ip, proc{
|
|
|
|
|
reset_game
|
1998-01-16 07:19:09 -05:00
|
|
|
|
}))
|
|
|
|
|
$pack.e(reset, "-side right -fill x")
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
$pack.e(panel, "-side bottom -fill x")
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
# root = Tk.root
|
|
|
|
|
$wm.e("title", $root, "Othello")
|
|
|
|
|
$wm.e("iconname", $root, "Othello")
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
@board.com_disk = WHITE
|
|
|
|
|
@game_over = FALSE
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
TclTk.mainloop
|
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
def switch_side
|
|
|
|
|
if @in_com_turn
|
2004-10-11 00:51:21 -04:00
|
|
|
|
@play_black.e("toggle")
|
1998-01-16 07:19:09 -05:00
|
|
|
|
else
|
2004-10-11 00:51:21 -04:00
|
|
|
|
@board.com_disk = @board.man_disk
|
|
|
|
|
com_turn unless @game_over
|
1998-01-16 07:19:09 -05:00
|
|
|
|
end
|
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
def reset_game
|
|
|
|
|
if @board.com_disk == BLACK
|
2004-10-11 00:51:21 -04:00
|
|
|
|
@board.com_disk = WHITE
|
|
|
|
|
@play_black.e("toggle")
|
1998-01-16 07:19:09 -05:00
|
|
|
|
end
|
|
|
|
|
@board_view.clear
|
|
|
|
|
@board.reset
|
|
|
|
|
$wm.e("title", $root, "Othello")
|
|
|
|
|
@game_over = FALSE
|
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
|
def com_turn
|
|
|
|
|
@in_com_turn = TRUE
|
|
|
|
|
$update.e()
|
|
|
|
|
sleep(0.5)
|
|
|
|
|
begin
|
2004-10-11 00:51:21 -04:00
|
|
|
|
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)
|
1998-01-16 07:19:09 -05:00
|
|
|
|
end while @board.search(@board.man_disk) == [nil, nil]
|
|
|
|
|
@in_com_turn = FALSE
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def show_point
|
|
|
|
|
black = @board.count_disk(BLACK)
|
|
|
|
|
white = @board.count_disk(WHITE)
|
|
|
|
|
@msg_label.e("configure -text",
|
2004-10-11 00:51:21 -04:00
|
|
|
|
%Q/{#{format("BLACK: %.2d WHITE: %.2d", black, white)}}/)
|
1998-01-16 07:19:09 -05:00
|
|
|
|
end
|
|
|
|
|
end #----------------------> class Othello ends here
|
|
|
|
|
|
|
|
|
|
Othello.new
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------> othello.rb ends here
|