/* AUTHOR: LAUREN DULICK DATE: APRIL 2023 - MAY 2023 TITLE: MINESWEEPER */ // MAIN.CPP #include #include #include #include #include #include "Counter.h" #include "FilePaths.h" #include "Board.h" #include "Random_Num_Gen.h" #include "Mine.h" #include "Flag.h" using namespace std; using sf::VideoMode; using sf::Mouse; using sf::Event; using sf::Vector2f; void setText(sf::Text& text, float x, float y) { sf::FloatRect textRect = text.getLocalBounds(); text.setOrigin(textRect.left + textRect.width / 2.0f, textRect.top + textRect.height / 2.0f); text.setPosition(sf::Vector2f(x, y)); } int main() { ifstream readFile; readFile.open("files/board_config.cfg"); string line; getline(readFile, line, '\n'); int col = stoi(line); getline(readFile, line, '\n'); int row = stoi(line); getline(readFile, line, '\n'); int numMines = stoi(line); Board gameBoard; gameBoard.SetCols(col); gameBoard.SetRows(row); gameBoard.SetMineNum(numMines); // WELCOME WINDOW dimensions int welcomeWidth = 800; int welcomeHeight = 600; // clockog = clock running the whole game auto clockog = chrono::high_resolution_clock::now(); // clockNew = clock running only when game is paused auto clockNew = chrono::high_resolution_clock::now(); auto clock2Dur = chrono::duration_cast(chrono::high_resolution_clock::now() - clockNew).count(); // map for digits -> CREDIT TA MADDIE FOR DEMO (THIS clockNums map IS HEAVILY INFLUENCED BY HER DEMO) map clockNums; sf::Texture digitsText; digitsText.loadFromFile("files/images/digits.png"); sf::Sprite digits; digits.setTexture(digitsText); sf::IntRect zeroRect(0, 0, 21, 32); digits.setTextureRect(zeroRect); sf::Sprite zero = digits; clockNums.emplace(0, zero); sf::IntRect oneRect(21, 0, 21, 32); digits.setTextureRect(oneRect); sf::Sprite one = digits; clockNums.emplace(1, one); sf::IntRect twoRect(42, 0, 21, 32); digits.setTextureRect(twoRect); sf::Sprite two = digits; clockNums.emplace(2, two); sf::IntRect threeRect(63, 0, 21, 32); digits.setTextureRect(threeRect); sf::Sprite three = digits; clockNums.emplace(3, three); sf::IntRect fourRect(84, 0, 21, 32); digits.setTextureRect(fourRect); sf::Sprite four = digits; clockNums.emplace(4, four); sf::IntRect fiveRect(105, 0, 21, 32); digits.setTextureRect(fiveRect); sf::Sprite five = digits; clockNums.emplace(5, five); sf::IntRect sixRect(126, 0, 21, 32); digits.setTextureRect(sixRect); sf::Sprite six = digits; clockNums.emplace(6, six); sf::IntRect sevenRect(147, 0, 21, 32); digits.setTextureRect(sevenRect); sf::Sprite seven = digits; clockNums.emplace(7, seven); sf::IntRect eightRect(168, 0, 21, 32); digits.setTextureRect(eightRect); sf::Sprite eight = digits; clockNums.emplace(8, eight); sf::IntRect nineRect(189, 0, 21, 32); digits.setTextureRect(nineRect); sf::Sprite nine = digits; clockNums.emplace(9, nine); bool tfClock = false; while (true) { sf::RenderWindow welcomeWindow(sf::VideoMode(welcomeWidth, welcomeHeight), "Welcome Window", sf::Style::Close); cout << "welcome window open!" << endl; // "WELCOME TO MINESWEEPER!" sf::Font Font; sf::Text Text1; Font.loadFromFile("files/font.ttf"); Text1.setFont(Font); Text1.setCharacterSize(24); Text1.setStyle(sf::Text::Bold | sf::Text::Underlined); Text1.setFillColor(sf::Color(255, 255, 255)); Text1.setString("WELCOME TO MINESWEEPER!"); setText(Text1, welcomeWidth / 2.0f, welcomeHeight / 2.0f - 150); // "Enter your name:" sf::Text Text2; Text2.setFont(Font); Text2.setCharacterSize(20); Text2.setStyle(sf::Text::Bold); Text2.setFillColor(sf::Color(255, 255, 255)); Text2.setString("Enter your name:"); setText(Text2, welcomeWidth / 2.0f, welcomeHeight / 2.0f - 75); // "|" to track user-inputted name sf::Text Text3; Text3.setFont(Font); Text3.setCharacterSize(18); Text3.setStyle(sf::Text::Bold); Text3.setFillColor(sf::Color::Yellow); Text3.setString("|"); setText(Text3, welcomeWidth / 2.0f, welcomeHeight / 2.0f - 45); string userName; // while WELCOME WINDOW is open while (welcomeWindow.isOpen()) { sf::Event event; while (welcomeWindow.pollEvent(event)) { if (event.type == sf::Event::Closed) { welcomeWindow.close(); return 0; } if (event.type == sf::Event::TextEntered) { if (isalpha(event.text.unicode)) { if (userName.length() < 11) { if (userName.length() == 0) { userName += toupper(event.text.unicode); Text3.setString(userName + "|"); setText(Text3, welcomeWidth / 2.0f, welcomeHeight / 2.0f - 45); cout << "| moved over successfully" << endl; } else if (userName.length() <= 9) { userName += tolower(event.text.unicode); Text3.setString(userName + "|"); setText(Text3, welcomeWidth / 2.0f, welcomeHeight / 2.0f - 45); cout << "| moved over successfully" << endl; } cout << "user name: " << userName << endl; } else { userName.erase(userName.length() - 1, 1); cout << "name was more than 10 chars" << endl; } } } if (event.type == sf::Event::KeyPressed) { if (event.key.code == sf::Keyboard::Backspace) { cout << "key was hit" << endl; userName.erase(userName.length() - 1, 1); Text3.setString(userName + "|"); setText(Text3, welcomeWidth / 2.0f, welcomeHeight / 2.0f - 45); } if (event.key.code == sf::Keyboard::Enter) { cout << "Enter was hit" << endl; if (userName.length() != 0) { welcomeWindow.close(); break; } else { cout << "no name was entered" << endl; } } } } welcomeWindow.clear(sf::Color(0, 0, 255, 255)); welcomeWindow.draw(Text1); welcomeWindow.draw(Text2); welcomeWindow.draw(Text3); welcomeWindow.display(); } sf::RenderWindow window(VideoMode(col * 32, (row * 32) + 100), "Minesweeper", sf::Style::Close); Tile** tiles = new Tile * [row]; for (int r = 0; r < row; r++) tiles[r] = new Tile[col]; Tile** revealedTiles = new Tile * [row]; for (int r = 0; r < row; r++) { revealedTiles[r] = new Tile[col]; for (int c = 0; c < col; c++) { revealedTiles[r][c].SetSprite("tile_revealed"); } } Mine* mines = new Mine[numMines]; char** store = new char* [row]; for (int r = 0; r < row; r++) store[r] = new char[col]; Event event; vector bNumVect; vector flagVect; vector counterVect; vector