-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCar.py
136 lines (112 loc) · 4.06 KB
/
Car.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
import numpy as np
from math import *
def genMoveDict():
def mergeSortDict(arr):
if len(arr) > 1:
mid = len(arr) // 2
L = arr[:mid]
R = arr[mid:]
mergeSortDict(L)
mergeSortDict(R)
i = j = k = 0
# Copy data to temp arrays L[] and R[]
while i < len(L) and j < len(R):
if list(L[i].keys())[0] < list(R[j].keys())[0]:
arr[k] = L[i]
i += 1
else:
arr[k] = R[j]
j += 1
k += 1
# Checking if any element was left
while i < len(L):
arr[k] = L[i]
i += 1
k += 1
while j < len(R):
arr[k] = R[j]
j += 1
k += 1
moveArr = [[] for i in range(7)]
for x in range(13):
for y in range(13):
adjPos = np.array([6, 6]) - np.array([x, y])
if (round(hypot(adjPos[1], adjPos[0]), 0) < 7):
d = round(hypot(adjPos[1], adjPos[0]))
a = round(atan2(adjPos[0], adjPos[1]), 2)
if a < 0:
a = a + round(2 * pi, 2)
moveArr[d].append({a: adjPos.tolist()})
for d in range(7):
mergeSortDict(moveArr[d])
moveArr[d] = [list(innerDict.values())[0] for innerDict in moveArr[d]]
return moveArr
# Holds all information that pertains to each individual car
class Car:
moveDict = genMoveDict()
def __init__(self, position = [0,0]):
self.position = position
self.a = 0
self.v = 0
def updatePosition(self):
self.position = map(sum, [self.velocity*x for x in self.direction], self.position)
def updateEdges(self, edges):
self.edges = edges
def right(self):
if self.position[0] < self.position[1]:
if sum(self.position) == 2:
self.direction = map(sum, self.direction, [1,-1])
if self.direction[1] >= 0:
self.direction = map(sum, self.direction, [1,1])
else:
if sum(self.position) == -2:
self.direction = map(sum, self.direction, [-1, 1])
else:
self.direction = map(sum, self.direction, [-1, -1])
def left(self):
if self.position[0] < self.position[1]:
if sum(self.position) == -2:
self.direction = map(sum, self.direction, [1,-1])
if self.direction[0] < 1:
self.direction = map(sum, self.direction, [-1,-1])
else:
if sum(self.position) == 2:
self.direction = map(sum, self.direction, [-1, 1])
else:
self.direction = map(sum, self.direction, [1, 1])
def velocityUp(self):
if self.velocity < 5:
self.velocity += 1
def velocityDown(self):
if self.velocity >= 0:
self.velocity -= 1
def getPosition(self):
return self.position
def genPossMoves(self):
"""
Generates the array of all valid next moves for the input car
:param car: The car that is going to move
:return: An array of possible next moves
"""
# all v = 1 moves valid for stopped car
if self.v == 0:
return moveDict[1]
# when v > 0
positionRatio = self.a / 2 * pi
possMoves = []
# adjacent distances
for i in range(-1, 2):
if self.v + i < 0 or self.v + i > 6:
continue
elif self.v + i == 0:
possMoves.append([0, 0])
continue
currAlignment = round(positionRatio * len(moveDict[self.v + i]))
# adjacent turns
for j in range(-1, 2):
if currAlignment + j > len(moveDict[self.v + i]):
j = 0
elif currAlignment + j < 0:
j = len(moveDict[self.v + i]) - 1
possMoves.append(moveDict[self.v + i][currAlignment + j])
return possMoves