Class: OthelloRuby::Game::User

Inherits:
Object
  • Object
show all
Includes:
BordWork
Defined in:
lib/othello_ruby.rb

Instance Method Summary collapse

Methods included from BordWork

#get_reverse_pattern, #left_direction, #left_under_direction, #left_upward_direction, #right_direction, #right_under_direction, #right_upward_direction, #under_direction, #upward_direction

Constructor Details

#initialize(color) ⇒ User

Returns a new instance of User.



162
163
164
# File 'lib/othello_ruby.rb', line 162

def initialize(color)
  @color = color
end

Instance Method Details

#count(bord) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/othello_ruby.rb', line 193

def count(bord)
  count = 0
  case @color
  when bord.first_move_color
    bord = bord.black
  when bord.passive_move_color
    bord = bord.white
  else
    nil
  end
  64.times do |i|
    count += 1 if bord[i] == 1
  end
  count
end

#set(bord, coordinate) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/othello_ruby.rb', line 166

def set(bord, coordinate)
  bit_string = convert_to_number(coordinate)
  case @color
  when bord.first_move_color
    patterns = get_reverse_pattern(bord.black, bord.white, bit_string)
  when bord.passive_move_color
    patterns = get_reverse_pattern(bord.white, bord.black, bit_string)
  else
    patterns = []
  end
  if patterns.empty?
    nil
  else
    pattern = patterns.inject(&:|)
    case @color
    when bord.first_move_color
      bord.black ^= (bit_string | pattern)
      bord.white ^= pattern
    when bord.passive_move_color
      bord.white ^= (bit_string | pattern)
      bord.black ^= pattern
    else
      nil
    end
  end
end