-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
307 lines (268 loc) · 8.07 KB
/
main.cpp
File metadata and controls
307 lines (268 loc) · 8.07 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
/*-----------------------------------------------------
- Main Minesweeper Game Code
-
- Initial Date: 5/2/2023
-----------------------------------------------------*/
//Included Headers
#include <ncurses.h>
#include <iostream>
#include <vector>
#include <ctime>
#include <thread>
#include <chrono>
#include <atomic>
#include "board.h"
//Global Variables
std::atomic<bool> end_thread (false);
int game_time;
//Game counting function for the
void count(){
game_time = 0;
while(game_time != 9999){
if(end_thread){
break;
}
game_time++;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
}
int main(int argv, char* argc[]){
//Variable Declaration
int scr_y, scr_x, input, print_x = 2, print_y = 2, tiles_left, colour, board_x, board_y, mines, finish_code = 0;
WINDOW* game_end;
//Briefly initialising a window to get the terminal's current size
initscr();
getmaxyx(stdscr, scr_y, scr_x);
endwin();
//Prompting the user for the board width
std::cout << "Please input a width for the board (12-" << (scr_x / 2) - 3 << "): ";
std::cin >> board_x;
//Testing if the input is valid
if(board_x < 12 || board_x > (scr_x / 2) - 3){
std::cout << "Error: Invalid width given" << std::endl;
exit(1);
}
//Prompting the user for the board height
std::cout << "Please input a height for the board (3-" << (scr_y / 2) - 3 << "): ";
std::cin >> board_y;
//Testing if the input is valid
if(board_y < 3 || board_y > (scr_y / 2) - 3){
std::cout << "Error: Invalid height given" << std::endl;
exit(1);
}
//Prompting the user for the number of mines
std::cout << "Please input the number of mines to play with (1-" << (board_x * board_y) - 1 << "): ";
std::cin >> mines;
//Testing if the input is valid
if(mines < 1 || mines > (board_x * board_y) - 1){
std::cout << "Error: Invalid number of mines given" << std::endl;
exit(1);
}
//Declare the board
std::vector<std::vector<tile*>> board(board_y, std::vector<tile*> (board_x));
//Seeding the random numbers
srand(time(NULL));
//Generate the Board
generate_board(board, board_y, board_x, mines);
//Enable mouse movement tracking
printf("\033[?1003h\n");
//Initialise ncurses space
initscr();
raw();
noecho();
curs_set(0);
keypad(stdscr, TRUE);
cbreak();
mouseinterval(0);
//Starting terminal colours
if(start_color() == ERR || !has_colors()){
endwin();
printf("Error: Colour could not be initialised!\n");
exit(1);
}
//Initialising ncurses colours
init_pair(COLOR_RED, COLOR_RED, COLOR_BLACK);
init_pair(COLOR_GREEN, COLOR_GREEN, COLOR_BLACK);
init_pair(COLOR_YELLOW, COLOR_YELLOW, COLOR_BLACK);
init_pair(COLOR_BLUE, COLOR_BLUE, COLOR_BLACK);
init_pair(COLOR_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
init_pair(COLOR_CYAN, COLOR_CYAN, COLOR_BLACK);
init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
//Setting correct terminal size
resizeterm(board_y * 2 + 3, board_x * 2 + 3);
//Setting up the screen's border
border('|', '|', '-', '-', '+', '+', '+', '+');
//Printing out the board
for(std::vector<tile*> vect1D : board){
for(tile* x : vect1D){
x->get_symbol(); //This needs to be here or x will count as unused
//Print the unrevealed symbol
mvaddch(print_y, print_x, '-');
print_x += 2;
}
//Move the cursor to the start of the next line
print_y += 2;
print_x = 2;
move(print_y, print_x);
}
//Get all of the mouse event
mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, NULL);
//Starting the timer thread
std::thread thread_obj(count);
//Calculate the number of tiles the player must reveal
tiles_left = (board_x * board_y) - mines;
//Main Game Loop
do{
mvprintw(1, 2, " ");
mvprintw(1, 2, "Mines Remaining: %d", mines);
//Resetting the cursor position variables
print_x = 2;
print_y = 2;
//Printing out the board
for(std::vector<tile*> vect1D : board){
for(tile* x : vect1D){
x->get_symbol(); //This needs to be here or x will count as unused
//Print a flag if flagged
if(board[print_y / 2 - 1][print_x / 2 - 1]->get_flagged()){
attron(COLOR_PAIR(COLOR_RED));
mvaddch(print_y, print_x, 'F');
attroff(COLOR_PAIR(COLOR_RED));
}
//Otherwise, print it if it is revealed
else if(board[print_y / 2 - 1][print_x / 2 - 1]->get_revealed()){
//Testing what symbol is being revealed to give it the correct colour
switch(board[print_y / 2 - 1][print_x / 2 - 1]->get_symbol()){
case '0':
colour = COLOR_WHITE;
break;
case '1':
colour = COLOR_CYAN;
break;
case '2':
colour = COLOR_MAGENTA;
break;
case '3':
colour = COLOR_GREEN;
break;
case '4':
colour = COLOR_YELLOW;
break;
case '5':
colour = COLOR_CYAN;
break;
case '6':
case '7':
case '8':
colour = COLOR_BLUE;
break;
}
//Set the colour to print in
attron(COLOR_PAIR(colour));
//Print out the tile
mvprintw(print_y, print_x, "%c", board[print_y / 2 - 1][print_x / 2 - 1]->get_symbol());
//Remove the colour
attroff(COLOR_PAIR(colour));
}
//Otherwise, print the unrevealed tile
else{
mvprintw(print_y, print_x, "%c", '-');
}
print_x += 2;
}
//Move the cursor to the start of the next line
print_y += 2;
print_x = 2;
move(print_y, print_x);
}
//Displaying the board
refresh();
//The game is won if there are no non-mine tiles left to reveal
if(!tiles_left){
finish_code = 1;
break;
}
//Get user input
input = wgetch(stdscr);
//Testing user input
if (input == KEY_MOUSE) {
MEVENT event;
if (getmouse(&event) == OK) {
int y_coord = event.y / 2 - 1;
int x_coord = event.x / 2 - 1;
if(x_coord < 0 || y_coord < 0 || x_coord >= board_x || y_coord >= board_y){
continue;
}
//Check if it was left click
if(event.bstate & BUTTON1_PRESSED && !board[y_coord][x_coord]->get_revealed()){
//Reveal the tile
reveal_tile(board, y_coord, x_coord, &tiles_left);
//Lose the game if the revealed tile was a mine
if(board[y_coord][x_coord]->get_symbol() == 'X'){
break;
}
}
//Check if it was right click
else if(event.bstate & BUTTON3_PRESSED){
//Testing if the tile has already been revealed
if(!board[y_coord][x_coord]->get_revealed()){
//If not, flag it
board[y_coord][x_coord]->flag();
//Decreases mine count in the tile was just flagged
if(board[y_coord][x_coord]->get_flagged()){
mines--;
}
//Otherwise, the tile was just unflagged, so mine count increases
else{
mines++;
}
}
}
}
}
}while(input != 'q');
//End the thread
end_thread = true;
thread_obj.join();
//Initialising the game ending screen
if(!(game_end = newwin(6, 24, 2, 2))){
//Exits program if the window failed initialisation
noraw();
endwin();
printf("Critical Error: Window could not be allocated!\n");
exit(1);
}
//Print the game over menu's border
wborder(game_end, '|', '|', '-', '-', '+', '+', '+', '+');
//Testing if the game was lost
if(!finish_code){
mvwprintw(game_end, 1, 2, "You Lose!");
}
//Otherwise, the game is won
else{
mvwprintw(game_end, 1, 2, "You Win!");
mvwprintw(game_end, 2, 2, "Your Score was: %d", game_time);
}
//Print the quit instructions
mvwprintw(game_end, 4, 2, "Press \'q\' to Quit");
//Refresh the game over window
wrefresh(game_end);
//Wait for the user to exit the program
do{
//Getting user input
input = wgetch(game_end);
}while(input != 'q');
//Deleting all of the tile objects from memory
for(std::vector<tile*> vect1D : board){
for(tile* x : vect1D){
delete(x);
}
std::cout << std::endl;
}
//Ending the ncurses window
delwin(game_end);
noraw();
endwin();
//Diable mouse movement tracking
printf("\033[?1003l\n");
return 0;
}