This repository was archived by the owner on May 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.py
More file actions
179 lines (137 loc) · 4.13 KB
/
Copy pathnode.py
File metadata and controls
179 lines (137 loc) · 4.13 KB
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
from car import *
from grid import *
from iomanager import *
import numpy as np
import time
GRID_MIN = 0
GRID_MAX = 5
class Node:
"""
Represents a node to be expanded. Each node is a state of the game.
"""
def __init__(self, grid:Grid, cars:list, parent=None, children=[], move=""):
self.parent = parent
self.children = children
self.grid = grid
self.cars = cars
self.move = move
self.exitedCars = list()
def __eq__(self, other):
if not isinstance(other, Node):
return False
if self.grid != other.grid:
return False
if len(self.cars) != len(other.cars):
return False
for i in range(len(self.cars)):
if self.cars[i] != other.cars[i]:
return False
return True
def __str__(self):
return self.grid.lineString()
def copy(self):
return Node(self.grid.copy(), copyCarsList(self.cars), self.parent, self.children)
def isGoal(self):
for car in self.cars:
if car.symbol == "A":
if car.isAtExit():
return True
else:
return False
# If A is not there, then automatically becomes the goal state.
return True
def generateChild(self, car:Car, move:int):
if move == 0:
raise Exception("Cannot Generate A Child Without A Move")
if not canMove(car, move, self.cars, self.grid):
return None
newCar = car.copy()
newCar.move(move)
newGrid = self.grid.copy()
newGrid.updateGrid(newCar, car)
# newCars = copyCarsList(self.cars)
newCars = self.cars.copy() # Shallow copy because we'll replace the moved car later.
exitingCar = None
# Updating cars list.
for i in range(len(newCars)):
if newCars[i].symbol == car.symbol:
if newCar.isAtExit() and newCar.orientation == "x" and newCar.symbol != "A":
exitingCar = newCars.pop(i)
break
else:
newCars[i] = newCar
break
newNode = Node(newGrid, newCars, self)
newNode.move = moveFromParent(car, move)
if exitingCar is not None:
newNode.exitedCars.append(exitingCar)
self.children.append(newNode)
return newNode
def expandChildren(self):
children = []
for car in self.cars:
for move in [4,3,2,1,-1,-2,-3,-4]:
child = self.generateChild(car, move)
if child is not None:
children.append(child)
return children
def isRoot(self):
return self.parent is None
def cost(self):
if self.isRoot():
return 0
cost = 1
parent = self.parent
while not parent.isRoot():
cost += 1
parent = parent.parent
return cost
def isInBorder(positions):
for pos in positions.position:
# Checking if position is within borders.
if pos[0] < GRID_MIN or pos[0] > GRID_MAX or pos[1] < GRID_MIN or pos[1] > GRID_MAX:
return False
return True
def hasPositionConflict(oldGrid:Grid, carSymbol:str, newPosition:Position):
for pos in newPosition.position:
if oldGrid.grid[pos[0]][pos[1]] != carSymbol and oldGrid.grid[pos[0]][pos[1]] != ".":
return True
return False
def canMove(car:Car, numOfMoves:int, cars:list, grid:Grid):
if not car.canUseFuel(numOfMoves):
return False
newPosition = car.nextPosition(numOfMoves)
if not isInBorder(newPosition):
return False
if hasPositionConflict(grid, car.symbol, newPosition):
return False
# Checking each position along the way.
if numOfMoves < 0:
start = numOfMoves
end = 0
else:
start = 0
end = numOfMoves
for intermediateMove in range(start, end):
intermediatePosition = car.nextPosition(intermediateMove)
if hasPositionConflict(grid, car.symbol, intermediatePosition):
return False
return True
def copyCarsList(cars):
newCars = list()
for car in cars:
newCars.append(car.copy())
return newCars
def moveFromParent(car:Car, move:int):
output = ""
if car.orientation == "x" and move > 0:
output = "{} {:>5} {}".format(car.symbol, "right", abs(move))
elif car.orientation == "x" and move < 0:
output = "{} {:>5} {}".format(car.symbol, "left", abs(move))
elif car.orientation == "y" and move > 0:
output = "{} {:>5} {}".format(car.symbol, "down", abs(move))
elif car.orientation == "y" and move < 0:
output = "{} {:>5} {}".format(car.symbol, "up", abs(move))
return output
def generateStartNode(puzzleLine:str):
return Node(Grid(getGrid(puzzleLine)), generateCarList(puzzleLine))