tic-far-toe/GameMap.py

40 lines
1.2 KiB
Python
Raw Normal View History

class GameMap:
def __init__(self, max_size_x = float('inf'), max_size_y = float('inf')):
self.max_size_x = max_size_x
self.max_size_y = max_size_y
self.data = {}
def __str__(self):
return str(self.data)
def get_max_size_x(self):
return self.size_x
def get_max_size_y(self):
return self.size_y
2022-07-25 14:48:34 +00:00
def add_mark(self, x, y, player):
2022-08-07 06:24:05 +00:00
# Can be 0 or can be a max value, so,
# this is border-inclusive.
if (x <= self.max_size_x and y <= self.max_size_y) and (x >= 0 and y >= 0):
if (x, y) not in self.data:
self.data[(x,y)] = player
2022-08-07 06:23:52 +00:00
player.add_mark(x,y)
else:
2022-08-07 06:24:27 +00:00
errorMessage = "The mark is already busy by the player '" + str(self.data[(x, y)].get_name()) + "'"
raise KeyError(errorMessage)
else:
errorMessage = "The mark" + str((x,y)) + " is beyond preset max values, max_x - " + str(self.max_size_x) + ", max_y - " + str(self.max_size_y)
raise IndexError(errorMessage)
2022-07-25 14:50:07 +00:00
def remove_mark(self, x, y):
data.pop((x,y))
def clear_map(self):
data.clear()