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": 20.0,
43                   "cell_percentage": 80.0,
44                   "wall_percentage": 60.0,
45                   "maze_width": 13,
46                   "maze_height": 10,
47                   "nb_cheese": 1,
48                   "trace_length": 1000}
49
50    # Instantiate a game with specified arguments
51    game = Game(**game_config)
52    
53    # Let's visualize the three players in the same maze, one after the other
54    # To make sure that the maze is the same for all players, we will add a fixed seed to the game configuration
55    # This is done by the `random_seed` argument of the Game class, defined in the dictionary above
56    for player in [Random1(), Random2(), Random3(), Random4()]:
57
58        # Add player to the game, starting at the bottom left corner
59        game.add_player(player, location=StartingLocation.BOTTOM_LEFT)
60
61        # Start the game
62        stats = game.start()
63        print(f"Statistics for {player.get_name()}:")
64        pprint.pprint(stats)
65
66        # Reset the game for the next player
67        # Argument same=True means that the game will keep the same maze and cheese distribution
68        game.reset(keep_players=False, same=True)
69
70#####################################################################################################################################################
71#####################################################################################################################################################