-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.py
58 lines (48 loc) · 1.63 KB
/
snake.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
class Snake:
def __init__(self):
self.locations=[(0,0),(1,0)]
self.direction="r"
self.popped=None
def isOccupied(self,x,y):
return (x,y) in self.locations
def move(self, dir=None, board=None):
last=self.locations[-1]
moveTo=None
if dir == None:
dir = self.direction
if dir=="r":
moveTo=(last[0]+1,last[1])
self.direction='r'
elif dir=="l":
moveTo=(last[0]-1,last[1])
self.direction = 'l'
elif dir=="u":
moveTo=(last[0],last[1]-1)
self.direction = 'u'
elif dir=="d":
moveTo=(last[0],last[1]+1)
self.direction = 'd'
if not (0<=moveTo[0]<len(board) and 0<=moveTo[1]<len(board)):
return False, False
if board[moveTo[0]][moveTo[1]] == 1:
return False, False
if board[moveTo[0]][moveTo[1]] == 2:
self.eat()
board[moveTo[0]][moveTo[1]] = 1
return True, True
self.locations.append(moveTo)
self.popped = self.locations.pop(0)
board[self.popped[0]][self.popped[1]]=0
board[self.locations[-1][0]][self.locations[-1][1]]=1
return True, False
def eat(self):
dir = self.direction
last = self.locations[-1]
if dir=="r":
self.locations.append((last[0]+1,last[1]))
elif dir=="l":
self.locations.append((last[0]-1,last[1]))
elif dir=="u":
self.locations.append((last[0],last[1]-1))
elif dir=="d":
self.locations.append((last[0],last[1]+1))