-
Notifications
You must be signed in to change notification settings - Fork 1
/
Board.py
111 lines (77 loc) · 2.68 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from random import randrange, randint
from Character import Character
from Constants import *
import json
class Board:
size_x = 0
size_y = 0
max_characters = 20
active_characters_count = 0
characters = list()
created = False
boardBody = None
def __init__(self, size_x, size_y):
self.size_x = size_x
self.size_y = size_y
self.boardBody = [[None for a in range(self.size_y)] for b in range(self.size_x)]
print('{0}').format(len(self.boardBody))
def _is_created(self):
return self.created
def _has_character_on_game(self, unique_id):
'''
:param unique_id:
:return: Character or False
'''
for key, char in enumerate(self.characters):
if char.unique_id == unique_id:
return char, key
return False
def _has_character_on_board(self, unique_id):
'''
:param unique_id:
:return: Tuple(Character, (x,y)) or False
'''
for x in range(0, len(self.boardBody)):
for y in range(0, len(self.boardBody[x])):
if isinstance(self.boardBody[x][y], Character) and self.boardBody[x][y].unique_id == unique_id:
return self.boardBody[x][y], dict({'x': x, 'y': y})
return False
def _is_slot_free(self, x, y):
print(self.boardBody)
if isinstance(self.boardBody[x][y], Character):
return False
else:
return True
def _spawn_character(self, character, side):
import math
max_x = 0
max_y = 0
min_x = 0
min_y = 0
if side == TEAM_GOODGUYS:
max_x = (self.size_x/2) - 1
max_y = (self.size_y/2) - 1
min_x = 0
min_y = 0
else:
max_x = self.size_x
max_y = self.size_y
min_x = (self.size_x/2) + 1
min_y = (self.size_y/2) + 1
rand_x = randint(math.floor(min_x), math.floor(max_x))
rand_y = randint(math.floor(min_y), math.floor(max_y))
if not self._has_character_on_board(character.unique_id):
character.x = rand_x
character.y = rand_y
self.boardBody[rand_x][rand_y] = character
else:
print('Character already on board or slot is not free')
def move_character(self, unique_id, x, y):
if x and y:
char, pos = self._has_character_on_game(unique_id)
self.characters[pos].x = x
self.characters[pos].y = y
return True
return False
def to_JSON(self):
return json.dumps(self, default=lambda o: o.__dict__, sort_keys=True, indent=4)