Class: Othello::BoardView

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

Overview

————————–> class Board ends here

Defined Under Namespace

Classes: Square

Constant Summary collapse

BACK_GROUND_COLOR =
"DarkGreen"
HILIT_BG_COLOR =
"green"
BORDER_COLOR =
"black"
BLACK_COLOR =
"black"
WHITE_COLOR =
"white"
STOP_COLOR =
"red"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from TclTkCommand

#e

Methods inherited from TclTkObject

#to_s

Constructor Details

#initialize(othello, board) ⇒ BoardView

———————–> class Square ends here



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'sample/tcltklib/sample2.rb', line 232

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)
   end
   @left = 1
   @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}")

   for row in 0 .. 7
      for col in 0 .. 7
         @squares[row][col] = Square.new(self, row, col)
      end
   end

   update
end

Instance Attribute Details

#bottomObject (readonly)

Returns the value of attribute bottom



190
191
192
# File 'sample/tcltklib/sample2.rb', line 190

def bottom
  @bottom
end

#leftObject (readonly)

Returns the value of attribute left



187
188
189
# File 'sample/tcltklib/sample2.rb', line 187

def left
  @left
end

#rightObject (readonly)

Returns the value of attribute right



189
190
191
# File 'sample/tcltklib/sample2.rb', line 189

def right
  @right
end

#topObject (readonly)

Returns the value of attribute top



188
189
190
# File 'sample/tcltklib/sample2.rb', line 188

def top
  @top
end

Instance Method Details

#clearObject



265
266
267
268
269
270
271
272
# File 'sample/tcltklib/sample2.rb', line 265

def clear
   each_square do |square|
      if square.oval != nil
         self.e("delete", square.oval)
         square.oval = nil
      end
   end
end

#click_square(square) ⇒ Object



324
325
326
327
328
329
330
331
332
333
334
# File 'sample/tcltklib/sample2.rb', line 324

def click_square(square)
   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
end

#each_squareObject



316
317
318
319
320
321
322
# File 'sample/tcltklib/sample2.rb', line 316

def each_square
   @squares.each do |rows|
      rows.each do |square|
         yield(square)
      end
   end
end

#tk_rect(left, top, right, bottom) ⇒ Object



260
261
262
263
# File 'sample/tcltklib/sample2.rb', line 260

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

#update(row = nil, col = nil) ⇒ Object



304
305
306
307
308
309
310
311
312
313
314
# File 'sample/tcltklib/sample2.rb', line 304

def update(row = nil, col = nil)
   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
end