81 lines
2.8 KiB
Python
81 lines
2.8 KiB
Python
import agent as ag
|
|
import project_constants as const
|
|
import tile as tl
|
|
from mine_models import standard_mine as sm
|
|
from mine_models import time_mine as tm
|
|
from mine_models import chained_mine as cm
|
|
import json_generator as jg
|
|
|
|
|
|
class Minefield:
|
|
def __init__(self, json_path):
|
|
self.turn = 0
|
|
|
|
self.agent = ag.Agent(const.MAP_RANDOM_10x10)
|
|
|
|
# open JSON with minefield info
|
|
json_gen = jg.JsonGenerator()
|
|
json_gen.load_from_a_file(json_path)
|
|
|
|
# create matrix of a desired size, fill it with default tile objects
|
|
self.matrix = [
|
|
[
|
|
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)
|
|
] for row in range(const.V_GRID_VER_TILES)
|
|
]
|
|
|
|
# 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
|
|
|
|
predecessor = self.matrix[predecessor_row][predecessor_column]
|
|
self.matrix[successor_row][successor_column].mine.predecessor = predecessor
|
|
|
|
# ================ #
|
|
# === MOVEMENT === #
|
|
# ================ #
|
|
|
|
# check if sapper's destination is accessible
|
|
# If Agent comes upon a tile with a mine his starting position shall be reestablished
|
|
def is_valid_move(self, target_row: int, target_column: int):
|
|
if 0 <= target_row < const.V_GRID_VER_TILES \
|
|
and 0 <= target_column < const.V_GRID_HOR_TILES \
|
|
and self.matrix[target_row][target_column].mine is None:
|
|
return True
|
|
|
|
return False
|
|
|
|
# distinguishes new mine's type and creates appropriate object
|
|
def _create_mine(self, mine_data, row, column):
|
|
mine_type = mine_data["mine_type"]
|
|
|
|
# TIME MINE
|
|
if mine_type == "time":
|
|
timer = mine_data["timer"]
|
|
mine = tm.TimeMine((row, column), int(timer))
|
|
|
|
# CHAINED MINE
|
|
elif mine_type == "chained":
|
|
if mine_data["predecessor"] is not None:
|
|
# locate predecessor
|
|
row, column = map(int, mine_data["predecessor"].split(','))
|
|
|
|
# get predecessor object and assign it to the new mine
|
|
predecessor = self.matrix[row][column].mine
|
|
mine = cm.ChainedMine((row, column), predecessor)
|
|
|
|
else:
|
|
mine = cm.ChainedMine((row, column))
|
|
|
|
# STANDARD MINE
|
|
else:
|
|
mine = sm.StandardMine((row, column))
|
|
|
|
return mine
|