forked from spaghetti/spaghetti-code-3
chore: more comments
This commit is contained in:
parent
4bb5d8bda6
commit
795622361e
1 changed files with 17 additions and 9 deletions
26
maze.py
26
maze.py
|
@ -34,10 +34,10 @@ class Direction(StrEnum):
|
|||
The coordinates to add to the current player's position for each possible direction
|
||||
"""
|
||||
coordinates_map = {
|
||||
"up": (-1, 0),
|
||||
"right": (0, 1),
|
||||
"down": (1, 0),
|
||||
"left": (0, -1),
|
||||
"⬆️": (-1, 0),
|
||||
"➡️": (0, 1),
|
||||
"⬇️": (1, 0),
|
||||
"⬅️": (0, -1),
|
||||
}
|
||||
|
||||
return coordinates_map[self.value]
|
||||
|
@ -50,6 +50,8 @@ class Maze:
|
|||
self.player: tuple[int, int] # Position of the player.
|
||||
self.end: tuple[int, int] # Position of the end.
|
||||
|
||||
self._input = fileinput.input(encoding='utf-8') # Reads the input. You shouldn't access this manually
|
||||
|
||||
self._get_state()
|
||||
|
||||
def move(self, dir: Direction) -> bool:
|
||||
|
@ -64,7 +66,7 @@ class Maze:
|
|||
"""
|
||||
old_player = self.player
|
||||
|
||||
print(dir.value) # Do move
|
||||
print(dir.value, flush=True) # Do move (Flush is necessary!)
|
||||
self._get_state() # Get new maze state
|
||||
|
||||
return old_player == self.player
|
||||
|
@ -80,7 +82,7 @@ class Maze:
|
|||
Block: The block
|
||||
"""
|
||||
dy, dx = dir.coordinates
|
||||
py, px = self.player[0] + dx, self.player[1] + dy
|
||||
py, px = self.player[0] + dy, self.player[1] + dx
|
||||
|
||||
return self.field[py][px]
|
||||
|
||||
|
@ -91,17 +93,23 @@ class Maze:
|
|||
"""
|
||||
self.field = []
|
||||
|
||||
lines = fileinput.input()
|
||||
for i, line in enumerate(lines.readline().split('\t')):
|
||||
# Split line by tab and loop over them
|
||||
for i, line in enumerate(self._input.readline().split('\t')):
|
||||
row: list[Block] = []
|
||||
# Loop over emojis
|
||||
for j, char in enumerate(line):
|
||||
if char in ['\t', '\n']: # Next line
|
||||
continue
|
||||
|
||||
row.append(Block(char))
|
||||
|
||||
# Check for player
|
||||
if char == Block.Player.value:
|
||||
self.player = (i, j)
|
||||
|
||||
# Check for the end
|
||||
if char == Block.End.value:
|
||||
self.player = (i, j)
|
||||
self.end = (i, j)
|
||||
|
||||
self.field.append(row)
|
||||
|
||||
|
|
Loading…
Reference in a new issue