Skip to content

Commit

Permalink
fix: env was hidden with gitignore this fixes it
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielBarton446 committed Apr 21, 2024
1 parent a74ede8 commit ac6818b
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 4 deletions.
2 changes: 1 addition & 1 deletion shogi-ai/agents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from typing import Optional

from env.environment import Environment
from environments.environment import Environment
from shogi import Board


Expand Down
2 changes: 1 addition & 1 deletion shogi-ai/agents/mcts_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion shogi-ai/agents/random_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Empty file.
60 changes: 60 additions & 0 deletions shogi-ai/environments/environment.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion shogi-ai/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down

0 comments on commit ac6818b

Please sign in to comment.