Class: Chess::Board
- Inherits:
-
Object
- Object
- Chess::Board
- Includes:
- BoardFromFenCode, BoardPos
- Defined in:
- lib/chess/board/board.rb
Overview
Board
Instance Attribute Summary collapse
-
#black_king ⇒ Object
Returns the value of attribute black_king.
-
#black_pieces ⇒ Object
Returns the value of attribute black_pieces.
-
#castling_rights ⇒ Object
Returns the value of attribute castling_rights.
-
#current_player ⇒ Object
Returns the value of attribute current_player.
-
#data ⇒ Hash
‘a’ to ‘h’ are files, rnbqkp are black pieces and RNBQKP are white pieces along the 8 ranks.
-
#full_move ⇒ Object
Returns the value of attribute full_move.
-
#half_move ⇒ Object
Returns the value of attribute half_move.
-
#possible_en_passant_target ⇒ Object
Returns the value of attribute possible_en_passant_target.
-
#white_king ⇒ Object
Returns the value of attribute white_king.
-
#white_pieces ⇒ Object
Returns the value of attribute white_pieces.
Instance Method Summary collapse
-
#empty_at?(file, rank) ⇒ Boolean
checks if a pos is empty.
-
#enemy_at?(file, rank) ⇒ Boolean
check if enemy at pos.
-
#initialize(fen_code) ⇒ Board
constructor
Returns a new instance of Board.
-
#insert_piece_at(piece, file, rank) ⇒ void
inserts given piece at given pos.
-
#piece_at(file, rank) ⇒ Object
returns piece at the given position.
-
#remove_piece_at(file, rank) ⇒ void
removes the piece from board and also from #black_pieces or #white_pieces.
-
#reset_half_move ⇒ Object
resets half move to 0.
Methods included from BoardPos
#east_pos, #north_east_pos, #north_pos, #north_west_pos, #pos_in_range?, #pos_nil?, #south_east_pos, #south_pos, #south_west_pos, #west_pos
Methods included from BoardFromFenCode
#black_piece, #create_board, #create_piece, #fill_board, #fill_rank, #generate_data, #insert_piece, #white_piece
Constructor Details
#initialize(fen_code) ⇒ Board
Returns a new instance of Board.
31 32 33 34 35 |
# File 'lib/chess/board/board.rb', line 31 def initialize(fen_code) @white_pieces = [] @black_pieces = [] @data = generate_data(fen_code) end |
Instance Attribute Details
#black_king ⇒ Object
Returns the value of attribute black_king.
26 27 28 |
# File 'lib/chess/board/board.rb', line 26 def black_king @black_king end |
#black_pieces ⇒ Object
Returns the value of attribute black_pieces.
26 27 28 |
# File 'lib/chess/board/board.rb', line 26 def black_pieces @black_pieces end |
#castling_rights ⇒ Object
Returns the value of attribute castling_rights.
26 27 28 |
# File 'lib/chess/board/board.rb', line 26 def castling_rights @castling_rights end |
#current_player ⇒ Object
Returns the value of attribute current_player.
26 27 28 |
# File 'lib/chess/board/board.rb', line 26 def current_player @current_player end |
#data ⇒ Hash
‘a’ to ‘h’ are files, rnbqkp are black pieces and RNBQKP are white pieces along the 8 ranks.
26 27 28 |
# File 'lib/chess/board/board.rb', line 26 def data @data end |
#full_move ⇒ Object
Returns the value of attribute full_move.
26 27 28 |
# File 'lib/chess/board/board.rb', line 26 def full_move @full_move end |
#half_move ⇒ Object
Returns the value of attribute half_move.
26 27 28 |
# File 'lib/chess/board/board.rb', line 26 def half_move @half_move end |
#possible_en_passant_target ⇒ Object
Returns the value of attribute possible_en_passant_target.
26 27 28 |
# File 'lib/chess/board/board.rb', line 26 def possible_en_passant_target @possible_en_passant_target end |
#white_king ⇒ Object
Returns the value of attribute white_king.
26 27 28 |
# File 'lib/chess/board/board.rb', line 26 def white_king @white_king end |
#white_pieces ⇒ Object
Returns the value of attribute white_pieces.
26 27 28 |
# File 'lib/chess/board/board.rb', line 26 def white_pieces @white_pieces end |
Instance Method Details
#empty_at?(file, rank) ⇒ Boolean
checks if a pos is empty
50 51 52 |
# File 'lib/chess/board/board.rb', line 50 def empty_at?(file, rank) piece_at(file, rank) == '' end |
#enemy_at?(file, rank) ⇒ Boolean
check if enemy at pos
82 83 84 85 |
# File 'lib/chess/board/board.rb', line 82 def enemy_at?(file, rank) piece = piece_at(file, rank) @current_player != piece.color.chr end |
#insert_piece_at(piece, file, rank) ⇒ void
This method returns an undefined value.
inserts given piece at given pos
69 70 71 72 73 74 75 76 77 |
# File 'lib/chess/board/board.rb', line 69 def insert_piece_at(piece, (file, rank)) @data[file][rank] = piece piece.pos = [file, rank] if @current_player == 'w' @white_pieces << piece unless @white_pieces.include?(piece) else @black_pieces << piece unless @white_pieces.include?(piece) end end |
#piece_at(file, rank) ⇒ Object
returns piece at the given position
41 42 43 44 45 |
# File 'lib/chess/board/board.rb', line 41 def piece_at(file, rank) return if @data[file].nil? @data[file][rank] end |
#remove_piece_at(file, rank) ⇒ void
This method returns an undefined value.
removes the piece from board and also from #black_pieces or #white_pieces
58 59 60 61 62 |
# File 'lib/chess/board/board.rb', line 58 def remove_piece_at((file, rank)) @data[file][rank] = '' @black_pieces.reject! { |piece| piece.pos == [file, rank] } @white_pieces.reject! { |piece| piece.pos == [file, rank] } end |
#reset_half_move ⇒ Object
resets half move to 0
88 89 90 |
# File 'lib/chess/board/board.rb', line 88 def reset_half_move @half_move = 0 end |