Jump to content

[C++] Minesweeper


Famicoman
 Share

Recommended Posts

Wrote this minesweeper console game in C++ class when I was bored earlier this year. You input your spaces using computer style coordinates (Top left is 0,0 bottem right is 9,9 etc). You can't label where you think there are mines though, which adds a layer of difficulty, and placement of the the 10 mines is random on each launch. Just thought I'd share.

#include<iostream.h>
#include<stdlib.h>
#include<time.h>

char Back[10][10]=
    {
        ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
        ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
        ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
        ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
        ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
        ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
        ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
        ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
        ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
        ' ',' ',' ',' ',' ',' ',' ',' ',' ',' '
    };

char Front[10][10]=
    {
        ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
        ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
        ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
        ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
        ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
        ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
        ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
        ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
        ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
        ' ',' ',' ',' ',' ',' ',' ',' ',' ',' '
    };


class Minesweeper
{
private:
    int x;
    int y;
    int nonmines;
public:
    Minesweeper();
    void Intro();
    void Placemines();
    void Countem();
    void Move();
    void Showboard_Front();
    void Outro();
};


Minesweeper::Minesweeper()
{
    nonmines=0;
}


void Minesweeper::Intro()
{
    cout<<" ________________________________________________________ "<<endl;
    cout<<"| Welcome to Minesweeper                                 |"<<endl;
    cout<<"|--------------------------------------------------------|"<<endl;
    cout<<"|  * Type coordinates when prompted                      |"<<endl;
    cout<<"|  * Coordinates go from top to bottem                   |"<<endl;
    cout<<"|  * If you hit a mine, you die                          |"<<endl;
    cout<<"|  * Do not uncover coordinates you think are mines      |"<<endl;
    cout<<"|                                                        |"<<endl;
    cout<<"|                                                        |"<<endl;
    cout<<"|                                                        |"<<endl;
    cout<<"| Good luck                                              |"<<endl;
    cout<<"|________________________________________________________|"<<endl;
    cout<<endl;
    system("pause");
    system("cls");

}

void Minesweeper::Placemines()
{
    for(int mines=0;mines<10;mines++)
    {
        int x=rand()%9;
        int y=rand()%9;

        if (Back[x][y]=='*')
        {            
            mines--;
        }
        else
        {
            Back[x][y]='*';
        }
    }
}

