-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake
More file actions
executable file
·127 lines (108 loc) · 4.11 KB
/
snake
File metadata and controls
executable file
·127 lines (108 loc) · 4.11 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
#!/bin/python3
import argparse
import ai.play as play
import ai.replay as replay
import ai.train as train
import numpy as np
from engine.world import World
from engine.direction import Direction
from engine.exception.gameover import GameOver
from engine.entity.snake import Snake
from engine.entity.apple import Apple, AppleType
def play_mode():
world = World()
snake = Snake(world)
world.spawn_entity(snake)
world.spawn_entity(Apple(world, AppleType.GREEN))
world.spawn_entity(Apple(world, AppleType.GREEN))
world.spawn_entity(Apple(world, AppleType.RED))
while True:
world.render()
try:
key = input("Move (W/A/S/D) : ").lower()
except EOFError:
continue
if key == 'q':
print('Quitting the program')
break
try:
if key == 'w':
snake.move(Direction.NORTH)
elif key == 's':
snake.move(Direction.SOUTH)
elif key == 'a':
snake.move(Direction.WEST)
elif key == 'd':
snake.move(Direction.EAST)
except GameOver:
print("Game Over")
break
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Snake AI utility script")
subparsers = parser.add_subparsers(dest="command", required=True)
# Command: load
load_parser = subparsers.add_parser("load",
help="Load and run saved AI")
load_parser.add_argument("file",
help="Path to saved AI file")
load_parser.add_argument("-n", type=int, default=1,
help="Number of simulations")
load_parser.add_argument("--visual", action="store_true",
help="Enable visual display")
load_parser.add_argument("--step", action="store_true",
help="Enable step by step")
# Command: replay
replay_parser = subparsers.add_parser("replay",
help="Replay from saved replay file")
replay_parser.add_argument("file",
help="Path to replay file")
replay_parser.add_argument("episode", type=int,
help="Path to replay file", default=-1)
replay_parser.add_argument("--step", action="store_true",
help="Enable step by step")
# Command: train
train_parser = subparsers.add_parser("train",
help="Train new AI and save progress")
train_parser.add_argument("filename",
help="Path to save training results")
train_parser.add_argument("--visual", action="store_true",
help="Enable visual display")
# Command: play
train_parser = subparsers.add_parser("play", help="Play a game")
args = parser.parse_args()
if args.command == "load":
if args.n < 1:
print("Number of simulations must be greater than 0")
exit(1)
sizes = []
for i in range(args.n):
try:
sizes.append(play.play(args.file, args.visual, args.step))
except FileNotFoundError:
print("{} not found".format(args.file))
exit(1)
except ValueError as e:
print(e)
exit(1)
print(f"[{i + 1}] Final size : {sizes[i]}")
print(f"\nSize mean: {np.mean(sizes)}, Max length: {np.max(sizes)}")
elif args.command == "replay":
try:
replay.play_replay(args.file, args.episode, args.step)
except FileNotFoundError:
print("{} not found".format(args.file))
exit(1)
except ValueError as e:
print(e)
exit(1)
elif args.command == "train":
try:
train.train(args.filename)
except FileNotFoundError:
print("{}.csv not access".format(args.filename))
exit(1)
except ValueError as e:
print(e)
exit(1)
elif args.command == "play":
play_mode()