Class: Board
- Inherits:
-
Object
- Object
- Board
- Defined in:
- lib/ari_chess/board.rb
Instance Attribute Summary collapse
-
#grid ⇒ Object
readonly
Returns the value of attribute grid.
Instance Method Summary collapse
- #[](pos) ⇒ Object
- #[]=(pos, value) ⇒ Object
- #base_row_pieces ⇒ Object
- #checkmate?(color) ⇒ Boolean
- #dup ⇒ Object
- #find_pieces(color, type = nil) ⇒ Object
- #in_check?(color) ⇒ Boolean
-
#initialize(grid = Array.new(8) { Array.new(8) }) ⇒ Board
constructor
A new instance of Board.
- #inspect ⇒ Object
- #move(start_pos, end_pos) ⇒ Object
- #move!(start_pos, end_pos) ⇒ Object
- #on_board?(pos) ⇒ Boolean
- #populate_row(row_idx, color, pieces) ⇒ Object
- #render ⇒ Object
- #setup_pieces ⇒ Object
- #validate_move(start_pos, end_pos) ⇒ Object
Constructor Details
#initialize(grid = Array.new(8) { Array.new(8) }) ⇒ Board
Returns a new instance of Board.
7 8 9 |
# File 'lib/ari_chess/board.rb', line 7 def initialize(grid = Array.new(8) { Array.new(8) }) @grid = grid end |
Instance Attribute Details
#grid ⇒ Object (readonly)
Returns the value of attribute grid.
5 6 7 |
# File 'lib/ari_chess/board.rb', line 5 def grid @grid end |
Instance Method Details
#[](pos) ⇒ Object
82 83 84 85 |
# File 'lib/ari_chess/board.rb', line 82 def [](pos) x, y = pos @grid[x][y] end |
#[]=(pos, value) ⇒ Object
87 88 89 90 |
# File 'lib/ari_chess/board.rb', line 87 def []=(pos, value) x, y = pos @grid[x][y] = value end |
#base_row_pieces ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/ari_chess/board.rb', line 133 def base_row_pieces [ Rook, Knight, Bishop, Queen, King, Bishop, Knight, Rook ] end |
#checkmate?(color) ⇒ Boolean
61 62 63 64 65 |
# File 'lib/ari_chess/board.rb', line 61 def checkmate?(color) in_check?(color) && find_pieces(color).all? do |piece| piece.valid_moves.empty? end end |
#dup ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/ari_chess/board.rb', line 48 def dup new_board = Board.new white_pieces = find_pieces(:W) black_pieces = find_pieces(:B) (white_pieces + black_pieces).each do |piece| new_board[piece.pos] = piece.dup(new_board) end new_board end |
#find_pieces(color, type = nil) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/ari_chess/board.rb', line 75 def find_pieces(color, type = nil) colored_pieces = grid.flatten.compact.select { |piece| piece.color == color } colored_pieces.select! { |piece| piece.class.to_s == type } unless type.nil? colored_pieces end |
#in_check?(color) ⇒ Boolean
67 68 69 70 71 72 73 |
# File 'lib/ari_chess/board.rb', line 67 def in_check?(color) opposing_color = (color == :W ? :B : :W) opposing_pieces = find_pieces(opposing_color) king = find_pieces(color, "King")[0] opposing_pieces.any? { |piece| piece.moves.include?(king.pos) } end |
#inspect ⇒ Object
92 93 94 |
# File 'lib/ari_chess/board.rb', line 92 def inspect "" end |
#move(start_pos, end_pos) ⇒ Object
15 16 17 18 19 |
# File 'lib/ari_chess/board.rb', line 15 def move(start_pos, end_pos) validate_move(start_pos, end_pos) move!(start_pos, end_pos) end |
#move!(start_pos, end_pos) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/ari_chess/board.rb', line 39 def move!(start_pos, end_pos) piece = self[start_pos] self[end_pos] = piece piece.update_piece(end_pos) self[start_pos] = nil end |
#on_board?(pos) ⇒ Boolean
11 12 13 |
# File 'lib/ari_chess/board.rb', line 11 def on_board?(pos) pos.all? { |coord| coord.between?(0, 7) } end |
#populate_row(row_idx, color, pieces) ⇒ Object
125 126 127 128 129 130 131 |
# File 'lib/ari_chess/board.rb', line 125 def populate_row(row_idx, color, pieces) grid[row_idx].each_index do |idx| self[[row_idx, idx]] = pieces[idx].new([row_idx, idx], self, color) end nil end |
#render ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/ari_chess/board.rb', line 96 def render square_num = 0 #tracks grid for alternating color row_num = 0 puts " a b c d e f g h" grid.each_with_index do |row, n| row_string = "" row.each do |square| color = (square_num + row_num).even? ? :light_white : :light_black row_string << (square.nil? ? " " : " " + square.to_s + " ").colorize(:background => color) square_num += 1 end puts "#{(-1 * n) + 8} " + row_string row_num += 1 end nil end |
#setup_pieces ⇒ Object
115 116 117 118 119 120 121 122 123 |
# File 'lib/ari_chess/board.rb', line 115 def setup_pieces populate_row(0, :B, base_row_pieces) populate_row(1, :B, Array.new(8) { Pawn }) populate_row(7, :W, base_row_pieces) populate_row(6, :W, Array.new(8) { Pawn }) nil end |
#validate_move(start_pos, end_pos) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/ari_chess/board.rb', line 21 def validate_move(start_pos, end_pos) piece = self[start_pos] = nil if piece.nil? = "Invalid move! Start position is empty." elsif !piece.moves.include?(end_pos) = "Invalid move!" elsif !piece.valid_moves.include?(end_pos) = "Invalid move! This move would move you into check." end raise InvalidMoveError.new() unless .nil? nil end |