class Rchess::Game

Attributes

board[RW]
current_player_color[R]
loosed_pieces[R]

Public Class Methods

new() click to toggle source
# File lib/rchess/game.rb, line 6
def initialize
  @current_player_color = Piece::WHITE_COLOR
  @loosed_pieces = {Piece::WHITE_COLOR => [], Piece::BLACK_COLOR =>  []}
end

Public Instance Methods

add_loosed_piece(dstPiece) click to toggle source
# File lib/rchess/game.rb, line 11
def add_loosed_piece(dstPiece)
  self.loosed_pieces[dstPiece.color] << dstPiece.type
end
checked?() click to toggle source
# File lib/rchess/game.rb, line 30
def checked?
  king = board.king_for_color(current_player_color)
  king.is_threaten?
end
move!(srcCoord, dstCoord) click to toggle source
# File lib/rchess/game.rb, line 15
def move!(srcCoord, dstCoord)
  return false unless board.movement_within_board?(srcCoord, dstCoord)

  piece = board.piece_at_coord(srcCoord)
  return false unless piece
  return false unless current_player_own_piece?(piece)
  return false unless board.valid_move?(piece, dstCoord)

  dstPiece = board.piece_at_coord(dstCoord)
  add_loosed_piece(dstPiece) if dstPiece
  board.move_src_to_dst!(piece, dstCoord)
  switch_current_player
  !checked?
end

Private Instance Methods

current_player_own_piece?(piece) click to toggle source
# File lib/rchess/game.rb, line 45
def current_player_own_piece?(piece)
  current_player_color == piece.color
end
switch_current_player() click to toggle source
# File lib/rchess/game.rb, line 41
def switch_current_player
  @current_player_color = @current_player_color == Piece::WHITE_COLOR ? Piece::BLACK_COLOR : Piece::WHITE_COLOR
end