-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
executable file
·126 lines (100 loc) · 3.65 KB
/
game.py
File metadata and controls
executable file
·126 lines (100 loc) · 3.65 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
import random
import numpy as np
from utils import clear, SNAKE, FRUIT, GRID_SIZE
class Game(object):
def __init__(self, seed = None):
if(seed != None):
random.seed(seed)
self.win = False
self.moves = 0
self.points = 0
self.finished = False
self.size = GRID_SIZE # constant
self.head = [self.size//2, self.size//2]
self.player = [[self.size//2, self.size//2], [self.size//2, self.size//2 -1], [self.size//2, self.size//2 -2]]
# self.fruit = [self.size//2, self.size//2 +1]
self.spawnFruit()
def step(self, move):
cur = self.player[0]
prox = [cur[0]+move[0], cur[1]+move[1]]
if not self.inBounds(prox) or self.isFinished() or prox in self.player:
self.finished = True
return
self.moves += 1
self.head = list(prox)
self.player.insert(0, list(prox))
if (prox == self.fruit):
self.points += 1
self.spawnFruit()
else:
self.player.pop()
def spawnFruit(self):
if len(self.player) == self.size**2 :
finished = True
win = True
return None
fruit = [random.randint(0, self.size-1), random.randint(0, self.size-1)]
while(fruit in self.player):
fruit = [random.randint(0, self.size-1), random.randint(0, self.size-1)]
self.fruit = fruit
def isFinished(self):
return self.finished
def inBounds(self, prox):
return not (prox[0] < 0 or prox[0] >= self.size or prox[1] < 0 or prox[1] >= self.size)
def vision(self, dir):
assert(len(dir) == 2)
cur = [self.player[0][0]+dir[0], self.player[0][1]+dir[1]]
body_found, fruit_found = False, False
body_dist, fruit_dist = np.inf, np.inf
dist = 1
while (self.inBounds(cur)):
if (not body_found and cur in self.player):
body_found = True
body_dist = dist
if (not fruit_found and cur == self.fruit):
fruit_found = True
fruit_dist = dist
dist+=1
cur[0] += dir[0]
cur[1] += dir[1]
body_dist = 1.0/body_dist
fruit_dist = 1.0/fruit_dist
wall_dist = 1.0/dist
return [fruit_dist, body_dist, wall_dist]
def getState(self):
state = []
# counter clockwise vision
directions = [[-1, 0], [-1, -1], [0, -1], [1, -1], # up, up-left, left, down-left
[1, 0], [1, 1], [0, 1], [-1, 1]] # down, down-right, right, up-right
for dir in directions:
state.extend(self.vision(dir))
return state
print(state)
def getReward(self):
return self.moves + 100*(self.points**2) + int(self.win)* 200*(self.size**2)
def getPoints(self):
return self.points
def getGrid(self, show_fruit = True):
grid = np.zeros(shape=(self.size, self.size), dtype = np.int32)
for x, y in self.player:
grid[int(x)][int(y)] = SNAKE
if (show_fruit):
grid[self.fruit[0], self.fruit[1]] = FRUIT
return grid
def getMoves(self):
return self.moves
def getWin(self):
return self.win
def printGrid(self):
clear()
grid = self.getGrid()
for x in range(self.size):
for y in range(self.size):
if(grid[x][y] == FRUIT):
print("X", end = "")
elif(grid[x][y] == SNAKE):
print("*", end = ""),
else:
print(".", end = ""),
print("\n")
# print(grid)