Source code for pyrat.src.players.fixed_player

##########################################################################################
########################################## INFO ##########################################
##########################################################################################

# This file is part of the PyRat library.
# It is meant to be used as a library, and not to be executed directly.
# Please import necessary elements using the following syntax:
#     from pyrat import <element_name>

"""
This module provides a player that follows a predetermined list of actions.
It is used when games are saved and replayed.
This player can be useful for testing purposes, for instance to evaluate the behavior of other players against a fixed strategy.
"""

##########################################################################################
######################################### IMPORTS ########################################
##########################################################################################

# PyRat imports
from pyrat.src.players.player import Player
from pyrat.src.mazes.maze import Maze
from pyrat.src.game.game_state import GameState
from pyrat.src.game.enums import Action
from pyrat.src.game.exceptions import PyRatException

##########################################################################################
######################################### CLASSES ########################################
##########################################################################################

[docs] class FixedPlayer (Player): """ *(This class inherits from* ``Player`` *).* This player follows a predetermined list of actions. This is useful to save and replay a game. """ ################################################################################## # CONSTRUCTOR # ##################################################################################
[docs] def __init__ ( self, actions: list[Action], *args: object, **kwargs: object ) -> None: """ Initializes a new instance of the class. The player is given a predetermined list of actions. Args: actions: List of actions to perform. *args: Arguments to pass to the parent constructor. **kwargs: Keyword arguments to pass to the parent constructor. """ # Inherit from parent class super().__init__(*args, **kwargs) # Check validity if not isinstance(actions, list): raise PyRatException(f"Argument 'actions' must be a list, got an object of type {type(actions).__name__}") if not all(isinstance(action, Action) for action in actions): raise PyRatException(f"All elements of 'actions' must be of type 'pyrat.Action', got element {next(action for action in actions if not isinstance(action, Action))!r}") # Private attributes self.__actions = actions
################################################################################## # PUBLIC METHODS # ##################################################################################
[docs] def turn ( self, maze: Maze, game_state: GameState ) -> Action: """ *(This method redefines the method of the parent class with the same name).* Called at each turn of the game to return the next action to perform. Args: maze: An object representing the maze in which the player plays. game_state: An object representing the state of the game. Returns: One of the possible actions. Raises: PyRatException: If all the actions given at initialization have already been performed. """ # Check validity if not (len(self.__actions) > 0): raise PyRatException(f"Player {self.get_name()!r} has no action left: all the actions given at initialization have already been performed") # Get next action action = self.__actions.pop(0) return action
########################################################################################## ##########################################################################################