The goal of this project is to create a battleship game plug-in.
In parallel I added code tests but didn't go deep into that.
An example Console program is included that can run the plug in. The plug in has also been tested in Unity and seems to work.
- Add another AI with parity https://www.datagenetics.com/blog/december32011/
- Check multiple players
Rules contain map size, ships to be added in maps, and the grid geometry.
Rules are created by defining the grid type and then adding the ships. The grid handles spatial logic like validation and neighbor detection.
var grid = new RectangularGrid(10, 10);
var rules = new Rules<RectangularDirection>(grid);
rules.AddShip("Cruiser", 3, 2);var grid = new HexagonalGrid(7, 7);
var rules = new Rules<HexagonalDirection>(grid);
rules.AddShip("Cruiser", 3, 2);Rules can also be quickly created by using the default method for a standard rectangular game.
var rules = Rules<RectangularDirection>.DefaultRectangular();setting a 10x10 rectangular grid and containing the following ships
- Carrier, size 5, 1 time
- Battleship, size 4, 1 time
- Destroyer, size 3, 2 times
- Submarine, size 3, 2 times
- Patrol Boat, size 2, 2 times
A session must then be created for the game. This will handle turns and win conditions. The session is generic over the direction type used by the grid.
var battleshipSession = new BattleshipSession<RectangularDirection>(rules);The game can be played with 2 players.
Players can be either AI or Human. AI players require an AI from a selection available. Currently only 2 are available, Random and Hunter. Both are compatible with all grid types.
An array with the player names will also need to be created that defines the player order.
var aiPlayer = new AIPlayer<RectangularDirection>("Hunter AI", AIFactory.GetAI(battleshipSession, AIType.Hunter));
var humanPlayer = new HumanPlayer("Konstantinos");
string[] playerOrder = { aiPlayer.Name, humanPlayer.Name };
battleshipSession.SetPlayers(new[] { aiPlayer }, new[] { humanPlayer }, playerOrder);It's possible to let AI play the game on its own just to try it out! Simply create two AI players and no humans.
var aiPlayer = new AIPlayer<RectangularDirection>("Hunter AI", AIFactory.GetAI(battleshipSession, AIType.Hunter));
var aiPlayer2 = new AIPlayer<RectangularDirection>("Random AI", AIFactory.GetAI(battleshipSession, AIType.Random));
string[] playerOrder = { aiPlayer.Name, aiPlayer2.Name };
battleshipSession.SetPlayers(new[] { aiPlayer, aiPlayer2 }, Array.Empty<HumanPlayer>(), playerOrder);Human maps must be created and be assigned their ships. AI players will create them on their own based on their Strategy class.
var map = new Map<RectangularDirection>(rules);
map.PositionShip(new ShipLocation<RectangularDirection>(rules.ShipsInMap[0], new MapCoordinates(0, 0), RectangularDirection.East));
map.PositionShip(new ShipLocation<RectangularDirection>(rules.ShipsInMap[1], new MapCoordinates(1, 1), RectangularDirection.South));
var humanPlayerMaps = new Dictionary<string, Map<RectangularDirection>>()
{
{ humanPlayer.Name, map }
};
battleshipSession.SetMaps(humanPlayerMaps);By calling battleshipSession.Start(); the turn is set and player input is expected. In case anything from the previous commands didn't run correctly an exception will be thrown.
Event OnTurnChanged will be invoked signaling the start of the first turn.
AI turns must be called for execution with battleshipSession.PlayAITurn().
Human turns must set the target player and the fire coordinates.
battleshipSession.PlayHumanTurn(aiPlayer, new MapCoordinates(0, 0));
Whenever a turn is executed event OnPlayerTurnResultExecuted will be invoked with the result of the turn.
Finally whenever event OnSessionCompleted is invoked there is a winner!