Random4

This module provides a player that performs random actions in a PyRat game. It is an improvement of the Random3 player. Here, we illustrate how to use the preprocessing() method to do things at the beginning of the game.

class Random4(*args, **kwargs)[source]

(This class inherits from Player ).

This player is an improvement of the Random3 player. A limitation of Random3 is that it can easily enter its fallback mode when visiting a dead-end. In this case, it may move randomly for a long time before reaching an unvisited cell. To improve our algorithm, we remove the dead-end cells from the maze, and keep the resulting maze in an attribute. Since the maze does not change during the game, this only needs to be done once, in the preprocessing() method.

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

Initializes a new instance of the class. Here, in addition to the attributes developed in the Random3 player, we also create an attribute for our reduced maze.

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 use it to create a reduced maze that contains only the cells that are not dead-ends. We define a dead-end as a cell that has only one neighbor and does not contain cheese or the player. Note that this is not the best way to define a dead-end, but it is a simple one.

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. Now, we work with the reduced maze to find the next action.

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.

remove_dead_ends(maze, locations_to_keep)[source]

This method removes the dead-ends from the maze, and returns it. A dead-end is defined as a cell that has only one neighbor and does not contain cheese or the player. The maze is modified in place: no copy is made, and the returned maze is the same object as the one given as argument.

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

  • locations_to_keep (list[int]) – A list of locations to keep in the reduced maze.

Return type:

Maze

Returns:

The maze given as argument, from which the dead-ends have been removed.