1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
/*Test #2, battleship game.
Write a C++ program that implements a bi-dimensional array of 4x4 elements and initialize it with 0s.
Each battleship is two positions long in either vertical or horizontal position.
The program should prompt the user as follow:
Enter #1 to position the battleships.
Enter #2 to start playing.
Enter #4 to quit.
If the user enters a number different than 1, 2 or 4, then display and error message and display the above options again.
If the user enters #1, then ask the user to enter each of the locations of the 3 battleships with specifying each position as (x,y), locations containing battleships should be set from 0 to 1.
0 0 0 0 0 1 0 0
0 0 0 0 ===> 0 1 0 0
0 0 0 0 0 0 1 1
0 0 0 0 1 1 0 0
In the example above, the user has position one battleships on (1,0)-(1,1) the second one on (2,2)-(3,2) and the third on on (0,3)-(1,3)
After the user has finished positioning the third ship, the program should go back to the main menu.
When user enters #2, the program should prompt the user to start shooting by asking to enter the value of x and the value of y, one shoot at the time, and then show if it Failed or Hit a battleship.
If the user enters 4 at any time the program must display a message saying “goodbye” and quit.
The user could keep shooting until hitting all the battleships or reaching a maximum number of 16 shoots, on either case, then the program should show the outcome as:
F H 0 F
F H F 0
0 F H H
H H F 0
Total shoots = 12, Hits = 6, Fails = 6*/
int x,y,x1,y1,x2,y2,h,f;
int gameboard[4][4];
void initialize_gameboard(int gameboard[4][4]);
const int noship = 0; //point without ship
const int ship = 1; //point with a ship
const char hit = h; //tried and hit ship
const char miss = f; //tried and missed ship
int num,num1,num2,num3,num4;
float ship1(float x,float y);
float ship2(float x1,float y1);
float ship3(float x2,float y2);
int main()
{
cout<<"Enter 1 to position the battleships.\n";
cout<<"Enter 2 to start playing.\n";
cout<<"Enter 4 to quit.\n";
cin>>num;
if (num==1)
{
cout<<"Enter three Battleship positions.\n";
cout<<"First Battleship.\n";
cin>>x,y;
{
for (int x=0;x<4;x++)
cout<<ship1[x][y]<<" ";
{
for (int y=0;y<4;y++);
}
}
cout<<"Second Battleship.\n";
cin>>x1,y1;
{
for (int x1=0;x1<4;x1++)
cout<<ship2[x1][y1]<<" ";
{
for (int y1=0;y1<4;y1++);
}
}
cout<<"Third Battleship.\n";
cin>>x2,y2;
{
for (int x2=0;x2<4;x2++)
cout<<ship3[x2][y2]<<" ";
{
for (int y2=0;y2<4;y2++)
cout<<endl;
}
}
}
else if (num==2)
{
}
else if (num==4)
{
cout<<"Goodbye!\n";
return 0;
}
}
|