I would like to create a battleship game in C++. I have the game working but I cant really decide how to determine what positions make up a ship. My game board is a 15 x 15 so, for example:
0000000000000010
0000000000000010
0000000000000010
0000000011000000
0001000000000000
0001000000000000
ect....
So what I would like is to be able to somehow say when all positions have been hit for a ship, OH you sank the frigate or OH you sank the Destroyer.
Here is what I have to far:
#include <iostream>
#include <cstdlib>
using namespace std;
int genrow(int dir);
int gencol(int dir);
int gendir();
void setupboard();
void insertship(int row, int col, int dir);
void positionShips();
void printarray();
void gatherCoord();
int xpos;
int ypos;
int mydir;
int shipsize;
int myGuess = 0;
int stop = 0;
int guessX, guessY;
int gameboard[15][15];
int shipPositions[2][15];
int main()
{
srand(1);
setupboard();
cout << "X" << "\tY" << endl;
positionShips();
gatherCoord();
printarray();
while (myGuess < 5)
{
cout << "Where would you like to attack: ";
cin >> guessX >> guessY;
int row = 0;
for (row = 0; row < 15; row = row + 1)
{
int col = 0;
for (col = 0; col < 15; col = col + 1)
{
gameboard[row][col];
if (gameboard[row][col] == 1 && guessX == row && guessY == col)
cout << "Thats a hit!" << endl;
else if (gameboard[row][col] == 0 && guessX == row && guessY == col)
cout << "Miss! Try again!" << endl;
else
"Do Nothing";
}
}
myGuess = myGuess + 1;
}
cout << endl;
system("pause");
return 0;
}
int genrow(int dir)
{
xpos = rand() % 14;
if (xpos < shipsize - 1 && dir == 0)
xpos = xpos + (shipsize - 1);
else if(xpos > 15 - shipsize && dir == 2)
xpos = xpos - (shipsize - 1);
else
"Do nothing";
return xpos;
}
int gencol(int dir)
{
ypos = rand() % 14;
if (ypos < shipsize - 1 && dir == 3)
ypos = ypos + (shipsize - 1);
else if(ypos > 15 - shipsize && dir == 1)
ypos = ypos - (shipsize - 1);
else
"Do nothing";
return ypos;
}
int gendir()
{
return rand() % 3;
}
void setupboard()
{
int row = 0;
while (row < 15)
{
int col = 0;
while (col < 15)
{
gameboard[row][col] = 0;
col = col + 1;
}
row = row + 1;
}
}
void insertship(int row, int col, int dir)
{
int mycount = 0;
gameboard[row][col] = 1;
while (mycount < shipsize - 1)
{
if (dir == 0)
{
row = row - 1;
gameboard[row][col] = 1;
}
else if(dir == 1)
{
col = col + 1;
gameboard[row][col] = 1;
}
else if(dir == 2)
{
row = row + 1;
gameboard[row][col] = 1;
}
else if(dir == 3)
{
col = col - 1;
gameboard[row][col] = 1;
}
else
cout << "You should not be here!";
mycount = mycount + 1;
}
}
void printarray()
{
int myrow = 0;
while (myrow < 15)
{
int mycol = 0;
while (mycol < 15)
{
cout << gameboard[myrow][mycol];
mycol = mycol + 1;
}
cout << endl;
myrow = myrow + 1;
}
}
void gatherCoord()
{
int row = 0;
for (row = 0; row < 15; row = row + 1)
{
int col = 0;
for (col = 0; col < 15; col = col + 1)
{
int cX = 0;
if (gameboard[row][col] == 1)
{
shipPositions[cX][0] = row;
shipPositions[cX][1] = col;
cout << shipPositions[cX][0] << "\t";
cout << shipPositions[cX][1] << endl;
cX = cX + 1;
}
else if (gameboard[row][col] == 0)
"Do Nothing";
else
"What happened?";
}
}
}
void positionShips()
{
while (stop < 5)
{
if (stop < 2)
shipsize = 2;
else if (stop < 4)
shipsize = 3;
else
shipsize = 4;
mydir = gendir();
xpos = genrow(mydir);
ypos = gencol(mydir);
insertship(xpos, ypos, mydir);
stop = stop + 1;
}
}
Thanks for help in advance