Hey all I have an assignment in a level 5 course. I'm lost on how to create a full 5x5 grid battleship game. Any help will be useful. We use Borlands C++. I look for help on-line but it is all coded a different way.
First of all, you should consider your grid. Before programming, planning out helps me and what I came up with is 5 states. These are your states.
Unset
Empty unhit
Empty Miss
Battleship unhit
Battleshit Hit
Now you are using characters which does work, but there is a way to use only bits so that you can make it more compact. You create 4 grids like the actual game. 2 are set with 1s for the battle ships while the other 2 are the actually grids for attacking. To check for a hit, you do an & check (AND) of the bits and if both bits are on, you get a hit.
on on = hit battleship
on off = not hit battlship
off on = hit water
off off = empty space not attacked
This will severely lower the ram your program uses because instead of using 8 bit for each spot, you are using only 2 so you are reducing the space used by at least 75%.
Since it is usually a 10 by 10 grid in a game, you will only need 2 64 bit integers ( 50 bits of each),for the whole grid. If you need help with using bits, I can give you more info but there is a low of great resources out there if you don't want to wait for a reply from me.
The reason I use bits is because it reduced CPU usage. I use a 64 bit integer to represent a hand in Texas Holdem (Wrote a server for the game) and then either compared bits or used numbers representing certain hands to check hand type. I found using bits not only sped up getting the result which is critical for a server that hosts many tables, but it also reduces how much of the proccessor. A suggestion on a way to go with the program.