-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
373 lines (345 loc) · 15.8 KB
/
main.cpp
File metadata and controls
373 lines (345 loc) · 15.8 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#include <iostream>
#include <fstream>
#include <string>
#include <SFML/Graphics.hpp>
#include "Board.h"
#include "Tiles.h"
#include "TextureManager.h"
using namespace std;
string capitalizeAlong(string& name) {
string capitalized;
bool first = true;
for(int i = 0; i < name.size(); i++) {
if (isalpha(name[i])) {
if (first) {
capitalized += toupper(name[i]);
first = false;
} else {
capitalized += tolower(name[i]);
}
} else {
capitalized += name[i];
}
}
return capitalized;
}
sf::Text& createWelcomeWindow(float windowWidth, float windowHeight, sf::Font& font, sf::Text& welcomeText, sf::Text& promptText, sf::Text& nameText) {
welcomeText.setString("WELCOME TO MINESWEEPER!");
welcomeText.setFont(font);
welcomeText.setCharacterSize(24);
welcomeText.setFillColor(sf::Color::White);
sf::FloatRect textRect = welcomeText.getLocalBounds();
welcomeText.setOrigin(textRect.left + textRect.width / 2.0f,textRect.top + textRect.height / 2.0f);
welcomeText.setPosition(windowWidth/2.0f, (windowHeight/2.0f) - 150.0f);
welcomeText.setStyle(sf::Text::Bold | sf::Text::Underlined);
promptText.setString("Enter your name:");
promptText.setFont(font);
promptText.setCharacterSize(20);
promptText.setFillColor(sf::Color::White);
sf::FloatRect textRect2 = promptText.getLocalBounds();
promptText.setOrigin(textRect2.left + textRect2.width / 2.0f,textRect2.top + textRect2.height / 2.0f);
promptText.setPosition(windowWidth/2.0f, (windowHeight/2.0f) - 75.0f);
promptText.setStyle(sf::Text::Bold);
nameText.setString("");
nameText.setFont(font);
nameText.setCharacterSize(18);
nameText.setFillColor(sf::Color::White);
sf::FloatRect textRect3 = nameText.getLocalBounds();
nameText.setOrigin(textRect3.left + textRect3.width / 2.0f,textRect3.top + textRect3.height / 2.0f);
nameText.setPosition(windowWidth/2.0f, (windowHeight/2.0f) - 45.0f);
nameText.setStyle(sf::Text::Bold);
return welcomeText, promptText, nameText;
}
string displayPlayerStandingsLeaderboard(map <string,string> &playerRecords) {
string players;
int count = 1;
for (auto it = playerRecords.begin(); it != playerRecords.end(); it++) {
players.append(to_string(count) + ".\t" + it->first + "\t" + it->second + "\n\n");
count++;
}
return players;
}
int main() {
//reading config.cfg
vector <int> configNum;
//, ../minesweeper/files/config.cfg
ifstream config("../files/config.cfg ");
string line;
while (getline(config, line)) {
configNum.push_back(stoi(line));
}
int col = configNum[0];
int row = configNum[1];
int totalMines = configNum[2];
float windowWidth = float(col) * 32.0f;
float windowHeight = (float(row)*32.0f) +100.0f;
sf::RenderWindow window(sf::VideoMode(col * 32, (row*32)+100), "Minesweeper");
//Loading font
sf::Font arialFont;
if (!arialFont.loadFromFile("../files/font.ttf")) {
cout << "failed to load font" << endl;
return -1;
}
//Create text objects for welcome window
sf::Text welcomeText;
sf::Text promptText;
sf::Text nameText;
createWelcomeWindow(windowWidth, windowHeight, arialFont, welcomeText, promptText, nameText);
bool inWelcomeScreen = true;
sf::FloatRect textRect4 = nameText.getLocalBounds();
nameText.setOrigin(textRect4.left + textRect4.width / 2.0f,textRect4.top + textRect4.height / 2.0f);
nameText.setPosition(windowWidth/2.0f, (windowHeight/2.0f) - 45.0f);
string playerName;
string officialName;
while(window.isOpen()) {
sf::Event event;
while(window.pollEvent(event)) {
if(event.type == sf::Event::Closed) {
window.close();
}
if (inWelcomeScreen) {
if (event.type == sf::Event::TextEntered) {
char enteredChar = static_cast<char>(event.text.unicode);
//Handle Backspace
if (event.text.unicode < 128) { //Keyboard check for characters
if (playerName.size() < 10) {
if (isalpha(enteredChar)) { //Makes sure we can only enter alphabets
playerName += enteredChar;
string formattedName = capitalizeAlong(playerName) + "|";
sf::FloatRect textRect4 = nameText.getLocalBounds();
nameText.setOrigin(textRect4.left + textRect4.width / 2.0f,textRect4.top + textRect4.height / 2.0f);
nameText.setPosition(windowWidth/2.0f, (windowHeight/2.0f) - 45.0f);
officialName = playerName;
nameText.setString(formattedName);
}
}
}
} else if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Backspace) {
if (!playerName.empty()) {
playerName.erase(playerName.length() - 1, 1);
string formattedName = capitalizeAlong(playerName) + "|";
sf::FloatRect textRect4 = nameText.getLocalBounds();
nameText.setOrigin(textRect4.left + textRect4.width / 2.0f,textRect4.top + textRect4.height / 2.0f);
nameText.setPosition(windowWidth/2.0f, (windowHeight/2.0f) - 45.0f);
officialName = playerName;
nameText.setString(formattedName);
}
}
if (event.key.code == sf::Keyboard::Enter) {
if (!playerName.empty()) {
//move to next window
inWelcomeScreen = false;
window.close();
}
}
}
}
}
window.clear(sf::Color::Blue);
window.draw(welcomeText);
window.draw(promptText);
window.draw(nameText);
window.display();
}
//Board Game Loop
sf::RenderWindow boardWindow(sf::VideoMode(col * 32, (row*32)+100), "Board");
sf::RenderWindow leaderboard;
float leaderWindowWidth = float(col) * 16;
float leaderWindowHeight = (float(row) * 16) + 50;
textureManager texturesLoad;
texturesLoad.loadAllTextures();
auto it = texturesLoad.textures.find("tile_revealed");
sf::Sprite revealedTileSprite;
revealedTileSprite.setTexture(it->second);
sf::Clock gameClock;
sf::Time accumulatedTime;
Board board(row, col,totalMines, boardWindow, texturesLoad, gameClock);
boardWindow.clear(sf::Color::White);
board.setMines(totalMines);
board.loadNumbers();
auto restartRect = board.getRestartButtonBounds();
auto debugRect = board.getDebugButtonBounds();
auto leaderRect = board.getLeaderBoardButtonBounds();
auto timerRect = board.getPausePlayButtonBounds();
//Declaring variables for Leaderboard file
map <string, string> playerRecords;
ifstream leaderBoardFile("../files/leaderboard.txt");
string line1;
while (getline(leaderBoardFile, line1)) {
string playerTime = line1.substr(0, line1.find(','));
string playerTextName = line1.substr(line1.find(',') + 1);
// int min = stoi(playerTime.substr(0,2));
// int sec = stoi(playerTime.substr(3,2));
playerRecords.emplace(playerTime, playerTextName);
}
leaderBoardFile.close();
bool gameRestart = false;
bool gameWon = false;
bool gameLost = false;
bool gamePaused = false;
bool showLeaderBoard = false;
int winTime;
int minutes;
int seconds;
while(boardWindow.isOpen()) {
sf::Event event;
while(boardWindow.pollEvent(event)) {
if(event.type == sf::Event::Closed) {
boardWindow.close();
}
if (event.type == sf::Event::MouseButtonPressed) {
float x = float(event.mouseButton.x);
float y = float(event.mouseButton.y);
int tileX = static_cast<int>(x/32);
int tileY = static_cast<int>(y/32);
if (showLeaderBoard) {
showLeaderBoard = false;
if (!leaderboard.isOpen()) {
leaderboard.create(sf::VideoMode(static_cast<int>(leaderWindowWidth), static_cast<int>(leaderWindowHeight)),"Leaderboard", sf::Style::Close | sf::Style::Titlebar);
}
sf::Text mainText("LEADERBOARD", arialFont, 20);
mainText.setFillColor(sf::Color::White);
sf::FloatRect maintextRect = mainText.getLocalBounds();
mainText.setOrigin(maintextRect.left + maintextRect.width / 2.0f,maintextRect.top + maintextRect.height / 2.0f);
mainText.setPosition(leaderWindowWidth/2, leaderWindowHeight/2 - 120);
mainText.setStyle(sf::Text::Bold | sf::Text::Underlined);
sf::Text playerText(displayPlayerStandingsLeaderboard(playerRecords), arialFont, 18);
playerText.setFillColor(sf::Color::White);
sf::FloatRect playerTextRect = playerText.getLocalBounds();
playerText.setOrigin(playerTextRect.left + playerTextRect.width / 2.0f,playerTextRect.top + playerTextRect.height / 2.0f);
playerText.setPosition(leaderWindowWidth/2, leaderWindowHeight/2 + 20);
playerText.setStyle(sf::Text::Bold);
while (leaderboard.isOpen()) {
sf::Event leaderEvent;
while (leaderboard.pollEvent(leaderEvent)) {
if (leaderEvent.type == sf::Event::Closed) {
board.leaderBoardShown = false;
leaderboard.close();
//showLeaderBoard = false;
}
}
leaderboard.clear(sf::Color::Blue);
leaderboard.draw(mainText);
leaderboard.draw(playerText);
leaderboard.display();
}
}
// Check which button was pressed.
if (event.mouseButton.button == sf::Mouse::Right) {
if (!gameLost && !gamePaused && !gameWon) {
board.setFlags(tileX, tileY);
}
continue;
} else if (event.mouseButton.button == sf::Mouse::Left) {
if (board.gameLost == true) {
if (restartRect.contains(x,y)) {
board.restartGame();
board.gameLost = false;
gamePaused = false;
} else if (leaderRect.contains(x,y)) {
board.leaderBoardShown = true;
showLeaderBoard = true;
}
continue;
}
if (board.gameWon == true) {
if (restartRect.contains(x,y)) {
board.restartGame();
} else if (leaderRect.contains(x,y)) {
board.leaderBoardShown = true;
showLeaderBoard = true;
}
continue;
}
if (timerRect.contains(x,y)) {
cout << "Pressing pause Button" << endl;
if (!board.isTimePaused) {
board.pauseTime();
gamePaused = true;
} else {
board.resumeTime();
gamePaused = false;
}
continue;
}
if (gamePaused) {
if (restartRect.contains(x,y)) {
board.restartGame();
board.resumeTime();
gamePaused = false;
} else if (leaderRect.contains(x,y)) {
cout << "Pressing leaderRect" << endl;
board.leaderBoardShown = true;
showLeaderBoard = true;
}
continue;
}
if (debugRect.contains(x,y)) {
board.toggleDebug();
} else if (leaderRect.contains(x,y)) {
cout << "Pressing leaderRect" << endl;
board.leaderBoardShown = true;
showLeaderBoard = true;
} else if (restartRect.contains(x,y)) {
board.restartGame();
}
if (tileX < 0 || tileX >= col || tileY < 0 || tileY >= row) {
continue;
}
if (board.tiles[tileX][tileY].isMine) {
board.lostGame();
board.pauseTime();
gameLost = true;
continue;
}
if (board.tiles[tileX][tileY].neighboringTiles == 0) {
board.recursiveEmptyTilesReveal(tileX, tileY);
} else {
board.tiles[tileX][tileY].isRevealed = true;
}
if (board.wonAGame()) {
board.wonGame();
board.pauseTime();
winTime = board.displayTime;
minutes = winTime/60;
seconds = winTime%60;
int minuteLeft = minutes/10%10;
int minuteRight = minutes%10;
int secondLeft = seconds/10%10;
int secondRight = seconds%10;
string finalMinute = to_string(minuteLeft) + to_string(minuteRight);
string finalSecond = to_string(secondLeft) + to_string(secondRight);
if (finalSecond.length()<2) {
finalSecond = "0" + finalSecond;
}
if (finalMinute.length()<2) {
finalMinute = "0" + finalMinute;
}
string finalTime = finalMinute + ":" + finalSecond;
playerRecords.emplace(finalTime, officialName);
while (playerRecords.size() > 5) {
playerRecords.erase(--playerRecords.end());
}
ofstream leaderFile("../files/leaderboard.txt", ios_base::out);
if (!leaderFile) {
cout << "CANT OPEN" << endl;
}
for (auto it = playerRecords.begin(); it != playerRecords.end(); it++) {
leaderFile << it->first << "," << it->second << endl;
}
leaderFile.close();
}
}
}
}
boardWindow.clear(sf::Color::White);
board.drawBoard();
boardWindow.display();
}
return 0;
}
// TIP See CLion help at <a
// href="https://www.jetbrains.com/help/clion/">jetbrains.com/help/clion/</a>.
// Also, you can try interactive lessons for CLion by selecting
// 'Help | Learn IDE Features' from the main menu.