PLEASE ANSWER USING C++ The rules of Battleship are as follows. Each player firs
ID: 3695910 • Letter: P
Question
PLEASE ANSWER USING C++
The rules of Battleship are as follows. Each player first sets up ships on their board in secret. Each player then guesses a coordinate (letter and number combination) to bombard on the enemies board. If a ship is hit in this bombardment, the player must say “Hit”. If all parts of a ship are hit, then the player must say “You sank my [name of ship]”. If no ship was hit, the player responds “miss”. This process continues until one player has sunk all of the opponent's ships. Here is a website about the rules if mine are inadequate: http://boardgames.about.com/od/battleship/a/Rules-of-Battleship.htm.
The pieces and lengths of pieces for Battleship are: Aircraft carrier = length 5 Battleship = length 4 Submarine = length 3 Cruiser = length 3 Destroyer = length 2 There is a lot of code here. Familiarize yourself with it some before programming (at the very least, you should look at main() and the names of all the functions in the classes). The navigator (left side of geany) can be quite useful to bounce around between methods. Your coding program can probably use Ctrl+f to search for parts of code as well. You can run the program as it is, and it should play the game Battleship for just one ship (the length 3 submarine) on a 10x10 board. For testing purposes, it is probably easiest to read from file (so you do not need to guess where the ships are). Problem A: Different ships (20 points) Currently, we always assume the ship is a submarine (length 3). Modify the code to make all five types of pieces possible. Your piece should always be a battleship (length 4). If the computer generates a random position, it should place a battleship as well. If the computer reads from the file, it should place different pieces based on the first char/string: 'D' for “Destroyer” 'C' for “Cruiser” 'S' for “Submarine” 'B' for “Battleship” 'A' for “Aircraft carrier” The sample file oneShip.txt should place an Aircraft carrier (length 5, denoted with 'A's) in the top left going down, while the fiveShips.txt will put a submarine (length 3, denoted with 'S's) in the top left going down.
oneShip.txt
fiveShips.txt
Battleship.cpp
Explanation / Answer
Answer:
#include <iostream.h>
#include <string.h>
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
void displaygamerwindow();
void shiplocation();
void systemlocation();
void systemoption(int capacity1, int leftsidedirection, int upwards, int word_taken);
void systemwindow();
void locationselect(int capacity, int left, int up, int word_input);
char shiplabel[10], systemlabel[10];
int x,locatevertical, locatehorizontal,locateupwards,locateleftwards,computevertical, computehorizontal,computeupwards,computeleftwards;
char gamerboard [10][10]=
{
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
};
char hiddensystem [10][10]=
{
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
};
char displayedsystem [10][10]=
{
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
};
main()
{
HANDLE result;
result = GetStdHandle(STD_OUTPUT_HANDLE);
cout<<"Loading Please Wait : "<<endl;
Sleep(1000);
system("CLS");
cout<<"Loading Please Wait :"<<endl;
Sleep(1000);
system("CLS");
cout<<"Loading Please Wait :"<<endl;
Sleep(1000);
system("CLS");
cout<<"Loading Please Wait :"<<endl;
Sleep(1000);
system("CLS");
SetConsoleTextAttribute(result, FOREGROUND_BLUE|BACKGROUND_CYAN);
cout<<"Lets Play BattleShip Game :"<<endl;
SetConsoleTextAttribute(result, FOREGROUND_CYAN|FOREGROUND_GREEN|FOREGROUND_BLUE);
systemlocation();
systemwindow();
return 0;
}
void systemwindow()
{
computehorizontal=0;
computevertical=0;
cout << "" << endl;
cout << " System Board " << endl;
cout << "........................................................." << endl;
cout << " |0|1|2|3|4|5|6|7|8|9|" ;
cout<<' ';
cout << computehorizontal << "|";
do
{
if(computehorizontal == 9 && computevertical == 10)
{break;}
if(computevertical > 9 && computehorizontal < 9)
{
cout << "|"<< endl;
computehorizontal = computehorizontal + 1;
cout << (computehorizontal) << "|";
computevertical = 0;
}
if(computehorizontal < 10)
{
if(computevertical < 9)
{
cout << hiddensystem[computehorizontal] [computevertical] << " ";
}
if(computevertical > 8)
{
cout << hiddensystem[computehorizontal] [computevertical];
}
}
computevertical = computevertical + 1;
}
while(computehorizontal < 10);
computehorizontal = 0;
computevertical = 0;
cout << "|" << endl;
cout << "................................................................" << endl;
cout << "" << endl;
}
void systemlocation()
{
srand(time(NULL));
for (x=5;x>=2;x--)
{
switch(x)
{
case 5:
strcpy(systemlabel,"Carrier");
break;
case 4:
strcpy(systemlabel,"Battleship");
break;
case 3:
strcpy(systemlabel,"Kruiser");
break;
case 2:
strcpy(systemlabel,"Destroyer");
break;
}
do
{
computeleftwards=rand()%(10);
computeupwards=rand()%(10);
}
while((hiddensystem[computeleftwards][computeupwards]!=00));
hiddensystem[computeleftwards][computeupwards]=int(systemlabel[0]);
systemoption(x,computeleftwards,computeupwards, (int(systemlabel[0])));
}
do
{
computeleftwards=rand()%(10);
computeupwards=rand()%(10);
}
while((hiddensystem[computeleftwards][computeupwards]!=00));
hiddensystem[computeleftwards][computeupwards]=00;
systemoption(3,computeleftwards,computeupwards, 00);
}
void systemoption(int capacity1, int leftsidedirection, int upwards, int word_taken)
{
srand(time(NULL));
int select,circle;
select=1+rand()%(4);
switch (select)
{
case 1:
for(circle=1;circle<capacity1;circle++)
{
if(hiddensystem[leftsidedirection-circle][upwards]!=00 || int(leftsidedirection-(capacity1-1))<(0))
{
hiddensystem[leftsidedirection][upwards]=00;
x=x+1;
break;
}
if(int(leftsidedirection-(capacity1-1)) >= (0) && x==capacity1)
{
for(circle=1;circle<capacity1;circle++)
{hiddensystem[leftsidedirection-circle][upwards]=word_taken;}
systemwindow();
}
}
break;
case 2:
for(circle=1;circle<capacity1;circle++)
{
if(hiddensystem[leftsidedirection+circle][upwards]!=00 || int(leftsidedirection-(capacity1-1))>(9))
{
hiddensystem[leftsidedirection][upwards]=00;
x=x+1;
break;
}
if(int(leftsidedirection-(capacity1-1)) <= (9) && x==capacity1)
{
for(circle=1;circle<capacity1;circle++)
{hiddensystem[leftsidedirection+circle][upwards]=word_taken;}
systemwindow();
}
}
break;
case 3:
for(circle=1;circle<capacity1;circle++)
{
if(hiddensystem[leftsidedirection-circle][upwards]!=00 || int(leftsidedirection-(capacity1-1))>(9))
{
hiddensystem[leftsidedirection][upwards]=00;
x=x+1;
break;
}
if(int(upwards+(capacity1-1)) <= (9) && x==capacity1)
{
for(circle=1;circle<capacity1;circle++)
{hiddensystem[leftsidedirection][upwards+ circle]=word_taken;}
systemwindow();
}
}
break;
case 4:
for(circle=1;circle<capacity1;circle++)
{
if(hiddensystem[leftsidedirection-circle][upwards]!=00 || int(upwards-(capacity1-1))<(0))
{
hiddensystem[leftsidedirection][upwards]=00;
x=x+1;
break;
}
if(int(upwards-(capacity1-1)) >= (0) && x==capacity1)
{
for(circle=1;circle<capacity1;circle++)
{hiddensystem[leftsidedirection][upwards-circle]=word_taken;}
systemwindow();
}
}
break;
}
}
void displaygamerwindow()
{
locatehorizontal=0;
locatevertical=0;
cout << "" << endl;
cout << " Gamer 's Board" << endl;
cout << "................................................................." << endl;
cout << " |0|1|2|3|4|5|6|7|8|9|" ;
cout<<' ';
cout << locatehorizontal << "|";
do
{
if(locatehorizontal == 9 && locatevertical == 10)
{break;}
if(locatevertical > 9 && locatehorizontal < 9)
{
cout << "|"<< endl;
locatehorizontal = locatehorizontal + 1;
cout << (locatehorizontal) << "|";
locatevertical = 0;
}
if(locatehorizontal < 10)
{
if(locatevertical < 9)
{
cout << gamerboard[locatehorizontal] [locatevertical] << " ";
}
if(locatevertical > 8)
{
cout << gamerboard[locatehorizontal] [locatevertical]<<flush;
}
}
locatevertical = locatevertical + 1;
}
while(locatehorizontal < 10);
locatehorizontal = 0;
locatevertical = 0;
cout << "|" << endl;
cout << "................................................................................" << endl;
cout << "" << endl;
}
void locationselect(int capacity,int left,int up,int word_input)
{
int select,circle;
cout<<"Do you need any ship to face: "<< ' ' << "1. UP 2.DOWN 3.RIGHT 4.LEFT"<<' ';
cin>>select;
switch (select)
{
case 1:
for(circle=1;circle<capacity;circle++)
{
if(gamerboard[left-circle][up]!=00 || int(left-(capacity-1))<(0))
{
gamerboard[left][up]=00;
cout<<"Don't place ships illegally..."<<endl;
x=x+1;
break;
}
if(int(left-(capacity-1)) >= (0) && x==capacity)
{
for(circle=1;circle<capacity;circle++)
{gamerboard[left-circle][up]=word_input;}
displaygamerwindow();
}
}
break;
case 2:
for(circle=1;circle<capacity;circle++)
{
if(gamerboard[left+circle][up]!=00 || int(left-(capacity-1))>(9))
{
gamerboard[left][up]=00;
cout<<"Please check ships place correctly."<<endl;
x=x+1;
break;
}
if(int(left-(capacity-1)) <= (9) && x==capacity)
{
for(circle=1;circle<capacity;circle++)
{gamerboard[left+circle][up]=word_input;}
displaygamerwindow();
}
}
break;
case 3:
for(circle=1;circle<capacity;circle++)
{
if(gamerboard[left-circle][up]!=00 || int(left-(capacity-1))>(9))
{
gamerboard[left][up]=00;
cout<<"Please check ships place correctly"<<endl;
x=x+1;
break;
}
if(int(up+(capacity-1)) <= (9) && x==capacity)
{
for(circle=1;circle<capacity;circle++)
{gamerboard[left][up+ circle]=word_input;}
displaygamerwindow();
}
}
break;
case 4:
for(circle=1;circle<capacity;circle++)
{
if(gamerboard[left-circle][up]!=00 || int(up-(capacity-1))<(0))
{
gamerboard[left][up]=00;
cout<<"Please check ships place correctly"<<endl;
x=x+1;
break;
}
if(int(up-(capacity-1)) >= (0) && x==capacity)
{
for(circle=1;circle<capacity;circle++)
{gamerboard[left][up-circle]=word_input;}
displaygamerwindow();
}
}
break;
}
}
void shiplocation()
{
for (x=5;x>=2;x--)
{
switch(x)
{
case 5:
strcpy(shiplabel,"Carrier");
break;
case 4:
strcpy(shiplabel,"Battleship");
break;
case 3:
strcpy(shiplabel,"Kruiser");
break;
case 2:
strcpy(shiplabel,"Destroyer");
break;
}
do
{
cout<<"Locating one end of the "<<shiplabel<<"."<<' ';
cout<<"Place left side coordinate followed by top :"<< ' ';
cin>>locateleftwards;
cin>>locateupwards;
}
while(locateleftwards<0||locateleftwards>9||locateupwards<0||locateupwards>9||(gamerboard[locateleftwards][locateupwards]!=00));
gamerboard[locateleftwards][locateupwards]=int(shiplabel[0]);
displaygamerwindow();
locationselect(x,locateleftwards,locateupwards, (int(shiplabel[0])));
}
do
{
cout<<"Locate one end of the Submarine."<<' ';
cout<<"Place left side coordinate followed by top "<< ' ';
cin>>locateleftwards;
cin>>locateupwards;
}
while(locateleftwards<0||locateleftwards>9||locateupwards<0||locateupwards>9||(gamerboard[locateleftwards][locateupwards]!=00));
gamerboard[locateleftwards][locateupwards]=00;
displaygamerwindow();
locationselect(3,locateleftwards,locateupwards, 00);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.