Thread: Minesweeper!

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

    Minesweeper!

    I'm nearly finished now and I need help fixing errors.
    Any help would be greatly appreciated.

    I get three Linker Errors. How do linker errors occur?


    Code:
    #include <iostream>
    #include <conio>
    #include <iomanip>
    using namespace std;
    
    void reset_array(char minefield[][10], int row, int col);
    
    void print_array(char minefield[][10], int row, int col);
    
    void print_array_without_bombs(char minefield[][10], int row, int col);
    
    void fill_array_with_bombs(char minefield[][10], int r1, int c1);
    
    bool check_for_hit(int r1, int c1);
    
    
    //-------------------------------------------------------------------------
    //    Behold, the main function
    //-------------------------------------------------------------------------
    void 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];
    
            //Sets all cells in the array to '-'
            reset_array(minefield, row, col);
    
            //Prints out the array with everything reveiled
            print_array(minefield, row, col);
    
            //Plants 5 bombs in the array-minefield
            fill_array_with_bombs(minefield, row, col);
    
            //clears the screen so 2nd player can't cheat
            system("CLS");
    
            //sets the gameboard for the 2nd player to guess for bomb locations
            print_array_without_bombs(minefield, row, col);
    
            cout<<"Time to start sweeping for mines!";
    
            //loop until all the bombs are found
            while (all_found==false)
            {
                    cout<<"Please enter a row and column number: ";
                    cin>>r1;
                    cin>>c1;
    
                    //If the 2nd player enters two zeros, quit program
                    if (r1==0 && c1==0)
                    {
                            cout<<"You quit.   Gave Over.";
                            all_found=true;
                    }
    
                    //decides whether location guessed is a bomb location and returns true
                    check_for_hit(r1, c1);
    
                    if (check_for_hit(r1, c1)==true)
                    {
                            //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)
                            {
                                    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, row, col);
    
                    //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
                    print_array(minefield, row, col);
    
    
            }
    
    }
    
    
    //-------------------------------------------------------------------------
    //Sets all cells in the array to '-'
    //-------------------------------------------------------------------------
    void reset_array(char minefield[][10], int row, int col)
    {
            for (row=0; row<10; row++)
            {
                    for (col=0; col<10; col++)
                    {
                    minefield[row][col]='-';
                    }
            }
    }
    
    
    //-------------------------------------------------------------------------
    //Prints out the array with everything reveiled
    //-------------------------------------------------------------------------
    void print_array(char minefield[][10], int row, int col)
    {
            int x=1;
            cout<<"         1 2 3 4 5 6 7 8 9 10";
    
            for (row=0; row<10; row++)
            {
                    cout<<"\nRow "<<setw(2)<<x<<": ";
                    x=x++;
    
                    for (col=0; col<10; col++)
                    {
                            cout<<" "<<minefield[row][col];
                    }
            }
    
    
    }
    
    
    //--------------------------------------------------------------------------
    //sets the gameboard for the 2nd player to guess for bomb locations
    //--------------------------------------------------------------------------
    void print_array_without_bombs(char minefield[][10], int row, int col)
    {
            int x=1;
            cout<<"         1 2 3 4 5 6 7 8 9 10";
    
            for (row=0; row<10; row++)
            {
                    for (col=0; col<10; col++)
                    {
                            if (minefield[row][col]== 'B')
                            {
                                    minefield[row][col]= '-';
                            }
                    }
            }
    
            for (row=0; row<10; row++)
            {
                    cout<<"\nRow "<<setw(2)<<x<<": ";
                    x=x++;
    
                    for (col=0; col<10; col++)
                    {
                            cout<<" "<<minefield[row][col];
                    }
            }
    
    
    
    }
    
    
    //--------------------------------------------------------------------------
    //Plants 5 bombs in the array-minefield
    //--------------------------------------------------------------------------
    void fill_array_with_bombs(char minefield[][10], int r1, int c1)
    {
            int t=0;
    
            cout<<"Now you will place 5 bombs in the minefield, in different locations.";
    
            while (t<5)
            {
                    cout<<"Enter a row number and column number coordinate ( example:r c ):";
                    cin>>r1;
                    cin>>c1;
    
                    if (r1>0 && r1<10 && c1>0 && c1<10)
                    {
    
                            if (minefield[r1][c1]=='B')
                            {
                                    cout<<"That bomb location has already been picked.";
                            }
                            else if (minefield[r1][c1]=='-')
                            {
                                    minefield[r1][c1]='B';
                                    t++;
                            }
    
                    }
                    else
                    {
                            cout<<"Make sure your coordinates are between 0 and 10.";
                    }
    
            }
                    system("PAUSE");
    }
    
    
    //---------------------------------------------------------------------------
    //decides whether location guessed is a bomb location and returns true
    //---------------------------------------------------------------------------
    bool check_for_hit(char minefield[][10], int r1, int c1)
    {
            if (minefield[r1][c1]== 'X')
            {
                    cout<<"Already blew that bomb.";
                    return false;
            }
            else if (minefield[r1][c1]== 'M')
            {
                    cout<<"Location already tried.";
                    return false;
            }
            else if (minefield[r1][c1]== '-')
            {
                    cout<<"MISS!";
                    return false;
            }
            else if (minefield[r1][c1]== 'B')
            {
                    cout<<"HIT!";
                    return true;
            }
            else
            {
                    return false;
            }
    
    };
    Last edited by Troll; 12-15-2005 at 12:58 PM. Reason: modifying code parameters

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    One reason ( the one in your case ) is a missmatch in declaration and implementation of functions.
    e.g. your declaration
    Code:
    bool check_for_hit(int r1, int c1);
    but you implemented
    Code:
    bool check_for_hit(char minefield[][10], int r1, int c1)
    Last edited by ZuK; 12-15-2005 at 01:05 PM.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    16
    Thank you! It sure does help.
    However, those linker errors are still haunting my code(and my conscience)

    Code:
    #include <iostream>
    #include <conio>
    #include <iomanip>
    using namespace std;
    
    void reset_array(char minefield[][10], int row, int col);
    
    void print_array(char minefield[][10], int row, int col);
    
    void print_array_without_bombs(char minefield[][10], int row, int col);
    
    void fill_array_with_bombs(char minefield[][10], int r1, int c1);
    
    bool check_for_hit(char minefield[][10], int r1, int c1);
    
    //-------------------------------------------------------------------------
    //        Behold, the main function
    //-------------------------------------------------------------------------
    void 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];
    
            //Sets all cells in the array to '-'
            reset_array(minefield, row, col);
    
            //Prints out the array with everything reveiled
            print_array(minefield, row, col);
    
            //Plants 5 bombs in the array-minefield
            fill_array_with_bombs(minefield, row, col);
    
            //clears the screen so 2nd player can't cheat
            system("CLS");
    
            //sets the gameboard for the 2nd player to guess for bomb locations
            print_array_without_bombs(minefield, row, col);
    
            cout<<"Time to start sweeping for mines!";
    
            //loop until all the bombs are found
            while (all_found==false)
            {
                    cout<<"Please enter a row and column number: ";
                    cin>>r1;
                    cin>>c1;
    
                    //If the 2nd player enters two zeros, quit program
                    if (r1==0 && c1==0)
                    {
                            cout<<"You quit.   Gave Over.";
                            all_found=true;
                    }
    
                    //decides whether location guessed is a bomb location and returns true
                    check_for_hit(minefield, r1, c1);
    
                    if (check_for_hit(minefield, r1, c1)==true)
                    {
                            //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)
                            {
                                    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, row, col);
    
                    //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
                    print_array(minefield, row, col);
    
    
            }
    
    }
    
    
    //-------------------------------------------------------------------------
    //Sets all cells in the array to '-'
    //-------------------------------------------------------------------------
    void reset_array(char minefield[][10], int row, int col)
    {
            for (row=0; row<10; row++)
            {
                    for (col=0; col<10; col++)
                    {
                    minefield[row][col]='-';
                    }
            }
    }
    
    
    //-------------------------------------------------------------------------
    //Prints out the array with everything reveiled
    //-------------------------------------------------------------------------
    void print_array(char minefield[][10], int row, int col)
    {
            int x=1;
            cout<<"         1 2 3 4 5 6 7 8 9 10";
    
            for (row=0; row<10; row++)
            {
                    cout<<"\nRow "<<setw(2)<<x<<": ";
                    x=x++;
    
                    for (col=0; col<10; col++)
                    {
                            cout<<" "<<minefield[row][col];
                    }
            }
    
    
    }
    
    
    //--------------------------------------------------------------------------
    //sets the gameboard for the 2nd player to guess for bomb locations
    //--------------------------------------------------------------------------
    void print_array_without_bombs(char minefield[][10], int row, int col)
    {
            int x=1;
            cout<<"         1 2 3 4 5 6 7 8 9 10";
    
            for (row=0; row<10; row++)
            {
                    for (col=0; col<10; col++)
                    {
                            if (minefield[row][col]== 'B')
                            {
                                    minefield[row][col]= '-';
                            }
                    }
            }
    
            for (row=0; row<10; row++)
            {
                    cout<<"\nRow "<<setw(2)<<x<<": ";
                    x=x++;
    
                    for (col=0; col<10; col++)
                    {
                            cout<<" "<<minefield[row][col];
                    }
            }
    
    
    
    }
    
    
    //--------------------------------------------------------------------------
    //Plants 5 bombs in the array-minefield
    //--------------------------------------------------------------------------
    void fill_array_with_bombs(char minefield[][10], int r1, int c1)
    {
            int t=0;
    
            cout<<"Now you will place 5 bombs in the minefield, in different locations.";
    
            while (t<5)
            {
                    cout<<"Enter a row number and column number coordinate ( example:r c ):";
                    cin>>r1;
                    cin>>c1;
    
                    if (r1>0 && r1<10 && c1>0 && c1<10)
                    {
    
                            if (minefield[r1][c1]=='B')
                            {
                                    cout<<"That bomb location has already been picked.";
                            }
                            else if (minefield[r1][c1]=='-')
                            {
                                    minefield[r1][c1]='B';
                                    t++;
                            }
    
                    }
                    else
                    {
                            cout<<"Make sure your coordinates are between 0 and 10.";
                    }
    
            }
                    system("PAUSE");
    }
    
    
    //---------------------------------------------------------------------------
    //decides whether location guessed is a bomb location and returns true
    //---------------------------------------------------------------------------
    bool check_for_hit(char minefield[][10], int r1, int c1)
    {
            if (minefield[r1][c1]== 'X')
            {
                    cout<<"Already blew that bomb.";
                    return false;
            }
            else if (minefield[r1][c1]== 'M')
            {
                    cout<<"Location already tried.";
                    return false;
            }
            else if (minefield[r1][c1]== '-')
            {
                    cout<<"MISS!";
                    return false;
            }
            else if (minefield[r1][c1]== 'B')
            {
                    cout<<"HIT!";
                    return true;
            }
            else
            {
                    return false;
            }
    
    };

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    If I comment out this funny #include <conio>
    and declare
    Code:
    int main()
    It compiles and links fine on my box
    Kurt

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    x=x++;
    ->
    Code:
    x++;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    print_array_without_bombs() clears all the bombs

    get rid of #include<conio>

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    16
    Thank you guys! Part of the problem was that I was using a bad console. I copy-pasted my code into a new console wizard and it compiled beautifully after the corrections.

    Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's the best way to draw a minesweeper field
    By Nazgulled in forum C Programming
    Replies: 1
    Last Post: 05-08-2007, 01:24 PM
  2. AI Contest - Minesweeper
    By Darryl in forum Contests Board
    Replies: 93
    Last Post: 04-24-2006, 05:48 PM
  3. minesweeper
    By sameintheend01 in forum C++ Programming
    Replies: 1
    Last Post: 02-18-2003, 09:50 AM
  4. minesweeper problem
    By adamrobbie in forum C Programming
    Replies: 2
    Last Post: 10-14-2002, 08:03 PM
  5. Home made Minesweeper using SDL
    By damyan in forum Game Programming
    Replies: 3
    Last Post: 11-02-2001, 06:17 PM