added turn counter, updated next move validation

This commit is contained in:
s452645 2021-03-26 16:29:53 +01:00
parent 37b4107aa4
commit 1ccb1277fc

View File

@ -33,6 +33,9 @@ def calculate_screen_position(x, y):
class Minefield:
def __init__(self, json_path):
self.turn = 0
# self.sapper = new Sapper Object (yet to be implemented)
# open JSON with minefield info
with open(json_path) as json_data:
@ -41,8 +44,8 @@ class Minefield:
# create matrix of a desired size, fill it with default tile objects
self.matrix = [
[
tl.Tile((i, j)) for i in range(const.V_GRID_VER_TILES)
] for j in range(const.V_GRID_HOR_TILES)
tl.Tile((x, y)) for y in range(const.V_GRID_VER_TILES)
] for x in range(const.V_GRID_HOR_TILES)
]
# iterate through tiles, set their colors and add mines
@ -54,7 +57,6 @@ class Minefield:
# create and add mine if there is one
if tile_data["mine"] is not None:
# 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))
@ -69,8 +71,8 @@ class Minefield:
def draw(self, window):
# iterate through tiles
for column in self.matrix:
for tile in column:
for raw in self.matrix:
for tile in raw:
# calculate tile position on the screen
tile_screen_coords = calculate_screen_position(tile.position[0], tile.position[1])
@ -92,32 +94,37 @@ class Minefield:
# === MOVEMENT === #
# ================ #
# Make sure that sapper won't step on the mine.
def check_legal_move(self, target):
# 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
if self.matrix[target[1]][target[0]].mine is None:
return target
else:
return self.sapper_position
return False
# Here are defined functions that move our agent. They are being called in main when certain key is pressed
def go_right(self):
if self.sapper_position[0] < const.V_GRID_HOR_TILES - 1:
self.sapper_position = self.check_legal_move(
(int(self.sapper_position[0]) + 1, int(self.sapper_position[1])))
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)
def go_left(self):
if self.sapper_position[0] > 0:
self.sapper_position = self.check_legal_move(
(int(self.sapper_position[0]) - 1, int(self.sapper_position[1])))
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)
def go_up(self):
if self.sapper_position[1] > 0:
self.sapper_position = self.check_legal_move(
(int(self.sapper_position[0]), int(self.sapper_position[1]) - 1))
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)
def go_down(self):
if self.sapper_position[1] < const.V_GRID_VER_TILES - 1:
self.sapper_position = self.check_legal_move(
(int(self.sapper_position[0]), int(self.sapper_position[1]) + 1))
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)