void Battleship :: placeships()//places players ships
{
int times, x, y, spot;
int shiplength, isVert;
char column, its,ans ;
int row;
int check, check1, check2;
setcolor(10);
cout <<"\nWould you like to place your own ships or would you like"<<endl;
cout <<"the computer to place them randomly for you"<<endl;
cout <<"press (y) for yes and (n) for no." <<endl<<endl;
for (times=0; times<5; times++)
{
//cout<<"times = "<<times<<endl;//first run is carrier and so on.
if (times ==0)
{
shiplength=5;
cout<<endl<<"Place your Carrier (5 units long)"<<endl;
}
if (times ==1)
{
shiplength=4;
cout<<"Place you Battleship (4 units long)"<<endl;
}
if (times ==2)
{
shiplength=3;
cout<<"Place you Sub (3 units long)"<<endl;
}
if (times ==3)
{
shiplength=3;
cout<<"Place you Destroyer (3 units long)"<<endl;
}
if (times ==4)
{
shiplength=2;
cout<<"Place you Patrol boat (2 units long)"<<endl;
}
check = 50; check1 = 50; check2 = 50;
do
{
do
//CHANGED: Input new data verification
{
setcolor(10); //********also line 215*** LOOK AT X>10
cout<<"Are you placing your ship Vertical(0) or Horizontal(1)? ";
cin>>its;//choose vert or horizontal.
isVert = change(its);
//cout<<isVert<<" = isVert"<<endl;
}while (isVert>1);
//cout<<"First Check"<<endl;
do
{
cout<<"Which column (A-J)?"; //player types in column.
cin>>column;
x = convert(column);
//cout<<" x convert = "<<x<<endl;
cout<<"And which row (1-10)";//player types in vert.
cin>> row;
y = row;
//cout<<" y = "<<y<<endl<<endl;
if (isVert == 0)//checks for ship fit on board.
{
if ((y+shiplength - 1) > 10 || x>10) {check1 = 50; cout<<"Can't fit"<<endl;} //CHANGED: Added || x>10 for data verification
else check1 = 100;
}
else if (isVert = 1)
{
if ((x+shiplength -1) > 10) {check1 = 50; cout<<"Can't fit!"<<endl;}
else check1 = 100;
}
}while (check1 != 100);
if (isVert == 0)//checks for ship fit on board.
{
for (spot=y; spot<(y+shiplength); spot++)
{
if (board2[spot][x] > 0)
{
check2 = 50;
break;
}
else check2 = 100;
}
}
else if (isVert == 1)//check for ship over lap, if spot is >0 then can not
{ //place a ship there
for (spot=x; spot<(x+shiplength); spot++)
{
if (board2[y][spot] > 0)
{
check2 = 50;
break;
}
else check2 = 100;
}
}
}while (check2 != 100);
if (isVert == 0)
{
for (spot=y; spot<(y+shiplength); spot++)
{
if (times ==0) board2[spot][x]=5;
if (times ==1) board2[spot][x]=6;
if (times ==2) board2[spot][x]=7;
if (times ==3) board2[spot][x]=8;
if (times ==4) board2[spot][x]=9;
}
}
if (isVert == 1)
{
for (spot=x; spot<(x+shiplength); spot++)
{
if (times ==0) board2[y][spot]=5;
if (times ==1) board2[y][spot]=6;
if (times ==2) board2[y][spot]=7;
if (times ==3) board2[y][spot]=8;
if (times ==4) board2[y][spot]=9;
}
}
system("cls");
showmyfleet();
}
}
ok after the first couts i where i have press yes or no
i want to use a if else statement to let the user pick if they want to place there ships or let the computer
void Battleship::place_computer_ships()
{
static int position_incriment=0, chosen_position_incriment=0, repeat=0,
overlap=0, occurence=0;
int n, random_direction, length, random_position, x_cord_placement,
y_cord_placement, z=0;
double position, row, col, chosen_ship_position[17];
//Seed random number function
srand((int)time(NULL));
//Repeats if ships overlap, generating new locations and rechecking
while(overlap>=0)
{
overlap=-1; //Will break out of loop if all checks are passed
//Generates 5 ship positions
while (repeat<5)
{
//Random numbers
n=rand(); random_direction=(n%2);
random_position=(n%100)+11;
if (position_incriment==0) length=5;
if (position_incriment==1) length=4;
if (position_incriment==2) length=3;
if (position_incriment==3) length=3;
if (position_incriment==4) length=2;
//verticle placement and check
if (random_direction==0)
{
for (int i=0; i<length; i++)
{
//Uses random number, and based on length, adds ten each loop,
// stores location
chosen_ship_position[chosen_position_incriment]=random_position+(i*10);
chosen_position_incriment++;
}
for (int j=chosen_position_incriment-length; j<chosen_position_incriment; j++)
{
// >110 is past row 10, so loop starts over at while(repeat<5)
if (chosen_ship_position[j]>110)
{
//reset all incriments
repeat=repeat-1;
position_incriment=position_incriment-1;
chosen_position_incriment=chosen_position_incriment-length;
length=10;//breaks out of loop
}
}
}
//Horizontal placement and check
if (random_direction==1)
{
for (int i=0; i<length; i++)
{
//Uses random number, and based on length, adds i each loop,
// stores location
chosen_ship_position[chosen_position_incriment]=random_position+i;
chosen_position_incriment++;
}
for (int j=chosen_position_incriment-length; j<chosen_position_incriment; j++)
{
//ex: if [j]=20 and [j+1]=21 then the ship is off board, so loop
// starts over at while(repeat<5)
if (fmod(chosen_ship_position[j], 10)==0 &&
fmod(chosen_ship_position[j+1], 10)==1)
{
//reset all incriments
repeat=repeat-1;
position_incriment=position_incriment-1;
chosen_position_incriment=chosen_position_incriment-length;
length=10;//breaks out of loop
}
}
}
position_incriment++;
repeat++;
}
//Check if ships overlap, by scanning for repeating numbers
if (position_incriment>0)
{
for (int i=0; i<17; i++)
{
occurence=0;
for (int j=0; j<17; j++)
{
if (chosen_ship_position[i]==chosen_ship_position[j])
{
occurence++;
}
}
//If ships repeat (overlap) then all incriments are reset and loop
// restarts at while(overlap>=0)
if (occurence>=2)
{
position_incriment=0;
chosen_position_incriment=0;
repeat=0;
occurence=0;
overlap=0;
}
}
}
}
z=0;
//Convert to row, col for use in Board1
for (int i=0; i<17; i++)
{
position=chosen_ship_position[i];
row=floor(position/10);
col=fmod(position, 10);
if (fmod(position, 10)==0)
{
row=int((position/10)-1); col=10;
}
y_cord_placement=int(row);
x_cord_placement=int(col);
if (z >= 0 && z<=4) board1[x_cord_placement][y_cord_placement]=5;
if (z >= 5 && z<=8) board1[x_cord_placement][y_cord_placement]=6;
if (z >= 9 && z<=11) board1[x_cord_placement][y_cord_placement]=7;
if (z >= 12 && z<=14) board1[x_cord_placement][y_cord_placement]=8;
if (z >= 15 && z<=17) board1[x_cord_placement][y_cord_placement]=9;
z++;
}
}
this is the function we used to generate ships randomly for the computer i want to use this same function to generate ships for the player if they choose so...when i tried to do the if else statement i can get the program to run but it skips all my couts about pressing yes and no and just goes straight to the game with random ships that are invisible