For my game StarGrid, I need to add some CPU opponents (note that I'm avoiding using any AI related wording here, since this gets a different connotation nowadays).
The game is divided into multiple factions, each faction have their own ships and one homebase.
The objective of the game is to either:
- Capture the flag of another base and return it to your homebase
- Destroy all enemies and bases
You can read more about the game in my previous post:
StarGrid: A new game I'm making for Palm OS in 2025Every turn, each ship gets one action it can perform:
- Move to another spot
- Attack an enemy (ship or base) with either phasers or torpedoes
- Cloak the ship (making it invisible to the others, unless it gets in close range of an enemy ship)
- Do nothing (skip turn)
To determine what actions the CPU player's ship needs to take I let the game generate multiple scenarios. Each of those scenarios gets assigned a score based on it's importance. When the scenarios are created, the scenario with the highest score is executed.
Here are the scenario's that I currently have in play:
Default score MEDIUM (= 50)
- If an enemy base is in movement range, capture their flag.
- If no enemy base is in range, move towards the closest enemy base
- If the ship is currently carrying an enemy flag, move towards home base
Default score HIGH (= 70)
- If an enemy is in range of the base, attack them.
- If our homebase flag is stolen and the thief enemy is range, attack them. If it is not in range, move towards the homebase of the thief.
- If there is an out of range enemy near the base, move towards the enemy.
Default score LOW (= 40)
- If enemy in range with stolen flag, attack them.
- If any other enemy in range, attack those with the lowest health
Default score LOW (= 40)
- If no enemy in range, activate cloak.
- If cloaked move towards enemy base but stay out of range of enemy ships
To give each CPU player a unique identity, the default scores are offset with a randomised value at the start of each game. This gives each CPU player it's own identity, some opting for offense over defense, some will prefer to steal the flag instead of attacking and others will try to sneak their way around the map until they can achieve their final objective.
To test it all out, I disable the human player and just let the CPU Players duke it all out on their own.
RetroGames, PalmOS, Development, StarGrid
You can get in touch through Mastodon:
@rxpz@social.linux.pizzaBuilding the CPU Player for StarGrid was published on 2025-03-06