2021-03-14 19:18:23 +01:00
|
|
|
import project_constants as const
|
2021-05-23 06:14:12 +02:00
|
|
|
from objects import tile as tl, agent as ag
|
2021-06-06 22:00:42 +02:00
|
|
|
from objects.mine_models.time_mine import TimeMine
|
2021-05-07 22:26:58 +02:00
|
|
|
import json_generator as jg
|
2021-03-12 11:49:19 +01:00
|
|
|
|
2021-03-26 16:36:21 +01:00
|
|
|
|
2021-03-12 11:49:19 +01:00
|
|
|
class Minefield:
|
2021-03-12 21:25:33 +01:00
|
|
|
def __init__(self, json_path):
|
2021-03-26 16:29:53 +01:00
|
|
|
self.turn = 0
|
|
|
|
|
2021-03-26 16:36:21 +01:00
|
|
|
self.agent = ag.Agent(const.MAP_RANDOM_10x10)
|
2021-05-21 14:00:26 +02:00
|
|
|
self.json_path = json_path
|
2021-03-12 11:49:19 +01:00
|
|
|
|
2021-03-12 21:25:33 +01:00
|
|
|
# open JSON with minefield info
|
2021-05-07 22:26:58 +02:00
|
|
|
json_gen = jg.JsonGenerator()
|
|
|
|
json_gen.load_from_a_file(json_path)
|
2021-03-12 11:49:19 +01:00
|
|
|
|
2021-03-12 21:25:33 +01:00
|
|
|
# create matrix of a desired size, fill it with default tile objects
|
|
|
|
self.matrix = [
|
|
|
|
[
|
2021-05-07 22:26:58 +02:00
|
|
|
tl.Tile(
|
|
|
|
(row, column),
|
|
|
|
terrain_type=json_gen.get_tile((row, column))["terrain"],
|
|
|
|
mine=jg.create_a_mine(json_gen.get_mine((row, column)), (row, column))
|
|
|
|
) for column in range(const.V_GRID_HOR_TILES)
|
2021-04-11 19:04:06 +02:00
|
|
|
] for row in range(const.V_GRID_VER_TILES)
|
2021-03-12 21:25:33 +01:00
|
|
|
]
|
2021-03-12 11:49:19 +01:00
|
|
|
|
2021-05-07 22:26:58 +02:00
|
|
|
# iterate through chained mines, set mine predecessors
|
|
|
|
for pair in jg.get_chained_mine_and_its_predecessor_pairs(json_gen.get_grid()):
|
|
|
|
successor_position, predecessor_position = pair
|
|
|
|
successor_row, successor_column = successor_position
|
|
|
|
predecessor_row, predecessor_column = predecessor_position
|
2021-03-12 11:49:19 +01:00
|
|
|
|
2021-05-07 22:26:58 +02:00
|
|
|
predecessor = self.matrix[predecessor_row][predecessor_column]
|
|
|
|
self.matrix[successor_row][successor_column].mine.predecessor = predecessor
|
2021-03-12 21:25:33 +01:00
|
|
|
|
2021-05-12 14:33:26 +02:00
|
|
|
def next_turn(self):
|
|
|
|
self.turn += 1
|
|
|
|
|
|
|
|
for row in range(const.V_GRID_VER_TILES):
|
|
|
|
for column in range(const.V_GRID_VER_TILES):
|
|
|
|
mine = self.matrix[row][column].mine
|
|
|
|
|
|
|
|
if mine is not None and isinstance(mine, TimeMine):
|
2021-05-18 20:37:31 +02:00
|
|
|
mine.timer = max(0, mine.starting_time - int(self.turn / 4))
|
2021-05-12 14:33:26 +02:00
|
|
|
|
2021-05-19 23:44:56 +02:00
|
|
|
def get_active_mines(self):
|
|
|
|
mines = list()
|
|
|
|
|
|
|
|
for row in range(const.V_GRID_VER_TILES):
|
|
|
|
for column in range(const.V_GRID_VER_TILES):
|
|
|
|
mine = self.matrix[row][column].mine
|
|
|
|
|
|
|
|
if mine is not None and mine.active:
|
|
|
|
# do not add mines with predecessors
|
|
|
|
if mine.type == 'chained' and mine.predecessor is not None:
|
|
|
|
continue
|
|
|
|
|
|
|
|
mines.append(mine)
|
|
|
|
|
|
|
|
return mines
|
|
|
|
|
2021-05-21 14:00:26 +02:00
|
|
|
def disarm_mine(self, x, y):
|
|
|
|
tile = self.matrix[x][y]
|
|
|
|
mine = tile.mine
|
|
|
|
mine.disarm()
|
|
|
|
|
2021-03-14 19:18:23 +01:00
|
|
|
# ================ #
|
|
|
|
# === MOVEMENT === #
|
|
|
|
# ================ #
|
2021-03-14 18:25:12 +01:00
|
|
|
|
2021-04-14 22:41:07 +02:00
|
|
|
# check if sapper's destination is accessible
|
2021-05-20 15:01:09 +02:00
|
|
|
@staticmethod
|
|
|
|
def is_valid_move(target_row: int, target_column: int):
|
2021-04-14 22:41:07 +02:00
|
|
|
if 0 <= target_row < const.V_GRID_VER_TILES \
|
2021-05-20 15:01:09 +02:00
|
|
|
and 0 <= target_column < const.V_GRID_HOR_TILES:
|
2021-04-14 22:41:07 +02:00
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
2021-05-20 15:01:09 +02:00
|
|
|
|
2021-05-21 14:00:26 +02:00
|
|
|
# ============= #
|
|
|
|
# === OTHER === #
|
|
|
|
# ============= #
|
2021-05-20 15:01:09 +02:00
|
|
|
|
2021-05-21 14:00:26 +02:00
|
|
|
# method that allows copying classes instance
|
|
|
|
def __copy__(self):
|
|
|
|
copy = Minefield(self.json_path)
|
|
|
|
return copy
|