-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard.cpp
More file actions
291 lines (259 loc) · 8.57 KB
/
board.cpp
File metadata and controls
291 lines (259 loc) · 8.57 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
#include "board.h"
#include <iostream>
#include <sstream>
using namespace std;
// board constructor, initialize all necessary variables
Board::Board() {
vector<int> size_of_ships = {3, 4, 4, 5, 6, 10};
for (int i=0; i< size_of_board; i++){
for (int j=0; j< size_of_board; j++){
this->state[i][j] = 1; // setting the state of the board to be blank first
}
}
// ships is 3D, elements accessed through ships[i][j][k]
// filling up the ships vector with the correct ship sizes, setting coords to -1 as placeholder
for (int i = 0; i < no_of_ships; i++) {
vector<vector<int>> temp1;
for (int j = 0; j < size_of_ships[i]; j++) {
vector<int> temp2;
temp2.push_back(-1);
temp2.push_back(-1);
temp1.push_back(temp2);
}
ships.push_back(temp1);
}
}
// bool method to return whether the entire ships vector is empty
bool Board::ship_empty() {
for (auto& i : ships) {
if (! i.empty()) {
return false;
}
}
return true;
}
// a getter method to return the ship vector for other classes to use
const vector<vector<vector<int>>> Board::get_ships() const {
return ships;
}
// return the number of empty ships in ships vector
int Board::ship_left() {
int count = 0;
for (auto i : ships) {
if (i.empty()) {
count++;
}
}
return (no_of_ships - count);
}
// draw method to draw the board on screen, input takes a window created using ncurses
void Board::draw(WINDOW* win) {
// make a box around the window
refresh();
box(win, 0, 0);
wrefresh(win);
int game_row = 2;
// drawing the board
mvwprintw(win, 1, 1, " +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+");
for (int i = 0; i < size_of_board; i++) {
int column = 1;
// handling the row name
char row_name = 'A' + i;
mvwprintw(win, game_row, 1, " %c |", row_name);
column += 4;
for (int j = 0; j < size_of_board; j++) {
// drawing different symbols according to different state
switch (state[j][i]) {
case -1:
mvwprintw(win, game_row, column, " ? |");
column += 4;
break;
case 1:
mvwprintw(win, game_row, column, " |");
column += 4;
break;
case 2:
mvwprintw(win, game_row, column, "███|");
column += 4;
break;
case 3:
mvwprintw(win, game_row, column, " + |");
column += 4;
break;
case 4:
mvwprintw(win, game_row, column, " X |");
column += 4;
break;
case 5:
mvwprintw(win, game_row, column, "█X█|");
column += 4;
break;
}
}
mvwprintw(win, game_row + 1, 1, " +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+");
game_row += 2;
}
// update memory with the changes so the board can be drawn
wrefresh(win);
refresh();
}
//display a message to ask whether player confirms quit or not.
//returns true if yes false if no
bool Board::confirmQuit() {
clear(); // Clear the screen
// Get the dimensions of the terminal window
int maxRows, maxCols;
getmaxyx(stdscr, maxRows, maxCols);
// Center the message on the screen
const char* message = "Do you want to quit?";
int messageLength = strlen(message);
int row = maxRows / 2;
int col = (maxCols - messageLength) / 2;
// Display the message
mvprintw(row, col, message);
// Display the options
const char* optionYes = "[ Yes ]";
const char* optionNo = "[ No ]";
int optionWidth = strlen(optionYes);
int optionRow = row + 2;
int optionCol = (maxCols - optionWidth * 2) / 2;
attron(A_REVERSE); // Highlight the first option
mvprintw(optionRow, optionCol, optionYes);
attroff(A_REVERSE);
mvprintw(optionRow, optionCol + optionWidth, optionNo);
refresh(); // Refresh the screen
int currentOption = 0;
int choice;
while ((choice = getch())) {
switch (choice) {
case KEY_LEFT:
if (currentOption == 1) {
// Highlight the first option
mvprintw(optionRow, optionCol, optionYes);
attroff(A_REVERSE);
mvprintw(optionRow, optionCol + optionWidth, optionNo);
currentOption = 0;
}
break;
case KEY_RIGHT:
if (currentOption == 0) {
// Highlight the second option
mvprintw(optionRow, optionCol, optionYes);
mvprintw(optionRow, optionCol + optionWidth, optionNo);
attron(A_REVERSE);
mvprintw(optionRow, optionCol + optionWidth, optionNo);
currentOption = 1;
}
break;
case '\n':
// User confirmed selection, clean up and restore the terminal
endwin();
if (currentOption == 0) {
// User selected "Yes"
return true;
} else {
// User selected "No"
clear();
return false;
}
}
}
}
//run when user exit
void Board::store_state(string fname) {
ofstream outputFile(fname.c_str());
if (outputFile.is_open()) {
for (int i = 0; i < size_of_board; i++) {
for (int j = 0; j < size_of_board; j++) {
outputFile << state[i][j] << " ";
}
outputFile << endl;
}
for (const auto& outerVec : ships) {
for (const auto& innerVec : outerVec) {
for (const int& element : innerVec) {
outputFile << element << " ";
}
}
outputFile << endl;
}
outputFile.close();
cout << "Game state saved successfully." << endl;
} else {
cerr << "Failed to open the output file." << endl;
}
}
//run when user continue
void Board::load_state(string fname) {
ifstream inputFile(fname.c_str());
if (inputFile.is_open()) {
for (int i = 0; i < size_of_board; i++) {
for (int j = 0; j < size_of_board; j++) {
inputFile >> state[i][j];
}
}
ships.clear();
string s;
getline(inputFile,s);
while(getline(inputFile,s)){
istringstream iss(s);
vector<vector<int>> coords_arr;
if (s != ""){
int i,j;
while (iss >> i >> j){
vector<int> coords;
coords.push_back(i);
coords.push_back(j);
coords_arr.push_back(coords);
}
}
ships.push_back(coords_arr);
}
inputFile.close();
cout << "Game state loaded successfully." << endl;
// ... code to resume the game ...
} else {
cerr << "Failed to open the input file." << endl;
}
}
//get accuracy
double Board::accuracy(){
double missed_attack_num = 0.0, hitted_attack_num = 0.0;
for (int i = 0; i < size_of_board; i++) {
for (int j = 0; j < size_of_board; j++) {
if (state[i][j] == 4)
missed_attack_num++;
if (state[i][j] == 5)
hitted_attack_num++;
}
}
return hitted_attack_num/(missed_attack_num+hitted_attack_num);
}
/* moved to manager
//run when user win
void Board::store_accuracy() {
ofstream outputFile("game_state.txt");
if (outputFile.is_open()) {
for (int i = 0; i < size_of_board; i++) {
for (int j = 0; j < size_of_board; j++) {
outputFile << state[i][j] << " ";
}
outputFile << endl;
}
outputFile.close();
cout << "Game state saved successfully." << endl;
} else {
cerr << "Failed to open the output file." << endl;
}
}
*/
// calculate score
double Board::score(){
int count = 32;
for (const auto& outerVec : ships) {
for (const auto& innerVec : outerVec) {
count--;
}
}
return count;
}