diff options
Diffstat (limited to 'ext/tk/sample/tcltklib')
-rw-r--r-- | ext/tk/sample/tcltklib/sample2.rb | 104 |
1 files changed, 52 insertions, 52 deletions
diff --git a/ext/tk/sample/tcltklib/sample2.rb b/ext/tk/sample/tcltklib/sample2.rb index 110e81ebc4..f187d6ce1d 100644 --- a/ext/tk/sample/tcltklib/sample2.rb +++ b/ext/tk/sample/tcltklib/sample2.rb @@ -23,37 +23,37 @@ $button, $canvas, $checkbutton, $frame, $label, $pack, $update, $wm = "button", "canvas", "checkbutton", "frame", "label", "pack", "update", "wm") class Othello - + EMPTY = 0 BLACK = 1 WHITE = - BLACK - + attr :in_com_turn attr :game_over - + class Board - + include Observable - + DIRECTIONS = [ [-1, -1], [-1, 0], [-1, 1], [ 0, -1], [ 0, 1], [ 1, -1], [ 1, 0], [ 1, 1] ] - + attr_accessor :com_disk - + def initialize(othello) @othello = othello reset end - + def notify_observers(*arg) if @observer_peers != nil super(*arg) end end - + def reset @data = [ [EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY], @@ -68,19 +68,19 @@ class Othello changed notify_observers end - + def man_disk return - @com_disk end - + def other_disk(disk) return - disk end - + def get_disk(row, col) return @data[row][col] end - + def reverse_to(row, col, my_disk, dir_y, dir_x) y = row x = col @@ -100,7 +100,7 @@ class Othello x -= dir_x end until y == row && x == col end - + def put_disk(row, col, disk) @data[row][col] = disk changed @@ -109,7 +109,7 @@ class Othello reverse_to(row, col, disk, *dir) end end - + def count_disk(disk) num = 0 @data.each do |rows| @@ -121,7 +121,7 @@ class Othello end return num end - + def count_point_to(row, col, my_disk, dir_y, dir_x) return 0 if @data[row][col] != EMPTY count = 0 @@ -140,7 +140,7 @@ class Othello end return 0 end - + def count_point(row, col, my_disk) count = 0 DIRECTIONS.each do |dir| @@ -148,14 +148,14 @@ class Othello end return count end - + def corner?(row, col) return (row == 0 && col == 0) || (row == 0 && col == 7) || (row == 7 && col == 0) || (row == 7 && col == 7) end - + def search(my_disk) max = 0 max_row = nil @@ -173,30 +173,30 @@ class Othello return max_row, max_col end end #--------------------------> class Board ends here - + class BoardView < TclTkWidget - + BACK_GROUND_COLOR = "DarkGreen" HILIT_BG_COLOR = "green" BORDER_COLOR = "black" BLACK_COLOR = "black" WHITE_COLOR = "white" STOP_COLOR = "red" - + attr :left attr :top attr :right attr :bottom - + class Square - + attr :oval, TRUE attr :row attr :col - + def initialize(view, row, col) @view = view - @id = @view.e("create rectangle", + @id = @view.e("create rectangle", *(view.tk_rect(view.left + col, view.top + row, view.left + col + 1, @@ -219,7 +219,7 @@ class Othello view.click_square(self) })) end - + def blink(color) @view.e("itemconfigure", @id, "-fill #{color}") $update.e() @@ -227,13 +227,13 @@ class Othello @view.e("itemconfigure", @id, "-fill #{BACK_GROUND_COLOR}") end end #-----------------------> class Square ends here - + def initialize(othello, board) super($ip, $root, $canvas) @othello = othello @board = board @board.add_observer(self) - + @squares = Array.new(8) for i in 0 .. 7 @squares[i] = Array.new(8) @@ -242,7 +242,7 @@ class Othello @top = 0.5 @right = @left + 8 @bottom = @top + 8 - + i = self.e("create rectangle", *tk_rect(@left, @top, @right, @bottom)) self.e("itemconfigure", i, "-width 1m -outline #{BORDER_COLOR} -fill #{BACK_GROUND_COLOR}") @@ -252,15 +252,15 @@ class Othello @squares[row][col] = Square.new(self, row, col) end end - + update end - + def tk_rect(left, top, right, bottom) return left.to_s + "c", top.to_s + "c", right.to_s + "c", bottom.to_s + "c" end - + def clear each_square do |square| if square.oval != nil @@ -269,7 +269,7 @@ class Othello end end end - + def draw_disk(row, col, disk) if disk == EMPTY if @squares[row][col].oval != nil @@ -278,7 +278,7 @@ class Othello end return end - + $update.e() sleep(0.05) oval = @squares[row][col].oval @@ -299,7 +299,7 @@ class Othello end self.e("itemconfigure", oval, "-outline #{color} -fill #{color}") end - + def update(row = nil, col = nil) if row && col draw_disk(row, col, @board.get_disk(row, col)) @@ -311,7 +311,7 @@ class Othello end @othello.show_point end - + def each_square @squares.each do |rows| rows.each do |square| @@ -319,7 +319,7 @@ class Othello end end end - + def click_square(square) if @othello.in_com_turn || @othello.game_over || @board.count_point(square.row, @@ -331,15 +331,15 @@ class Othello @board.put_disk(square.row, square.col, @board.man_disk) @othello.com_turn end - + private :draw_disk public :update end #----------------------> class BoardView ends here - + 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 @@ -352,39 +352,39 @@ class Othello "}") #### ¤³¤³¤Þ¤Ç $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 - + def switch_side if @in_com_turn @play_black.e("toggle") @@ -393,7 +393,7 @@ class Othello com_turn unless @game_over end end - + def reset_game if @board.com_disk == BLACK @board.com_disk = WHITE @@ -404,7 +404,7 @@ class Othello $wm.e("title", $root, "Othello") @game_over = FALSE end - + def com_turn @in_com_turn = TRUE $update.e() |