Compare commits

...

3 Commits

Author SHA1 Message Date
Maximilian Moser
dedf852090 Make AI smarter 2024-03-04 23:27:49 +01:00
Maximilian Moser
5d4b3c55c6 Add support for multiple AI-controlled players 2024-03-04 23:15:24 +01:00
Maximilian Moser
0dd62af930 Add further players 2024-03-04 23:08:29 +01:00
2 changed files with 45 additions and 10 deletions

View File

@@ -23,4 +23,4 @@ Other key bindings are:
## Playing against AI ## 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 <number of AI players>`

View File

@@ -1,10 +1,10 @@
#!/bin/env python3 #!/bin/env python3
import abc import abc
import argparse
import dataclasses import dataclasses
import math import math
import random import random
import sys
from typing import List from typing import List
import pygame import pygame
@@ -115,11 +115,14 @@ class AI:
return return
target = self.target target = self.target
if random.random() * 100 < 10: if (target and target.size > self.man.size) or random.random() * 100 < 10:
target = None target = None
if target is None: if target is None:
if targets := [e for e in self.enemies or [] if e.size < self.man.size]: targets = [
e for e in self.enemies or [] if e.alive and e.size < self.man.size
]
if targets:
target = random.choice(targets) target = random.choice(targets)
if target is None: if target is None:
@@ -157,10 +160,9 @@ dt = 0
# set up board # set up board
sw, sh = screen.get_width(), screen.get_height() sw, sh = screen.get_width(), screen.get_height()
ai = AI()
maxman = Man(pygame.Vector2(sw / 4, sh / 4), name="Maxman") maxman = Man(pygame.Vector2(sw / 4, sh / 4), name="Maxman")
blackman = Man( blackman = Man(
pygame.Vector2(sw * 0.75, sh * 0.75), pygame.Vector2(sw * 0.75, sh * 0.25),
name="Blackman", name="Blackman",
color="black", color="black",
key_up=pygame.K_i, key_up=pygame.K_i,
@@ -168,11 +170,41 @@ blackman = Man(
key_left=pygame.K_j, key_left=pygame.K_j,
key_right=pygame.K_l, key_right=pygame.K_l,
) )
players = [maxman, blackman] chrisman = Man(
pygame.Vector2(sw * 0.25, sh * 0.75),
name="Chrisman",
color="red",
key_up=pygame.K_UP,
key_down=pygame.K_DOWN,
key_left=pygame.K_LEFT,
key_right=pygame.K_RIGHT,
)
manman = Man(
pygame.Vector2(sw * 0.75, sh * 0.75),
name="Manman",
color="green",
key_up=pygame.K_KP_8,
key_down=pygame.K_KP_2,
key_left=pygame.K_KP_4,
key_right=pygame.K_KP_6,
)
players = [maxman, blackman, chrisman, manman]
food = Food.new() food = Food.new()
if any([arg in ["--ai", "-a"] for arg in sys.argv]): parser = argparse.ArgumentParser()
ai.hook(blackman) 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: while running:
# check recent events # check recent events
@@ -206,6 +238,7 @@ while running:
for p in players: for p in players:
for op in players: for op in players:
if p != op and p.can_eat(op): if p != op and p.can_eat(op):
op.alive = False
players.remove(op) players.remove(op)
if p.can_eat(food): if p.can_eat(food):
@@ -222,7 +255,9 @@ while running:
) )
else: else:
ai.decide() for ai in ais:
ai.decide()
for p in players: for p in players:
p.handle_input(keys, dt) p.handle_input(keys, dt)