Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 72d82b0c authored by PEDERSEN Ny Aina's avatar PEDERSEN Ny Aina
Browse files

Objects: fix bug, add custom directions mapping

- Update player character rep according to its direction.
- Fix init with empty string
- Handle attribute error, so a non fully fetched variable do not
raise an exception.
parent 0bc17cea
No related branches found
No related tags found
No related merge requests found
......@@ -3,12 +3,15 @@
from typing import Any, Union
# Default direction
_DIRECTION_DICT = {"up": 0, "down": 1, "left": 2, "right": 3}
# pylint: disable=eval-used
def _value(var: str, env: dict[str, Any]) -> Any:
"""Gets expression value."""
try:
return eval(var, {}, env)
except KeyError:
except (KeyError, AttributeError):
return None
......@@ -25,6 +28,7 @@ class Object:
var_dir: str,
char_rep: str = "o",
visibility_condition: str = "True",
direction_dict: dict[str, Union[int, str]] = _DIRECTION_DICT # type: ignore
):
"""
Object initialization.
......@@ -40,8 +44,9 @@ class Object:
self.var_x = var_x
self.var_y = var_y
self.var_dir = var_dir
self.char_rep = char_rep
self._char_rep = char_rep
self.visibility_condition = visibility_condition
self.dir_mapping = direction_dict
# dynamic properties
self.coord_x: Union[int, None] = None
self.coord_y: Union[int, None] = None
......@@ -61,13 +66,13 @@ class Object:
updated = False
if self.var_x.startswith(var):
self.coord_x = _value(var, self.env)
self.coord_x = _value(self.var_x, self.env)
updated = True
if self.var_y.startswith(var):
self.coord_y = _value(var, self.env)
self.coord_y = _value(self.var_y, self.env)
updated = True
if self.var_dir.startswith(var):
self.direction = _value(var, self.env)
self.direction = _value(self.var_dir, self.env)
updated = True
if not updated:
raise Exception(f"Update error: nothing to do with the variable {var}")
......@@ -82,8 +87,28 @@ class Object:
and self.direction >= 0
)
@property
def char_rep(self) -> str:
"""Get the character representation of the player."""
return self._char_rep
class Player(Object):
"""Player class."""
# TODO
@property
def char_rep(self) -> str:
"""Get the character representation of the player."""
up_value = int(self.dir_mapping["up"])
down_value = int(self.dir_mapping["down"])
left_value = int(self.dir_mapping["left"])
right_value = int(self.dir_mapping["right"])
if self.direction == up_value:
return "^"
if self.direction == down_value:
return "v"
if self.direction == left_value:
return "<"
if self.direction == right_value:
return ">"
return "?" # Unknown direction
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment