-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard.py
More file actions
92 lines (73 loc) · 3.3 KB
/
board.py
File metadata and controls
92 lines (73 loc) · 3.3 KB
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
from random import randint
from gameobjects import *
class Board:
max_random_tries = 5
def __init__(self, board_width, board_height, canvas_width, canvas_height, snake, max_nr_food, nr_walls, test_config):
self.snake = snake
self.width = board_width
self.height = board_height
self.board = [[GameObject.EMPTY for x in range(board_width)] for y in range(board_height)]
self.block_width = canvas_width / board_width
self.block_height = canvas_height / board_height
self.max_nr_food = max_nr_food
if not test_config:
for i in range(nr_walls):
self.spawn_wall()
for i in range(max_nr_food):
self.spawn_new_food()
def get_game_object_at(self, x, y):
if self.board[x][y] is None:
return GameObject.EMPTY
if self.snake.contains_head(x, y):
return GameObject.SNAKE_HEAD
if self.snake.contains_body(x, y):
return GameObject.SNAKE_BODY
return self.board[x][y]
def is_wall_at(self, x, y):
return self.board[x][y] == GameObject.WALL
def set_game_object_at(self, x, y, game_object):
self.board[x][y] = game_object
def draw(self, canvas):
for x in range(0, self.width):
for y in range(0, self.height):
draw_x = x * self.block_width
draw_y = y * self.block_height
canvas.create_rectangle(draw_x, draw_y, draw_x + self.block_width, draw_y + self.block_height,
fill=self.get_game_object_at(x, y).getColor(), outline="")
def eat_food(self, x, y):
self.board[x][y] = GameObject.EMPTY
self.spawn_new_food()
def get_copy(self):
copy = [[GameObject.EMPTY for x in range(self.width)] for y in range(self.height)]
for x in range(self.width):
for y in range(self.height):
copy[x][y] = self.get_game_object_at(x, y)
return copy
def spawn_new_food(self):
self.board[4][0] = GameObject.FOOD
def spawn_wall(self):
self.spawn_random_object(GameObject.WALL)
def spawn_random_object(self, gameObjectType):
new_x, new_y = self.get_free_xy()
self.set_game_object_at(new_x, new_y, gameObjectType)
def get_free_xy(self):
new_x = randint(0, self.width - 1)
new_y = randint(0, self.height - 1)
count = 0
# try to find a random position
while not (self.get_game_object_at(new_x, new_y) == GameObject.EMPTY) and count < self.max_random_tries:
count += 1
new_x = randint(0, self.width - 1)
new_y = randint(0, self.height - 1)
# if no random position could be guessed, try a more decent approach
if not self.get_game_object_at(new_x, new_y) == GameObject.EMPTY:
available = []
for x in range(self.width):
for y in range(self.height):
if self.get_game_object_at(x, y) == GameObject.EMPTY:
available.append((x, y))
if len(available) == 0:
raise RuntimeError("Congratulations, you broke the game by filling each cell of the board!")
else:
new_x, new_y = available[randint(0, len(available) - 1)]
return new_x, new_y