-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer_class.py
More file actions
70 lines (57 loc) · 2.97 KB
/
Copy pathPlayer_class.py
File metadata and controls
70 lines (57 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from Cell_class import Cell
class Player:
def __init__(self, maze, initial_position: tuple[int, int]) -> None:
self.Maze = maze
self.position = initial_position # tuple of X coordinate and y Coordinate
def get_current_cell(self) -> Cell:
"""Get the current cell the player is in."""
x, y = self.position
return self.Maze.grid[y][x]
def get_current_cell(self) -> Cell:
"""Get the current cell the player is in."""
x, y = self.position
return self.Maze.grid[y][x]
def get_possible_moves(self, allow_visited: bool = False) -> list[Cell]:
"""Get all possible cells the player can move to from the current position.
Parameters:
allow_visited (bool): If False, exclude cells that have already been visited.
Returns:
list[Cell]: A list of valid cells the player can move to.
"""
x, y = self.position
possible_moves = []
# Define potential moves with directions
directions = {
'up': (x, y - 1),
'down': (x, y + 1),
'left': (x - 1, y),
'right': (x + 1, y)
}
# Check each direction for wall constraints and boundaries
for direction, (nx, ny) in directions.items():
# Ensure we don't go out of bounds
if 0 <= nx < self.Maze.width and 0 <= ny < self.Maze.height:
current_cell = self.Maze.grid[y][x]
new_cell = self.Maze.grid[ny][nx]
# Check for walls and visited status if required
if direction == 'up' and not current_cell.has_top_wall and not new_cell.has_bottom_wall:
if allow_visited or not new_cell.visited:
possible_moves.append(new_cell)
elif direction == 'down' and not current_cell.has_bottom_wall and not new_cell.has_top_wall:
if allow_visited or not new_cell.visited:
possible_moves.append(new_cell)
elif direction == 'left' and not current_cell.has_left_wall and not new_cell.has_right_wall:
if allow_visited or not new_cell.visited:
possible_moves.append(new_cell)
elif direction == 'right' and not current_cell.has_right_wall and not new_cell.has_left_wall:
if allow_visited or not new_cell.visited:
possible_moves.append(new_cell)
return possible_moves
def move_to(self, cell: Cell, undo=False) -> None:
"""Move the player to the specified cell if it's adjacent and accessible."""
if cell in self.get_possible_moves():
self.get_current_cell().draw_move(cell, undo=undo)
self.position = (cell.grid_coordinate_x, cell.grid_coordinate_y)
cell.visited = True # Mark the new cell as visited upon moving
return
raise Exception("Impossible move for the player object.")