Class: ChessEngine::Board

Inherits:
Object
  • Object
show all
Defined in:
lib/chess_engine/board.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBoard

Returns a new instance of Board.



6
7
8
# File 'lib/chess_engine/board.rb', line 6

def initialize
  @board = Array.new(8) { Array.new(8) { nil } }
end

Class Method Details

.coordinates_listObject



74
75
76
77
78
79
80
# File 'lib/chess_engine/board.rb', line 74

def Board.coordinates_list
  list = []
  (0..7).each do |x|
    (0..7).each { |y| list << [x, y]}
  end
  list
end

Instance Method Details

#[](column, row) ⇒ Object



22
23
24
# File 'lib/chess_engine/board.rb', line 22

def [](column, row)
  @board[column][row]
end

#[]=(column, row, piece) ⇒ Object



26
27
28
# File 'lib/chess_engine/board.rb', line 26

def []=(column, row, piece)
  @board[column][row] = piece
end

#at(coordinates) ⇒ Object



30
31
32
33
# File 'lib/chess_engine/board.rb', line 30

def at(coordinates)
  return nil unless self.exists_at?(coordinates)
  @board[coordinates[0]][coordinates[1]]
end

#exists_at?(coordinates) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/chess_engine/board.rb', line 39

def exists_at?(coordinates)
  coordinates.all? { |c| c.between?(0, 7) }
end

#king_coords(color) ⇒ Object



82
83
84
85
86
# File 'lib/chess_engine/board.rb', line 82

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



64
65
66
67
68
# File 'lib/chess_engine/board.rb', line 64

def move_piece(from, to)
  piece = self.at(from)
  self.set_at(from, nil)
  self.set_at(to, piece)
end

#piece_coordinates(color) ⇒ Object



88
89
90
91
92
93
# File 'lib/chess_engine/board.rb', line 88

def piece_coordinates(color)
  Board.coordinates_list.select do |coord|
    piece = at(coord)
    !piece.nil? && piece.color == color
  end
end

#pieces(color) ⇒ Object



70
71
72
# File 'lib/chess_engine/board.rb', line 70

def pieces(color)
  @board.flatten.compact.select { |piece| piece.color == color }
end

#set_at(coordinates, piece) ⇒ Object



35
36
37
# File 'lib/chess_engine/board.rb', line 35

def set_at(coordinates, piece)
  @board[coordinates[0]][coordinates[1]] = piece
end

#set_defaultObject



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/chess_engine/board.rb', line 10

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_sObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/chess_engine/board.rb', line 43

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