Minesweeper / src / main.cpp
main.cpp
Raw
#include <SFML/Graphics.hpp>
#include <iostream>
#include <map>
#include "Tiles.h"
#include "TextureManager.h"
#include "Board.h"
#include "Random.h"
using std::ofstream;
using namespace std;
//----------------------------------------------------------------------------------------------------------------------
int main()
{
    sf::Sprite tile_hidden(TextureManager::GetTexture("tile_hidden"));
    //--------------------------------------------------------------------------------------------------------------
    //Get board data
ifstream inFile("boards/config.cfg");
vector<string> data;
if(inFile.is_open()){
    string fileLine;

    while(inFile>>fileLine){
        istringstream stream(fileLine);
        data.push_back(fileLine);
    }
}
    int width =  stoi(data[0]);
    int height = stoi(data[1]);
    int mine_ct =  stoi(data[2]);
//----------------------------------------------------------------------------------------------------------------------
    sf::RenderWindow window(sf::VideoMode(width*32, height*32 + 88), "Minesweeper");
    //---------------------------------
    //1. Draw the main game board with all the hidden tiles
    //Get tile sprite dimensions
    vector<Tiles> tiles;
    Tiles tile;
    bool debug = false;

    int w = tile_hidden.getTextureRect().width;
    int h =  tile_hidden.getTextureRect().height; //32
    //-------------------------------------------------------------------------------------------
    //2.Draw the smiley face, and debugging buttons (the mine toggle, test board buttons)
    Board setup;
    setup.SetPositionTest1Button(window);
    setup.setMainBoard(width,height,w,h,mine_ct,tiles,tile);
    setup.Draw(window);

    while (window.isOpen())
    {
        //3.Left-click to reveal tiles and show either the mine, number, or empty tile underneath
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed){
                window.close();
            }


            if(event.type == sf::Event::MouseButtonReleased){
                if(event.mouseButton.button == sf::Mouse::Left){
                    sf::Vector2f mousePosition((float)sf::Mouse::getPosition(window).x,(float)sf::Mouse::getPosition(window).y);

                  for(unsigned int i = 0; i < width;i++){
                        for(unsigned int j = 0; j < height; j++){


                            if(tile.getTile(i,j,height,tiles)->GetBounds().contains(mousePosition) && !tile.getTile(i,j,height,tiles)->getFlag()){

                                if(tile.NotOutOfBounds(i,j,width,height) && !tile.getTile(i,j,height,tiles)->getMine() && setup.getGameInProgress()){

                                    tile.getTile(i,j, height,tiles)->Reveal(i,j,width,height,tiles,tile,true);
                                    setup.GameOverCheck(width,height,tiles,tile);
                                }
                                if(tile.getTile(i, j, height, tiles)->getMine()){
                                    for(unsigned int i = 0; i < width; i++){
                                        for(unsigned int j = 0; j < height; j++){

                                            if(tile.getTile(i,j,height,tiles)->getFlipped()){
                                                tile.getTile(i,j,height,tiles)->FlipTile(false);
                                            }
                                            tile.getTile(i,j,height,tiles)->setDebugOff();

                                            setup.getGameLost(true);
                                        }
                                    }
                                    setup.setDebugOn(window,width,height,tiles,tile);

                                }
                            }
                        }
                    }

                    if(setup.GetBoundstest_1().contains(mousePosition)){
                        tiles.clear();
                        setup.setTests("boards/testboard1.brd", window, tiles);
                        setup.getGameLost(false);
                    }
                    if(setup.GetBoundstest_2().contains(mousePosition)){
                        tiles.clear();
                        setup.setTests("boards/testboard2.brd", window, tiles);
                        setup.getGameLost(false);
                    }
                    if(setup.GetBoundstest_3().contains(mousePosition)){
                        tiles.clear();
                        setup.setTests("boards/testboard3.brd", window, tiles);
                        setup.getGameLost(false);

                    }
                    if(setup.GetBoundsdebug().contains(mousePosition)){
                        setup.setDebugOn(window, width, height, tiles, tile);
                        debug = !debug;
                    }
                    if(setup.GetBoundsSmiley().contains(mousePosition)){
                        tiles.clear();
                        setup.setMainBoard(width,height,w,h,mine_ct,tiles,tile);
                        if(debug){
                            setup.setDebugOn(window, width, height, tiles, tile);
                        }
                    }
                }
            }
            //4. Add and remove flags with right-click
            if(event.type == sf::Event::MouseButtonReleased){
                if(event.mouseButton.button == sf::Mouse::Right){
                    sf::Vector2f mousePosition((float)sf::Mouse::getPosition(window).x,(float)sf::Mouse::getPosition(window).y);
                    for(unsigned int i = 0; i < width;i++){
                        for(unsigned int j = 0; j < height; j++){
                            if(tile.getTile(i,j,height,tiles)->GetBounds().contains(mousePosition) && setup.getGameInProgress()){
                                tile.getTile(i,j,height,tiles)->putFlag();
                                setup.DigitSubtract(tiles,setup,i,j,height);
                            }
                        }
                    }
                }
            }
        }
        window.clear();
        window.clear(sf::Color(255,255,255));

        for(int i = 0; i < tiles.size(); i++){
        tiles[i].Draw(window);
        }
        setup.Draw(window);
        setup.MinesRemaining(window,tiles,setup);
        window.display();
    }
    TextureManager::Clear();
    return 0;
}