2021-03-12 21:25:33 +01:00
|
|
|
import json
|
2021-04-14 12:01:20 +02:00
|
|
|
import ctypes
|
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
|
2021-03-27 21:41:57 +01:00
|
|
|
from mine_models import standard_mine as sm
|
|
|
|
from mine_models import time_mine as tm
|
|
|
|
from mine_models import chained_mine as cm
|
2021-03-12 11:49:19 +01:00
|
|
|
|
2021-03-26 16:36:21 +01:00
|
|
|
|
2021-03-12 21:25:33 +01:00
|
|
|
tile_asset_options = {
|
2021-03-14 19:18:23 +01:00
|
|
|
"BLUE": const.ASSET_TILE_BLUE,
|
|
|
|
"GREEN": const.ASSET_TILE_GREEN,
|
|
|
|
"ORANGE": const.ASSET_TILE_ORANGE,
|
|
|
|
"PURPLE": const.ASSET_TILE_PURPLE,
|
|
|
|
"RED": const.ASSET_TILE_RED,
|
|
|
|
"WHITE": const.ASSET_TILE_WHITE,
|
|
|
|
"YELLOW": const.ASSET_TILE_YELLOW
|
2021-03-12 21:25:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
mine_asset_options = {
|
2021-03-14 19:18:23 +01:00
|
|
|
'A': const.ASSET_MINE_A,
|
|
|
|
'B': const.ASSET_MINE_B,
|
|
|
|
'F': const.ASSET_MINE_F,
|
|
|
|
'K': const.ASSET_MINE_K
|
2021-03-12 21:25:33 +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-03-12 11:49:19 +01:00
|
|
|
|
2021-03-12 21:25:33 +01:00
|
|
|
# open JSON with minefield info
|
|
|
|
with open(json_path) as json_data:
|
|
|
|
data = json.load(json_data)
|
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-04-11 19:04:06 +02:00
|
|
|
tl.Tile((row, column)) for column in range(const.V_GRID_HOR_TILES)
|
|
|
|
] 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-03-12 21:25:33 +01:00
|
|
|
# iterate through tiles, set their colors and add mines
|
2021-04-11 19:04:06 +02:00
|
|
|
for row in range(const.V_GRID_VER_TILES):
|
|
|
|
for column in range(const.V_GRID_HOR_TILES):
|
2021-03-12 11:49:19 +01:00
|
|
|
|
2021-03-12 21:25:33 +01:00
|
|
|
# load tile's data from json
|
2021-04-11 19:04:06 +02:00
|
|
|
tile_data = data[f"{row},{column}"]
|
2021-03-12 21:25:33 +01:00
|
|
|
|
2021-03-27 21:41:57 +01:00
|
|
|
# if there is a mine, create & assign new Mine object (type recognition included)
|
2021-03-12 21:25:33 +01:00
|
|
|
if tile_data["mine"] is not None:
|
2021-04-11 19:04:06 +02:00
|
|
|
mine = self._create_mine(tile_data["mine"], row, column)
|
|
|
|
self.matrix[row][column].mine = mine
|
2021-03-12 21:25:33 +01:00
|
|
|
|
2021-04-11 19:04:06 +02:00
|
|
|
self.matrix[row][column].color = tile_data["color"].upper()
|
2021-03-12 21:25:33 +01:00
|
|
|
|
2021-04-11 19:29:12 +02:00
|
|
|
@staticmethod
|
|
|
|
def calculate_screen_position(row, column):
|
|
|
|
coords = (
|
|
|
|
const.V_SCREEN_PADDING + const.V_TILE_SIZE * column,
|
|
|
|
const.V_SCREEN_PADDING + const.V_TILE_SIZE * row,
|
|
|
|
)
|
|
|
|
|
|
|
|
return coords
|
|
|
|
|
2021-03-12 11:49:19 +01:00
|
|
|
def draw(self, window):
|
|
|
|
# iterate through tiles
|
2021-04-11 19:04:06 +02:00
|
|
|
for row in self.matrix:
|
|
|
|
for tile in row:
|
2021-03-12 11:49:19 +01:00
|
|
|
|
2021-03-12 21:25:33 +01:00
|
|
|
# calculate tile position on the screen
|
2021-04-11 19:29:12 +02:00
|
|
|
tile_screen_coords = self.calculate_screen_position(tile.position[0], tile.position[1])
|
2021-03-12 11:49:19 +01:00
|
|
|
|
2021-03-12 21:25:33 +01:00
|
|
|
# draw a tile
|
|
|
|
window.blit(tile_asset_options.get(tile.color), tile_screen_coords)
|
2021-03-12 11:49:19 +01:00
|
|
|
|
2021-03-12 21:25:33 +01:00
|
|
|
# draw a mine on top if there is one
|
|
|
|
if tile.mine is not None:
|
2021-03-26 15:46:00 +01:00
|
|
|
# TODO: blit appropriate mine type
|
|
|
|
# current icons don't represent actual types, thus every mine has the same icon (temporary solution)
|
|
|
|
window.blit(mine_asset_options['A'], tile_screen_coords)
|
2021-03-26 16:36:21 +01:00
|
|
|
|
2021-03-12 21:25:33 +01:00
|
|
|
# draw the sapper
|
2021-04-11 19:29:12 +02:00
|
|
|
sapper_screen_coords = self.calculate_screen_position(self.agent.position[0], self.agent.position[1])
|
2021-03-14 19:18:23 +01:00
|
|
|
window.blit(const.ASSET_SAPPER, sapper_screen_coords)
|
|
|
|
|
|
|
|
# ================ #
|
|
|
|
# === MOVEMENT === #
|
|
|
|
# ================ #
|
2021-03-14 18:25:12 +01:00
|
|
|
|
2021-04-14 22:41:07 +02:00
|
|
|
# check if sapper's destination is accessible
|
|
|
|
# If Agent comes upon a tile with a mine his starting position shall be reestablished
|
2021-04-11 19:04:06 +02:00
|
|
|
def is_valid_move(self, target_row: int, target_column: int):
|
2021-04-14 22:41:07 +02:00
|
|
|
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] is None:
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
2021-03-14 19:18:23 +01:00
|
|
|
|
2021-03-27 21:41:57 +01:00
|
|
|
# distinguishes new mine's type and creates appropriate object
|
2021-04-11 19:04:06 +02:00
|
|
|
def _create_mine(self, mine_data, row, column):
|
2021-03-27 21:41:57 +01:00
|
|
|
mine_type = mine_data["mine_type"]
|
|
|
|
|
|
|
|
# TIME MINE
|
|
|
|
if mine_type == "time":
|
|
|
|
timer = mine_data["timer"]
|
2021-04-11 19:04:06 +02:00
|
|
|
mine = tm.TimeMine((row, column), int(timer))
|
2021-03-27 21:41:57 +01:00
|
|
|
|
|
|
|
# CHAINED MINE
|
|
|
|
elif mine_type == "chained":
|
|
|
|
if mine_data["predecessor"] is not None:
|
|
|
|
# locate predecessor
|
2021-04-11 19:29:12 +02:00
|
|
|
row, column = map(int, mine_data["predecessor"].split(','))
|
2021-03-27 21:41:57 +01:00
|
|
|
|
|
|
|
# get predecessor object and assign it to the new mine
|
2021-04-11 19:04:06 +02:00
|
|
|
predecessor = self.matrix[row][column].mine
|
|
|
|
mine = cm.ChainedMine((row, column), predecessor)
|
2021-03-27 21:41:57 +01:00
|
|
|
|
|
|
|
else:
|
2021-04-11 19:04:06 +02:00
|
|
|
mine = cm.ChainedMine((row, column))
|
2021-03-27 21:41:57 +01:00
|
|
|
|
|
|
|
# STANDARD MINE
|
|
|
|
else:
|
2021-04-11 19:04:06 +02:00
|
|
|
mine = sm.StandardMine((row, column))
|
2021-03-27 21:41:57 +01:00
|
|
|
|
|
|
|
return mine
|