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
Random3player. A limitation ofRandom3is 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 thepreprocessing()method.- __init__(*args, **kwargs)[source]¶
Initializes a new instance of the class. Here, in addition to the attributes developed in the
Random3player, 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.
- 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
Actionenumeration. We also update the set of visited cells at each turn. Now, we work with the reduced maze to find the next action.
- find_next_action(maze, game_state)[source]¶
This method returns an action to perform among the possible actions, defined in the
Actionenumeration. 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.
- 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.