-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBattleshipProject.cpp
More file actions
210 lines (181 loc) · 4.06 KB
/
BattleshipProject.cpp
File metadata and controls
210 lines (181 loc) · 4.06 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
#include <iostream>
#include <ctime>
using namespace std;
//settings for the game
const int rows = 3;
const int column = 3;
int maxships = 3;
//board inputs
int board [rows][column];
int board2 [rows][column];
//Shows the results of the board once the game has concluded
void Show()
{
cout << "--------" << endl << "Enemy board" << endl << "--------" << endl;
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < column; j ++)
{
cout << board[i][j] << " ";
}
cout << endl;
}
cout << "--------" << endl << "Your Board" << endl << "--------" << endl;
for(int a = 0; a < rows; a++)
{
for(int b = 0; b < column; b++)
{
cout << board2[a][b] << " ";
}
cout << endl;
}
}
//the number of ships duh
int NumberOfShips()
{
int c = 0;
int c2 = 0;
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < column; j++)
{
if(board[i][j] == 1)
c++;
else if(board2[i][j] == 1)
c2++;
}
}
return c;
}
//Randomly setting ship location for convinience
void SetShips()
{
int s = 0;
int s2 = 0;
while(s < maxships)
{
int x = rand() % rows;
int y = rand() % column;
if (board[x][y] != 1)
{
s++;
board[x][y] = 1;
}
}
while (s2 < maxships)
{
int x = rand() % rows;
int y = rand() % column;
if(board2[x][y] != 1)
{
s2++;
board2[x][y] = 1;
}
}
}
//Checking player fire
bool Fire(int x, int y)
{
if(board[x][y] == 1)
{
board[x][y] = 2;
return true;
}
return false;
}
bool Miss(int x, int y)
{
if(board[x][y] == 0)
{
return true;
}
return false;
}
//Checking AI fire
bool Fire2(int x, int y)
{
if(board2[x][y] == 1)
{
board2[x][y] = 2;
return true;
}
return false;
}
bool Miss2(int x, int y)
{
if(board2[x][y] == 0)
{
return true;
}
return false;
}
// Checking the board to see if either of the sides lost, also keeps while(!(Lost)) going
bool Lost()
{
int ShipsLeft = 0;
int ShipsLeft2 = 0;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < column; j++)
{
if(board[i][j] == 2)
{
ShipsLeft++;
}
else if (board2[i][j] == 2)
{
ShipsLeft2++;
}
else if(ShipsLeft == 3 || ShipsLeft2 == 3)
{
return true;
}
}
}
return false;
}
int main()
{
srand(time(NULL));
SetShips();
char prompt;
int pos1, pos2,/*pos3 and 4 are the bot's inputs*/ pos3, pos4;
while(!(Lost()))
{
// if players dont enter a number between 0 and 2 here, they instantly lose
cout << "Please input numbers between 0 and 2 one at a time captain! (x,y): " << endl;
cin >> pos1; cin >> pos2;
if(!((pos1 >= 0) && (pos1 <= 2)))
{
if(!((pos1 >= 0) && (pos1 <= 2)))
{
cout << "That is not a valid coordinate! YOU ARE NOT A TRUST WORTH CAPTAIN!" << endl;
break;
}
}
cout << "(" << pos1 << "," << pos2 << ")" << " ";
if(Fire(pos1, pos2) == true)
{
cout << "Enemy hit!" << endl;
}
if (Miss(pos1, pos2) == true)
cout << "is a miss captain!" << endl;
cout << "Surrender? (y/n): "; cin >> prompt;
//Bot's turn
pos3 = rand() % 2;
pos4 = rand() % 2;
if(Fire2(pos3, pos4) == true)
cout << "Captain! The enemy fired and hit!" << endl;
if(Miss2(pos3, pos4) == true)
cout << "The enemy fired and missed! But surrender was never an option for him..." << endl;
if(Lost() == true)
{
break;
}
if(prompt == 'y')
break;
}
cout << "Game Over!" << endl;
Show();
return 0;
}