Skip to content

Commit

Permalink
Add docstrings to Tablut class methods
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoFasulo committed Nov 29, 2023
1 parent 359fb95 commit f322b83
Showing 1 changed file with 69 additions and 6 deletions.
75 changes: 69 additions & 6 deletions tablut.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,30 @@

class Tablut(Game):
def __init__(self, height: int = 9, width: int = 9):
"""
Initializes a Tablut game object.
Parameters:
- height (int): The height of the game board. Default is 9.
- width (int): The width of the game board. Default is 9.
"""
self.initial = Board(height=height, width=width,
to_move='WHITE', utility=0)

self.width = width
self.height = height

def update_state(self, pieces, turn):
"""
Update the state of the board.
Args:
pieces (list): The positions of the pieces on the board.
turn (str): The current turn ('WHITE' or 'BLACK').
Returns:
None
"""
# Update board state
self.initial.pieces = pieces
self.initial.to_move = turn
Expand Down Expand Up @@ -55,6 +72,15 @@ def update_state(self, pieces, turn):
# self.initial.black_moves_to_eat_king = self.eat_king_in_castle()

def move(self, move):
"""
Moves a pawn on the board according to the given move.
Args:
move (tuple): A tuple containing the starting and ending positions of the move.
Returns:
Tablut: The updated Tablut object after the move has been made.
"""
# Extract the starting and ending position from the move
# print(f"OLD STATE:\n{self.initial.pieces}")
from_pos, to_pos = move
Expand Down Expand Up @@ -101,7 +127,17 @@ def move(self, move):
return self

def actions(self, pieces, player, board) -> set:
# TODO "mossa che scavalca una citadel è vietata"
"""
Returns a set of allowed moves for the current player.
Args:
pieces (numpy.ndarray): The current state of the game board.
player (str): The current player ('WHITE' or 'BLACK').
board (numpy.ndarray): The current state of the game board.
Returns:
set: A set of allowed moves for the current player.
"""
pawns = np.where(pieces == Pawn.WHITE.value)
coordinates = list(zip(pawns[0], pawns[1]))
white = coordinates
Expand Down Expand Up @@ -248,7 +284,18 @@ def actions(self, pieces, player, board) -> set:
return allowed_moves

def result(self, state, move, alpha0, beta0, gamma0, theta0, epsilon0, flag: bool = False):
# TODO dyanmic analysis here
"""
Apply the given move to the state and return the resulting state.
Parameters:
- state: The current state of the game.
- move: The move to be applied to the state.
- alpha0, beta0, gamma0, theta0, epsilon0: Parameters for computing the utility of the board.
- flag: A flag indicating whether the move should be applied to the state or not. Default is False.
Returns:
The resulting state after applying the move.
"""
# Apply the move locally and check for captures
# print("OLD STATE BEFORE MOVE\n", state)
game = Tablut()
Expand Down Expand Up @@ -289,14 +336,30 @@ def result(self, state, move, alpha0, beta0, gamma0, theta0, epsilon0, flag: boo
return game.initial

def utility(self, board, player):
"""Return the value to player; 1 for win, -1 for loss, 0 otherwise."""
"""Return the value to player; 1 for win, -1 for loss, 0 otherwise.
Args:
board (Board): The current game board.
player (str): The player for whom to calculate the utility.
Returns:
int: The utility value for the specified player.
"""
return board.utility if player == 'WHITE' else -board.utility

def terminal_test(self, board):
# Check if it is a winning move or if there are no more starting positions (no more moves)
"""
Check if the current board state is a terminal state.
Parameters:
- board: The current board state.
Returns:
- True if the game is in a terminal state (winning move or no more moves), False otherwise.
"""
return self.check_win(board)

def compute_utility(self, board, move, alpha0,beta0,gamma0,theta0, epsilon0, player) -> float:
def compute_utility(self, board, move, alpha0, beta0, gamma0, theta0, epsilon0, player) -> float:
"""
alpha : is_white_gonna_be_eaten
beta : external_pawn_more_fitness
Expand Down Expand Up @@ -455,4 +518,4 @@ def eat_king_outside_castle(self):
return [black, [position[0][0], position[0][1]]]
if len(position) == 2 and ((black[0], black[1]), (position[1][0], position[1][1])) in self.initial.moves:
return [black, [position[1][0], position[1][1]]]
return [[-1, -1], [-1, -1]]
return [[-1, -1], [-1, -1]]

0 comments on commit f322b83

Please sign in to comment.