Class: Othello::Board
- Includes:
- Observable
- Defined in:
- sample/tcltklib/sample2.rb
Constant Summary collapse
- DIRECTIONS =
[ [-1, -1], [-1, 0], [-1, 1], [ 0, -1], [ 0, 1], [ 1, -1], [ 1, 0], [ 1, 1] ]
Instance Attribute Summary collapse
-
#com_disk ⇒ Object
Returns the value of attribute com_disk.
Instance Method Summary collapse
- #corner?(row, col) ⇒ Boolean
- #count_disk(disk) ⇒ Object
- #count_point(row, col, my_disk) ⇒ Object
- #count_point_to(row, col, my_disk, dir_y, dir_x) ⇒ Object
- #get_disk(row, col) ⇒ Object
-
#initialize(othello) ⇒ Board
constructor
A new instance of Board.
- #man_disk ⇒ Object
- #notify_observers(*arg) ⇒ Object
- #other_disk(disk) ⇒ Object
- #put_disk(row, col, disk) ⇒ Object
- #reset ⇒ Object
- #reverse_to(row, col, my_disk, dir_y, dir_x) ⇒ Object
- #search(my_disk) ⇒ Object
Constructor Details
#initialize(othello) ⇒ Board
Returns a new instance of Board.
46 47 48 49 |
# File 'sample/tcltklib/sample2.rb', line 46 def initialize(othello) @othello = othello reset end |
Instance Attribute Details
#com_disk ⇒ Object
Returns the value of attribute com_disk
44 45 46 |
# File 'sample/tcltklib/sample2.rb', line 44 def com_disk @com_disk end |
Instance Method Details
#corner?(row, col) ⇒ Boolean
152 153 154 155 156 157 |
# File 'sample/tcltklib/sample2.rb', line 152 def corner?(row, col) return (row == 0 && col == 0) || (row == 0 && col == 7) || (row == 7 && col == 0) || (row == 7 && col == 7) end |
#count_disk(disk) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 |
# File 'sample/tcltklib/sample2.rb', line 113 def count_disk(disk) num = 0 @data.each do |rows| rows.each do |d| if d == disk num += 1 end end end return num end |
#count_point(row, col, my_disk) ⇒ Object
144 145 146 147 148 149 150 |
# File 'sample/tcltklib/sample2.rb', line 144 def count_point(row, col, my_disk) count = 0 DIRECTIONS.each do |dir| count += count_point_to(row, col, my_disk, *dir) end return count end |
#count_point_to(row, col, my_disk, dir_y, dir_x) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'sample/tcltklib/sample2.rb', line 125 def count_point_to(row, col, my_disk, dir_y, dir_x) 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 end |
#get_disk(row, col) ⇒ Object
80 81 82 |
# File 'sample/tcltklib/sample2.rb', line 80 def get_disk(row, col) return @data[row][col] end |
#man_disk ⇒ Object
72 73 74 |
# File 'sample/tcltklib/sample2.rb', line 72 def man_disk return - @com_disk end |
#notify_observers(*arg) ⇒ Object
51 52 53 54 55 |
# File 'sample/tcltklib/sample2.rb', line 51 def notify_observers(*arg) if @observer_peers != nil super(*arg) end end |
#other_disk(disk) ⇒ Object
76 77 78 |
# File 'sample/tcltklib/sample2.rb', line 76 def other_disk(disk) return - disk end |
#put_disk(row, col, disk) ⇒ Object
104 105 106 107 108 109 110 111 |
# File 'sample/tcltklib/sample2.rb', line 104 def put_disk(row, col, disk) @data[row][col] = disk changed notify_observers(row, col) DIRECTIONS.each do |dir| reverse_to(row, col, disk, *dir) end end |
#reset ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'sample/tcltklib/sample2.rb', line 57 def reset @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 end |
#reverse_to(row, col, my_disk, dir_y, dir_x) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'sample/tcltklib/sample2.rb', line 84 def reverse_to(row, col, my_disk, dir_y, dir_x) 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 end |
#search(my_disk) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'sample/tcltklib/sample2.rb', line 159 def search(my_disk) 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 end |