-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake_ai_rev_01_nogui.py
210 lines (210 loc) · 7.53 KB
/
snake_ai_rev_01_nogui.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
import random
import time
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 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):
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 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'
elif currentPos[0] + 1 == nextHamilPos[0]:
return 's'
elif currentPos[1] - 1 == nextHamilPos[1]:
return 'a'
else:
return 'd'
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.')
timeStart = time.time()
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
while ongoing:
move = moveAi(board,hamiltonian)
data = move_snake(board, move)
board = data[0]
ongoing = data[1]
length = np.max(board)
moveCount += 1
if length == boardSize ** 2:
ongoing = False
print('Game Won. Took {} Moves.'.format(moveCount))
timeEnd = time.time()
timeTotal = timeEnd - timeStart
print('Time To Complete: {}'.format(timeTotal))
print('Time Per Thousand Moves: {}'.format(timeTotal/(moveCount/1000)))