2021-03-12 21:25:33 +01:00
|
|
|
import json
|
2021-03-26 15:46:00 +01:00
|
|
|
|
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-26 15:46:00 +01:00
|
|
|
from mines_models import standard_mine as sm
|
2021-03-12 11:49:19 +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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def calculate_screen_position(x, y):
|
|
|
|
coords = (
|
2021-03-14 19:18:23 +01:00
|
|
|
const.V_SCREEN_PADDING + const.V_TILE_SIZE * x,
|
|
|
|
const.V_SCREEN_PADDING + const.V_TILE_SIZE * y,
|
2021-03-12 21:25:33 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
return coords
|
|
|
|
|
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
|
|
|
|
|
|
|
|
# self.sapper = new Sapper Object (yet to be implemented)
|
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-03-26 16:29:53 +01:00
|
|
|
tl.Tile((x, y)) for y in range(const.V_GRID_VER_TILES)
|
|
|
|
] for x in range(const.V_GRID_HOR_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-03-14 19:18:23 +01:00
|
|
|
for x in range(const.V_GRID_HOR_TILES):
|
|
|
|
for y in range(const.V_GRID_VER_TILES):
|
2021-03-12 11:49:19 +01:00
|
|
|
|
2021-03-12 21:25:33 +01:00
|
|
|
# load tile's data from json
|
|
|
|
tile_data = data[f"{x},{y}"]
|
|
|
|
|
|
|
|
# create and add mine if there is one
|
|
|
|
if tile_data["mine"] is not None:
|
2021-03-26 15:46:00 +01:00
|
|
|
# TODO: check mine type and create suitable one
|
|
|
|
# JSON file doesn't represent new mine types yet, so every mine is a standard one at the moment
|
|
|
|
mine = sm.StandardMine((x, y))
|
2021-03-12 21:25:33 +01:00
|
|
|
self.matrix[x][y].mine = mine
|
|
|
|
|
|
|
|
self.matrix[x][y].color = tile_data["color"].upper()
|
|
|
|
|
|
|
|
# probably a temporary solution
|
|
|
|
# (depends on sapper's movement implementation)
|
|
|
|
sapper_x, sapper_y = data['agent_starting_position'].split(",")
|
|
|
|
self.sapper_position = (int(sapper_x), int(sapper_y))
|
2021-03-12 11:49:19 +01:00
|
|
|
|
|
|
|
def draw(self, window):
|
|
|
|
# iterate through tiles
|
2021-03-26 16:29:53 +01:00
|
|
|
for raw in self.matrix:
|
|
|
|
for tile in raw:
|
2021-03-12 11:49:19 +01:00
|
|
|
|
2021-03-12 21:25:33 +01:00
|
|
|
# calculate tile position on the screen
|
|
|
|
tile_screen_coords = 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-12 11:49:19 +01:00
|
|
|
|
2021-03-12 21:25:33 +01:00
|
|
|
# draw the sapper
|
|
|
|
sapper_screen_coords = calculate_screen_position(self.sapper_position[0], self.sapper_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-03-26 16:29:53 +01:00
|
|
|
# check if sapper's destination is accessible
|
|
|
|
def is_valid_move(self, target_x: int, target_y: int):
|
|
|
|
if 0 <= target_x < const.V_GRID_HOR_TILES and \
|
|
|
|
0 <= target_y < const.V_GRID_VER_TILES and \
|
|
|
|
self.matrix[target_x][target_y].mine is None:
|
|
|
|
return True
|
2021-03-14 19:18:23 +01:00
|
|
|
|
2021-03-26 16:29:53 +01:00
|
|
|
return False
|
2021-03-14 18:25:12 +01:00
|
|
|
|
2021-03-14 11:32:44 +01:00
|
|
|
# Here are defined functions that move our agent. They are being called in main when certain key is pressed
|
2021-03-14 19:18:23 +01:00
|
|
|
|
2021-03-14 11:32:44 +01:00
|
|
|
def go_right(self):
|
2021-03-26 16:29:53 +01:00
|
|
|
target_x = self.sapper_position[0] + 1
|
|
|
|
target_y = self.sapper_position[1]
|
|
|
|
if self.is_valid_move(target_x, target_y):
|
|
|
|
self.sapper_position = (target_x, target_y)
|
2021-03-14 18:25:12 +01:00
|
|
|
|
|
|
|
def go_left(self):
|
2021-03-26 16:29:53 +01:00
|
|
|
target_x = self.sapper_position[0] - 1
|
|
|
|
target_y = self.sapper_position[1]
|
|
|
|
if self.is_valid_move(target_x, target_y):
|
|
|
|
self.sapper_position = (target_x, target_y)
|
2021-03-14 18:25:12 +01:00
|
|
|
|
|
|
|
def go_up(self):
|
2021-03-26 16:29:53 +01:00
|
|
|
target_x = self.sapper_position[0]
|
|
|
|
target_y = self.sapper_position[1] - 1
|
|
|
|
if self.is_valid_move(target_x, target_y):
|
|
|
|
self.sapper_position = (target_x, target_y)
|
2021-03-14 18:25:12 +01:00
|
|
|
|
|
|
|
def go_down(self):
|
2021-03-26 16:29:53 +01:00
|
|
|
target_x = self.sapper_position[0]
|
|
|
|
target_y = self.sapper_position[1] + 1
|
|
|
|
if self.is_valid_move(target_x, target_y):
|
|
|
|
self.sapper_position = (target_x, target_y)
|