Method: Board#drop_piece

Defined in:
lib/command_four/board.rb

#drop_piece(column, color) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/command_four/board.rb', line 14

def drop_piece(column, color)
  if game_over?()
    raise PieceDropError.new("Game is already over")
  elsif invalid_index?(column)
    raise PieceDropError.new("Invalid column index: #{column}")
  elsif full?(column)
    raise PieceDropError.new("Column #{column} is already full")
  else
    first_empty_cell_index = @board[column].index {|cell| cell == :empty}
    @board[column][first_empty_cell_index] = color
    @completed_moves += 1
    @state = ConnectNChecker.new(@board, @connect_n, column, first_empty_cell_index).check
    if @completed_moves >= @max_moves && !game_over?()
      @state = GameState.new(true, [])
    end
  end
end