Class: Othello::Board

Inherits:
Object show all
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

Instance Method Summary collapse

Constructor Details

#initialize(othello) ⇒ Board

Returns a new instance of Board.



47
48
49
50
# File 'sample/tcltklib/sample2.rb', line 47

def initialize(othello)
   @othello = othello
   reset
end

Instance Attribute Details

#com_diskObject

Returns the value of attribute com_disk



45
46
47
# File 'sample/tcltklib/sample2.rb', line 45

def com_disk
  @com_disk
end

Instance Method Details

#corner?(row, col) ⇒ Boolean

Returns:

  • (Boolean)


153
154
155
156
157
158
# File 'sample/tcltklib/sample2.rb', line 153

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



114
115
116
117
118
119
120
121
122
123
124
# File 'sample/tcltklib/sample2.rb', line 114

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



145
146
147
148
149
150
151
# File 'sample/tcltklib/sample2.rb', line 145

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



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'sample/tcltklib/sample2.rb', line 126

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



81
82
83
# File 'sample/tcltklib/sample2.rb', line 81

def get_disk(row, col)
   return @data[row][col]
end

#man_diskObject



73
74
75
# File 'sample/tcltklib/sample2.rb', line 73

def man_disk
   return - @com_disk
end

#notify_observers(*arg) ⇒ Object



52
53
54
55
56
# File 'sample/tcltklib/sample2.rb', line 52

def notify_observers(*arg)
   if @observer_peers != nil
      super(*arg)
   end
end

#other_disk(disk) ⇒ Object



77
78
79
# File 'sample/tcltklib/sample2.rb', line 77

def other_disk(disk)
   return - disk
end

#put_disk(row, col, disk) ⇒ Object



105
106
107
108
109
110
111
112
# File 'sample/tcltklib/sample2.rb', line 105

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

#resetObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'sample/tcltklib/sample2.rb', line 58

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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'sample/tcltklib/sample2.rb', line 85

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



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'sample/tcltklib/sample2.rb', line 160

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