forked from bennett1412/Battleship
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.cpp
More file actions
351 lines (322 loc) · 7.19 KB
/
Code.cpp
File metadata and controls
351 lines (322 loc) · 7.19 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
//Battleship - A Game of Strategy
#include<iostream.h>
#include<conio.h>
#include<process.h>
void rules()
{
//Function to display rules of the game
cout<<"1.The computer has placed three boats of sizes 2,3 and 4 randomly.\n";
cout<<"2.None of the boats are placed diagonally.\n";
cout<<"3.No two boats are touching each other.\n";
cout<<"4.To shoot your missiles onto your opponent's grid, follow the instructions \n";
cout<<"5.Cannot exit the game while inputing a value\n";
cout<<"6.Scores will be provided on the basis of hits and misses.";
}
//Fuction to display the grid
void grid(char sea[8][8])
{
int i,j,c;
cout<<" ";
for(i=0,j=0;j<8;j++)
{
cout<<j<<" ";
}
cout<<"\n";
for(i=0;i<8;i++)
{ for(j=0;j<8;j++)
{ if(j==0)
{
cout<<i<<" ";
}
sea[i][j]='+';
cout<<sea[i][j]<<" ";
}
cout<<"\n";
}
}
//Function to store the coordinates
//Boat 1
void boat1(int boat1x[2], int boat1y[2], char boat1t[2]){
for (int i=0;i<2;i++) {
boat1x[i]=7;
boat1y[i]=i+1;
boat1t[i]='+';
}
}
//Function to check hit or miss
//Boat 1
int checkBoat1(int x, int y, int boat1x[2], int boat1y[2], char boat1t[2],char sea1[8][8]){
for (int i=0;i<2;i++) {
if ((boat1x[i]==x) && (boat1y[i]==y)) {
boat1t[i]='#';
sea1[x][y]='#';
return 1;
}
}
return 0;
}
//Function to check if the boat is destroyed or not destroyed
//boat 1
int checkDestroy1(char boat1t[2]){
for (int i=0;i<2;i++) {
if (boat1t[i]=='+') return 0;
}
return 1;
}
//Function store the coordinates
//boat 2
void boat2(int boat2x[3], int boat2y[3], char boat2t[3]){
for (int i=0;i<3;i++) {
boat2x[i]=i+2;
boat2y[i]=7;
boat2t[i]='+';
}
}
//Function to check hit or miss
//Boat 2
int checkBoat2(int x, int y, int boat2x[3], int boat2y[3], char boat2t[3],char sea1[8][8]){
for (int i=0;i<3;i++) {
if ((boat2x[i]==x) && (boat2y[i]==y)) {
boat2t[i]='#';
sea1[x][y]='#';
return 1;
}
}
return 0;
}
//Function to check if the boat is destroyed or not
//Boat 2
int checkDestroy2(char boat2t[3]){
for (int i=0;i<3;i++) {
if (boat2t[i]=='+') return 0;
}
return 2;
}
//Function store coordinates of the boat
//boat3
void boat3(int boat3x[4],int boat3y[4],char boat3t[4]){
for (int i=0;i<4;i++) {
boat3x[i]=i;
boat3y[i]=3;
boat3t[i]='+';
}
}
//Function to check hit or miss
//boat3
int checkBoat3(int x, int y, int boat3x[4], int boat3y[4], char boat3t[4],char sea1[8][8]){
for (int i=0;i<4;i++) {
if ((boat3x[i]==x) && (boat3y[i]==y)) {
boat3t[i]='#';
sea1[x][y]='#';
return 1;
}
}
return 0;
}
//Function to check if the boat is destroyed or not
//boat3
int checkDestroy3(char boat3t[4]){
for (int i=0;i<4;i++) {
if (boat3t[i]=='+') return 0;
}
return 3;
}
//Function to store the coordinates of the boat
//boat4
void boat4(int boat4x[5],int boat4y[5],char boat4t[5]){
for (int i=0;i<5;i++) {
boat4x[i]=5;
boat4y[i]=i;
boat4t[i]='+';
}
}
// new change
//Function to check hit or miss
//boat 4
int checkBoat4(int x, int y, int boat4x[5], int boat4y[5], char boat4t[5],char sea1[8][8]){
for (int i=0;i<5;i++) {
if ((boat4x[i]==x) && (boat4y[i]==y)) {
boat4t[i]='#';
sea1[x][y]='#';
return 1;
}
}
return 0;
}
//Function to check if the boat is destroyed or not
//boat4
int checkDestroy4(char boat4t[5]){
for (int i=0;i<5;i++) {
if (boat4t[i]=='+') return 0;
}
return 4;
}
//Function to store the boats in an array
void comp(char sea[8][8])
{
int i,j,c;
for(i=0;i<8;i++)
{ for(j=0;j<8;j++)
{
sea[i][j]='+';
}
}
//Size of the boat is 2
for(i=7,j=1;j<3;j++)
{
sea[i][j]='#';
}
//Size of the boat is 3
for(i=2,j=7;i<5;i++)
{
sea[i][j]='#';
}
//Size of the boat is 4
for(i=0,j=3;i<4;i++)
{
sea[i][j]='#';
}
//Size of the boat is 5
for(i=5,j=0;j<5;j++)
{
sea[i][j]='#';
}
}
//Program to accept coordinates to which the user wants to shoot
void shoot(int &x,int &y)
{
cout<<"\n\nEnter the coordinates at which wanna shoot:\n";
do{
cout<<"Enter the row number in which u wanna shoot:\n";
cin>>x;
if(x>7) {
cout<<"Error!! row number should be between 0-7\n";
}
}while(x>7) ;
do {
cout<<"Enter the column number at which u wanna shoot:\n";
cin>>y;
if(y>7) {
cout<<"Error!! column number should be between 0-7\n";
}
} while(y>7);
}
//Function to show the user's hits and misses
void prgrid(char sea[8][8])
{
int i,j,c;
cout<<" ";
for(i=0,j=0;j<8;j++)
{
cout<<j<<" ";
}
cout<<"\n";
for(i=0;i<8;i++)
{ for(j=0;j<8;j++)
{ if(j==0)
{
cout<<i<<" ";
}
cout<<sea[i][j]<<" ";
}
cout<<"\n";
}
}
//Function to check gameover or not
int gameover(char sea[8][8],char sea1[8][8])
{int i,j;
for(i=0;i<8;i++)
{ for(j=0;j<8;j++)
{
if (sea[i][j]=='#') {
if(sea[i][j]!=sea1[i][j])
return 0;
}
}
}
return 1;
}
void main()
{
clrscr();
int i,j,a,b,c,s,result,count;
count=0;//Var to check score
char se[8][8],se1[8][8];//computer grids
int boat1x[2],boat1y[2];//boat1 coordinates
char boat1t[2];//var to check hit(#) or miss($)
int boat2x[3],boat2y[3];//boat2 coordinates
char boat2t[3];//var to check hit or miss
int boat3x[4],boat3y[4];//boat3 coordinates
char boat3t[4];//var to check hit or miss
int boat4x[5],boat4y[5];//boat 4 coordinates
char boat4t[5];//var to check hit or miss
cout<<"Battleship - A game of strategy\n\n";
grid(se1); //show grid
//initialize grid array
comp(se); //set computer boats
cout<<"This is the grid this is where the boats are hidden.\n\n";
cout<<"These are the rules for the game";
rules(); //show rules of the game
cout<<"\n\n";
boat1(boat1x,boat1y,boat1t);//store boat1 co
boat2(boat2x,boat2y,boat2t);//store boat2 co
boat3(boat3x,boat3y,boat3t);//store boat4 co
boat4(boat4x,boat4y,boat4t);//store boat3 co
//game loop
do{
shoot(a,b);
if (checkBoat1(a,b,boat1x,boat1y,boat1t,se1)) cout<<"\nHit Boat1\n\n"<<"# denotes hit and $ denotes miss\n\n" ;
else {
if (checkBoat2(a,b,boat2x,boat2y,boat2t,se1)) cout <<"\nHit Boat2\n\n"<<"# denotes hit and $ denotes miss\n\n";
else {
if(checkBoat3(a,b,boat3x,boat3y,boat3t,se1)) cout<<"\nHit Boat3\n\n"<<"# denotes hit and $ denotes miss\n\n";
else{
if(checkBoat4(a,b,boat4x,boat4y,boat4t,se1)) cout<<"\nHit Boat4\n\n"<<"# denotes hit and $ denotes miss\n\n";
else{
se1[a][b]='$';cout<<"\nMiss\n\n"<<"# denotes hit and $ denotes miss\n\n";
count=count+1;
};
}
}
}
prgrid(se1);
int result1=checkDestroy1(boat1t);
if (result1) {
cout<<"Boat 1 sinked\t";
}
int result2=checkDestroy2(boat2t);
if (result2) {
cout<<"Boat 2 sinked\t";
}
int result3=checkDestroy3(boat3t);
if (result3) {
cout<<"Boat 3 sinked\t";
}
int result4=checkDestroy4(boat4t);
if (result4) {
cout<<"Boat 4 sinked\t";
}
if (result1&&result2&&result3&&result4){
cout<<"\n\nGameover!!\n\n";
cout<<"Scores are as follows:-\n\n";
if(count<10){
cout<<"Score=50/50";}
else{
if((count>10)&&(count<20)){
cout<<"Score=40/50";}
else{
if((count>20)&&(count<30)){
cout<<"Score=30/50";}
else{cout<<"You lost!!";}
}
}
cout<<"\n\nPress e to quit the screen.\n\n";
c=getch();
if(c=='e') exit(0);
}
cout<<"\nPress e to Exit or any other key to continue\n";
c=getch();
if (c=='e') result=1;
} while(result!=1);
}
//The end