-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
180 lines (145 loc) · 6.45 KB
/
game.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
import numpy as np
from collections import deque
import gym
import random
import torch
import torch.nn.functional as F
SNAKE=1
FRUIT=2
BLACK = ( 17., 18., 13.)
RED = (255., 0., 0.)
GREEN = ( 0., 255., 0.)
WHITE = (255., 255., 255.)
directions=[(0,1),(-1,0),(0,-1),(1,0)]
def taxi_distance(t1,t2):
s=0
for x,y in zip(t1,t2):
s+=abs(x-y)
return s
class SnakeGame(gym.Env):
def __init__(self,dim=(10,10),walls=False,store_render=True,device='cuda',**kwargs):
self.on_noob=kwargs.get('on_noob','stay')
self.big_snake=kwargs.get('big_snake',True)
self.mult_channels=kwargs.get('mult_channels',True)
self.dim=dim
self.action_space=gym.spaces.Discrete(4)
self.observation_space=gym.spaces.Box(0,3,shape=self.dim)
self.reward_range=(-1,1)
self.device=device
self.walls=walls
self.observation_size=kwargs.get('observation_size',None) ##tuple saying
assert not self.observation_size or \
( self.observation_size%2 ==0 and self.walls and self.observation_size<dim[0] and self.observation_size<dim[1])
self.store_render=store_render
self.reset()
def get_board(self):
if not self.mult_channels:
return torch.tensor(self.board,device=self.device,dtype=torch.float32).unsqueeze(0).unsqueeze(0)
elif not self.observation_size:
tb=torch.zeros(1,3,self.dim[0],self.dim[1],dtype=torch.float,device=self.device)
tb[(0,0)+self.fruit]=1
tb[(0,1)+self.snake[-1]]=1
for idx,tup in enumerate(self.snake):
tb[(0,2)+tup]=idx+1
return tb
##if we observe only the surroundings of the head
observation_size=self.observation_size
img=torch.tensor(self.render()).transpose(0,1).transpose(0,2)
img = img.float() / 255
# Pad envs so we ge tthe correct size observation even when the head of the snake
# is close to the edge of the environment
padding = [int(observation_size/2), int(observation_size/2), ] * 2
padded_img = F.pad(img, padding)
sh=self.snake[-1]
print(padded_img.shape,sh)
observations = padded_img[
:,
int(sh[0]):int(sh[0]+observation_size),
int(sh[1]):int(sh[1]+observation_size)
]
print(observations.shape)
observations = observations.reshape(1,-1)
return observations.to(device=self.device)
def reset(self):
self.board=np.zeros(self.dim)
self.snake=deque()
self.empty=set([(i,j) for i in range(self.dim[0]) for j in range(self.dim[1])])
snake_head=self.random_pos()
self.snake.append(snake_head)
self.board[snake_head]=SNAKE
if self.big_snake: ##begin with snake of length 3
dir1=random.randint(0,3)
dir2=random.randint(0,3)
if dir2!=dir1 and dir2%2==dir1%2:
dir2=dir1
snak=snake_head
for d in [dir1,dir2]:
snak=tuple(((snak[i]+directions[d][i]) for i in [0,1]))
snak=tuple(snak[i]%self.dim[i] for i in [0,1])
self.empty.remove(snak)
self.snake.append(snak)
self.board[snak]=SNAKE
self.fruit=self.random_pos()
self.board[self.fruit]=FRUIT
self.t=0
self.last_t_eat=0
self.d=taxi_distance(snake_head,self.fruit)
self.last_move=None
if self.store_render: self.board_store=[self.render()]
return self.board
def random_pos(self):
if not self.empty: return None
pos=random.sample(self.empty,1)
self.empty.remove(pos[0])
return pos[0]
def step(self,action):
snake_head=self.snake[-1]
self.t+=1
snake_head=tuple(((snake_head[i]+directions[action][i]) for i in [0,1]))
##Some death conditions
if not self.walls: snake_head=tuple(snake_head[i]%self.dim[i] for i in range(2))
if self.walls and (not 0<=snake_head[0]<self.dim[0] \
or not 0<=snake_head[1]<self.dim[1]): ##wall colision
return self.get_board(),-0.25,True,{}
if self.last_move and len(self.snake)>1 and action%2==self.last_move%2 and action!=self.last_move:
if self.on_noob=='stay':
return self.get_board(),0,False,{}
elif self.on_noob=='forward':
action=(action+2)%4
self.last_move=action
if self.board[snake_head]==SNAKE:
return self.get_board(),-0.25,True,{}
self.snake.append(snake_head)
self.board[snake_head]=SNAKE
if snake_head==self.fruit:
self.fruit=self.random_pos()
self.board[self.fruit]=FRUIT
self.d=taxi_distance(snake_head,self.fruit)
self.last_t_eat=self.t
if self.store_render: self.board_store.append(self.render())
return self.get_board(),1,len(self.snake)==self.dim[0]*self.dim[1],{}
self.empty.remove(snake_head)
snake_tail=self.snake.popleft()
self.empty.add(snake_tail)
self.board[snake_tail]=0
if self.store_render: self.board_store.append(self.render())
return self.get_board(),0,False,{}
"""
renders the game.
Creates a rgb array with the colors corresponding to the current state of the game
@:parameter mode mode of render request
'rgb' : @return array of rgb
"""
def render(self):
#grid = np.zeros((self.dim[0],self.dim[1],3))
grid = np.zeros((self.dim[0],self.dim[1]), dtype = (int,3))
for i in range(self.dim[0]):
for j in range(self.dim[1]):
if self.board[i,j] == SNAKE:
grid[i,j] = np.array(GREEN)
elif self.board[i,j] == FRUIT:
grid[i,j] = np.array(RED)
#We use a different color to the snake's head:
head = self.snake[-1]
grid[head[0],head[1]] = np.array(WHITE)
return grid