Module: Checkers::Board::Score
- Included in:
- Checkers::Board
- Defined in:
- lib/checkers/board/score.rb
Instance Method Summary collapse
- #movable_pieces(player:) ⇒ Object
- #number_of_pieces(player:) ⇒ Object
- #number_of_pieces_on_opponets_side(player:) ⇒ Object
-
#number_of_unoccupied_promotion_squares(player:) ⇒ Object
for kings implementation.
Instance Method Details
#movable_pieces(player:) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/checkers/board/score.rb', line 47 def movable_pieces(player:) opponent = opponent(player) opponent_pieces = 0 player_pieces = 0 board.each_with_index do |piece, row, col| next if piece.zero? if player_pieces(player).include?(piece) && movable_squares(row: row, col: col, player: player).any? player_pieces += 1 elsif movable_squares(row: row, col: col, player: opponent).any? opponent_pieces += 1 end end opponent_pieces - player_pieces end |
#number_of_pieces(player:) ⇒ Object
6 7 8 9 10 11 |
# File 'lib/checkers/board/score.rb', line 6 def number_of_pieces(player:) opponent = opponent(player) player_pieces = board.count { |piece| player_pieces(player).include?(piece) } opponent_pieces = board.count { |piece| player_pieces(opponent).include?(piece) } opponent_pieces - player_pieces end |
#number_of_pieces_on_opponets_side(player:) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/checkers/board/score.rb', line 13 def number_of_pieces_on_opponets_side(player:) opponent = opponent(player) player_pieces = board.each_with_index.count do |piece, row, _col| next if row >= 3 player_pieces(player).include?(piece) end opponent_pieces = board.each_with_index.count do |piece, row, _col| next unless row == 5 player_pieces(opponent).include?(piece) end opponent_pieces - player_pieces end |
#number_of_unoccupied_promotion_squares(player:) ⇒ Object
for kings implementation
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/checkers/board/score.rb', line 32 def number_of_unoccupied_promotion_squares(player:) opponent = opponent(player) opponent_squares = 0 player_squares = 0 board.each_with_index do |square, row, _col| next if player_pieces(player).include?(square) || player_pieces(opponent).include?(square) opponent_squares += 1 if row == 7 player_squares += 1 if row.zero? end opponent_squares - player_squares end |