From 5d4b3c55c6d7d0566e8a70b91f81eee8aff44ea8 Mon Sep 17 00:00:00 2001 From: Maximilian Moser Date: Mon, 4 Mar 2024 23:14:11 +0100 Subject: [PATCH] Add support for multiple AI-controlled players --- README.md | 2 +- maxman.py | 23 ++++++++++++++++++----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e5bfc03..5300f28 100644 --- a/README.md +++ b/README.md @@ -23,4 +23,4 @@ Other key bindings are: ## Playing against AI -If you want to play against the AI instead of other players, supply the corresponding CLI flag: `pipenv run ./maxman.py --ai` +If you want to play against the AI instead of other players, supply the corresponding CLI flag: `pipenv run ./maxman.py --ai ` diff --git a/maxman.py b/maxman.py index 342507f..c9d8367 100755 --- a/maxman.py +++ b/maxman.py @@ -1,10 +1,10 @@ #!/bin/env python3 import abc +import argparse import dataclasses import math import random -import sys from typing import List import pygame @@ -157,7 +157,6 @@ dt = 0 # set up board sw, sh = screen.get_width(), screen.get_height() -ai = AI() maxman = Man(pygame.Vector2(sw / 4, sh / 4), name="Maxman") blackman = Man( pygame.Vector2(sw * 0.75, sh * 0.25), @@ -189,8 +188,20 @@ manman = Man( players = [maxman, blackman, chrisman, manman] food = Food.new() -if any([arg in ["--ai", "-a"] for arg in sys.argv]): - ai.hook(blackman) +parser = argparse.ArgumentParser() +parser.add_argument( + "--ai", + type=int, + choices=[0, 1, 2, 3, 4], + default=1, + help="number of AI players", +) +args = parser.parse_args() +ais = [] +for i in range(args.ai): + ai = AI() + ai.hook(players[-i - 1]) + ais.append(ai) while running: # check recent events @@ -240,7 +251,9 @@ while running: ) else: - ai.decide() + for ai in ais: + ai.decide() + for p in players: p.handle_input(keys, dt)