The game.py
sets up and runs a game where multiple agents (attackers
and defenders
) interact based on defined strategies. It manages game initialization, the game loop, agent actions, visualization updates, and logging.
<aside> ⚠️
Warning
Please make sure you have read through
before many any changes.
Still not sure? Please reach out to the League team by submitting an issue in ‣.
</aside>
Standard Imports:
import os
import sys
The script starts by importing essential Python modules (os
and sys
) to handle file paths and system-specific parameters.
Path Configuration:
root_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
if not root_dir.endswith("games"):
current_file_path = os.path.abspath(__file__)
current_dir = os.path.dirname(current_file_path)
parent_dir = os.path.dirname(current_dir)
root_dir = parent_dir
sys.path.append(root_dir)
To resolve path issues when importing self-defined libraries, the game.py
dynamically adjusts the root directory. This ensures that all required modules within the project can be located and imported successfully.
Library Imports:
from lib.core import *
check_and_install_dependencies()
from lib.interface import *
from lib.utilities import *
import config as cfg
import attacker_strategy
import defender_strategy
from gamms.VisualizationEngine import Color
print(colored("Imported libraries successfully", "green"))
print("Root Directory: ", root_dir)
This part imports several libraries and modules, including:
lib.core
check_and_install_dependencies()
is called to ensure all required dependencies are installed.lib.interface
and lib.utilities
config
attackers
and defenders
from attacker_strategy
and defender_strategy
gamms.VisualizationEngine
The script begins by initializing several game components and configurations:
Variables:
time
and payoff
, are initialized to track the elapsed game time and the current payoff, respectively.Game Rule Loading:
# Load predefined game rules if specified
if cfg.GAME_RULE is not None:
load_game_rule(cfg, cfg.GAME_RULE)
cfg.GAME_RULE
), the script loads the specified rule set using load_game_rule()
.Game Context and Graph Initialization:
ctx, G = initialize_game_context(cfg.VISUALIZATION_ENGINE, cfg.GRAPH_PATH, cfg.LOCATION, cfg.RESOLUTION)
initialize_game_context()
sets up the game environment based on visualization settings, graph path, real-world location, and resolution.