############################################################################################################################################################################################################################# 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 importsfrompyrat.src.PlayerimportPlayerfrompyrat.src.MazeimportMazefrompyrat.src.GameStateimportGameStatefrompyrat.src.enumsimportAction########################################################################################################################################################################################################################### CLASSES ###########################################################################################################################################################################################################################
[docs]classFixedPlayer(Player):""" *(This class inherits from* ``Player`` *).* This player follows a predetermined list of actions. This is useful to save and replay a game. """############################################################################################################################################## MAGIC METHODS ##############################################################################################################################################
[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 classsuper().__init__(*args,**kwargs)# Debugassertisinstance(actions,list),"Argument 'actions' must be a list"assertall(actioninActionforactioninactions),"All elements of 'actions' must be of type 'pyrat.Action'"# Private attributesself.__actions=actions
############################################################################################################################################## PUBLIC METHODS ##############################################################################################################################################
[docs]defturn(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. """# Get next actionaction=self.__actions.pop(0)returnaction