-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.rb
55 lines (42 loc) · 1.13 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
require 'byebug'
require_relative 'pieces'
class Board
attr_reader :rows
def initialize(make_grid = true)
make_starting_grid(make_grid)
end
def [](pos)
raise 'Hate to break it to you, but this position is not a thing' unless valid_pos?(pos)
row, col = pos
@rows[row][col]
end
def []=(pos, piece)
raise 'Hate to break it to you, but this position is not a thing' unless valid_pos?(pos)
row, col = pos
@rows[row][col] = piece
end
def empty?(pos)
self[pos].empty?
end
def valid_move?(from_pos, to_pos)
self[pos].moves(from_pos,dir)
end
def move_piece(color, from_pos, to_pos)
if self[from_pos].is_a?(NullPiece)
raise "That isnt a piece."
elsif piece.color != turn.color
raise "that isn't your piece."
elsif valid_move?(from_pos, to_pos) == false
raise "Your piece doesn't move like that."
else
move_piece!(from_pos,to_pos)
end
end
def move_piece!(from_pos, to_pos)
self[to_pos] = self[from_pos]
self[from_pos] = NullPiece.new(from_pos)
end
def make_starting_grid
@grid = Array.new(8) {Array.new(8) {NullPiece.new}}
end
end