Revisiting A*- A simple strategy game
Tags: Programming, Object Oriented, Board Games, C Sharp Programming, Videogames

Turn based action in SPACE!

Last we saw the A* , I had a very simple console program that was able to output the position of my token and a computer controlled token that chased my own around a game board (made up of python lists). I wanted to extend that concept to more functional program that started to resemble a basic turn based strategy game ,a particular favorite genre of mine.

Taking a page from such classics such as Homeworld or Sins of a Solar Empire (I know they’re RTS games but they happen to be two of my favorites games ever) I decided to design my game with a space theme. I thus proudly present to you Star Admiral V0.0!!!

Cue for applause!

I build the game using .Net Forms , for two reasons , first I wanted into a language more suited to desktop applications development than Python, .NET provides great flexibility to create user interfaces and make them interact with the core logic. It also allows for a greater versatility in Object Oriented Design , for the way the C# language is structured, I’m not going to go into the details of it because that’s not the scope for this entry, but I will provide a GitHub repository where the full code for it is.

The program itself is not too dissimilar to the initial console program for A* that I made on python but it does have a key difference, in the original program, the objective was merely to cross the board from one side to another without getting caught by the enemy. In Star Admiral there’s a twist, now we have to defeat the enemy in a turn based tactical space combat, exciting!.

Basic Rules

Each player (human and computer) has 3 action points, or ap. Each of these ap’s can be used to move around the board or to attack the opposite player. When the player is out of ap’s it has to pass its turn to the opposite player. The game finishes when one of the players is dead.

Game Architecture

Although the artificial intelligence in this game has an almost identical implementation that the previous version of A*, the adding functionalities and the graphical interface required a more expansive class architecture that allowed for an effective and traceable control of the game elements . With that in mind , we have 7 distinct classes here: Admiral, Ship, Board, Enemy Queue, Node and Form1

Admiral : Holds both players data and behavior , fleet sizes , number of action points and implements the methods that are necessary to control the ships and attack the opposite player

Ship: Ship contains the data relative to the ship that is used by most of the Admiral behavior methods , it gives information about the health, attack power, name of the ship and other useful stats that are necessary to keep track of the actions the ship can carry out. The ship class also has a reference to the Node class to know which node a particular ship is at at a given time.

Board: The board class is a data structure in the form of a Node array that keeps track of the positions of the ships provides the Admirals with information about the relative positions of their ships within the board

Node: The node class is the basic constitutive element in the Board class, it keeps track of the X, Y coordinates of a particular node in the Board array , as well as keeping a reference to a Ship object to verify whether or not a Ship is present at a particular node

Queue: A standard Queue structure, used in the A* implementation

Enemy: The enemy class inherits from Admiral and handles the AI logic of the Enemy player

Form1: Form 1 is the default class created by the .Net framework to handle all the interface elements and their interaction with the core logic

Program architecture

Fun functionality

So you’ve seen from the main screen that my interface is , I hope , somewhat intuitive , if not a AAA standard polish and appeal. This layout however , did not came intuitively to me but rather is the product of several trials that allowed me to increasingly refine the way the command inputs where given to the program, also I needed to solve the way the sprites move around the screen in such a way that it was both connected to the direction the player or the computer gave and at the same time it was coordinated with the Board and Node data structure. Ultimately I decided to have that the sprites are to move in a fixed quantity and in just 1 direction at the time, is possible to have them move in any direction and even skip nodes to “teleport” around the board, but such movement rules sort of break the game. When the user presses one of the movement buttons, the ship moves in the selected direction , if the ship runs out of moves then the program informs us of it and disables all the action buttons except for pass

I can move!

Upon hitting pass , the enemy ship executes its turn , calling upon A* to move towards our sprite, if the enemy detects our ship it begins the attack cycle.

Woah it chases me!

Is important to mention that , every time one of the ships enemy or player move, the program scans the surrounding nodes to see if a ship is present , if it is , then it indicates that the player has acquired a target and that it can be attacked.

The players attack each other in succession until only one stands, the attack method itself makes use of random probabilities to introduce an element of chance and make the game more interesting.

Final Thoughts and Next Steps

Designing and creating this project took me about 3–4 weeks, I had a great advantage in that I had already the basis of A* defined and it was just a matter of translating them from python to C#. The main problems that I had to deal with in this project were mostly related to the flow of information from the interface to the node/board logic and also how to effectively display the actions of both the player and the enemy, but figuring out the whole deal turned out to be a great deal of fun. The solution to easily communicate the user interface to the battle board was to use a Windows Form Picture Box element that let me show the sprite and , at the same time , could be contained as a reference in the Ship object associated with it , thus allowing to issue move commands and track the ship around the board.

Where I can go from this? well , now that I’ve been able to create a working board game , the next step would be to port it to a game engine (such as Unity MonoGame) and add cooler features such as music , animations , better looking sprites and a funnier looking UI . The AI of the enemy can also be much more improved to make it more engaging and challenging.

Another aspect that can be improved with the program is the exception handling and testing of use cases for the game; under certain conditions the game breaks (for example when I try to move without previously selecting a ship, or when a ship) causing the application to crash.

Programming games represents a considerable challenge, significant efforts and work hours are needed to program even a limited functionality game such as Star Admiral, however one learns much about Object Oriented Programming, Designs Patterns and UI design, plus they’re really fun projects.

Below I’ve left a GitHub repository for the whole code in case you want to see the details of it or maybe have some fun with it yourself