// // Created by Priyal Patel on 11/21/21. // #include "Board.h" Board::Board() { face_win.setTexture(TextureManager::GetTexture("face_win")); face_lose.setTexture(TextureManager::GetTexture("face_lose")); face_happy.setTexture(TextureManager::GetTexture("face_happy")); debug.setTexture(TextureManager::GetTexture("debug")); test_1.setTexture(TextureManager::GetTexture("test_1")); test_2.setTexture(TextureManager::GetTexture("test_2")); test_3.setTexture(TextureManager::GetTexture("test_3")); tile_hidden.setTexture(TextureManager::GetTexture("tile_hidden")); digits.setTexture(TextureManager::GetTexture("digits")); gameLost = false; gameWon = false; gameInProgress = true; } void Board::SetPositionTest1Button(sf::RenderWindow &window) { int x = window.getSize().x/2 - face_happy.getTextureRect().height/2; int y = window.getSize().y - 88; face_happy.setPosition(x,y); face_lose.setPosition(x,y); face_win.setPosition(x,y); debug.setPosition(sf::Vector2f (face_happy.getPosition().x + 4*tile_hidden.getTextureRect().width ,y)); test_1.setPosition(sf::Vector2f (debug.getPosition().x +2*tile_hidden.getTextureRect().width ,y)); test_2.setPosition(sf::Vector2f (test_1.getPosition().x + 2*tile_hidden.getTextureRect().width ,y)); test_3.setPosition(sf::Vector2f (test_2.getPosition().x + 2*tile_hidden.getTextureRect().width ,y)); } void Board::MinesRemaining(sf::RenderWindow &window, vector &tiles, Board &board){ int x = 0 + tile_hidden.getTextureRect().width/2; int y = window.getSize().y- 88; string mineCount = to_string(board.mineCount); string str_digits = "0123456789-"; string negative; if(board.gameWon){ mineCount = to_string(0); } if(stoi(mineCount) < 0){ negative = mineCount.substr(1,mineCount.length()); while(negative.length() < 3){ negative = "0" + negative; } mineCount = "-" + negative; }else{ while(mineCount.length() < 3){ mineCount = "0" + mineCount; } } for(int i = 0; i<=10; i++){ if(mineCount.at(0) == str_digits.at(i)){ digits.setTextureRect(sf::IntRect(21*i,0,21,32)); digits.setPosition(x,y); window.draw(digits); } if(mineCount.at(1) == str_digits.at(i)){ digits.setTextureRect(sf::IntRect(21*i,0,21,32)); digits.setPosition(x*2.1,y); window.draw(digits); } if(mineCount.at(2) == str_digits.at(i)){ digits.setTextureRect(sf::IntRect(21*i,0,21,32)); digits.setPosition(x*3.3,y); window.draw(digits); } if(mineCount.length() == 4){ if(mineCount.at(3) == str_digits.at(i)){ digits.setTextureRect(sf::IntRect(21*i,0,21,32)); digits.setPosition(x*4.4,y); window.draw(digits); } } } } void Board::DigitSubtract(vector &tiles, Board& board, int i, int j, int height){ Tiles tile; if(tile.getTile(i, j, height, tiles)->getFlag()){ board.mineCount--; }else{ board.mineCount++; } } int Board::getMineCount() { return mineCount; } void Board::Draw(sf::RenderWindow& window){ if(gameLost){ window.draw(face_lose); } if(gameWon){ window.draw(face_win); } if(gameInProgress){ window.draw(face_happy); } window.draw(debug); window.draw(test_1); window.draw(test_2); window.draw(test_3); } sf::FloatRect Board::GetBoundstest_1(){ return test_1.getGlobalBounds(); } sf::FloatRect Board::GetBoundstest_2(){ return test_2.getGlobalBounds(); } sf::FloatRect Board::GetBoundstest_3(){ return test_3.getGlobalBounds(); } sf::FloatRect Board::GetBoundsdebug(){ return debug.getGlobalBounds(); } sf::FloatRect Board::GetBoundsSmiley() { return face_happy.getGlobalBounds(); } void Board::setTests(string filename, sf::RenderWindow &window, vector&tiles) { mineCount = 0; gameWon = false; gameLost = false; gameInProgress = true; int w = tile_hidden.getTextureRect().width; int h = tile_hidden.getTextureRect().height; Tiles tile; int width; int height = 0; vector data; ifstream inFile(filename); string fileLine; if(inFile.is_open()){ istringstream stream(fileLine); while(inFile>>fileLine){ data.push_back(fileLine); width = fileLine.length(); height++; } } for(unsigned int i = 0; i < width; i++){ for(unsigned int j = 0; j < height; j++){ if(data[j].at(i) == '0'){ tile.SetPosition((float)(w*i),(float)(h*j)); tile.putMine(false); tile.FlipTile(true); tiles.push_back(tile); }else{ tile.SetPosition((float)(w*i),(float)(h*j)); tile.putMine(true); mineCount++; tile.FlipTile(true); tiles.push_back(tile); } } } for(int i = 0; i < width; i++){ for(int j =0; j < height; j++){ tile.adjacenttiles(i,j,width,height,tiles,tile); } } } void Board::setDebugOn(sf::RenderWindow &window, int width, int height, vector &tiles, Tiles &tile){ for(int i = 0; i < width; i++){ for(int j = 0; j < height; j++){ if(tile.getTile(i,j,height,tiles)->getMine()){ tile.getTile(i,j,height,tiles)->setDebugOn(); } } } } void Board::getGameLost(bool val){ gameLost = val; if(val){ gameInProgress = false; } } void Board::setMainBoard(int width, int height, int w, int h, int mine_ct, vector &tiles, Tiles &tile) { mineCount = mine_ct; gameWon = false; gameLost = false; gameInProgress = true; for(unsigned int i = 0; i < width; i++){ for(unsigned int j = 0; j < height; j++){ tile.putMine(false); tile.FlipTile(true); tile.SetPosition((float)(w*i),(float)(h*j)); tiles.push_back(tile); } } //put mines set duplicate; int randomTile; while(duplicate.size() != mine_ct){ randomTile = Random::Int(0,tiles.size()-1); duplicate.insert(randomTile); } int i = 0; vector m; for(auto it = duplicate.begin(); it != duplicate.end(); it++){ tiles[*it].putMine(true); m.push_back(*it); i++; } for(int i = 0; i < width; i++){ for(int j =0; j < height; j++){ tile.adjacenttiles(i,j,width,height,tiles,tile); } } } void Board::GameOverCheck(int width, int height, vector &tiles, Tiles tile) { int totalTiles = height*width; int numRevealed = 0; int numMineAndNotRevealed = 0; for(int i =0; i < width; i++){ for(int j = 0; j < height; j++){ if(!tile.getTile(i,j,height,tiles)->getFlipped()){ numRevealed++; } if(tile.getTile(i,j,height,tiles)->getFlipped() && tile.getTile(i,j,height,tiles)->getMine()){ numMineAndNotRevealed++; } } } int total = numRevealed + numMineAndNotRevealed; if(total == totalTiles){ gameInProgress = false; gameWon = true; gameLost = false; for(int i =0; i < width; i++){ for(int j = 0; j < height; j++){ if(tile.getTile(i,j,height,tiles)->getMine()){ numRevealed++; tile.getTile(i,j,height,tiles)->setFlagOn(); tile.getTile(i,j,height,tiles)->putMine(false); tile.getTile(i,j,height,tiles)->setDebugOff(); } } } } } bool Board::getGameInProgress() { return gameInProgress; }