Your program will accept one or two command line arguments. The first argument i
ID: 3678967 • Letter: Y
Question
Your program will accept one or two command line arguments. The first argument is the name of a battleship puzzle board file similar to the file shown below. This sample file begins with the dimensions of the board, in this case 4 rows and 5 columns. Next, we give the number of cells in each row and each column that are occupied by a ship. The other cells in the row are open water. Then, we have a simple list of the ships that must be placed on that board. All ships are 1 cell wide, but each ship type has a different length (# of cells) submarine = 1, destroyer 2, cruiser 3, battleship = 4, carrier 5, cargo-6, and tanker = 7 4 board 4 5 rows 4 0 2 1 cols 1 2 1 2 1 cruiser destroyer submarine submarine 0 Your task is to place the ships on the board satisfying the counts for each row. One important rule in placing the ships is that no two ships may occupy adjacent cells (including the diagonal). The sample puzzle above actually has two solutions. The diagram and sample output below show one of the solutions. Can you manually find the other? Solution cruiser 0 0 horizontal submarine 0 4 submarine 21 destroyer 2 3 vertical 0 10 12121Explanation / Answer
#include <iostream.h>
#include <string.h>
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
void Player_Screen();
void shipplace();
void computerplace();
void Choice(int size1, int left1, int up1, int char11);
void compscreen();
void Place_Choice(int size, int left, int up, int char1);
char shipname[10], compname[10];
int x,intcolumn, introw,intup,intleft,compcolumn, comprow,compup,compleft;
char playerboard [10][10]=
{
{08,08,08,08,08,08,08,08,08,08},
{08,08,08,08,08,08,08,08,08,08},
{08,08,08,08,08,08,08,08,08,08},
{08,08,08,08,08,08,08,08,08,08},
{08,08,08,08,08,08,08,08,08,08},
{08,08,08,08,08,08,08,08,08,08},
{08,08,08,08,08,08,08,08,08,08},
{08,08,08,08,08,08,08,08,08,08},
{08,08,08,08,08,08,08,08,08,08},
{08,08,08,08,08,08,08,08,08,08},
};
char computation [10][10]=
{
{08,08,08,08,08,08,08,08,08,08},
{08,08,08,08,08,08,08,08,08,08},
{08,08,08,08,08,08,08,08,08,08},
{08,08,08,08,08,08,08,08,08,08},
{08,08,08,08,08,08,08,08,08,08},
{08,08,08,08,08,08,08,08,08,08},
{08,08,08,08,08,08,08,08,08,08},
{08,08,08,08,08,08,08,08,08,08},
{08,08,08,08,08,08,08,08,08,08},
{08,08,08,08,08,08,08,08,08,08},
};
char showncomp [10][10]=
{
{08,08,08,08,08,08,08,08,08,08},
{08,08,08,08,08,08,08,08,08,08},
{08,08,08,08,08,08,08,08,08,08},
{08,08,08,08,08,08,08,08,08,08},
{08,08,08,08,08,08,08,08,08,08},
{08,08,08,08,08,08,08,08,08,08},
{08,08,08,08,08,08,08,08,08,08},
{08,08,08,08,08,08,08,08,08,08},
{08,08,08,08,08,08,08,08,08,08},
{08,08,08,08,08,08,08,08,08,08},
};
main()
{
HANDLE hStdout;
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
cout<<"Loading"<<endl;
Sleep(1000);
system("CLS");
cout<<"Loading."<<endl;
Sleep(1000);
system("CLS");
cout<<"Loading.."<<endl;
Sleep(1000);
system("CLS");
cout<<"Loading..."<<endl;
Sleep(1000);
system("CLS");
SetConsoleTextAttribute(hStdout, FOREGROUND_BLUE|BACKGROUND_RED);
cout<<"Welcome to BATTLESHIP..."<<endl;
SetConsoleTextAttribute(hStdout, FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
computerplace();
compscreen();
return 0;
}
void compscreen()
{
comprow=0;
compcolumn=0;
cout << " Computer's Board (debug)" << endl;
cout<<' ';
cout << comprow << "|";
do
{
if(comprow == 9 && compcolumn == 10)
{break;}
if(compcolumn > 9 && comprow < 9)
{
cout << "|"<< endl;
comprow = comprow + 1;
cout << (comprow) << "|";
compcolumn = 0;
}
if(comprow < 10)
{
if(compcolumn < 9)
{
cout << computation[comprow] [compcolumn] << " ";
}
if(compcolumn > 8)
{
cout << computation[comprow] [compcolumn];
}
}
compcolumn = compcolumn + 1;
}
while(comprow < 10);
comprow = 0;
compcolumn = 0;
cout << "|" << endl;
}
void computerplace()
{
srand(time(NULL));
for (x=7;x>=1;x--)
{
switch(x)
{
case 7:
strcpy(shipname,"Tanker");
break;
case 6:
strcpy(shipname,"Cargo");
break;
case 5:
strcpy(shipname,"Carrier");
break;
case 4:
strcpy(shipname,"Battleship");
break;
case 3:
strcpy(shipname,"Cruise");
break;
case 2:
strcpy(shipname,"Destroyer");
break;
case 1:
strcpy(shipname,"Submarine");
break;
}
do
{
compleft=rand()%(10);
compup=rand()%(10);
}
while((computation[compleft][compup]!=08));
computation[compleft][compup]=int(compname[0]);
Choice(x,compleft,compup, (int(compname[0])));
}
do
{
compleft=rand()%(10);
compup=rand()%(10);
}
while((computation[compleft][compup]!=08));
computation[compleft][compup]=83;
Choice(3,compleft,compup, 83);
}
void Choice(int size1, int left1, int up1, int char11)
{
srand(time(NULL));
int choice,loop;
choice=1+rand()%(4);
switch (choice)
{
case 1:
for(loop=1;loop<size1;loop++)
{
if(computation[left1-loop][up1]!=08 || int(left1-(size1-1))<(0))
{
computation[left1][up1]=08;
x=x+1;
break;
}
if(int(left1-(size1-1)) >= (0) && x==size1)
{
for(loop=1;loop<size1;loop++)
{computation[left1-loop][up1]=char11;}
compscreen();
}
}
break;
case 2:
for(loop=1;loop<size1;loop++)
{
if(computation[left1+loop][up1]!=08 || int(left1-(size1-1))>(9))
{
computation[left1][up1]=08;
x=x+1;
break;
}
if(int(left1-(size1-1)) <= (9) && x==size1)
{
for(loop=1;loop<size1;loop++)
{computation[left1+loop][up1]=char11;}
compscreen();
}
}
break;
case 3:
for(loop=1;loop<size1;loop++)
{
if(computation[left1-loop][up1]!=08 || int(left1-(size1-1))>(9))
{
computation[left1][up1]=08;
x=x+1;
break;
}
if(int(up1+(size1-1)) <= (9) && x==size1)
{
for(loop=1;loop<size1;loop++)
{computation[left1][up1+ loop]=char11;}
compscreen();
}
}
break;
case 4:
for(loop=1;loop<size1;loop++)
{
if(computation[left1-loop][up1]!=08 || int(up1-(size1-1))<(0))
{
computation[left1][up1]=08;
x=x+1;
break;
}
if(int(up1-(size1-1)) >= (0) && x==size1)
{
for(loop=1;loop<size1;loop++)
{computation[left1][up1-loop]=char11;}
compscreen();
}
}
break;
}
}
void Player_Screen()
{
introw=0;
intcolumn=0;
cout << "" << endl;
cout << " Player's Board" << endl;
cout << " |0|1|2|3|4|5|6|7|8|9|" ;
cout<<' ';
cout << introw << "|";
do
{
if(introw == 9 && intcolumn == 10)
{break;}
if(intcolumn > 9 && introw < 9)
{
cout << "|"<< endl;
introw = introw + 1;
cout << (introw) << "|";
intcolumn = 0;
}
if(introw < 10)
{
if(intcolumn < 9)
{
cout << playerboard[introw] [intcolumn] << " ";
}
if(intcolumn > 8)
{
cout << playerboard[introw] [intcolumn]<<flush;
}
}
intcolumn = intcolumn + 1;
}
while(introw < 10);
introw = 0;
intcolumn = 0;
cout << "|" << endl;
}
void Place_Choice(int size,int left,int up,int char1)
{
int choice,loop;
cout<<"Do you want your ship to face: "<< ' ' << "1. UP 2.DOWN 3.RIGHT 4.LEFT"<<' ';
cin>>choice;
switch (choice)
{
case 1:
for(loop=1;loop<size;loop++)
{
if(playerboard[left-loop][up]!=08 || int(left-(size-1))<(0))
{
playerboard[left][up]=08;
cout<<"Don't place ships illegally..."<<endl;
x=x+1;
break;
}
if(int(left-(size-1)) >= (0) && x==size)
{
for(loop=1;loop<size;loop++)
{playerboard[left-loop][up]=char1;}
Player_Screen();
}
}
break;
case 2:
for(loop=1;loop<size;loop++)
{
if(playerboard[left+loop][up]!=08 || int(left-(size-1))>(9))
{
playerboard[left][up]=08;
cout<<"Don't place ships illegally..."<<endl;
x=x+1;
break;
}
if(int(left-(size-1)) <= (9) && x==size)
{
for(loop=1;loop<size;loop++)
{playerboard[left+loop][up]=char1;}
Player_Screen();
}
}
break;
case 3:
for(loop=1;loop<size;loop++)
{
if(playerboard[left-loop][up]!=08 || int(left-(size-1))>(9))
{
playerboard[left][up]=08;
cout<<"Don't place ships illegally..."<<endl;
x=x+1;
break;
}
if(int(up+(size-1)) <= (9) && x==size)
{
for(loop=1;loop<size;loop++)
{playerboard[left][up+ loop]=char1;}
Player_Screen();
}
}
break;
case 4:
for(loop=1;loop<size;loop++)
{
if(playerboard[left-loop][up]!=08 || int(up-(size-1))<(0))
{
playerboard[left][up]=08;
cout<<"Don't place ships illegally..."<<endl;
x=x+1;
break;
}
if(int(up-(size-1)) >= (0) && x==size)
{
for(loop=1;loop<size;loop++)
{playerboard[left][up-loop]=char1;}
Player_Screen();
}
}
break;
}
}
void shipplace()
{
for (x=7;x>=1;x--)
{
switch(x)
{
case 7:
strcpy(shipname,"Tanker");
break;
case 6:
strcpy(shipname,"Cargo");
break;
case 5:
strcpy(shipname,"Carrier");
break;
case 4:
strcpy(shipname,"Battleship");
break;
case 3:
strcpy(shipname,"Cruise");
break;
case 2:
strcpy(shipname,"Destroyer");
break;
case 1:
strcpy(shipname,"Submarine");
break;
}
do
{
cout<<"Plot one end of the "<<shipname<<"."<<' ';
cout<<"Please put the left side coordinate, then the top"<< ' ';
cin>>intleft;
cin>>intup;
}
while(intleft<0||intleft>9||intup<0||intup>9||(playerboard[intleft][intup]!=08));
playerboard[intleft][intup]=int(shipname[0]);
Player_Screen();
Place_Choice(x,intleft,intup, (int(shipname[0])));
}
do
{
cout<<"Plot one end of the Submarine."<<' ';
cout<<"Please put the left side coordinate, then the top"<< ' ';
cin>>intleft;
cin>>intup;
}
while(intleft<0||intleft>9||intup<0||intup>9||(playerboard[intleft][intup]!=08));
playerboard[intleft][intup]=83;
Player_Screen();
Place_Choice(3,intleft,intup, 83);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.