Class: ChessEngine::Board
- Inherits:
-
Object
- Object
- ChessEngine::Board
- Defined in:
- lib/chess_engine/board.rb
Overview
This class provides a data structure for the chess board. It is responsible for storing information about pieces positions and moving them. It doesn’t implement move validations and chess rules, so it is possible to make any moves at this level of abstraction.
Instance Method Summary collapse
- #at(coordinates) ⇒ Object
-
#exists_at?(coordinates) ⇒ Boolean
Checks if the values of
coordinates
are between 0 and 7 === Example exists_at?([0, 0]) #=> true exists_at?([8, -1]) #=> false. -
#initialize ⇒ Board
constructor
Creates an empty 8x8 board as a 2-dimensional array.
-
#king_coords(color) ⇒ Object
Returns the coordinates of the king of given
color
. -
#move_piece(from, to) ⇒ Object
Moves the value of
from
coords toto
coords. -
#piece_coordinates(color) ⇒ Object
Returns the array of coordinates where pieces of given
color
a located. -
#set_at(coordinates, piece) ⇒ Object
Sets the board on given coordinates to
piece
. -
#set_default ⇒ Object
Sets the initial board position according to the classic chess rules.
-
#to_s ⇒ Object
Returns a string containing the board in printable format (uses colorize gem to paint the squares).
Constructor Details
#initialize ⇒ Board
Creates an empty 8x8 board as a 2-dimensional array
15 16 17 |
# File 'lib/chess_engine/board.rb', line 15 def initialize @board = Array.new(8) { Array.new(8) { nil } } end |
Instance Method Details
#at(coordinates) ⇒ Object
41 42 43 44 |
# File 'lib/chess_engine/board.rb', line 41 def at(coordinates) return nil unless self.exists_at?(coordinates) @board[coordinates[0]][coordinates[1]] end |
#exists_at?(coordinates) ⇒ Boolean
Checks if the values of coordinates
are between 0 and 7
Example
exists_at?([0, 0]) #=> true
exists_at?([8, -1]) #=> false
59 60 61 |
# File 'lib/chess_engine/board.rb', line 59 def exists_at?(coordinates) coordinates.all? { |c| c.between?(0, 7) } end |
#king_coords(color) ⇒ Object
Returns the coordinates of the king of given color
100 101 102 103 104 |
# File 'lib/chess_engine/board.rb', line 100 def king_coords(color) Board.coordinates_list.find do |coord| at(coord) && at(coord).king? && at(coord).color == color end end |
#move_piece(from, to) ⇒ Object
Moves the value of from
coords to to
coords. Sets the value of to
to nil
91 92 93 94 95 |
# File 'lib/chess_engine/board.rb', line 91 def move_piece(from, to) piece = self.at(from) self.set_at(from, nil) self.set_at(to, piece) end |
#piece_coordinates(color) ⇒ Object
Returns the array of coordinates where pieces of given color
a located
109 110 111 112 113 114 |
# File 'lib/chess_engine/board.rb', line 109 def piece_coordinates(color) Board.coordinates_list.select do |coord| piece = at(coord) !piece.nil? && piece.color == color end end |
#set_at(coordinates, piece) ⇒ Object
Sets the board on given coordinates to piece
49 50 51 |
# File 'lib/chess_engine/board.rb', line 49 def set_at(coordinates, piece) @board[coordinates[0]][coordinates[1]] = piece end |
#set_default ⇒ Object
Sets the initial board position according to the classic chess rules
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/chess_engine/board.rb', line 22 def set_default [[:white, 0, 1], [:black, 7, 6]].each do |color, row1, row2| ["Rook", "Knight", "Elephant", "Queen", "King", "Elephant", "Knight", "Rook"].each.with_index do |class_name, column| self[column, row1] = Module.const_get("ChessEngine::#{class_name}").new(color) end 0.upto(7) do |column| self[column, row2] = Pawn.new(color) end end end |
#to_s ⇒ Object
Returns a string containing the board in printable format (uses colorize gem to paint the squares)
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/chess_engine/board.rb', line 67 def to_s string = "" colors = [[:default, :light_white].cycle, [:light_white, :default].cycle].cycle 7.downto(0) do |row| string += "#{row + 1} " colors_cycle = colors.next 0.upto(7) do |column| piece = self[column, row] string += piece.nil? ? " " : piece.symbol string += " " string[-2..-1] = string[-2..-1].colorize(background: colors_cycle.next) end string += "\n" end string += " a b c d e f g h" string end |