-
Notifications
You must be signed in to change notification settings - Fork 3
/
Player.py
59 lines (51 loc) · 1.7 KB
/
Player.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
__author__ = 'Aaron'
# PLAYER DEFINITION
class Player():
def __init__(self, start_position, direction, player_color, player_number):
self.positions = [start_position]
self.color = player_color
self.direction = direction
self.number = player_number
self.position_set = set(self.positions)
self.transitions = []
self.delete = None, None
self.length = 1
self.score = 0
def update(self):
"""
Calculate the next space to occupy.
"""
row, column = self.get_position()
if self.direction == 'LEFT':
column += -1
elif self.direction == 'RIGHT':
column += 1
elif self.direction == 'UP':
row += -1
elif self.direction == 'DOWN':
row += 1
return row, column
def set_position(self, row, column):
"""
Cement the calculated position once validated.
Make sure not to add more positions than current snake length.
:param row: row to move to
:param column: column to move to
:return -1 if doubling back on itself, 0 otherwise
"""
if (row, column) in self.position_set:
return -1
else:
self.transitions.append(self.direction)
self.positions.append((row, column))
self.position_set.add((row, column))
# Restrict length of snake
if len(self.positions) > self.length:
self.delete = self.positions.pop(0), self.transitions.pop(0)
return 0
def get_position(self):
"""
Returns current position of player.
:return: (row, column)
"""
return self.positions[-1]