-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.rb
72 lines (60 loc) · 1.17 KB
/
board.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
require_relative 'piece'
require 'colorize'
class Board
attr_accessor :grid
def initialize(setup = true)
@grid = Array.new(8) { Array.new(8)}
if setup == true
set_up_board
end
end
def [](pos)
x,y = pos
@grid[x][y]
end
def []=(pos, future)
x,y = pos
@grid[x][y] = future
end
def set_up_board
(0..2).each do |row|
8.times do |col|
if (row.even? && col.odd?) || (row.odd? && col.even?)
@grid[row][col] = Piece.new(self, :red, [row, col])
end
end
end
(5..7).each do |row|
8.times do |col|
if (row.even? && col.odd?) || (row.odd? && col.even?)
@grid[row][col] = Piece.new(self, :white, [row, col])
end
end
end
end
def display_board
puts " 0 1 2 3 4 5 6 7"
puts
@grid.each_with_index do |row, index|
print "#{index} "
row.each do |spot|
if spot.nil?
print "_ "
else
print "#{spot.to_s} "
end
end
puts
end
end
def dup
duped_board = Board.new(false)
pieces.each do |piece|
duped_board[piece.position] = piece.class.new(duped_board, piece.color, piece.position.dup, piece.promoted)
end
duped_board
end
def pieces
@grid.flatten.compact
end
end