diff --git a/shogi-ai/agents/agent.py b/shogi-ai/agents/agent.py index 79166ce..ca2c67d 100644 --- a/shogi-ai/agents/agent.py +++ b/shogi-ai/agents/agent.py @@ -4,7 +4,7 @@ from typing import Optional -from env.environment import Environment +from environments.environment import Environment from shogi import Board diff --git a/shogi-ai/agents/mcts_agent.py b/shogi-ai/agents/mcts_agent.py index a8ad6b0..c1bf845 100644 --- a/shogi-ai/agents/mcts_agent.py +++ b/shogi-ai/agents/mcts_agent.py @@ -12,7 +12,7 @@ from typing import List, Optional, Tuple from agents.agent import Agent -from env.environment import Environment +from environments.environment import Environment from shogi import Board, Move from util.common import get_logger diff --git a/shogi-ai/agents/random_agent.py b/shogi-ai/agents/random_agent.py index f198b62..3d6e00f 100644 --- a/shogi-ai/agents/random_agent.py +++ b/shogi-ai/agents/random_agent.py @@ -9,7 +9,7 @@ from typing import Optional from agents.agent import Agent -from env.environment import Environment +from environments.environment import Environment from shogi import Board from shogi.Move import Move diff --git a/shogi-ai/environments/__init__.py b/shogi-ai/environments/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/shogi-ai/environments/environment.py b/shogi-ai/environments/environment.py new file mode 100644 index 0000000..2ccc444 --- /dev/null +++ b/shogi-ai/environments/environment.py @@ -0,0 +1,60 @@ +""" +You can use the Environment class to generate an environment from a shogi board + +``` +from shogi import Board +from environment import Environment + +board = Board() +env = Environment(board) +``` + +""" + +from typing import List + +from shogi import Board + + +class Environment: + """ + Environment class for a shogi board. + """ + def __init__(self, board: Board): + self.board = board + self._moves: List = [] + self._last_state = board.piece_bb + + def get_player(self): + """ + Get the player of the environment. + """ + return self.board.turn + + @property + def action_space(self): + """ + generate all legal moves if we haven't already and + return them + """ + if self._moves and (self.board == self._last_state): + return self._moves + + self._moves = [] + self._last_state = self.board.piece_bb + for move in self.board.legal_moves: + self._moves.append(move) + return self._moves + + def is_terminal_state(self) -> bool: + """ + Check if the game is in a terminal state. + """ + return self.board.is_game_over() + + @classmethod + def from_board(cls, board: Board): + """ + currently just is another constructor basically. + """ + return Environment(board) diff --git a/shogi-ai/main.py b/shogi-ai/main.py index 8aab2e5..0582a57 100644 --- a/shogi-ai/main.py +++ b/shogi-ai/main.py @@ -11,7 +11,7 @@ import shogi from agents.mcts_agent import MctsAgent from agents.random_agent import RandomAgent -from env.environment import Environment +from environments.environment import Environment from shogi import Board, Move