Thread: minesweeper

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    14

    minesweeper

    Ok, my wonderful teacher has assigned us to create a minesweeper type game. This is my problem. When a coordinate is inputted, instead of displaying the number of surrounding mines, it will display a heart or club...literally. Que Paso? Also, the random mine generator is not working at all.

    Code:
    #include<iostream>
    #include<iomanip>
    #include<cstdlib>				//includes libraries
    
    using namespace std;
    
    char board[5][8];
    char board2[5][8];
    
    void printMatrix(void)			//precond: none
    {								//postcond: prints a 5 row 8 col board
    	cout<<"  ";
    	cout<<"12345678"<<endl;
    	for(int x=0;x<5;x++){				//prints out board.Blank spaces for 
    		if(x==0)						//"covered" cells, number of bombs 
    			cout<<" +--------+"<<endl;	//surrounding cells that have been 
    		for(int y=0;y<8;y++){			//uncovered, and '?' spots.
    			if(y==0)
    				cout<<" |";
    			if(board[x][y] == '0')
    				cout<<setw(1);
    			else 
    				cout<<board[x][y];
    			if(y==7)
    				cout<<"|";
    			}
    		cout<<endl;
    		
    	}
    	cout<<" +--------+"<<endl;
    }
    
    int checkMatrix(int col,int row)		//precond: receives a cell to check
    {										//postcond: counts the number of mines in 
    	int mineCounter=0;					//surrounding cells and ignores ? and 
    										//cells with nothing in them
    	if(col>7 && row<2){					//calculates mines 4 corners
    		if(board[col-1][row]!= ('0'||'?'))
    			mineCounter++;	
    		if(board[col-1][row-1]!= ('0'||'?'))
    			mineCounter++;
    		if(board[col][row-1]!= ('0'||'?'))
    			mineCounter++;
    		return(mineCounter);
    		}
    
    	if(col<2 && row>4){
    		if(board[col][row-1]!= ('0'||'?'))
    			mineCounter++;	
    		if(board[col+1][row-1]!= ('0'||'?'))
    			mineCounter++;
    		if(board[col+1][row]!= ('0'||'?'))
    			mineCounter++;
    		return(mineCounter);
    		}
    	if(col>7 && row>4){
    		if(board[col-1][row]!= ('0'||'?'))
    			mineCounter++;	
    		if(board[col-11][row-1]!= ('0'||'?'))
    			mineCounter++;
    		if(board[col][row-1]!= ('0'||'?'))
    			mineCounter++;
    		return(mineCounter);
    		}
    	if(col<2 && row<2){	
    		if(board[col+1][row]!= ('0'||'?'))
    			mineCounter++;
    		if(board[col+1][row+1]!= ('0'||'?'))
    			mineCounter++;
    		if(board[col][row+1]!= ('0'||'?'))
    			mineCounter++;
    		return(mineCounter);
    		}
    	if(col<2){								//calculates mines in edges (not corners)
    		if(board[col+1][row]!= ('0'||'?'))
    			mineCounter++;
    		if(board[col+1][row+1]!= ('0'||'?'))
    			mineCounter++;
    		if(board[col][row+1]!= ('0'||'?'))
    			mineCounter++;
    		if(board[col][row-11]!= ('0'||'?'))
    			mineCounter++;
    		if(board[col+1][row-1]!= ('0'||'?'))
    			mineCounter++;
    		return(mineCounter);
    		}
    	if(col>7){	
    		if(board[col-1][row]!= ('0'||'?'))
    			mineCounter++;
    		if(board[col-1][row+1]!= ('0'||'?'))
    			mineCounter++;
    		if(board[col][row+1]!= ('0'||'?'))
    			mineCounter++;
    		if(board[col][row-1]!= ('0'||'?'))
    			mineCounter++;
    		if(board[col-1][row-1]!= ('0'||'?'))
    			mineCounter++;
    		return(mineCounter);
    		}
    	if(row>4){	
    		if(board[col-1][row]!= ('0'||'?'))
    			mineCounter++;
    		if(board[col-1][row-1]!= ('0'||'?'))
    			mineCounter++;
    		if(board[col][row-1]!= ('0'||'?'))
    			mineCounter++;
    		if(board[col+1][row]!= ('0'||'?'))
    			mineCounter++;
    		if(board[col+1][row-1]!= ('0'||'?'))
    			mineCounter++;
    		return(mineCounter);
    		}
    	if(row<4){	
    		if(board[col-1][row]!= ('0'||'?'))
    			mineCounter++;
    		if(board[col-1][row+1]!= ('0'||'?'))
    			mineCounter++;
    		if(board[col][row+1]!= ('0'||'?'))
    			mineCounter++;
    		if(board[col+1][row]!= ('0'||'?'))
    			mineCounter++;
    		if(board[col+1][row+1]!= ('0'||'?'))
    			mineCounter++;
    		return(mineCounter);
    		}
    	if(board[col-1][row]!= ('0'||'?'))		//calculates surrounding mines in all 
    			mineCounter++;					//other areas
    		if(board[col-1][row+1]!= ('0'||'?'))
    			mineCounter++;
    		if(board[col][row+1]!= ('0'||'?'))
    			mineCounter++;
    		if(board[col+1][row]!= ('0'||'?'))
    			mineCounter++;
    		if(board[col+1][row+1]!= ('0'||'?'))
    			mineCounter++;	
    		if(board[col-1][row]!= ('0'||'?'))
    			mineCounter++;
    		if(board[col-1][row-1]!= ('0'||'?'))
    			mineCounter++;
    		if(board[col+1][row-1]!= ('0'||'?'))
    			mineCounter++;
    		if(board[col][row-1]!= ('0'||'?'))
    			mineCounter++;
    		return(mineCounter);
    }
    
    int changeMatrix(int x, int y)            //precond:receives rows and cols
    {										  //postcond:checks to see if inputed , 
    	if (board[x][y] == '?'){			  //cell is already a '?' needs to 
    		board[x][y]=board2[x][y];		  //have surrounding cells checked, 
    		return(0);}						  //makes a cell a '?',or displays 
    	if (x>=0 && y>=0){					  //"Boom" if location was a bomb. 
    		board[x][y]= checkMatrix(x,y);    //Returns 0 in allcases except 
    		return(0);}						  //last(to keep playing game).
    	if (x<0 || y<0){
    		board[x][y]= '?';
    		return(0);}
    	if(board[x][y]==1){
    		cout<<"BBBBBOOOOOOOOOOOOMMMMM!!!!!"<<endl;
    		return(1);}
    	
    }
    
    int manualBomb()						//precond:none
    {										//postcond:allows user manually input 
    	int x,y,infinite=0;					//cells that will have bombs in them
    	
    	cout<<"Enter the coordinates of the bombs one at a time"<<endl;
    	cout<<"Input '0 0' to end"<<endl;
    	do{									//users inputs places where bombs are
    		cin>>x;							//to be placed until '0 0' is entered.
    		cin>>y;							//The board is then modified to for bombs
    		if(x==0 && y==0)
    			break;
    		board[x-1][y-1]='*';
    		board2[x-1][y-1]='*';
    	}while(infinite==0);
    	
    	return(0);
    }
    
    int autoBomb()							//precond:none
    {										//postcond:receives an inputted 
    	int i, number=0, numbombsx10;		//% from user to see what % of 
    	double numbBombs;					//cells should have bombs. Then places
    										//bombs in randomly chosen cells
    	cout<<"What percentage of the spaces would you like to have a bomb?"<<endl;
    	cin>>numbBombs;
    	numbombsx10=numbBombs*10;
    	
    	for(int x=0;x<6;x++){				//At each cell, it is automatically 
    		for(int y=0;y<8;y++){			//calculated if a bomb should be placed
    			for(i=0;i<100;i++){			//and it is placed
    				number=rand()%101;
    				if(number<numbombsx10){
    					board[x][y]='*';
    					board2[x][y]='*';}
    			}
    		}
    	}
    	return(0);
    
    }
    
    int main()
    {
    	int rows,cols, bomb;
    	
    	cout<<"Welcome to the Terrorist Game."<<endl<<endl;
    	cout<<"Press 0 for random bomb placement"<<endl;
    	cout<<"Press 1 for manual bomb placement"<<endl;				
    	cin>>bomb;
    	if (bomb == 0)
    		autoBomb();
    	if (bomb == 1)
    		manualBomb();					//user inputs what method of bomb placement
    										//is wanted
    	printMatrix();
    
    	do{
    	cout<<"Enter a postition that you would like to add a car or think"<<endl;
    	cou<<"that there is a bomb..."<<endl;
    	cin>>rows>>cols;
    	cols--;
    	rows--;
    	if (changeMatrix(rows, cols)==1)
    		break;
    	printMatrix();
    	}while(1==1);
    
    	
    	
    	cout<<"I hope you had fun...."<<endl;
    	
    	return 0;
    
    }
    Any suggestions would be great. Laughing at em would be kinda funny too. Another long nite of C++, or C-- in my case....
    Last edited by sameintheend01; 02-18-2003 at 05:01 AM.

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Its printing out a heart or a spade because your checkmatrix returns an int and your board is of type char. So your board is converting the integer into a char and the chars 1-10 are happy faces/card suits etc.

    Change

    return(mineCounter);

    to

    return(mineCounter+'0');

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. Recreating Minesweeper.
    By j0hnb in forum C Programming
    Replies: 15
    Last Post: 02-18-2003, 07:12 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