-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.py
71 lines (61 loc) · 1.7 KB
/
user.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
import random
import copy
import sys
import os
UP = 0
RIGHT = 1
DOWN = 2
LEFT = 3
class User:
num = 1
def __init__(self, board_size):
self.name = User.num
User.num += 1
self.board_size = board_size
self.pos = [[random.randint(10, board_size - 10), random.randint(10, board_size - 10)]]
self.last_move = random.randint(0, 3)
self.eaten = False
def __del__(self):
User.num -= 1
def move(self):
head = copy.copy(self.pos[len(self.pos) - 1])
if self.last_move == DOWN:
if head[0] == self.board_size - 1:
print("Lost! 1")
return True
else:
head[0] += 1
self.pos.append(head)
elif self.last_move == RIGHT:
if head[1] == self.board_size - 1:
print("Lost! 2")
return True
else:
head[1] += 1
self.pos.append(head)
elif self.last_move == UP:
if head[0] == 0:
print("Lost! 3")
return True
else:
head[0] -= 1
self.pos.append(head)
elif self.last_move == LEFT:
if head[1] == 0:
print("Lost! 4")
return True
else:
head[1] -= 1
self.pos.append(head)
else:
print("Error")
exit()
if not self.eaten:
self.pos.pop(0)
else:
self.eaten = False
return False
def go(self, direction):
self.last_move = direction
def get_head(self):
return self.pos[len(self.pos) - 1]