From 1ccb1277fc0a3962a01ececf71e5207b9943cd20 Mon Sep 17 00:00:00 2001 From: s452645 Date: Fri, 26 Mar 2021 16:29:53 +0100 Subject: [PATCH] added turn counter, updated next move validation --- minefield.py | 53 +++++++++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/minefield.py b/minefield.py index b7dcc51..3db11f1 100644 --- a/minefield.py +++ b/minefield.py @@ -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)