Class: Rchess::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/rchess/game.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGame

Returns a new instance of Game.



6
7
8
9
# File 'lib/rchess/game.rb', line 6

def initialize
  @current_player_color = Piece::WHITE_COLOR
  @loosed_pieces = {Piece::WHITE_COLOR => [], Piece::BLACK_COLOR =>  []}
end

Instance Attribute Details

#boardObject

Returns the value of attribute board.



3
4
5
# File 'lib/rchess/game.rb', line 3

def board
  @board
end

#current_player_colorObject (readonly)

Returns the value of attribute current_player_color.



4
5
6
# File 'lib/rchess/game.rb', line 4

def current_player_color
  @current_player_color
end

#loosed_piecesObject (readonly)

Returns the value of attribute loosed_pieces.



4
5
6
# File 'lib/rchess/game.rb', line 4

def loosed_pieces
  @loosed_pieces
end

Instance Method Details

#add_loosed_piece(dstPiece) ⇒ Object



11
12
13
# File 'lib/rchess/game.rb', line 11

def add_loosed_piece(dstPiece)
  self.loosed_pieces[dstPiece.color] << dstPiece.type
end

#checked?Boolean

Returns:

  • (Boolean)


30
31
32
33
# File 'lib/rchess/game.rb', line 30

def checked?
  king = board.king_for_color(current_player_color)
  king.is_threaten?
end

#move!(srcCoord, dstCoord) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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