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.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 are going to create a new maze attribute that is the same as the original maze, but with the dead-end cells removed. Since the maze is only provided at the beginning of the game, we will use the preprocessing() method to create this new maze.

__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 returns a new maze that contains only the cells that are not dead-ends. A dead-end is defined as a cell that has only one neighbor and does not contain cheese or the player.

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

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

Return type:

Maze

Returns:

A new maze with only the cells that are not dead-ends.