/
board.py
237 lines (212 loc) · 8.12 KB
/
board.py
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import random
"""
tile_puzzle, an emulated sliding tile board and solver
Copyright (C) 2012 James Heslin
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""
class board():
def __init__(self, size, grid=None):
self.size = size
if not grid == None:
self.grid = grid
else:
self.grid = []
numbers = range(0, (self.size**2))
numbers.remove(0) # Take out leading zero
numbers.append(0) # Drop it back in at the end
#print numbers
for i in xrange(0, size):
self.grid.append(numbers[size*i:size*i+size])
#print self.grid
self.blank_pos = (size-1, size-1)
#print self.grid[self.blank_pos[0]][self.blank_pos[1]]
self.blank_pos = self.find_blank_pos(self.grid)
#print "Found blank_pos:", self.findblank_pos(self.grid)
#print "Actual blank_pos:", self.blank_pos
def find_blank_pos(self, grid):
for i in xrange(len(grid[0])):
for j in xrange(len(grid)):
if grid[i][j] == 0:
return (i, j)
print "Couldn't find the blank tile."
return (0, 0)
def copy_grid(self):
l = []
for i in list(self.grid):
e = (list(i))
l.append(e)
#print l
return l
def get_legal_moves(self):
moves = []
for i in range(0, 4):
if self.is_valid_move(i):
b = board(self.size, self.copy_grid())
b.move_blank(i)
moves.append(b)
#print moves
return moves
def is_same_grid(self, board):
if not self.size == board.size:
return False
for i in xrange(0, self.size):
for j in xrange(0, self.size):
if not self.grid[i][j] == board.grid[i][j]:
return False
return True
def get_blank_pos(self):
return self.blank_pos
def show_board(self):
for i in self.grid:
line = "|"
for j in i:
if j < 10:
line += " "
line += str(j)
line += "|"
print line
print ""
def is_valid_move(self, direction):
if direction == 3: # up
if self.blank_pos[0] < 1:
return False
else:
#print ("Can move up from", str(self.blank_pos[0]),
#str(self.blank_pos[1]))
return True
elif direction == 2: # right
if self.blank_pos[1] > self.size-2:
return False
else:
#print ("Can move right from", str(self.blank_pos[0]),
#str(self.blank_pos[1]))
return True
elif direction == 1: # down
if self.blank_pos[0] > self.size-2:
return False
else:
#print ("Can move down from", str(self.blank_pos[0]),
#str(self.blank_pos[1]))
return True
elif direction == 0: # left
if self.blank_pos[1] < 1:
return False
else:
#print ("Can move left from", str(self.blank_pos[0]),
#str(self.blank_pos[1]))
return True
def tiles_out_of_place(self, goal):
t = 0
for i in xrange(self.size):
for j in xrange(self.size):
if self.grid[i][j] is not goal.grid[i][j]:
t += 1
return t
def set_grid(self, grid):
self.grid = grid
self.blank_pos = self.find_blank_pos(grid)
def moves_to_state(self, goal):
m = 0
tiles = []
# Check which tiles are out of place compared to the goal state
for i in xrange(self.size):
for j in xrange(self.size):
if not self.grid[i][j] == goal.grid[i][j]:
tiles.append((i, j, goal.grid[i][j], 0))
# Find the Manhattan distance between goal and actual position
# of those tiles and add it to m
i_diff = 0
j_diff = 0
for t in tiles:
for i in xrange(self.size):
for j in xrange(self.size):
if self.grid[i][j] == t[2]:
if i > t[0]:
i_diff = i - t[0]
else:
i_diff += t[0] - i
if j > t[1]:
j_diff += j - t[1]
else:
j_diff += t[1] - j
"""# is this a corner tile?
if ((i == 0 and j == 0) or
(i == self.size and j == 0) or
(i == 0 and j == self.size) or
(i == self.size and j == self.size)):
m += 1"""
#print i_diff, j_diff
m += i_diff
m += j_diff
#print m
return m
def move_blank(self, direction): # 3: up 2: right 1: down 0: left
#if self.is_valid_move(direction):
bP = self.blank_pos
#print bP
if direction == 0: # left
self.grid[bP[0]][bP[1]] = (self.grid
[bP[0]][bP[1]-1])
self.grid[bP[0]][bP[1]-1] = 0
self.blank_pos = (bP[0], bP[1]-1)
elif direction == 1: # down
self.grid[bP[0]][bP[1]] = (self.grid
[bP[0]+1][bP[1]])
self.grid[bP[0]+1][bP[1]] = 0
self.blank_pos = (bP[0]+1, bP[1])
elif direction == 2: # right
self.grid[bP[0]][bP[1]] = (self.grid
[bP[0]][bP[1]+1])
self.grid[bP[0]][bP[1]+1] = 0
self.blank_pos = (bP[0], bP[1]+1)
elif direction == 3: # up
self.grid[bP[0]][bP[1]] = (self.grid
[bP[0]-1][bP[1]])
self.grid[bP[0]-1][bP[1]] = 0
self.blank_pos = (bP[0]-1, bP[1])
#self.show_board()
#print bP, "->", self.blank_pos
#print
def randomise(self, iterations):
num = 0
states = [] # Keep track of moves made
state_done = False # Whether currently considered move has been made
print "Randomising:", iterations, "iterations"
while num < iterations:
state_done = False
move = random.choice(self.get_legal_moves()) # Get random move
if self.size > 2:
for s in states: # Step through states
if move.is_same_grid(s): # Has this move been made?
num = states.index(s) + 2
# Can have made a max of two moves since then
state_done = True # Set flag
# If this move hasn't been made
if not state_done:
states.append(move) # Add it to the list of moves made
self.set_grid(move.grid) # Set current board state to that
num += 1 # Increment number of moves made
#print num
for s in states:
s.show_board()
def main():
# Test the board class
b = board(3)
b.show_board()
b.randomise(50)
c = board(b.size, b.copy_grid())
c.show_board()
print c.is_same_grid(b)
print c.get_legal_moves()
if __name__ == "__main__":
main()