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


231
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
# File 'sample/tcltklib/sample2.rb', line 231

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


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

def bottom
  @bottom
end

#leftObject (readonly)

Returns the value of attribute left


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

def left
  @left
end

#rightObject (readonly)

Returns the value of attribute right


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

def right
  @right
end

#topObject (readonly)

Returns the value of attribute top


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

def top
  @top
end

Instance Method Details

#clearObject


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

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


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

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


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

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

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


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

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


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

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