visualize_random_players

Source Code

 1##########################################################################################
 2########################################## INFO ##########################################
 3##########################################################################################
 4
 5# This file is provided as an example by the PyRat library.
 6# It describes a script that creates a PyRat game.
 7# Please import necessary elements using the following syntax:
 8#     from pyrat import <element_name>
 9#     from players.<player_name> import <player_name>
10
11"""
12In this script, we visualize four players in the same maze, one after the other.
13Each player is a random player that performs random actions.
14This is useful to see how the players behave in the same environment.
15The maze is the same for all players, thanks to a fixed random seed.
16"""
17
18##########################################################################################
19######################################### IMPORTS ########################################
20##########################################################################################
21
22# External imports
23import pprint
24
25# PyRat imports
26from pyrat import Game, StartingLocation
27from players.Random1 import Random1
28from players.Random2 import Random2
29from players.Random3 import Random3
30from players.Random4 import Random4
31
32##########################################################################################
33######################################### SCRIPT #########################################
34##########################################################################################
35
36if __name__ == "__main__":
37
38    # First, let's customize the game elements
39    # This is done by setting the arguments of the Game class when instantiating it
40    # In Python, we can also create a dictionary `d` with these arguments and pass it to the Game class using `game = Game(**d)`
41    # This can be convenient for code organization and readability
42    game_config = {"mud_percentage": 0.0,
43                   "maze_width": 13,
44                   "maze_height": 10,
45                   "nb_cheese": 1,
46                   "random_seed": 42,
47                   "trace_length": 1000}
48
49    # Instantiate a game with specified arguments
50    game = Game(**game_config)
51    
52    # Let's visualize the three players in the same maze, one after the other
53    # To make sure that the maze is the same for all players, we will add a fixed seed to the game configuration
54    # This is done by the `random_seed` argument of the Game class, defined in the dictionary above
55    for player in [Random1(), Random2(), Random3(), Random4()]:
56
57        # Add player to the game, starting at the bottom left corner
58        game.add_player(player, location=StartingLocation.BOTTOM_LEFT)
59
60        # Start the game
61        stats = game.start()
62        print(f"Statistics for {player.get_name()}:")
63        pprint.pprint(stats)
64
65        # Reset the game for the next player
66        # Argument same=True means that the game will keep the same maze and cheese distribution
67        game.reset(keep_players=False, same=True)
68
69##########################################################################################
70##########################################################################################