Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   DOS Minesweeper (http://www.programmingforums.org/showthread.php?t=13213)

physicist May 27th, 2007 12:40 AM

DOS Minesweeper
 
Printing the grid works, and in general the array structures and everything updates as it should, random mines are generated; but no matter which square you choose, it CHANGES that square to a mine then says you lose cause you selected a mine; even if it wasnt originally a mine
i have narrowed it down to the bolded function; again, initially about 30% of the squares are mines, and when you click on a mine it works properly, when you select a mineless square it changes it to a mine and says you lose, when it should just continue the game (ill implement neighbor function later once this works)
:

#include<iostream>
#include<cstdlib>
#include<ctime>
#include<string>

using namespace std;
void initialize();
void paint();
string matrix[100];
int mine[100];
void updatematrix(int r, int c);
int main()
{
    initialize();
    int rguess;
    int cguess;
    paint();
    while(1)
    {
            cout <<"Enter row of square to open (0-9)";
            cin >> rguess;
            cout <<"Enter column of square to open (0-9)";
            cin >> cguess;
            updatematrix(rguess,cguess);
    }
    system("PAUSE");
    return 0;
}

void paint()
{
    int i=0;
    for (int row=10; row>0; row--)
    { 
        for (int column=10; column>0; column--)
        {
            cout <<matrix[i];
            cout <<mine[i];
            i++;
        }
        cout<<"\n";
    }     
}
void initialize()
{
    bool a;
    int b;
    srand( (unsigned int) time( NULL ) );
    int c=0;
    while (c<100)
    {
          matrix[c]="|_|";
          b= rand()%10+1;
          if (b<3){a=1;}
          else {a=0;}
          mine[c]=a;
          c++; 
    }
}
void updatematrix(int r, int c)
{
    int square =r*10 + c;
    if (mine[square]=1)
    {cout <<"You lose";}
    else if (mine[square]=0)
    {matrix[square]="|X|";}
    paint();
}


edit: the only reason the paint() function prints the mine value of each square was for testing to see which squares were mines...

andro May 27th, 2007 4:00 AM

Your if and else are using assignment ( = vs == ).

physicist May 27th, 2007 12:20 PM

thank you


All times are GMT -5. The time now is 2:36 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC