class OthelloRuby::Game::Bord

Attributes

black[RW]
first_move_color[R]
passive_move_color[R]
white[RW]

Public Class Methods

new(**options) click to toggle source
# File lib/othello_ruby.rb, line 73
def initialize(**options)
  @white_square = options[:white_square] || "0"
  @black_square = options[:black_square] || "1"
  @first_move_color    = options[:first_move_color]  || "black"
  @passive_move_color  = options[:passive_move_color]  || "white"
  @white = 0x0000001008000000
  @black = 0x0000000810000000
end

Public Instance Methods

black_count() click to toggle source
# File lib/othello_ruby.rb, line 109
def black_count
  count = 0
  64.times do |i|
    count += 1 if @black[i] == 1
  end
  count
end
black_set(coordinate) click to toggle source
# File lib/othello_ruby.rb, line 129
def black_set(coordinate)
  bit_string = convert_to_number(coordinate)
  patterns   = get_reverse_pattern(@black, @white, bit_string)
  if patterns.empty?
    nil
  else
    pattern = patterns.inject(&:|)
    @black ^= (bit_string | pattern)
    @white ^= pattern
  end
end
search_black_position() click to toggle source
# File lib/othello_ruby.rb, line 150
def search_black_position
  positions = []
  64.times do |num|
    positions << num unless get_reverse_pattern(@black, @white, 1 << num).empty?
  end
  positions.map{|position| 1 << position }
  positions.inject(&:|)
end
search_white_position() click to toggle source
# File lib/othello_ruby.rb, line 141
def search_white_position
  positions = []
  64.times do |num|
    positions << num unless  get_reverse_pattern(@white, @black, 1 << num).empty?
  end
  positions.map!{|position| 1 << position }
  positions.inject(&:|)
end
show() click to toggle source
# File lib/othello_ruby.rb, line 82
def show
  message = "    a   b   c   d   e   f   g   h\n  +---+---+---+---+---+---+---+---+\n"
  8.times do |row|
    message << "#{row+1} |"
    8.times do |col|
      if  @white[row * 8 + col] == 1
        message << " #{@white_square} "
      elsif @black[row * 8 + col] == 1
        message << " #{@black_square} "
      else
        message << "   "
      end
      message << "|"
    end
    message << "\n  +---+---+---+---+---+---+---+---+\n"
  end
  message
end
white_count() click to toggle source
# File lib/othello_ruby.rb, line 101
def white_count
  count = 0
  64.times do |i|
    count += 1 if @white[i] == 1
  end
  count
end
white_set(coordinate) click to toggle source
# File lib/othello_ruby.rb, line 117
def white_set(coordinate)
  bit_string = convert_to_number(coordinate)
  patterns   = get_reverse_pattern(@white, @black, bit_string)
  if patterns.empty?
    nil
  else
    pattern = patterns.inject(&:+)
    @white ^= (bit_string | pattern)
    @black ^= pattern
  end
end