-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRushHour.py
400 lines (325 loc) · 12.8 KB
/
RushHour.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
from asyncio.windows_events import NULL
from pickle import REDUCE
import sys
import copy
import time
import pygame
import tkinter
from tkinter import *
from tkinter import messagebox
from pygame.locals import KEYDOWN, K_q, K_LEFT, K_RIGHT, K_DOWN, K_UP, K_1, K_9
from Board import *
from Graph import *
from Button import *
import threading
from tkinter import filedialog
# CONSTANTS:
SCREENSIZE = WIDTH, HEIGHT = 875, 650
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
GREY = (160, 160, 160)
# SCREEN DATA
_VARS = {'surf': False, 'gridWH': 400,
'gridOrigin': (100, 150), 'gridCells': 0, 'lineWidth': 2}
# CURRENT SELECTED VEHICLE
global CURR_VEHICLE
# INITIALIZES GAME BOARD, WIN POS AT x:6, y:2 (OUTSIDE MAIN PLAY AREA)
def createNodes(node: Node, board, openNodes, closeNodes):
movementCount = node.movements+1
board.boardMAP = node.state
board.vehicles = node.vehicles
possibleStates = board.expandPossibleStates()
# THIS ALREADY CHECKS IF EACH NODE STATE DOESN'T EXIST ALREADY
for index in range(0, len(possibleStates[0])):
board.boardMAP = possibleStates[0][index]
board.vehicles = possibleStates[1][index]
hCost = board.calculateCurrentStateCost()
newNode = Node(node, movementCount, hCost,
board.boardMAP, board.vehicles)
if checkNodeRepetition(newNode, openNodes) == False:
if(checkNodeRepetition(newNode, closeNodes) == False):
openNodes.append(newNode)
# SORTS NODES BASED ON COST
openNodes = sorted(openNodes, key=lambda node: node.get_Fn(), reverse=True)
return openNodes
# Function to insert open nodes avoiding repetitions
def checkNodeRepetition(newNode, nodeList):
maxR = len(newNode.vehicles)
for node in nodeList:
test = False
for index in range(0, maxR):
if(node.vehicles[index].position != newNode.vehicles[index].position):
break
if(index == maxR-1):
test = True
if(test):
return test
# Depending on the intent, this could be changed to an ordered insert
return False
# Function to check if a node was already considerate before
def a_estrella(root: Node, open_nodes, close_nodes, GAMEBOARD):
try:
currentNode = root
open_nodes.append(currentNode)
while (currentNode.blocked > 0):
currentNode = open_nodes.pop()
open_nodes = createNodes(
currentNode, GAMEBOARD, open_nodes, close_nodes)
close_nodes.append(currentNode)
solution = []
while(currentNode != 0):
solution.append(currentNode)
currentNode = currentNode.father
solution.reverse()
currentNode = solution[-1]
while(GAMEBOARD.hasWon() != True):
GAMEBOARD.boardMAP = currentNode.state.copy()
GAMEBOARD.vehicles = copy.deepcopy(currentNode.vehicles)
GAMEBOARD.moveVehicleMain()
newNode = Node(currentNode, currentNode.movements+1, 0,
GAMEBOARD.boardMAP.copy(), copy.deepcopy(GAMEBOARD.vehicles))
solution.append(newNode)
currentNode = newNode
return solution
except:
messagebox.showinfo(
message="Solution was not found. Select another file.", title="ERROR")
showRoot()
return False
# Draw cells
def placeCells(BOARD):
# GET CELL DIMENSIONS...
cellBorder = 6
celldimX = celldimY = (_VARS['gridWH']/_VARS['gridCells']) - (cellBorder*2)
# DOUBLE LOOP
for row in range(BOARD.boardMAP.shape[0]):
for column in range(BOARD.boardMAP.shape[1]):
# CHECKS IF VEHICLES ARE WITHIN A TILE
if(BOARD.boardMAP[row][column] != 0):
v = BOARD.getVehicle(BOARD.boardMAP[row][column])
x = _VARS['gridOrigin'][0] + (celldimY*row) + cellBorder + \
(2*row*cellBorder) + _VARS['lineWidth']/2
y = _VARS['gridOrigin'][1] + (celldimX*column) + cellBorder + (
2*column*cellBorder) + _VARS['lineWidth']/2
drawSquareCell(x-5, y-5, celldimX+11, celldimY+11, v.color)
# DRAWING CORRESPONDING ID FOR EACH VEHICLE CELL
font = pygame.font.SysFont('arial', 20)
vId = BOARD.boardMAP[row][column]
# IF VEHICLE IS SELECTED, DRAW THE TEXT WHITE
if(vId == CURR_VEHICLE):
text = font.render(
str(vId), True, WHITE)
else:
text = font.render(
str(vId), True, BLACK)
_VARS['surf'].blit(text, (x+cellBorder, y))
# DRAW WIN TILE
if(BOARD.hasWon() == False):
drawSquareCell(
_VARS['gridOrigin'][0] + (celldimY*BOARD.goalPos[0])
+ cellBorder + (2*BOARD.goalPos[0]
* cellBorder) + _VARS['lineWidth']/2 - 5,
_VARS['gridOrigin'][1] + (celldimX*BOARD.goalPos[1])
+ cellBorder + (2*BOARD.goalPos[1]*cellBorder) +
_VARS['lineWidth']/2 - 5,
celldimX + 11, celldimY + 11, GREEN)
# Draw filled rectangle at coordinates
def drawButton(buttonStart):
# elevation logic
buttonStart.top_rect.y = buttonStart.y - buttonStart.elevation
buttonStart.text_rect.center = buttonStart.top_rect.center
buttonStart.bottom_rect.midtop = buttonStart.top_rect.midtop
buttonStart.bottom_rect.height = buttonStart.top_rect.height + buttonStart.movement
pygame.draw.rect(_VARS['surf'], buttonStart.bottom_color,
buttonStart.bottom_rect, border_radius=12)
pygame.draw.rect(_VARS['surf'], buttonStart.top_color,
buttonStart.top_rect, border_radius=12)
_VARS['surf'].blit(buttonStart.text, buttonStart.text_rect)
# chech start buttom is clicked
def check_click(buttonStart):
while(1):
time.sleep(0)
mouse_pos = pygame.mouse.get_pos()
if buttonStart.top_rect.collidepoint(mouse_pos):
buttonStart.top_color = (0, 86, 31)
if pygame.mouse.get_pressed()[0]:
buttonStart.movement = 0
buttonStart.pressed = True
return True
else:
buttonStart.movement = buttonStart.elevation
if buttonStart.pressed == True:
buttonStart.pressed = False
else:
buttonStart.movement = buttonStart.elevation
buttonStart.top_color = (0, 140, 51)
# draw square
def drawSquareCell(x, y, dimX, dimY, color):
i = 0
pygame.draw.rect(
_VARS['surf'], color,
(x, y, dimX, dimY)
)
# Draw grid
def drawSquareGrid(origin, gridWH, cells):
CONTAINER_WIDTH_HEIGHT = gridWH
cont_x, cont_y = origin
# DRAW Grid Border:
# TOP lEFT TO RIGHT
pygame.draw.line(
_VARS['surf'], BLACK,
(cont_x, cont_y),
(CONTAINER_WIDTH_HEIGHT + cont_x, cont_y), _VARS['lineWidth'])
# # BOTTOM lEFT TO RIGHT
pygame.draw.line(
_VARS['surf'], BLACK,
(cont_x, CONTAINER_WIDTH_HEIGHT + cont_y),
(CONTAINER_WIDTH_HEIGHT + cont_x,
CONTAINER_WIDTH_HEIGHT + cont_y), _VARS['lineWidth'])
# # LEFT TOP TO BOTTOM
pygame.draw.line(
_VARS['surf'], BLACK,
(cont_x, cont_y),
(cont_x, cont_y + CONTAINER_WIDTH_HEIGHT), _VARS['lineWidth'])
# # RIGHT TOP TO BOTTOM
pygame.draw.line(
_VARS['surf'], BLACK,
(CONTAINER_WIDTH_HEIGHT + cont_x, cont_y),
(CONTAINER_WIDTH_HEIGHT + cont_x,
CONTAINER_WIDTH_HEIGHT + cont_y), _VARS['lineWidth'])
# Get cell size, just one since its a square grid.
cellSize = CONTAINER_WIDTH_HEIGHT/cells
# VERTICAL DIVISIONS: (0,1,2) for grid(3) for example
for x in range(cells):
pygame.draw.line(
_VARS['surf'], BLACK,
(cont_x + (cellSize * x), cont_y),
(cont_x + (cellSize * x), CONTAINER_WIDTH_HEIGHT + cont_y), 2)
# # HORIZONTAl DIVISIONS
pygame.draw.line(
_VARS['surf'], BLACK,
(cont_x, cont_y + (cellSize*x)),
(cont_x + CONTAINER_WIDTH_HEIGHT, cont_y + (cellSize*x)), 2)
#If is clicked
def openFile():
filepath = filedialog.askopenfilename(initialdir="C:\\Users\\Cakow\\PycharmProjects\\Main",
title="Select one file",
filetypes=(("text files", "*.txt"),
("all files", "*.*")))
file = open(filepath, 'r')
file.close()
RushH(filepath)
# Select file window
def prompt_file():
"""Create a Tk file dialog and cleanup when finished"""
global root
root = tkinter.Tk()
root.config(width=300, height=200)
root.title("Rush Hour")
root.eval('tk::PlaceWindow . center')
Welcome = tkinter.Label(text=" Welcome to Rush Hour Game! ",
font=('arial bold', 18))
Welcome.pack()
File = tkinter.Label(text=" Select the file of the game you want to resolve. ",
font=('Helvetica roman', 13))
File.pack()
boton = tkinter.Button(
text="Select file",
command=openFile,
bg='#008C33',
fg='White',
activebackground='#007A2C',
activeforeground='White',
font=('arial bold', 15)).pack(pady=20)
root.mainloop()
# Shows file window
def showRoot():
root.deiconify()
# Check events
def checkEvents(BOARD, solution, pos_solution, buttonStart):
global CURR_VEHICLE
if(pos_solution < len(solution)):
BOARD.boardMAP = solution[pos_solution].state
BOARD.vehicles = solution[pos_solution].vehicles
time.sleep(1) # Se mueve cuando se presiona
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
return
# Main Function
def RushH(file):
global CURR_VEHICLE, buttonStart
# NO SELECTED VEHICLE
CURR_VEHICLE = -1
# FILE PATH SELECTION SHOULD BE WITHIN INTERFACE
GAMEBOARD = Board(6, file)
GAMEBOARD.generatePuzzle()
open_nodes = []
close_nodes = []
length_solucion = 0
father_node = Node(
NULL, 0, GAMEBOARD.calculateCurrentStateCost(), GAMEBOARD.boardMAP, copy.deepcopy(GAMEBOARD.vehicles))
# Inicial Time
start_time = time.time()
test = a_estrella(father_node, open_nodes, close_nodes, GAMEBOARD)
if(test == False):
return
# Finish Time
total_time = time.time() - start_time
# Total Movements
movement = len(test)-1
root.withdraw()
pygame.init()
_VARS['gridCells'] = GAMEBOARD.boardMAP.shape[0]
pygame.display.set_caption('Rush Hour')
_VARS['surf'] = pygame.display.set_mode(SCREENSIZE)
buttonStart = Button('Start', 100, 30)
tr = threading.Thread(target=check_click, args=(buttonStart,))
tr.start()
GAMEBOARD.boardMAP = test[0].state
GAMEBOARD.vehicles = test[0].vehicles
while True:
checkEvents(GAMEBOARD, test, length_solucion, buttonStart)
_VARS['surf'].fill('#eae4e9')
if (tr.is_alive()):
drawButton(buttonStart)
# Shows movements and time
fontT = pygame.font.SysFont(None, 50)
tittle2 = fontT.render('Rush Hour', True, '#277da1')
_VARS['surf'].blit(tittle2, (350, 30))
font = pygame.font.SysFont(None, 24)
tittle2 = font.render('Problem solved in:', True, 'black')
_VARS['surf'].blit(tittle2, (620, 150))
tittle2 = font.render(str(movement) + ' movements', True, 'black')
_VARS['surf'].blit(tittle2, (620, 180))
tittle2 = font.render('Seconds it took to solve it: ', True, 'black')
_VARS['surf'].blit(tittle2, (620, 220))
tittle2 = font.render(str(total_time), True, 'black')
_VARS['surf'].blit(tittle2, (620, 250))
drawSquareGrid(
_VARS['gridOrigin'], _VARS['gridWH'], _VARS['gridCells'])
placeCells(GAMEBOARD)
pygame.display.update()
# CHECKS FOR WIN STATE
if(length_solucion < len(test) and tr.is_alive() == False):
length_solucion += 1
if(GAMEBOARD.hasWon() == True):
time.sleep(0)
Tk().wm_withdraw() # to hide the main window
# answer saves what user wants (yes, no)
answer = messagebox.askquestion(title="¿Do you want to select another problem?",
message="NOTE: If you don't, the actual problem will be repeated.")
# Next level
if(answer == 'yes'):
showRoot()
pygame.quit()
return
else:
pygame.quit()
RushH(file)
return
# Run program
if __name__ == "__main__":
prompt_file()