forked from R3DHULK/cpp-for-gamers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbattleship.cpp
218 lines (207 loc) · 5.11 KB
/
battleship.cpp
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
#include <iostream>
#include <vector>
#include <ctime>
#include <algorithm>
using namespace std;
const int BOARD_SIZE = 10;
const int NUM_SHIPS = 3;
// Represents a square on the board
struct Square
{
int row, col;
bool occupied = false;
bool hit = false;
};
// Represents a ship on the board
struct Ship
{
int size;
vector<Square> squares;
};
// Represents the game board
struct Board
{
vector<vector<Square>> squares;
vector<Ship> ships;
};
// Initialize the game board
void init_board(Board &board)
{
board.squares.resize(BOARD_SIZE, vector<Square>(BOARD_SIZE));
for (int i = 0; i < BOARD_SIZE; i++)
{
for (int j = 0; j < BOARD_SIZE; j++)
{
board.squares[i][j].row = i;
board.squares[i][j].col = j;
}
}
}
// Print the game board
void print_board(const Board &board)
{
cout << " ";
for (int i = 0; i < BOARD_SIZE; i++)
{
cout << char('A' + i) << " ";
}
cout << endl;
for (int i = 0; i < BOARD_SIZE; i++)
{
cout << " " << i << " ";
for (int j = 0; j < BOARD_SIZE; j++)
{
if (board.squares[i][j].hit)
{
cout << "X ";
}
else if (board.squares[i][j].occupied)
{
cout << "O ";
}
else
{
cout << ". ";
}
}
cout << endl;
}
}
// Place the ships randomly on the board
void place_ships(Board &board)
{
int sizes[NUM_SHIPS] = {5, 4, 3};
for (int i = 0; i < NUM_SHIPS; i++)
{
Ship ship;
ship.size = sizes[i];
bool placed = false;
while (!placed)
{
int row = rand() % BOARD_SIZE;
int col = rand() % BOARD_SIZE;
int dir = rand() % 2;
if (dir == 0 && col + ship.size <= BOARD_SIZE)
{
bool valid = true;
for (int j = col; j < col + ship.size; j++)
{
if (board.squares[row][j].occupied)
{
valid = false;
break;
}
}
if (valid)
{
for (int j = col; j < col + ship.size; j++)
{
board.squares[row][j].occupied = true;
ship.squares.push_back(board.squares[row][j]);
}
placed = true;
}
}
else if (dir == 1 && row + ship.size <= BOARD_SIZE)
{
bool valid = true;
for (int j = row; j < row + ship.size; j++)
{
if (board.squares[j][col].occupied)
{
valid = false;
break;
}
}
if (valid)
{
for (int j = row; j < row + ship.size; j++)
{
board.squares[j][col].occupied = true;
ship.squares.push_back(board.squares[j][col]);
}
placed = true;
}
}
}
board.ships.push_back(ship);
}
}
// Check if all ships are sunk
bool all_ships_sunk(const Board &board)
{
for (const auto &ship : board.ships)
{
for (const auto &square : ship.squares)
{
if (!square.hit)
{
return false;
}
}
}
return true;
}
// Play one turn of the game
void play_turn(Board &board)
{
cout << "Your turn." << endl;
int row, col;
cout << "Enter a row (0-" << BOARD_SIZE - 1 << "): ";
cin >> row;
cout << "Enter a column (A-" << char('A' + BOARD_SIZE - 1) << "): ";
char col_char;
cin >> col_char;
col = col_char - 'A';
if (row < 0 || row >= BOARD_SIZE || col < 0 || col >= BOARD_SIZE)
{
cout << "Invalid square." << endl;
return;
}
Square &square = board.squares[row][col];
if (square.hit)
{
cout << "Square already hit." << endl;
return;
}
square.hit = true;
if (square.occupied)
{
cout << "Hit!" << endl;
for (auto &ship : board.ships)
{
for (auto &s : ship.squares)
{
if (s.row == row && s.col == col)
{
s.hit = true;
if (all_of(ship.squares.begin(), ship.squares.end(), [](const Square &s)
{ return s.hit; }))
{
cout << "You sunk a ship!" << endl;
}
break;
}
}
}
}
else
{
cout << "Miss." << endl;
}
}
// Main function
int main()
{
srand(time(nullptr));
Board board;
init_board(board);
place_ships(board);
while (!all_ships_sunk(board))
{
print_board(board);
play_turn(board);
}
cout << "Congratulations, you won!" << endl;
return 0;
}