Class: Checkers::Board

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Moves, Score
Defined in:
lib/checkers/board.rb,
lib/checkers/board/moves.rb,
lib/checkers/board/score.rb

Defined Under Namespace

Modules: Moves, Score

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Moves

#basic_moves, #find_available_moves, #jump_moves

Methods included from Score

#movable_pieces, #number_of_pieces, #number_of_pieces_on_opponets_side, #number_of_unoccupied_promotion_squares

Constructor Details

#initialize(board: nil, jumped: false, last_move: nil) ⇒ Board

Returns a new instance of Board.



50
51
52
53
54
# File 'lib/checkers/board.rb', line 50

def initialize(board: nil, jumped: false, last_move: nil)
  @board = board || set_board
  @jumped = jumped
  @last_move = last_move
end

Instance Attribute Details

#boardObject (readonly)

Returns the value of attribute board.



17
18
19
# File 'lib/checkers/board.rb', line 17

def board
  @board
end

#jumpedObject (readonly)

Returns the value of attribute jumped.



17
18
19
# File 'lib/checkers/board.rb', line 17

def jumped
  @jumped
end

#last_moveObject (readonly)

Returns the value of attribute last_move.



17
18
19
# File 'lib/checkers/board.rb', line 17

def last_move
  @last_move
end

Class Method Details

.generate_boards(board_object, player) ⇒ Object



22
23
24
25
26
27
# File 'lib/checkers/board.rb', line 22

def generate_boards(board_object, player)
  moves = board_object.find_moves_for_player(player: player)
  moves.map do |move|
    make_move(board_object, move)
  end
end

.make_move(board_object, move) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/checkers/board.rb', line 29

def make_move(board_object, move)
  new_board = board_object.board.dup
  jumped = false

  new_board[*move.end_square] = if move.end_square[0].zero? && new_board[*move.start_square] == HUMAN_PIECE
                                  HUMAN_KING
                                elsif move.end_square[0] == 7 && new_board[*move.start_square] == AI_PIECE
                                  AI_KING
                                else
                                  new_board[*move.start_square]
                                end
  new_board[*move.start_square] = 0

  if move.is_a?(JumpMove)
    new_board[*move.jump_over_square] = 0
    jumped = true
  end
  Board.new(board: new_board, jumped: jumped, last_move: move)
end

Instance Method Details

#any_jump_moves?(player:) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/checkers/board.rb', line 79

def any_jump_moves?(player:)
  find_moves_for_player(player: player).one? { |move| move.is_a?(JumpMove) }
end

#calculate_score(player:) ⇒ Object



56
57
58
59
60
61
# File 'lib/checkers/board.rb', line 56

def calculate_score(player:)
  number_of_pieces(player: player) +
    number_of_pieces_on_opponets_side(player: player) +
    3 * movable_pieces(player: player) +
    4 * number_of_unoccupied_promotion_squares(player: player)
end

#count_pieces(player:) ⇒ Object



63
64
65
# File 'lib/checkers/board.rb', line 63

def count_pieces(player:)
  board.count { |piece| player_pieces(player).include?(piece) }
end

#find_moves_for_player(player:) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/checkers/board.rb', line 67

def find_moves_for_player(player:)
  found_moves = []
  @board.each_with_index do |e, row, col|
    next unless player_pieces(player).include?(e)

    moves = find_available_moves(row: row, col: col, player: player)
    found_moves += moves
    break found_moves = moves if moves.any? { |move| move.is_a?(JumpMove) }
  end
  found_moves
end