Class: OthelloRuby::Game::Bord

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

Instance Attribute Summary collapse

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(**options) ⇒ Bord

Returns a new instance of Bord.



73
74
75
76
77
78
79
80
# 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

Instance Attribute Details

#blackObject

Returns the value of attribute black.



71
72
73
# File 'lib/othello_ruby.rb', line 71

def black
  @black
end

#first_move_colorObject (readonly)

Returns the value of attribute first_move_color.



72
73
74
# File 'lib/othello_ruby.rb', line 72

def first_move_color
  @first_move_color
end

#passive_move_colorObject (readonly)

Returns the value of attribute passive_move_color.



72
73
74
# File 'lib/othello_ruby.rb', line 72

def passive_move_color
  @passive_move_color
end

#whiteObject

Returns the value of attribute white.



71
72
73
# File 'lib/othello_ruby.rb', line 71

def white
  @white
end

Instance Method Details

#black_countObject



109
110
111
112
113
114
115
# 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) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
# 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_positionObject



150
151
152
153
154
155
156
157
# 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_positionObject



141
142
143
144
145
146
147
148
# 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

#showObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# 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_countObject



101
102
103
104
105
106
107
# 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) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
# 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