Minesweeper errors...

This is a discussion on Minesweeper errors... within the C Programming forums, part of the General Programming Boards category; I'm trying to recreate minesweeper in C and I am pretty sure that i have finished the program but i ...

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

    Minesweeper errors...

    I'm trying to recreate minesweeper in C and I am pretty sure that i have finished the program but i have 2 problems with it. The game runs but it will only let me inter the coordinates 2 time before it quits out and i'm not sure where it is quiting out from. The second problem is with the floodfill. It is ment to use recursion to uncover all the cells next to the one that have been entered then store them in the structure and print them. But for some reason it isn't working at all. I have the program set up for now to print the game board showing all the bombs first to I can see what i am doing untill I get these erros fixed. I have included my code and the compiled program so you can see these errors. Thank you for any help that you will be able to provide.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    struct ms{
    	int bombs;
    	int uncovered;
    	int printvalue;
    } field[9][9];
    
    void floodfill (struct ms field[][9], int, int);
    void winning (struct ms field[][9], int, int);
    void printing (struct ms field[][9], int, int);
    void cell_numbering (struct ms field[][9], int, int);
    void compair (struct ms field[][9], int, int);
    
    int main (void)
    {
    
    	int b,i,o;
    
    	srand(time(NULL));
    	for(b=0;b<10;b++){
    		i=rand()%9;
    		o=rand()%9;
    		if (field[i][o].bombs == 42)b--;
    		field[i][o].bombs = 42;
    			}
    
    	for (i=0;i<9;i++){
    		for(o=0;o<9;o++){
    			if (field[i][o].bombs != 42){
    				field[i][o].bombs = 48;}}
    	}
    
    cell_numbering(field, i, o);
    
    printf("\n\n\t*******************\n\t*   MineSweeper   *\n\t*******************\n"); 
    printf("\n\t  0 1 2 3 4 5 6 7 8\n\t  -----------------\n");
    	for(i=0;i<9;i++)printf("\t%d|%c %c %c %c %c %c %c %c %c|\n", i, field[i][0].bombs,field[i][1].bombs,field[i][2].bombs,field[i][3].bombs,field[i][4].bombs,field[i][5].bombs,field[i][6].bombs,field[i][7].bombs,field[i][8].bombs); 
    printf("\t  -----------------");
    printf("\n\nEnter Coordinates Values: ");
    scanf("%d %d", &i, &o);
    field[i][o].uncovered = 1;
    if (field[i][o].bombs == 42){printf("\nBOOM!!! Game Over, you hit a bomb.\n"); return 0;}
    	
    for(b=2; b != 1; b++){
    	compair(field, i, o);
    	printing(field, i, o);
    
    }
    
    return 0;
    	
    	}
    
    void cell_numbering (struct ms field[][9], int i, int o){
    
    		/*outer bonds, courners, 00, 80, 08, 88*/
    	if (field[0][0].bombs == 42){if (field[1][0].bombs != 42)field[1][0].bombs++; if (field[1][1].bombs != 42)field[1][1].bombs++; if (field[0][1].bombs != 42)field[0][1].bombs++;}
    	if (field[0][8].bombs == 42){if (field[0][7].bombs != 42)field[0][7].bombs++; if (field[1][7].bombs != 42)field[1][7].bombs++; if (field[1][8].bombs != 42)field[1][8].bombs++;}
    	if (field[8][0].bombs == 42){if (field[7][0].bombs != 42)field[7][0].bombs++; if (field[7][1].bombs != 42)field[7][1].bombs++; if (field[8][1].bombs != 42)field[8][1].bombs++;}
    	if (field[8][8].bombs == 42){if (field[7][7].bombs != 42)field[7][7].bombs++; if (field[7][8].bombs != 42)field[7][8].bombs++; if (field[8][7].bombs != 42)field[8][7].bombs++;}
    	
    	/*Sides left*/
    	for(i=1;i<8;i++){
    			if (field[i][0].bombs == 42){
    				if (field[i-1][0].bombs != 42)field[i-1][0].bombs++; 
    				if (field[i+1][0].bombs != 42)field[i+1][0].bombs++; 
    				if (field[i][1].bombs != 42)field[i][1].bombs++;
    				if (field[i+1][1].bombs != 42)field[i+1][1].bombs++; 
    				if (field[i-1][1].bombs != 42)field[i-1][1].bombs++;
    			}
    	}
    	/*Side Right*/
    	for(i=1;i<8;i++){
    		if (field[i][8].bombs == 42){
    			if (field[i-1][8].bombs != 42)field[i-1][8].bombs++; 
    			if (field[i+1][8].bombs != 42)field[i+1][8].bombs++; 
    			if (field[i][7].bombs != 42)field[i][7].bombs++;
    			if (field[i+1][7].bombs != 42)field[i+1][7].bombs++; 
    			if (field[i-1][7].bombs != 42)field[i-1][7].bombs++;
    		}
    	}
    	/*Top wall*/
    	for(o=1;o<8;o++){
    		if (field[0][o].bombs == 42){
    			if (field[0][o-1].bombs != 42)field[0][o-1].bombs++; 
    			if (field[0][o+1].bombs != 42)field[0][o+1].bombs++; 
    			if (field[1][o].bombs != 42)field[1][o].bombs++;
    			if (field[1][o-1].bombs != 42)field[1][o-1].bombs++; 
    			if (field[1][o+1].bombs != 42)field[1][o+1].bombs++;
    		}
    	}
    	/*Bottom wall*/
    	for(o=1;o<8;o++){
    		if (field[8][o].bombs == 42){
    			if (field[8][o-1].bombs != 42)field[8][o-1].bombs++; 
    			if (field[8][o+1].bombs != 42)field[8][o+1].bombs++; 
    			if (field[7][o].bombs != 42)field[7][o].bombs++;
    			if (field[7][o-1].bombs != 42)field[7][o-1].bombs++; 
    			if (field[7][o+1].bombs != 42)field[7][o+1].bombs++;
    		}
    	}
    	/*for i = 1-8 and o = 1-8, inner bonds*/
    	for (i=1;i<8;i++){
    		for (o=1;o<8;o++){
    			if (field[i][o].bombs == 42){
    					if (field[i-1][o].bombs != 42)field[i-1][o].bombs++;
    					if (field[i+1][o].bombs != 42)field[i+1][o].bombs++;
    					if (field[i-1][o+1].bombs != 42)field[i-1][o+1].bombs++;
    					if (field[i][o+1].bombs != 42)field[i][o+1].bombs++;
    					if (field[i+1][o+1].bombs != 42)field[i+1][o+1].bombs++;
    					if (field[i-1][o-1].bombs != 42)field[i-1][o-1].bombs++;
    					if (field[i][o-1].bombs != 42)field[i][o-1].bombs++;
    					if (field[i+1][o-1].bombs != 42)field[i+1][o-1].bombs++;
    				}
    			}
    		}
    
    	return;
    	}
    	
    void printing (struct ms field[][9], int i, int o){
    printf("\n\t  0 1 2 3 4 5 6 7 8\n\t  -----------------\n");
    	for(i=0;i<9;i++)printf("\t%d|%c %c %c %c %c %c %c %c %c|\n", i, field[i][0].printvalue,field[i][1].printvalue,field[i][2].printvalue,field[i][3].printvalue,field[i][4].printvalue,field[i][5].printvalue,field[i][6].printvalue,field[i][7].printvalue,field[i][8].printvalue); 
    printf("\t  -----------------");
    printf("\n\nEnter Coordinates Values: ");
    scanf("%d %d", &i, &o);
    field[i][o].uncovered = 1;
    if (field[i][o].bombs == 42){printf("\nBOOM!!! Game Over, you hit a bomb.\n"); exit (0);}
    	floodfill(field, i, o);
    	winning(field, i, o);
    }
    
    
    void floodfill (struct ms field[][9], int i, int o){
    		if((i<0) || (o<0) || (i>8) || (o>8))return;
    		
    	field[i][o].uncovered = 1;
     	
    	floodfill(field, i-1, o);
     	floodfill(field, i, o-1);
    	floodfill(field, i, o+1);
     	floodfill(field, i+1, o);
    			}
    
    
    void winning (struct ms field[][9], int i, int o){
    	int cnt;
    	
    	for(i=0;i<9;i++){
    		for(o=0;o<9;o++){
    			if(field[i][o].uncovered == 0)cnt++;
    		}
    	}
    
    	if(cnt == 10){printf("\nYou Win!!!");exit (0);}
    	}
    
    void compair (struct ms field[][9], int i, int o){
    
    /*compaire*/
    for(i=0;i<9;i++){
    	for(o=0;o<9;o++){
    		if (field[i][o].uncovered == 1)field[i][o].printvalue = field[i][o].bombs;
    		if(field[i][o].uncovered == 0)field[i][o].printvalue = 45;
    		if(field[i][o].printvalue == 48)field[i][o].printvalue = 32;
    			}
    		}
    }
    Attached Files Attached Files

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 04:11 PM
  2. Ten Errors
    By AverageSoftware in forum Contests Board
    Replies: 0
    Last Post: 07-20-2007, 11:50 AM
  3. Winsock compilation errors
    By jmd15 in forum Networking/Device Communication
    Replies: 2
    Last Post: 08-03-2005, 09:00 AM
  4. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  5. errors in class(urgent)
    By ayesha in forum C++ Programming
    Replies: 2
    Last Post: 11-10-2001, 06:51 PM

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