Projekt_Sztuczna_Inteligencja/minefield.py

61 lines
2.2 KiB
Python
Raw Normal View History

2021-03-26 16:36:21 +01:00
import agent as ag
2021-03-14 19:18:23 +01:00
import project_constants as const
2021-03-12 11:49:19 +01:00
import tile as tl
from mine_models.time_mine import TimeMine
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:
def __init__(self, json_path):
self.turn = 0
2021-03-26 16:36:21 +01:00
self.agent = ag.Agent(const.MAP_RANDOM_10x10)
2021-03-12 11:49:19 +01:00
# open JSON with minefield info
json_gen = jg.JsonGenerator()
json_gen.load_from_a_file(json_path)
2021-03-12 11:49:19 +01:00
# 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)
]
2021-03-12 11:49:19 +01: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
predecessor = self.matrix[predecessor_row][predecessor_column]
self.matrix[successor_row][successor_column].mine.predecessor = predecessor
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):
mine.timer = max(0, mine.starting_time - self.turn)
2021-03-14 19:18:23 +01:00
# ================ #
# === 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