I'm so close! Minesweeper

This is a discussion on I'm so close! Minesweeper within the C++ Programming forums, part of the General Programming Boards category; There's just this tiny logical error in my code somewhere. There's mulitple print outs that won't appear in the final ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    16

    I'm so close! Minesweeper

    There's just this tiny logical error in my code somewhere.

    There's mulitple print outs that won't appear in the final program, but I'm using them to figure out were I went wrong. How do I get the B's to change to X's?

    Code:
    #include <iostream>
    #include <iomanip>
    #include <conio>
    using namespace std;
    
    void reset_array(char minefield[10][10], char temp_minefield[10][10]);
    
    void print_array(char temp_minefield[10][10]);
    
    void print_array_without_bombs(char minefield[10][10], char temp_minefield[10][10]);
    
    void fill_array_with_bombs(char minefield[10][10], int r1, int c1);
    
    bool check_for_hit(char minefield[10][10], char temp_minefield[10][10], int r1, int c1);
    
    //-------------------------------------------------------------------------
    //        Behold, the main function
    //-------------------------------------------------------------------------
    int main()
    {
            int row, col, bombs_left=5, r1, c1;
            int num_found=0, num_attempts=0;
            bool all_found=false;
    
            //Declaring the two-dimensional array
            char minefield[10][10];
            //This minefield will keep track of bombs
            char temp_minefield[10][10];
    
            //Sets all cells in the array to '-'
            reset_array(minefield, temp_minefield);
    
            //Prints out the array with everything reveiled
            print_array(temp_minefield);
    
            //Plants 5 bombs in the array-minefield
            fill_array_with_bombs(temp_minefield, r1, c1);
    
            //clears the screen so 2nd player can't cheat
            system("CLS");
    
            //sets the gameboard for the 2nd player to guess for bomb locations
    
    
            cout<<"\n\nTime to start sweeping for mines!";
    
            //loop until all the bombs are found
            while (all_found==false)
            {
                    cout<<"\n\nPlease enter a row and column number: ";
                    cin>>r1;
                    cin>>c1;
    
                    print_array(temp_minefield);
    
                    //If the 2nd player enters two zeros, quit program
                    if (r1==0 && c1==0)
                    {
                            cout<<"\n\n\nYou quit.   Game Over.";
                            all_found=true;
                            getch();
                    }
    
                    if (check_for_hit(minefield, temp_minefield, r1, c1))
                    {
                            //1 bomb closer to finishing the program
                            num_found++;
                            cout<<"There are now "<<bombs_left--<<"bombs left in the minefield";
    
                            //If all the bombs are found, the while loop will break
                            if (bombs_left==0)
                            {
                                    print_array(temp_minefield);
                                    all_found=true;
                            }
                    }
    
                    //Counts the number of attempts to poke fun of you later.
                    num_attempts++;
    
                    //Prints the up-to-date minefield
                    print_array_without_bombs(minefield, temp_minefield);
    
                    //Once all the bombs are found, it will print out how many attempts it took
                    if (all_found==true)
                    {
                            cout<<"It took you "<<num_attempts<<" guesses to find all 5 bombs!  HAHA!";
                    }
    
                    //Reveils the bomb locations and all other occurances
    
    
    
            }
    
    }
    
    
    //-------------------------------------------------------------------------
    //Sets all cells in the array to '-'
    //-------------------------------------------------------------------------
    void reset_array(char minefield[10][10], char temp_minefield[10][10])
    {
            int row, col;
    
            for (row=1; row<=10; row++)
            {
                    for (col=1; col<=10; col++)
                    {
                    minefield[row-1][col-1]='-';
                    temp_minefield[row-1][col-1]='-';
                    }
            }
    }
    
    
    //-------------------------------------------------------------------------
    //Prints out the array with everything reveiled
    //-------------------------------------------------------------------------
    void print_array(char temp_minefield[10][10])
    {
            int x=1;
            int row, col;
            cout<<"         1 2 3 4 5 6 7 8 9 10";
    
            for (row=1; row<=10; row++)
            {
                    cout<<"\nRow "<<setw(2)<<x<<": ";
                    x++;
    
                    for (col=1; col<=10; col++)
                    {
                            cout<<" "<<temp_minefield[row-1][col-1];
                    }
            }
    
    
    }
    
    
    //--------------------------------------------------------------------------
    //sets the gameboard for the 2nd player to guess for bomb locations
    //--------------------------------------------------------------------------
    void print_array_without_bombs(char minefield[10][10], char temp_minefield[10][10])
    {
            int x=1;
            int row, col;
    
            cout<<"\n\n         1 2 3 4 5 6 7 8 9 10";
    
            for (row=1; row<=10; row++)
            {
                    cout<<"\nRow "<<setw(2)<<x<<": ";
                    x++;
    
                    for (col=1; col<=10; col++)
                    {
                            cout<<" "<<minefield[row-1][col-1];
                    }
            }
    
            cout<<"\n\n         1 2 3 4 5 6 7 8 9 10";
    
            for (row=1; row<=10; row++)
            {
                    cout<<"\nRow "<<setw(2)<<x<<": ";
                    x++;
    
                    for (col=1; col<=10; col++)
                    {
                            cout<<" "<<temp_minefield[row-1][col-1];
                    }
            }
    
    }
    
    
    //--------------------------------------------------------------------------
    //Plants 5 bombs in the array-minefield
    //--------------------------------------------------------------------------
    void fill_array_with_bombs(char temp_minefield[][10], int r1, int c1)
    {
            int t=0;
    
            cout<<"\n\nNow you will place 5 bombs in the minefield, in different locations.";
    
            while (t<5)
            {
                    cout<<"\n\nEnter a row number and column number coordinate ( example:r c ):";
                    cin>>r1;
                    cin>>c1;
    
                    if (r1>=0 && r1<=10 && c1>=0 && c1<=10)
                    {
    
    
                            if (temp_minefield[r1-1][c1-1]=='-')
                            {
                                    temp_minefield[r1-1][c1-1]='B';
                                    t++;
                            }
                            else if (temp_minefield[r1-1][c1-1]=='B')
                            {
                                    cout<<"\n\nThat bomb location has already been picked.";
                            }
    
                    }
                    else
                    {
                            cout<<"Make sure your coordinates are between 0 and 10.";
                    }
    
            }
            int row, col, x=1;
            cout<<"\n\n         1 2 3 4 5 6 7 8 9 10";
    
            for (row=1; row<=10; row++)
            {
                    cout<<"\nRow "<<setw(2)<<x<<": ";
                    x++;
    
                    for (col=1; col<=10; col++)
                    {
                            cout<<" "<<temp_minefield[row-1][col-1];
                    }
            }
            getch();
            system("PAUSE");
    }
    
    
    //---------------------------------------------------------------------------
    //decides whether location guessed is a bomb location and returns true
    //---------------------------------------------------------------------------
    bool check_for_hit(char temp_minefield[10][10], char minefield[10][10], int r1, int c1)
    {
            if (temp_minefield[r1-1][c1-1]== 'B')
            {
                    cout<<"\nHIT!";
                    minefield[r1-1][c1-1]='X';
                    return true;
            }
            else if (temp_minefield[r1-1][c1-1]== '-')
            {
                    cout<<"\nMISS!";
                    minefield[r1-1][c1-1]='M';
                    return false;
            }
            else if (temp_minefield[r1-1][c1-1]== 'X')
            {
                    cout<<"\nAlready blew that bomb.";
                    return false;
            }
            else if (temp_minefield[r1-1][c1-1]== 'M')
            {
                    cout<<"\nLocation already tried.";
                    return false;
            }
            else
            {
                    return false;
            }
    
    };

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    You should have continued in the previous thread.

    You've mucked up the logic by declaring two arrays. It's not needed.

    Currently, you fill the bombs into temp_minefield, but check for bombs against mine_field. So they will always miss. (if you just reorder the params to the function call it might work)

    You only need one array. The only reason you made two was because sometimes you want to display bombs and sometimes you don't. That logic should just occurr when you go to print the array.

    something like
    Code:
    print_field(field[][], hidebombs)
    {
      for(i=0; i<row; i++) {
        for(j=0; j<col; j++) {
          if(field[i][j] == 'B' && hidebombs) {
            cout << '-';
          }else{
            cout << field[i][j];
          }
        }
    }
    Last edited by spydoor; 12-15-2005 at 03:57 PM.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    16
    You are a genius.

    Much thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Close an HTTPListenerResponse.OutputStreram
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 04-23-2008, 11:00 PM
  2. User input to close window
    By NoFearXD in forum Windows Programming
    Replies: 2
    Last Post: 05-09-2007, 07:38 PM
  3. open() and close()
    By SoFarAway in forum C Programming
    Replies: 3
    Last Post: 04-08-2005, 01:16 PM
  4. XWindows- Close window event?
    By Exile in forum Linux Programming
    Replies: 8
    Last Post: 01-09-2005, 09:39 PM
  5. Ghost in the CD Drive
    By Natase in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 10-12-2001, 05:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21