added turn counter, updated next move validation
This commit is contained in:
parent
37b4107aa4
commit
1ccb1277fc
53
minefield.py
53
minefield.py
@ -33,6 +33,9 @@ def calculate_screen_position(x, y):
|
|||||||
|
|
||||||
class Minefield:
|
class Minefield:
|
||||||
def __init__(self, json_path):
|
def __init__(self, json_path):
|
||||||
|
self.turn = 0
|
||||||
|
|
||||||
|
# self.sapper = new Sapper Object (yet to be implemented)
|
||||||
|
|
||||||
# open JSON with minefield info
|
# open JSON with minefield info
|
||||||
with open(json_path) as json_data:
|
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
|
# create matrix of a desired size, fill it with default tile objects
|
||||||
self.matrix = [
|
self.matrix = [
|
||||||
[
|
[
|
||||||
tl.Tile((i, j)) for i in range(const.V_GRID_VER_TILES)
|
tl.Tile((x, y)) for y in range(const.V_GRID_VER_TILES)
|
||||||
] for j in range(const.V_GRID_HOR_TILES)
|
] for x in range(const.V_GRID_HOR_TILES)
|
||||||
]
|
]
|
||||||
|
|
||||||
# iterate through tiles, set their colors and add mines
|
# iterate through tiles, set their colors and add mines
|
||||||
@ -54,7 +57,6 @@ class Minefield:
|
|||||||
|
|
||||||
# create and add mine if there is one
|
# create and add mine if there is one
|
||||||
if tile_data["mine"] is not None:
|
if tile_data["mine"] is not None:
|
||||||
|
|
||||||
# TODO: check mine type and create suitable one
|
# 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
|
# JSON file doesn't represent new mine types yet, so every mine is a standard one at the moment
|
||||||
mine = sm.StandardMine((x, y))
|
mine = sm.StandardMine((x, y))
|
||||||
@ -69,8 +71,8 @@ class Minefield:
|
|||||||
|
|
||||||
def draw(self, window):
|
def draw(self, window):
|
||||||
# iterate through tiles
|
# iterate through tiles
|
||||||
for column in self.matrix:
|
for raw in self.matrix:
|
||||||
for tile in column:
|
for tile in raw:
|
||||||
|
|
||||||
# calculate tile position on the screen
|
# calculate tile position on the screen
|
||||||
tile_screen_coords = calculate_screen_position(tile.position[0], tile.position[1])
|
tile_screen_coords = calculate_screen_position(tile.position[0], tile.position[1])
|
||||||
@ -92,32 +94,37 @@ class Minefield:
|
|||||||
# === MOVEMENT === #
|
# === MOVEMENT === #
|
||||||
# ================ #
|
# ================ #
|
||||||
|
|
||||||
# Make sure that sapper won't step on the mine.
|
# check if sapper's destination is accessible
|
||||||
def check_legal_move(self, target):
|
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 False
|
||||||
return target
|
|
||||||
else:
|
|
||||||
return self.sapper_position
|
|
||||||
|
|
||||||
# Here are defined functions that move our agent. They are being called in main when certain key is pressed
|
# Here are defined functions that move our agent. They are being called in main when certain key is pressed
|
||||||
|
|
||||||
def go_right(self):
|
def go_right(self):
|
||||||
if self.sapper_position[0] < const.V_GRID_HOR_TILES - 1:
|
target_x = self.sapper_position[0] + 1
|
||||||
self.sapper_position = self.check_legal_move(
|
target_y = self.sapper_position[1]
|
||||||
(int(self.sapper_position[0]) + 1, int(self.sapper_position[1])))
|
if self.is_valid_move(target_x, target_y):
|
||||||
|
self.sapper_position = (target_x, target_y)
|
||||||
|
|
||||||
def go_left(self):
|
def go_left(self):
|
||||||
if self.sapper_position[0] > 0:
|
target_x = self.sapper_position[0] - 1
|
||||||
self.sapper_position = self.check_legal_move(
|
target_y = self.sapper_position[1]
|
||||||
(int(self.sapper_position[0]) - 1, int(self.sapper_position[1])))
|
if self.is_valid_move(target_x, target_y):
|
||||||
|
self.sapper_position = (target_x, target_y)
|
||||||
|
|
||||||
def go_up(self):
|
def go_up(self):
|
||||||
if self.sapper_position[1] > 0:
|
target_x = self.sapper_position[0]
|
||||||
self.sapper_position = self.check_legal_move(
|
target_y = self.sapper_position[1] - 1
|
||||||
(int(self.sapper_position[0]), int(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):
|
def go_down(self):
|
||||||
if self.sapper_position[1] < const.V_GRID_VER_TILES - 1:
|
target_x = self.sapper_position[0]
|
||||||
self.sapper_position = self.check_legal_move(
|
target_y = self.sapper_position[1] + 1
|
||||||
(int(self.sapper_position[0]), int(self.sapper_position[1]) + 1))
|
if self.is_valid_move(target_x, target_y):
|
||||||
|
self.sapper_position = (target_x, target_y)
|
||||||
|
Loading…
Reference in New Issue
Block a user