-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.rb
188 lines (168 loc) · 4.43 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
141
142
143
144
145
146
147
148
149
150
151
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
182
183
184
185
186
187
188
require "./block.rb"
class Board
attr_accessor :board, :blocks, :lettersToUse, :width, :height, :parent, :prisoner
def initialize(width, height)
@width = width
@height = height
@board = Array.new(width) { Array.new(height, " ") }
@blocks = []
@lettersToUse = ('a'..'z').to_a
end
def eql?(other)
return false if self.width != other.width
return false if self.height != other.height
self.board.each_with_index do |row, i|
return false if !self.board[i].eql?(other.board[i])
end
return true
end
def hash
hash = 0
@board.each_with_index { |row, i| hash += row.hash*(10*(i+1)) }
return hash
end
# generates all possible boards that can arise out of a given board
# returns array of all the boards
def generateAllBoards
boards = []
@blocks.each do |block|
x, y = block.x, block.y
case block.type
when VERTICAL
if self.canMove?(block, UP)
up = self.dup
up.move(x, y, UP)
up.parent = self
boards << up
end
if self.canMove?(block, DOWN)
down = self.dup
down.move(x, y, DOWN)
down.parent = self
boards << down
end
when HORIZONTAL
if self.canMove?(block, LEFT)
left = self.dup
left.move(x, y, LEFT)
left.parent = self
boards << left
end
if self.canMove?(block, RIGHT)
right = self.dup
right.move(x, y, RIGHT)
right.parent = self
boards << right
end
end
end
return boards
end
# returns true if the prisoner can escape
def canPrisonerEscape?
return false if !@prisoner
x,y = @prisoner.position
len = @prisoner.length
return true if x+len == @width
(x+len).upto(@width-1) do |z|
return false if !@board[z][y].eql?(" ")
end
return true
end
# moves a block in a given direction by one space
# void method
def move(x,y, direction)
# initialize block to something
block = self.blocks[0]
self.blocks.each do |b|
block = b if b.x == x && b.y == y
end
letter = block.letter
len = block.length
case direction
when UP
@board[x][y-1] = "#{letter}#{letter}"
@board[x][y+len-1] = " "
block.y -= 1
when DOWN
@board[x][y+len] = "#{letter}#{letter}"
@board[x][y] = " "
block.y += 1
when LEFT
@board[x-1][y] = "#{letter}#{letter}"
@board[x+len-1][y] = " "
block.x -= 1
when RIGHT
@board[x+len][y] = "#{letter}#{letter}"
@board[x][y] = " "
block.x += 1
end
end
# adds a block to the board
# block should be initialized with coordinates
# void method
def add(block)
@blocks << block
letter = @lettersToUse.shift if !block.special
if block.special
letter = "@"
@prisoner = block
end
block.board = self
block.letter = letter
x,y = block.position
length = block.length
case block.type
when HORIZONTAL
x.upto(x+length-1) do |w|
@board[w][y] = "#{letter}#{letter}"
end
when VERTICAL
y.upto(y+length-1) do |z|
@board[x][z] = "#{letter}#{letter}"
end
end
end
# checks to see if a block can move in a certain direction
# returns true if it can
def canMove?(block, direction)
type = block.type
x, y, length = block.x, block.y, block.length
case direction
when UP
return false if type == HORIZONTAL || y == 0
return @board[x][y-1].eql?(" ")
when DOWN
return false if type == HORIZONTAL || y+length == @height
return @board[x][y+length].eql?(" ")
when LEFT
return false if type == VERTICAL || x == 0
return @board[x-1][y].eql?(" ")
when RIGHT
return false if type == VERTICAL || x+length == @width
return @board[x+length][y].eql?(" ")
end
end
def print
puts "+"+( ["---"]*width).join()+"+"
@board.each_with_index do |row, x|
#puts "|"+row.join(" ")+" |"
s = "|"
row.each_with_index do |el, y|
s+= @board[y][x]+" "
end
s+="|" if x != self.prisoner.y
puts s
end
puts "+"+( ["---"]*width).join()+"+"
end
def dup
new = Board.new(self.width, self.height)
@blocks.each do |b|
new.add(Block.new(b.type, b.length, b.x,b.y,b.special))
end
new.lettersToUse = self.lettersToUse
new.parent = self.parent
return new
end
end