// // Created by Priyal Patel on 11/21/21. // #include "Tiles.h" #include <utility> #include "TextureManager.h" Tiles::Tiles(){ tile_hidden.setTexture(TextureManager::GetTexture("tile_hidden")); tile_revealed.setTexture(TextureManager::GetTexture("tile_revealed")); flag.setTexture(TextureManager::GetTexture("flag")); mine.setTexture(TextureManager::GetTexture("mine")); num1.setTexture(TextureManager::GetTexture("number_1")); num2.setTexture(TextureManager::GetTexture("number_2")); num3.setTexture(TextureManager::GetTexture("number_3")); num4.setTexture(TextureManager::GetTexture("number_4")); num5.setTexture(TextureManager::GetTexture("number_5")); num6.setTexture(TextureManager::GetTexture("number_6")); num7.setTexture(TextureManager::GetTexture("number_7")); num8.setTexture(TextureManager::GetTexture("number_8")); num1On = false; num2On = false; num3On = false; num4On = false; num5On = false; num6On = false; num7On = false; num8On = false; debugOn = false; flag_on = false; hidden = getFlipped(); mine_set = getMine(); } void Tiles::SetPosition(float xPosition, float yPosition) { tile_hidden.setPosition(xPosition,yPosition); tile_revealed.setPosition(xPosition,yPosition); flag.setPosition(xPosition,yPosition); mine.setPosition(xPosition,yPosition); num1.setPosition(xPosition,yPosition); num2.setPosition(xPosition,yPosition); num3.setPosition(xPosition,yPosition); num4.setPosition(xPosition,yPosition); num5.setPosition(xPosition,yPosition); num6.setPosition(xPosition,yPosition); num7.setPosition(xPosition,yPosition); num8.setPosition(xPosition,yPosition); } Tiles* Tiles::getTile(int x, int y, int columns, vector<Tiles> &tiles) { return &tiles[(x * columns) + y]; } void Tiles::setMineCount(int count){ mine_counter = count; if(mine_counter == 1){ num1On = true; } if(mine_counter == 2){ num2On = true; } if(mine_counter == 3){ num3On = true; } if(mine_counter == 4){ num4On = true; } if(mine_counter == 5){ num5On = true; } if(mine_counter == 6){ num6On = true; } if(mine_counter == 7){ num7On = true; } if(mine_counter == 8){ num8On = true; } } void Tiles::Draw(sf::RenderWindow &window) { if(hidden){ window.draw(tile_hidden); } else { window.draw(tile_revealed); if(mine_set){ window.draw(mine); }else{ if(num1On){ window.draw(num1); } if(num2On){ window.draw(num2); } if(num3On){ window.draw(num3); } if(num4On){ window.draw(num4); } if(num5On){ window.draw(num5); } if(num6On){ window.draw(num6); } if(num7On){ window.draw(num7); } if(num8On){ window.draw(num8); } } } if(flag_on ){ window.draw(flag); } if(debugOn){ window.draw(mine); } } int Tiles:: GetMineCount(){ return mine_counter; } bool Tiles::getMine() { return mine_set; } void Tiles::putFlag() { flag_on = !flag_on; } bool Tiles::getFlag() { return flag_on; } sf::FloatRect Tiles::GetBounds() { return tile_hidden.getGlobalBounds(); } void Tiles::FlipTile(bool val){ hidden = val; } bool Tiles::getFlipped() { return hidden; } void Tiles::putMine(bool mine) { mine_set = mine; if(mine){ val = 1; }else{ val = 0; } } void Tiles::setDebugOn() { debugOn = !debugOn; } void Tiles::adjacenttiles(int i, int j, int width, int height, vector<Tiles>& tiles, Tiles &tile){ int count_mines = 0; if(NotOutOfBounds(i-1,j,width,height)){ if(tile.getTile(i-1,j,height,tiles)->getMine()){ count_mines++; } } if(NotOutOfBounds(i+1,j,width,height)){ if(tile.getTile(i+1,j,height,tiles)->getMine()){ count_mines++; } } if(NotOutOfBounds(i,j+1,width,height)){ if(tile.getTile(i,j+1,height,tiles)->getMine()){ count_mines++; } } if(NotOutOfBounds(i,j-1,width,height)){ if(tile.getTile(i,j-1,height,tiles)->getMine()){ count_mines++; } } if(NotOutOfBounds(i-1,j+1,width,height)){ if(tile.getTile(i-1,j+1,height,tiles)->getMine()){ count_mines++; } } if(NotOutOfBounds(i-1,j-1,width,height)){ if(tile.getTile(i-1,j-1,height,tiles)->getMine()){ count_mines++; } } if(NotOutOfBounds(i+1,j+1,width,height)){ if(tile.getTile(i+1,j+1,height,tiles)->getMine()){ count_mines++; } } if(NotOutOfBounds(i+1,j-1,width,height)){ if(tile.getTile(i+1,j-1,height,tiles)->getMine()){ count_mines++; } } tile.getTile(i,j,height,tiles)->setMineCount(count_mines); } bool Tiles::NotOutOfBounds(int i, int j, int rows, int columns) { return (i >= 0 && i < rows) && (j >= 0 && j < columns ); } void Tiles::setDebugOff() { debugOn = false; } void Tiles :: Reveal(int i, int j, int width, int height, vector<Tiles>& tiles, Tiles tile, bool firstClick){ if(tile.getTile(i,j,height,tiles)->getFlipped()){ if((NotOutOfBounds(i,j,width,height) && !getTile(i,j,height,tiles)->getMine())){ if(!tile.getTile(i,j,height,tiles)->flag_on){ tile.getTile(i,j,height,tiles)->FlipTile(false); } if(getTile(i,j,height,tiles)->mine_counter == 0){ for(int a = -1 ; a < 2; a++){ for( int b = -1; b < 2; b++){ if(a == 0 && b == 0){ continue; } if(NotOutOfBounds(i,j,width,height) && getTile(i,j,height,tiles)->mine_counter == 0 && !getTile(i,j,height,tiles)->getMine() && !getTile(i,j,height,tiles)->getFlag()){ Reveal(a+i,b+j,width,height,tiles,tile,true); } else { break; } } } }else{ return; } } } } void Tiles::setFlagOn() { flag_on = true; }