-
Notifications
You must be signed in to change notification settings - Fork 0
/
ai.py
62 lines (44 loc) · 1.22 KB
/
ai.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
from snake import *
from multiprocessing import Process
import SendKeys
import json
# --------------------------------------
possible_commands = list('wasd')
key_to_hex = {
'w': 0x57,
'a': 0x41,
's': 0x53,
'd': 0x44,
}
# --------------------------------------
def game_dict():
f = open('log.txt', 'r').read()
return json.loads(f)
# --------------------------------------
class Session:
def __init__(self):
self.game = Game(10)
self.fitness = 0
self.moves = []
self.distances = []
def play(self):
game_process = Process(target=self.game.loop)
game_process.start()
first = True
while True:
if first:
first = False
continue
elif not game_dict()['play']:
break
self.send_command()
game_process.terminate()
def send_command(self):
curr_key = random.choice(possible_commands)
self.moves.append(curr_key)
SendKeys.SendKeys(curr_key)
def head_distance_from_food(self):
head_y = game_dict()['snake'][-1][0]
head_x = game_dict()['snake'][-1][1]
# def evaluate(self):
# return game_dict()['score'] *