Random3

This module provides a player that performs random actions in a PyRat game. It is an improvement of the Random2 player. Here, we illustrate how attributes can be used to keep track of visited cells.

class Random3.Random3(*args, **kwargs)[source]

(This class inherits from Player ).

This player is an improvement of the Random2 player. Here, we add elements that help us explore better the maze. More precisely, we keep a set of cells that have already been visited in the game. Then, at each turn, we choose in priority a random move among those that lead us to an unvisited cell. If no such move exists, we move randomly.

__init__(*args, **kwargs)[source]

Initializes a new instance of the class. Here, the constructor is only used to initialize a set that will keep track of visited cells. This set can later be updated at each turn of the game to avoid going back to cells that have already been visited.

Parameters:
  • args (object) – Arguments to pass to the parent constructor.

  • kwargs (object) – Keyword arguments to pass to the parent constructor.

preprocessing(maze, game_state)[source]

(This method redefines the method of the parent class with the same name).

This method is called once at the beginning of the game. Here, we just initialize the set of visited cells.

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

turn(maze, game_state)[source]

(This method redefines the method of the parent class with the same name).

It is called at each turn of the game. It returns an action to perform among the possible actions, defined in the Action enumeration. We also update the set of visited cells at each turn.

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 actions.

find_next_action(maze, game_state)[source]

This method returns an action to perform among the possible actions, defined in the Action enumeration. Here, the action is chosen randomly among those that don’t hit a wall, and that lead to an unvisited cell if possible. If no such action exists, we choose randomly among all possible actions that don’t hit a wall.

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 actions that leads to a valid neighbor.