-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from corail-research/light_action
Light action
- Loading branch information
Showing
7 changed files
with
218 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,30 @@ | ||
from __future__ import annotations | ||
|
||
from abc import abstractmethod | ||
from typing import TYPE_CHECKING | ||
|
||
from seahorse.utils.serializer import Serializable | ||
|
||
if TYPE_CHECKING: | ||
from seahorse.game.game_state import GameState | ||
|
||
|
||
class Action(Serializable): | ||
""" | ||
A class representing an action in the game. | ||
A generic class representing an action in the game. | ||
Attributes: | ||
past_gs (GameState): The past game state. | ||
new_gs (GameState): The new game state. | ||
""" | ||
|
||
def __init__(self, current_game_state: GameState, next_game_state: GameState) -> None: | ||
def __init__(self) -> None: | ||
""" | ||
Initializes a new instance of the Action class. | ||
Args: | ||
past_gs (GameState): The past game state. | ||
new_gs (GameState): The new game state. | ||
""" | ||
self.current_game_state = current_game_state | ||
self.next_game_state = next_game_state | ||
|
||
def get_current_game_state(self) -> GameState: | ||
""" | ||
Returns the past game state. | ||
Returns: | ||
GameState: The past game state. | ||
""" | ||
return self.current_game_state | ||
pass | ||
|
||
def get_next_game_state(self) -> GameState: | ||
@abstractmethod | ||
def get_heavy_action(self, **kwargs) -> Action: | ||
""" | ||
Returns the new game state. | ||
Returns the heavy action. | ||
Returns: | ||
GameState: The new game state. | ||
Action: The heavy action. | ||
""" | ||
return self.next_game_state | ||
|
||
def __hash__(self) -> int: | ||
return hash((hash(self.get_next_game_state()), hash(self.get_current_game_state()))) | ||
|
||
def __eq__(self, value: object) -> bool: | ||
return hash(self) == hash(value) | ||
|
||
def __str__(self) -> str: | ||
return "From:\n" + self.get_current_game_state().get_rep().__str__() + "\nto:\n" + self.get_next_game_state().get_rep().__str__() | ||
raise NotImplementedError | ||
|
||
def to_json(self) -> dict: | ||
return self.__dict__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from seahorse.game.action import Action | ||
from seahorse.utils.serializer import Serializable | ||
|
||
if TYPE_CHECKING: | ||
from seahorse.game.game_state import GameState | ||
|
||
|
||
class HeavyAction(Action): | ||
""" | ||
A class representing an action in the game. | ||
Attributes: | ||
past_gs (GameState): The past game state. | ||
new_gs (GameState): The new game state. | ||
""" | ||
|
||
def __init__(self, current_game_state: GameState, next_game_state: GameState) -> None: | ||
""" | ||
Initializes a new instance of the Action class. | ||
Args: | ||
past_gs (GameState): The past game state. | ||
new_gs (GameState): The new game state. | ||
""" | ||
self.current_game_state = current_game_state | ||
self.next_game_state = next_game_state | ||
|
||
def get_current_game_state(self) -> GameState: | ||
""" | ||
Returns the past game state. | ||
Returns: | ||
GameState: The past game state. | ||
""" | ||
return self.current_game_state | ||
|
||
def get_next_game_state(self) -> GameState: | ||
""" | ||
Returns the new game state. | ||
Returns: | ||
GameState: The new game state. | ||
""" | ||
return self.next_game_state | ||
|
||
def get_heavy_action(self, game_state: GameState = None) -> HeavyAction: | ||
""" | ||
Returns the heavy action. | ||
Returns: | ||
HeavyAction: The heavy action. | ||
""" | ||
return self | ||
|
||
def __hash__(self) -> int: | ||
return hash((hash(self.get_next_game_state()), hash(self.get_current_game_state()))) | ||
|
||
def __eq__(self, value: object) -> bool: | ||
return hash(self) == hash(value) | ||
|
||
def __str__(self) -> str: | ||
return "From:\n" + self.get_current_game_state().get_rep().__str__() + "\nto:\n" + self.get_next_game_state().get_rep().__str__() | ||
|
||
def to_json(self) -> dict: | ||
return self.__dict__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from seahorse.game.action import Action | ||
from seahorse.game.heavy_action import HeavyAction | ||
|
||
if TYPE_CHECKING: | ||
from seahorse.game.game_state import GameState | ||
|
||
|
||
class LightAction(Action): | ||
""" | ||
A class representing an action in the game. | ||
Attributes: | ||
data (dict): The data of the light action. | ||
""" | ||
|
||
def __init__(self, data: dict) -> None: | ||
""" | ||
Initializes a new instance of the Action class. | ||
Args: data (dict): The data of the light action. | ||
""" | ||
self.data = data | ||
|
||
|
||
def get_heavy_action(self, game_state: GameState = None) -> HeavyAction: | ||
""" | ||
Returns the heavy action. | ||
Returns: | ||
HeavyAction: The heavy action. | ||
""" | ||
if game_state is None: | ||
raise ValueError("Cannot apply a light action without current game state.") | ||
|
||
return HeavyAction(game_state, game_state.apply_action(self)) | ||
|
||
|
||
def __hash__(self) -> int: | ||
return hash(tuple(self.data.items())) | ||
|
||
def __eq__(self, value: object) -> bool: | ||
return hash(self) == hash(value) | ||
|
||
def __str__(self) -> str: | ||
return "LightAction: " + str(self.data) | ||
|
||
def to_json(self) -> dict: | ||
return self.__dict__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.