Class: Checkers::GUI::Scene

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/checkers/gui/scene.rb,
lib/checkers/gui/scene/board.rb,
lib/checkers/gui/scene/piece_animation.rb

Defined Under Namespace

Classes: Board, PieceAnimation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(state, game_enigne) ⇒ Scene

Returns a new instance of Scene.



12
13
14
15
16
17
# File 'lib/checkers/gui/scene.rb', line 12

def initialize(state, game_enigne)
  @state = state
  @board = Board.new(state, game_enigne)
  @allowed_squares = []
  @allowed_moves = []
end

Instance Attribute Details

#boardObject (readonly)

Returns the value of attribute board.



8
9
10
# File 'lib/checkers/gui/scene.rb', line 8

def board
  @board
end

Instance Method Details

#handle_click(x, y) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/checkers/gui/scene.rb', line 19

def handle_click(x, y)
  row, col = click_board_indices(x, y)

  if piece_clicked?(x, y)
    @allowed_moves = @state.board.find_available_moves(row: row, col: col, player: :human)
    @allowed_squares = @allowed_moves.map { |move| @board.square_at(*move.end_square) }
  else
    return if @allowed_squares.empty? && @allowed_moves.empty?

    move_made = @allowed_moves.find { |move| move.end_square == [row, col] }
    if move_made
      @allowed_moves = []
      @allowed_squares = []
      new_board = Checkers::Board.make_move(@state.board, move_made)
      turn = if new_board.jumped
               new_board.any_jump_moves?(player: :human) ? :human : :ai
             else
               :ai
             end
      @state.set_state(board: new_board, turn: turn)
    end
  end
end