Thread: inputting and checking

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    10

    inputting and checking

    Basically i'm looking for how you can input two numbers which are then checked against a grid. if there is a mine in the co-ordinates you entered (3,3) for example the program ends. if not it continues and you type another two numbers and so on. here's what i have so far

    Code:
    #include <stdio.h>
    char grid[12][12];
    void display();
    void intialise();
    void place_1(int x1,int y1, char c);
    main()
    {
        printf("Welcome To Minesweeper \n");
        intialise();
        place_1(11,11,'x'); 
        printf("Enter 2 numbers: \n");
        scanf("%d, %d");
        system("PAUSE");
    }
    
    void display()
    {
        int x,y;
        printf("\n");
        for (x=0;x<12;x=x+1)
        {
            for (y=0;y<12;y++)
            {
                    printf("%c ",grid[x][y]);
            }
            printf("\n");
        }
    }
    
    void intialise()
    {
        int x,y;
        for (x=0;x<12;x=x+1)
        {
            for (y=0;y<12;y++)
            {
                    grid[x][y]='o';
            }
        }
    }
    void place_1(int x1,int y1, char c)
    {
        grid[x1][y1]=c;
        display();
    }
    can anyone help?

    I don't need anythingwriting i just need to know how to get the scanf fuction and that working really. basically if you type:

    12
    12

    and there is no mine there (as previously set) then it will continue (i can add a message) but if there is a mine there then it will say game over and exit when you press any key.

    If anyone can help or give me a few pointers please let me know.
    cheers

  2. #2
    Registered User
    Join Date
    Apr 2004
    Posts
    11
    hope this helps

    if you have any questions, shoot.. (my C is better than my English so, if i was unclear..)

    Code:
    #include <stdio.h>
    
    #define MAX 12
    #define FALSE 0
    #define TRUE 1
    
    void display();
    void intialise();
    void placeMine(int x1,int y1, char c);
    
    int grid[MAX][MAX];			/* FALSE: no mine, TRUE: mine */
    int visible[MAX][MAX];			/* true when 'clicked' */
    
    int main(void)
    {
        printf("Welcome To Minesweeper \n");
    
        int x,y;		                /* user coords.. */
    
        intialise();			
    
        placeMine(11,11,'x'); 		/* this should become a minerandomiser */
    
        printf("Enter 2 numbers: \n");
        scanf("%d %d", &x, &y);
    
        visible[x][y] = TRUE;		/* 'clicked' on (x,y) */
    
        if(grid[x][y])		        /* if mine */
    	printf("Game Over\n");
        else  
            display();				/* display grid */
    
        system("PAUSE");
    
        return 0;
    }
    
    void display()
    {
        int i, j;
    
        printf("\n");
    
        for (i = 0; i < MAX; i++) {
           for (j = 0; j < MAX; j++) 
               if(!visible[i][j])
    		printf("0");		
    	   else if(grid[i][j])			/* if visible and mine */
    		printf("X");
    	   else 
    		printf("_");
    		/* write a function that calculates how many mines are next to mine[i][j], display this number (only when visible) */
            
           
           printf("\n");
        }
    }
    
    void intialise()
    {
        int i, j;
    
        for (i = 0; i < 12; i++)
            for (j = 0; j < 12; j++){
               grid[i][j] = FALSE;			/* you should write a function that randomises the mines,.. */
    	   visible[i][j] = FALSE;               
            }
    }
    
    void placeMine(int x,int y, char c)		/* this should become the randomise mine function */
    {
        grid[x][y] = TRUE;			        /* mine is placed on (x,y) */
    }

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>i'm looking for how you can input two numbers
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    10
    Hi thanks zuiplap but maybe i am doing something wrong as i can't get that to work

    before you posted that code i was thinking maybe i could do something with If statments.

    so if i define where the 10 mines are to be to start with say grid's (1,1) (3,4) etc...

    then use if statments.

    if 1,1 then game over
    else if

    but i don't really think that would work as the if stament would be massive and i don't know about C as i've not been doing it that long but i know with excel nested if statments were a nightmate.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    users_guess_x = 2;
    users_guess_y = 5;
    
    if (grid[users_guess_x][users_guess_y] == BOMB)
    { 
      You're dead
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    10
    It's ok i got the original code working.

    I've changed it a bit but am struggaling on trying to put a loop in. so you have to keep guessing till you hit a mine. I've managed to get it to ask you a few times but if you hit a mine it still try's to continue. I've added another mine so i could try testing it and it worked.

    Whats the best way to make it continue till you hit a mine?

    I tried adding another system function in with an if statment so that if it did hit a mine in it would exit but i could not seem to get this to work. any ideas?

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    post the relevant bits of code you're having trouble with.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    May 2004
    Posts
    10
    basically i'm trying to add a loop below this bit of code

    Code:
      intialise();			
    
        placeMine(11,11,'x'); 		
        placeMine(10,11,'x'); 
    
        printf("Enter 2 numbers: \n");
        scanf("%d %d", &x, &y);
    
        visible[x][y] = TRUE;		/* 'clicked' on (x,y) */
    
        if(grid[x][y])		        /* if mine */
    	printf("Sorry You Hit a Mine Game Over\n");
        else
            display();				/* display grid */
    so that if you don't hit a mine it will ask you for another go, but if you do hit a mine you won't get another go.

    although i've been messin about with it an i'm not sure if a loops the best way to go

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Conceptually:
    Code:
     TotalMines = 10;
     
     While TotalMines > 0
       GetUsersInput
       if (grid[x][y] == MINE)
          TotalMines--;
     End While
    There's a lot more to it, the actual design/implementation will be up to you.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User
    Join Date
    May 2004
    Posts
    10
    yeah cheers.

    I know there's quite a bit to do i just wanted to get the basic concept so i can play about with it abit thanks for your help

  11. #11
    Registered User
    Join Date
    May 2004
    Posts
    10
    Code:
       intialise();			
    
        placeMine(11,11,'.'); 		/*Where a Mine Is placed*/
        placeMine(10,11,'.');     /*Where a Mine Is placed*/
    
        printf("Enter 2 numbers: \n"); /*Text Displayed*/
        scanf("%d %d", &x, &y);        /*Get Numbers*/
    
        visible[x][y] = TRUE;		/* 'clicked' on (x,y) */
    
        if(grid[x][y])            /* if mine */   
    	printf("Sorry You Hit a Mine Game Over\n");  /*Text Displayed*/
        else 
        display();				/* display grid */
         printf("Well done You now have 10 points Try again\n");
          printf("\n");
          
          /*User's Second Attempt at Finding a Mine*/
          
             printf("Enter 2 numbers: \n"); /*Text Displayed*/
        scanf("%d %d", &x, &y);        /*Get Numbers*/
    
        visible[x][y] = TRUE;		/* 'clicked' on (x,y) */
    
        if(grid[x][y])            /* if mine */   
    	printf("Sorry You Hit a Mine Game Over\n");  /*Text Displayed*/
        else 
        display();				/* display grid */
         printf("Well done You now have 10 points Try again\n");
          printf("\n");
           
       system("PAUSE");
    
        return 0;
    
    }
    I've got it so it ask's you again if you do infact not hit a mine. But how can i get it so if you DO hit a mine it displays the grid showing you where you hit the mine? IT works for if you don't hit a mine but i can't seem to get it to work for when you DO hit a mine. It also needs to end once you hit a mine and not continue like it does now. I've tried end if and i can't seem to get it to work

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Where is your big loop? You need to encase elements of your code in a loop (either "for" or "while" for example), a bit like my sample code above.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed