-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathboard.py
89 lines (65 loc) · 2.67 KB
/
board.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
from random import randint
class Board:
def __init__(self, columns, rows, fill):
self.board = [[0 for j in range(columns)] for i in range(rows)]
self.fill = fill
self.col = columns
self.rows = rows
self.first = fill[-1]
self.put_food(fill)
def board_init(self, fill):
self.board = [[0 for j in range(self.col)] for i in range(self.rows)]
self.fill = fill
self.first = fill[-1]
for i in self.fill:
if i == self.first:
self.board[i[0]%self.rows][i[1]%self.col] = 2
else:
self.board[i[0]%self.rows][i[1]%self.col] = 1
self.board[self.food[0]][self.food[1]] = 3
def food_process(self, fill):
if self.check_food(fill):
self.eaten = True
self.put_food(fill)
else:
self.eaten = False
def normalize_fill(self, fill):
return [[i[0]%self.rows, i[1]%self.col] for i in fill]
def check_food(self, fill):
if self.food in self.normalize_fill(fill):
return True
return False
def put_food(self, fill):
while True:
x,y = randint(0,self.col-1), randint(0, self.rows-1)
if [x,y] not in self.normalize_fill(fill):
self.board[x][y] = 3
self.food = [x,y]
return
def show_board(self, snake):
board_ = "|"
for i in self.board:
for j in i:
# Snake
if j==1:
board_ += " ■"
# Arrows of the snake
elif j==2:
if snake.dir == "UP":
board_ += " ▲"
elif snake.dir == "LEFT":
board_ += " ◀"
elif snake.dir == "RIGHT":
board_ += " ▶"
elif snake.dir == "DOWN":
board_ += " ▼"
elif j==3:
# Eating Stuff
board_ += " ♥"
# Board BackGround
else:
board_ += "⬜"
board_ += "|\n|"
# board_ += "".join(["_ "*self.col])
# board_ += "\n`"
print(board_)