battleship game help

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.

here is my code so far

#include<iostream.h>
#include<conio.h>
//Name:
//Author:
//Date:

void drawfiller(int,int);
char bship[6][6];

main()
{

int x,fcnt = 0,shots = 8;
char topl[25]={201,205,205,205,203,205,205,205,203,205,205,205,203,205,205,205,203,205,205,205,187};
char midl[25]={204,205,205,205,206,205,205,205,206,205,205,205,206,205,205,205,206,205,205,205,185};
char bottoml[25]={200,205,205,205,202,205,205,205,202,205,205,205,202,205,205,205,202,205,205,205,188};
char filler[25]={186,32,32,32,186,32,32,32,186,32,32,32,186,32,32,32,186,32,32,32,186};

bship[3][4] = 'X';
bship[2][1] = 'H';
bship[4][2] = 'S';

cout<<topl<<endl;

for(x=1;x<6;x++)
{
drawfiller(x,shots);
if (x<5)
cout<<midl<<endl;
}
cout<<bottoml<<endl;


getch();
}

void drawfiller(int row,int goes)
{
int y;
for(y=1; y<6;y++)
{
if(goes < 8)
{
if (bship[row][y] == 'X')
cout<<char(186)<<" X ";
else
if(bship[row][y] == 'H')
cout<<char(186)<<" H ";
else
cout<<char(186)<<" ";
}
else
{
if (bship[row][y] == 'X')
cout<<char(186)<<" X ";
else
if(bship[row][y] == 'H')
cout<<char(186)<<" H ";
else
if(bship[row][y] == 'S')
cout<<char(186)<<" S ";
else
cout<<char(186)<<" ";
}
}
cout<< char(186)<<endl;
}
1
2
3
bship[3][4] = 'X';
bship[2][1] = 'H';
bship[4][2] = 'S';

What is that supposed to mean? how can you assign a char to a char matrix?
Is this suppose to be Object orientated or simple procedural?
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.
Topic archived. No new replies allowed.