class Checkers::GUI::Scene::Board
Attributes
animation_queue[R]
Public Class Methods
new(state, game_engine)
click to toggle source
# File lib/checkers/gui/scene/board.rb, line 11 def initialize(state, game_engine) @state = state @state.add_observer(self) @game_engine = game_engine @animation_queue = [] render_board end
Public Instance Methods
clear()
click to toggle source
# File lib/checkers/gui/scene/board.rb, line 41 def clear @board_objects.each(&:remove) if @board_objects&.any? end
each(&block)
click to toggle source
# File lib/checkers/gui/scene/board.rb, line 33 def each(&block) @board_objects.each(&block) end
find_index(&block)
click to toggle source
# File lib/checkers/gui/scene/board.rb, line 37 def find_index(&block) @board_objects.find_index(&block) end
piece_at(row, col)
click to toggle source
# File lib/checkers/gui/scene/board.rb, line 49 def piece_at(row, col) return @board_objects[row, col] if @board_objects[row, col].is_a?(Ruby2D::SquareWithPiece) end
square_at(row, col)
click to toggle source
# File lib/checkers/gui/scene/board.rb, line 45 def square_at(row, col) return @board_objects[row, col] if @board_objects[row, col].is_a?(Square) end
update()
click to toggle source
# File lib/checkers/gui/scene/board.rb, line 19 def update return if @state.winner || @state.tie check_win check_tie @animation_queue.unshift( PieceAnimation.animate(self, @state.board.last_move) do render_board @game_engine.play end ) end
Private Instance Methods
check_tie()
click to toggle source
# File lib/checkers/gui/scene/board.rb, line 63 def check_tie @state.set_state(tie: true) if tie? end
check_win()
click to toggle source
# File lib/checkers/gui/scene/board.rb, line 55 def check_win human_pieces = @state.board.count_pieces(player: :human) ai_pieces = @state.board.count_pieces(player: :ai) @state.set_state(winner: :human) if ai_pieces.zero? @state.set_state(winner: :ai) if human_pieces.zero? end
render_board()
click to toggle source
# File lib/checkers/gui/scene/board.rb, line 79 def render_board clear @board_objects = Matrix.zero(8) x = y = 0 square_color = 'white' @state.board.each_with_index do |piece, row, col| x = col * SQUARE_SIZE y = row * SQUARE_SIZE @board_objects[row, col] = if piece.zero? Square.new(x: x, y: y, size: SQUARE_SIZE, color: square_color) else Ruby2D::SquareWithPiece.new( x: x, y: y, size: SQUARE_SIZE, color: square_color, piece: piece ) end square_color = square_color == 'white' ? 'black' : 'white' square_color = square_color == 'white' ? 'black' : 'white' if col == @state.board.row_count - 1 end end
tie?()
click to toggle source
# File lib/checkers/gui/scene/board.rb, line 67 def tie? return false unless @state.winner.nil? if @state.board.find_moves_for_player(player: @state.turn).length.zero? turn = @state.turn == :human ? :ai : :human return true if @state.board.find_moves_for_player(player: turn).length.zero? end false end