Class: Board

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(setup = true) ⇒ Board

Returns a new instance of Board.



11
12
13
14
15
16
# File 'lib/board.rb', line 11

def initialize(setup = true)
  @grid = Array.new(8) { Array.new(8) }
  @cursor = [0,0]

  populate_board if setup
end

Instance Attribute Details

#cursorObject (readonly)

Returns the value of attribute cursor.



9
10
11
# File 'lib/board.rb', line 9

def cursor
  @cursor
end

#gridObject (readonly)

Returns the value of attribute grid.



9
10
11
# File 'lib/board.rb', line 9

def grid
  @grid
end

Instance Method Details

#[](pos) ⇒ Object



53
54
55
# File 'lib/board.rb', line 53

def [](pos)
  @grid[pos.first][pos.last]
end

#[]=(pos, piece) ⇒ Object



57
58
59
# File 'lib/board.rb', line 57

def []=(pos, piece)
  @grid[pos.first][pos.last] = piece
end

#checkmate?(color) ⇒ Boolean

Returns:

  • (Boolean)


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

def checkmate?(color)
  pieces(color).all? { |piece| piece.valid_moves.empty? } &&
  in_check?(color)
end

#deep_dupObject



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/board.rb', line 95

def deep_dup
  dup_board = Board.new(false)

  [:white, :black].each do |color|
    pieces(color).each do |piece|
      duped_piece = piece.dup(dup_board)
      dup_board[duped_piece.pos] = duped_piece
    end
  end

  dup_board
end

#in_check?(color) ⇒ Boolean

Returns:

  • (Boolean)


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

def in_check?(color)
  opponent_color = color == :white ? :black : :white
  king_pos = king(color).pos
  pieces(opponent_color).any? do |piece|
    piece.moves.include?(king_pos)
  end
end

#king(color) ⇒ Object



143
144
145
# File 'lib/board.rb', line 143

def king(color)
  pieces(color).find { |piece| piece.is_a?(King)}
end

#move(start_pos, end_pos, color) ⇒ Object

Raises:



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/board.rb', line 18

def move(start_pos, end_pos, color)
  raise OffBoardError unless on_board?(start_pos) && on_board?(end_pos)
  raise NoPieceError unless piece_at(start_pos)
  raise WrongColorError unless piece_at(start_pos).color == color

  current_piece = piece_at(start_pos)
  if current_piece.valid_moves.include?(end_pos)
    move!(start_pos, end_pos)
  else
    raise InvalidMoveError
  end
end

#move!(start_pos, end_pos) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/board.rb', line 31

def move!(start_pos, end_pos)
  current_piece = piece_at(start_pos)
  self[start_pos] = nil
  self[end_pos] = current_piece
  current_piece.pos = end_pos
  current_piece.moved = true
end

#move_cursorObject



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/board.rb', line 39

def move_cursor
  input = ""
  until input == "\r"
    render(@cursor)
    input = STDIN.getch
    @cursor[0] += 1 if input == 'k' && @cursor[0] + 1 < 8
    @cursor[0] -= 1 if input == 'i' && @cursor[0] - 1 >= 0
    @cursor[1] -= 1 if input == 'j' && @cursor[1] - 1 >= 0
    @cursor[1] += 1 if input == 'l' && @cursor[1] + 1 < 8

  end
  @cursor.dup
end

#occupied?(pos) ⇒ Boolean

Returns:

  • (Boolean)


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

def occupied?(pos)
  return false unless on_board?(pos)
  !!self[pos]
end

#on_board?(pos) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/board.rb', line 61

def on_board?(pos)
  (pos[0]).between?(0, 7) && (pos[1]).between?(0, 7)
end

#piece_at(pos) ⇒ Object



78
79
80
# File 'lib/board.rb', line 78

def piece_at(pos)
  self[pos]
end

#pieces(color) ⇒ Object



147
148
149
150
# File 'lib/board.rb', line 147

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

#populate_boardObject



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/board.rb', line 152

def populate_board
  [[1,:white], [6, :black]].each do |row|
    @grid[row.first].map!.with_index do |space, i|
      Pawn.new(self, row.last, [row.first, i])
    end
  end

  [[0, :white], [7, :black]].each do |row|
    @grid[row.first].map!.with_index do |space, i|
      case i
      when 0
        Rook.new(self, row.last, [row.first, i])
      when 1
        Knight.new(self, row.last, [row.first, i])
      when 2
        Bishop.new(self, row.last, [row.first, i])
      when 3
        King.new(self, row.last, [row.first, i])
      when 4
        Queen.new(self, row.last, [row.first, i])
      when 5
        Bishop.new(self, row.last, [row.first, i])
      when 6
        Knight.new(self, row.last, [row.first, i])
      when 7
        Rook.new(self, row.last, [row.first, i])
      end
    end
  end
end

#render(cursor = nil) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/board.rb', line 108

def render(cursor = nil)
  system('clear')
  print ("   " + ('a'..'h').to_a.join(" ") + "\n")
  self.grid.each_with_index do |row, i|
    print "#{i + 1} "
    row.each_with_index do |space, j|
      if cursor.nil?
        if (i + j).odd?
          background = :light_black
        else
          background = :light_blue
        end
      else
        if @cursor == [i,j]
          background = :yellow
        elsif (i + j).odd?
          background = :light_black
        else
          background = :light_blue
        end
      end
      if space.nil?
        print '  '.colorize(:background => background)
      else
        print "#{space.symbol} ".colorize(:color => space.color,
          :background => background)
      end
    end
    print " #{i + 1}"
    puts
  end
  print ("  " + ('a'..'h').to_a.join(" ") + "\n")
end

#stalemate?(color) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/board.rb', line 74

def stalemate?(color)
  pieces(color).all? { |piece| piece.valid_moves.empty? } && !in_check?(color)
end

#won?Boolean

Returns:

  • (Boolean)


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

def won?
  checkmate?(:white) || checkmate?(:black)
end