-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake_ai_rev_05.py
284 lines (282 loc) · 10.8 KB
/
snake_ai_rev_05.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
import random
import numpy as np
def create_apple(board):
possibleList = []
for i in range(len(board)):
for j in range(len(board[i])):
if board[i][j] == 0:
possibleList.append((i,j))
if len(possibleList) >= 1:
appleSpot = possibleList[random.randint(0, len(possibleList) - 1)]
board[appleSpot[0]][appleSpot[1]] = -1
return board
def display_board(board):
for i in range(len(board)):
string = ''
for j in range(len(board[i])):
currentVal = board[i][j]
if currentVal == 0:
string += '· '
elif currentVal == -1:
string += 'A '
elif currentVal == 1:
string += 'S '
else:
string += '* '
print(string)
def move_snake(board, move):
for i in range(len(board)):
for j in range(len(board[i])):
# Increment The Snake Pos
if not board[i][j] <= 0:
board[i][j] += 1
maxPos = (0,0)
max = 0
boardMaximum = 0
for i in range(len(board)):
for j in range(len(board[i])):
if board[i][j] > boardMaximum:
boardMaximum = board[i][j]
# Get current position of the snake head
currentPos = (0,0)
for i in range(len(board)):
for j in range(len(board[i])):
if board[i][j] == 2:
currentPos = (i,j)
applePos = (0,0)
for i in range(len(board)):
for j in range(len(board[i])):
if board[i][j] == -1:
applePos = (i,j)
if move == 'w':
# Check if out of board range
if not (currentPos[0] - 1 < 0):
# Check if snake hit itself
if not (board[currentPos[0] - 1][currentPos[1]] >= 1) or (board[currentPos[0] - 1][currentPos[1]] == boardMaximum):
ongoing = True
if (currentPos[0] - 1, currentPos[1]) == applePos:
create_apple(board)
else:
# Delete the last part of the snake
for i in range(len(board)):
for j in range(len(board[i])):
if board[i][j] > max:
maxPos = (i,j)
max = board[i][j]
board[maxPos[0]][maxPos[1]] = 0
board[currentPos[0] - 1][currentPos[1]] = 1
else:
ongoing = False
else:
ongoing = False
elif move == 'a':
if not (currentPos[1] - 1 < 0):
if not (board[currentPos[0]][currentPos[1] - 1] >= 1) or (board[currentPos[0]][currentPos[1] - 1] == boardMaximum):
ongoing = True
if (currentPos[0], currentPos[1] - 1) == applePos:
create_apple(board)
else:
# Delete the last part of the snake
for i in range(len(board)):
for j in range(len(board[i])):
if board[i][j] > max:
maxPos = (i,j)
max = board[i][j]
board[maxPos[0]][maxPos[1]] = 0
board[currentPos[0]][currentPos[1] - 1] = 1
else:
ongoing = False
else:
ongoing = False
elif move == 's':
# Check if out of board range
if not (currentPos[0] + 1 >= len(board)):
# Check if snake hit itself
if not (board[currentPos[0] + 1][currentPos[1]] >= 1) or (board[currentPos[0] + 1][currentPos[1]] == boardMaximum):
ongoing = True
if (currentPos[0] + 1,currentPos[1]) == applePos:
create_apple(board)
else:
# Delete the last part of the snake
for i in range(len(board)):
for j in range(len(board[i])):
if board[i][j] > max:
maxPos = (i,j)
max = board[i][j]
board[maxPos[0]][maxPos[1]] = 0
board[currentPos[0] + 1][currentPos[1]] = 1
else:
ongoing = False
else:
ongoing = False
elif move == 'd':
if not (currentPos[1] + 1 >= len(board[0])):
if not (board[currentPos[0]][currentPos[1] + 1] >= 1) or (board[currentPos[0]][currentPos[1] + 1] == boardMaximum):
ongoing = True
if (currentPos[0], currentPos[1] + 1) == applePos:
create_apple(board)
else:
# Delete the last part of the snake
for i in range(len(board)):
for j in range(len(board[i])):
if board[i][j] > max:
maxPos = (i,j)
max = board[i][j]
board[maxPos[0]][maxPos[1]] = 0
board[currentPos[0]][currentPos[1] + 1] = 1
else:
ongoing = False
else:
ongoing = False
return (board, ongoing)
def createHamiltonian(boardSize):
# Create Board Of All Zeroes
board = []
for i in range(boardSize):
board.append([])
for j in range(boardSize):
board[i].append(0)
# Top Left = 1
board[0][0] = 1
count = 2
# Go up and down the rows excluding the first row and increment
# Looping through columns
for i in range(len(board)):
# if going down
if i % 2 == 0:
for j in range(1, len(board)):
board[j][i] = count
count += 1
# if going up
else:
for j in range(len(board) -1, 0, -1):
board[j][i] = count
count += 1
# Increment the rest of the top row excluding the top left
for i in range(len(board[0]) - 1, 0, -1):
board[0][i] = count
count += 1
return board
def moveAi(board, hamiltonian, boardSize, skipCount):
# Check if you can jump ahead in the cycle
newHamiltonian = hamiltonian.copy()
currentPos = (0,0)
for i in range(len(board)):
for j in range(len(board)):
if board[i][j] == 1:
currentPos = (i,j)
currentHamil = hamiltonian[currentPos[0]][currentPos[1]]
if skipCount <= 0:
# Modify the hamiltonian so the current snake head is at pos 1 is at cycle. Shift everything else
for i in range(len(newHamiltonian)):
for j in range(len(newHamiltonian[i])):
if newHamiltonian[i][j] >= currentHamil:
newHamiltonian[i][j] -= (currentHamil - 1)
else:
newHamiltonian[i][j] += ((boardSize ** 2) - currentHamil) + 1
modifiedHeadSpot = 1
boardMax = np.max(board)
tailPosList = []
for i in range(len(board)):
for j in range(len(board[i])):
if board[i][j] >= 1:
tailPosList.append(newHamiltonian[i][j])
tailPos = min(tailPosList)
# Down,up,left,right checks to see how much would be skipped if moved there
spotSkips = []
if currentPos[0] + 1 < boardSize:
spotSkips.append((newHamiltonian[currentPos[0] + 1][currentPos[1]] - 1, 's'))
if currentPos[0] - 1 >= 0:
spotSkips.append((newHamiltonian[currentPos[0] - 1][currentPos[1]] - 1, 'w'))
if currentPos[1] + 1 < boardSize:
spotSkips.append((newHamiltonian[currentPos[0]][currentPos[1] + 1] - 1, 'd'))
if currentPos[1] - 1 >= 0:
spotSkips.append((newHamiltonian[currentPos[0]][currentPos[1] - 1] - 1, 'a'))
spotSkips.sort(reverse = True)
blockedSpots = []
for i in range(len(board)):
for j in range(len(board[i])):
if board[i][j] >= 1 and not board[i][j] == boardMax:
blockedSpots.append(newHamiltonian[i][j] - 1)
for i in range(len(spotSkips) - 1, -1, -1):
if spotSkips[i][0] in blockedSpots:
spotSkips.pop(i)
# Find the maximum distance that can be skipped
maxPossibleSkip = (boardSize ** 2) - tailPos
# Find distance to apple through normal hamiltonian cycle
applePos = 0
for i in range(len(board)):
for j in range(len(board[i])):
if board[i][j] == -1:
applePos = newHamiltonian[i][j] - 1
break
if not applePos == 0:
break
tempSkipCount = len(tailPosList)
# loop through possible moves, prioritizing the maximum possible jump towards the apple at each spot
for skip in spotSkips:
if skip[0] < applePos:
return (skip[1], skipCount)
elif skip[0] == applePos:
return (skip[1], len(tailPosList))
# If you can't jump ahead in the cycle, move normally through the hamiltonian
if currentHamil == len(board) ** 2:
nextHamil = 1
else:
nextHamil = currentHamil + 1
nextHamilPos = (0,0)
for i in range(len(hamiltonian)):
for j in range(len(hamiltonian)):
if hamiltonian[i][j] == nextHamil:
nextHamilPos = (i,j)
if currentPos[0] - 1 == nextHamilPos[0]:
return ('w', skipCount - 1)
elif currentPos[0] + 1 == nextHamilPos[0]:
return ('s', skipCount - 1)
elif currentPos[1] - 1 == nextHamilPos[1]:
return ('a', skipCount - 1)
else:
return ('d', skipCount - 1)
valid = False
while not valid:
boardSize = int(input('Enter Board Size: ').strip())
if boardSize % 2 == 0:
valid = True
else:
print('Invalid Board Size! Enter In A Even Number.')
board = []
snake_length = 1
# Create The Base Board
for i in range(boardSize):
board.append([])
for j in range(boardSize):
board[i].append(0)
# Add The Snakes Starting Position
snake_starting_length = random.randint(0, boardSize - 1)
snake_starting_width = random.randint(0, boardSize - 1)
board[snake_starting_length][snake_starting_width] = 1
# Generate Original Apple
board = create_apple(board)
# Create Initial Hamiltonian
hamiltonian = createHamiltonian(boardSize)
# Loop To Play Game
ongoing = True
moveCount = 0
skipCount = 0
while ongoing:
display_board(board)
print()
move = moveAi(board,hamiltonian, boardSize, skipCount)
print('Move: {}'.format(move[0]))
skipCount = move[1]
data = move_snake(board, move[0])
board = data[0]
ongoing = data[1]
length = np.max(board)
print('Length: {}'.format(length))
print('Moves before it can skip: {}'.format(skipCount))
moveCount += 1
if length == boardSize ** 2:
ongoing = False
display_board(board)
print('Game Won. Took {} Moves.'.format(moveCount))