Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhanced Snake Game Script: Some of the new features in this release include Verbose Logging, Custom Configuration and Enhanced Error Handling. #60

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions ai-enhanced-snake-game.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import argparse
import logging

from snake.game import Game, GameConf, GameMode

def main():
# Mapping of solvers and modes
dict_solver = {
"greedy": "GreedySolver",
"hamilton": "HamiltonSolver",
"dqn": "DQNSolver",
}

dict_mode = {
"normal": GameMode.NORMAL,
"bcmk": GameMode.BENCHMARK,
"train_dqn": GameMode.TRAIN_DQN,
"train_dqn_gui": GameMode.TRAIN_DQN_GUI,
}

# Setup command-line argument parser
parser = argparse.ArgumentParser(description="Run snake game agent with various configurations.")
parser.add_argument(
"-s",
default="hamilton",
choices=dict_solver.keys(),
help="Name of the solver to direct the snake (default: hamilton)."
)
parser.add_argument(
"-m",
default="normal",
choices=dict_mode.keys(),
help="Game mode (default: normal)."
)
parser.add_argument(
"-v",
"--verbose",
action="store_true",
help="Enable verbose output for debugging purposes."
)
parser.add_argument(
"--custom_config",
type=str,
default=None,
help="Path to a custom configuration file for the game."
)
args = parser.parse_args()

# Setup logging
logging_level = logging.DEBUG if args.verbose else logging.INFO
logging.basicConfig(level=logging_level, format='%(asctime)s - %(levelname)s - %(message)s')

try:
# Load and apply custom configuration if provided
conf = GameConf()
if args.custom_config:
# Placeholder for custom configuration loading logic
logging.info(f"Loading custom configuration from {args.custom_config}.")
# Implement loading logic based on the format of custom_config file
# Example: conf.load_from_file(args.custom_config)
else:
logging.info("Using default configuration.")

conf.solver_name = dict_solver[args.s]
conf.mode = dict_mode[args.m]
logging.info(f"Solver: {conf.solver_name} Mode: {conf.mode}")

# Run the game
Game(conf).run()

except Exception as e:
logging.error(f"An error occurred: {e}")
raise

if __name__ == "__main__":
main()