I made a battleship program. In a nutshell, my board is an array of 10 characters by 10 characters. The assemble function creates the board such that the ships are aligned in straight lines and do not cross over each other.
In the main function, I prompt the user for the number of ships and the ship size. My board output is consistent for every input integar (less than 10) that is not 6. I don't know why 6 gives me the error because sizes 7,8, and 9 work okay.
My code shows the main function and the assemble function. The output should show the user the battleship board of all dots and an "S" where a ship is present. I haven't made the function yet that allows the user to bomb the board.
# include<iostream>
# include<cstdlib>
# include<ctime>
# include<vector>
usingnamespace std;
constint rows=10;
constint cols=10;
void assemble(char grid[rows][cols], int sizeOfShip,int numOfShips)
/*For loop fills board with little dots */
for (int i=0;i<rows;i++)
{
for (int j=0;j<cols;j++)
{
grid[i][j]='.';
}
}
/* For loop counts every ship that is declared */
for (int k=0;k<numOfShips;k++)
{
int a,b,dx,dy; // initial ship locations and directions
do //loop picks a random initial location,x=a and y=b
{
srand(time(NULL));
a=rand()%cols;
b=rand()%rows;
}
while (grid[b][a]=='S');
// Ships are represented by vectors on the board
vector<int> x(sizeOfShip);
vector<int> y(sizeOfShip);
x[0]=a;
y[0]=b;
bool goodDirection; //variable determines if the direction works
bool inverted=false; //you'll understand the purpose of this variable in a later loop
int n=0; //integar n is the index of vectors x and y
do
{
goodDirection=true;
/*Finds the orientation of the ship */
do
{
srand(time(NULL));
dx=-1+rand()%3; //dx and dy are random directions
dy=-1+rand()%3;
}
while (grid[y[n]+dy][x[n]+dx]=='S' or (dx==0 and dy==0) or x[n]+dx<0 or x[n]+dx>cols-1 or y[n]+dy<0 or y[n]+dy>rows-1); //while the new orientation isn't valid
/* While the orientation is good */
while (goodDirection==true and n<sizeOfShip)
{
x[n+1]=x[n]+dx;
y[n+1]=y[n]+dy;
n=n+1;
/*The direction fails and has not yet been inverted */
if ((grid[y[n]][x[n]] =='S' or x[n]<0 or x[n]>cols-1 or y[n]<0 or y[n]>rows-1) and inverted==false)
{
dx=-dx;
dy=-dy; //go the opposite direction for efficiency
inverted=true;
x[n]=a+dx;
y[n]=b+dy; //tries opposite orientation from the starting point
}
/*The direction fails and was already inverted */
if ((grid[y[n]][x[n]] =='S' or x[n]<0 or x[n]>cols-1 or y[n]<0 or y[n]>rows-1) and inverted==true)
{
goodDirection=false;
n=0;
inverted=false;
}
} // end while loop for if the direction is good and the size of ship is less than sizeOfShip
}
while (goodDirection==false);
/*Assuming that every point of the ship along a straight line is valid and we can print out the ship*/
for (int s=0;s<sizeOfShip;s++)
{
grid[y[s]][x[s]]='S';
}
} // end for loop that counts every ship
return;
}
int main()
{
char board[rows][cols]; // ten by ten board
int nShips; // number of enemy ships to bomb
int shipSize; // size of enemy ship
cout<<"Number of enemy ships? "<<endl;
cin>>nShips;
cout<<endl<<"Size of enemy ship? "<<endl;
cin>>shipSize;
cout<<endl;
assemble(board,shipSize,nShips);
/*Print the board */
for (int i=0;i<rows;i++)
{
for (int j=0;j<cols;j++)
{
cout<<board[i][j];
}
cout<<endl;
}
cout<<endl;
return 0;
}
vector<int> x(sizeOfShip);
vector<int> y(sizeOfShip);
int n=0; //integar n is the index of vectors x and y
1 2 3 4 5 6
/* While the orientation is good */
while (goodDirection==true and n < sizeOfShip)
{
x[n+1]=x[n]+dx; // Access violation here on the last iteration.
y[n+1]=y[n]+dy; // ^
n=n+1;