Player

This module provides the base class for players in the PyRat game. It defines the interface that all players must implement, including important methods preprocessing(), turn(), and postprocessing(). Players can be implemented as subclasses of this class, and they can use the provided methods to interact with the game state. The turn() method is mandatory and must be implemented in the subclasses.

class Player.Player(name=None, skin=PlayerSkin.RAT)[source]

A player is an agent that can play a PyRat game. The preprocessing() method is called once at the beginning of the game. The turn() method is called at each turn of the game. The postprocessing() method is called once at the end of the game. Only the turn() method is mandatory. If you want to keep track of some information between turns, you can define a constructor and add attributes to the object. Check examples in the provided workspace to see how to do it properly.

__init__(name=None, skin=PlayerSkin.RAT)[source]

(This class is abstract and meant to be subclassed, not instantiated directly).

Initializes a new instance of the class.

Parameters:
  • name (str | None) – Name of the player (if None, we take the name of the class).

  • skin (PlayerSkin) – Skin of the player.

get_name()[source]

Returns the name of the player.

Return type:

str

Returns:

Name of the player.

get_skin()[source]

Returns the skin of the player.

Return type:

PlayerSkin

Returns:

Skin of the player.

postprocessing(maze, game_state, stats)[source]

This method is called once at the end of the game. It can be used to perform any cleanup that is needed after the game ends. It is not timed, and can be used to analyze the completed game, train models, etc.

Parameters:
  • maze (Maze) – An object representing the maze in which the player plays.

  • game_state (GameState) – An object representing the state of the game.

  • stats (dict[str, object]) – A dictionary containing statistics about the game.

Return type:

None

preprocessing(maze, game_state)[source]

This method is called once at the beginning of the game. It can be used to initialize attributes or to perform any other setup that is needed before the game starts. It typically is given more computational resources than the turn() method. Therefore, it is a good place to perform any heavy computations that are needed for the player to function correctly.

Parameters:
  • maze (Maze) – An object representing the maze in which the player plays.

  • game_state (GameState) – An object representing the state of the game.

Return type:

None

abstractmethod turn(maze, game_state)[source]

(This method is abstract and must be implemented in the subclasses.)

This method is called at each turn of the game. It returns an action to perform among the possible actions, defined in the Action enumeration. It is generally given less computational resources than the preprocessing() method. Therefore, you should limit the amount of computations you perform in this method to those that require real-time information.

Parameters:
  • maze (Maze) – An object representing the maze in which the player plays.

  • game_state (GameState) – An object representing the state of the game.

Return type:

Action

Returns:

One of the possible action, defined in the Action enumeration.

Raises:

NotImplementedError – If the method is not implemented in the subclass.