The game begins with both the user and bot sending a negotiation message set with random values. Both side's message values are compared to determine who goes first. Whether its the bot or the user, they send a guess coordinate string that details coordinates for guesses and the other side decides whether they send hit strings and or send guess strings if they miss. The cycle of sending guesses and hits continues until one side has sunken every ship. When the game finishes, the side who wins has their LEDs flashing.
The way these strings are encrypted are done so by checksums. The simplest way I did this is by XORing every bit after '$' and before '*' in the string, creating 2 ascii hexadecimal characters. The reason I need to encrypt the strings is to keep information between only the user and the bot. As the hashing operation is XORing both parties know to decode the string.
|Negotiation set 1||
|Negotiation set 2||
|Negotiation set 3||
string template="$HIT,row,column,hit or miss(# for ship size)*checksum"
The entire game is handled with two state machines. One named protocol.c that handles parsing and recording information of the received encrypted message. The other state machine, ArtificialAgent.c, handles the states of the entire game such as guessing coordinates or determining who goes first. The diagrams below describe the states and their conditions labeled in bold.