void Minesweeper::Countem()
{
    for (int a=0;a<10;a++)
    {
        for (int b=0;b<10;b++)
        {
            if(Back[a][b]!='*')
            {
                int temp=0;
                
                
                if (a==0 && b==0)
                {
                    
                    if (Back[a+1][b]=='*')
                        temp++;
                    if (Back[a+1][b+1]=='*')
                        temp++;
                    if (Back[a][b+1]=='*')
                        temp++;

                }
                else if (a==0 && b==9)
                {
                    
                    if (Back[a][b-1]=='*')
                        temp++;
                    if (Back[a+1][b-1]=='*')
                        temp++;
                    if (Back[a+1][b]=='*')
                        temp++;

                }
                else if (a==9 && b==9)
                {
                    
                    if (Back[a-1][b]=='*')
                        temp++;
                    if (Back[a-1][b-1]=='*')
                        temp++;
                    if (Back[a][b-1]=='*')
                        temp++;

                }
                else if (a==9 && b==0)
                {
                    
                    if (Back[a-1][b]=='*')
                        temp++;
                    if (Back[a-1][b+1]=='*')
                        temp++;
                    if (Back[a][b+1]=='*')
                        temp++;

                }                                
                else if(a==0)
                {
                    if (Back[a][b-1]=='*')
                        temp++;
                    if (Back[a][b+1]=='*')
                        temp++;
                    if (Back[a+1][b-1]=='*')
                        temp++;
                    if (Back[a+1][b+1]=='*')
                        temp++;
                    if (Back[a+1][b]=='*')
                        temp++;

                }
                else if(a==9)
                {
                    if (Back[a][b-1]=='*')
                        temp++;
                    if (Back[a][b+1]=='*')
                        temp++;
                    if (Back[a-1][b-1]=='*')
                        temp++;
                    if (Back[a-1][b+1]=='*')
                        temp++;
                    if (Back[a-1][b]=='*')
                        temp++;

                }
                else if(b==0)
                {
                    if (Back[a-1][b]=='*')
                        temp++;
                    if (Back[a+1][b]=='*')
                        temp++;
                    if (Back[a-1][b+1]=='*')
                        temp++;
                    if (Back[a+1][b+1]=='*')
                        temp++;
                    if (Back[a][b+1]=='*')
                        temp++;

                }
                else if(b==9)
                {

                    if (Back[a-1][b]=='*')
                        temp++;
                    if (Back[a+1][b]=='*')
                        temp++;
                    if (Back[a-1][b-1]=='*')
                        temp++;
                    if (Back[a+1][b-1]=='*')
                        temp++;
                    if (Back[a][b-1]=='*')
                        temp++;

                }
                else
                {
                
                    if (Back[a-1][b]=='*')
                        temp++;
                    if (Back[a][b-1]=='*')
                        temp++;
                    if (Back[a-1][b-1]=='*')
                        temp++;
                    if (Back[a+1][b+1]=='*')
                        temp++;
                    if (Back[a-1][b+1]=='*')
                        temp++;
                    if (Back[a+1][b-1]=='*')
                        temp++;
                    if (Back[a+1][b]=='*')
                        temp++;
                    if (Back[a][b+1]=='*')
                        temp++;
                }
                    temp=temp+48;
                    Back[a][b]=temp;

                
            }
        }
    }


}



void Minesweeper::Move()
{
    Showboard_Front();

    cout<<"Please enter a row number (0-9)";
    cin>>x;
    if (x>9 || x<0)
    {
        cout<<"Please enter a number between 1 and 10"<<endl;
        system("pause");
        system("cls");
        Move();
    }
    cout<<"Please enter a columm number (0-9)";
    cin>>y;
    if (y>9 || y<0)
    {
        cout<<"Please enter a number between 1 and 10"<<endl;
        system("pause");
        system("cls");
        Move();
    }

if (Front[x][y]==Back[x][y])
{
    system("cls");
    Move();
}
else
{
    Front[x][y]=Back[x][y];
    nonmines++;
    system("cls");
}


if (Back[x][y]=='*' || nonmines>=90)
{
    Outro();
}
else
Move();

}


void Minesweeper::Showboard_Front()
{
    for (int a=0;a<10;a++)
    {
        for (int b=0;b<10;b++)
        {
            cout<<Front[a][b]<<" ";
        }
        cout<<endl;
    }

}

void Minesweeper::Outro()
{
    for (int a=0;a<10;a++)
    {
        for (int b=0;b<10;b++)
        {
            cout<<Back[a][b]<<" ";
        }
        cout<<endl;
    }


    cout<<"Thank you for playing minesweeper"<<endl;
    if (nonmines==90)
    {
        cout<<"You have beat the game"<<endl;
    }
    else
    {
        cout<<"You have hit a mine and died"<<endl;
    }

    cout<<endl<<"Thanks for playing"<<endl;

}

int main()
{
    srand(time(0));
    Minesweeper thisgame;
      thisgame.Intro();
    thisgame.Placemines();
    thisgame.Countem();
    thisgame.Move();
    return 0;
}

Link to comment
Share on other sites

  • 2 weeks later...

I love it.  You did a good job.  It does have a couple issues though.

When you check for a game win, you need to use "==" instead of "=" because if you lose it tells you that you win the game. 

The other thing is that it doesn't pause at the end of a game to give time for the player to actually read the text on win or lose.

Other than that, wonderful!.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...