Thread: Problem with code

  1. #1
    Registered User
    Join Date
    Apr 2015
    Location
    Maceió, Brazil
    Posts
    4

    Problem with code

    Hi, I started taking programming classes this year, and I have to program a minesweeper game for a project due tomorrow. Here is the code.

    Code:
    #include <stdio.h>
    #define DIM 9
    
    
    int menu();
    int start(int jogo[][DIM], char num[][DIM]);
    void whoosh();
    void set(int jogo[][DIM]);
    void show(int jogo[][DIM], char num[][DIM]);
    
    
    main()
    {
        int startpoint;
        int jogo[DIM][DIM] = {{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0}};
        char num[DIM][DIM] = {{'0', '1', '1', '2', 'X', '2', '1', '1', '0'}, {'1', '2', 'X', '2', '1', '2', 'X', '2', '1'}, {'X', '2', '1', '1', '0', '1', '1', '2', 'X'}, {'1', '1', '0', '0', '0','0', '0', '1', '1'}, {'1', '1', '2', '1', '1', '0', '0', '0', '0'}, {'1', 'X', '2', 'X', '2', '1', '1', '0', '0'}, {'1', '1', '2', '1', '3', 'X', '2', '0', '0'}, {'0', '1', '1', '1', '2', 'X', '2', '0', '0'}, {'0', '1', 'X', '1', '1', '1', '1', '0', '0'}};
        do
        {
    
    
      startpoint = menu();
       if(startpoint == 1)
        start(jogo, num);
        }
        while(startpoint!=2);
        return 0;
    
    
    }
    
    
    int menu()
    {
        int valor;
        printf("\tCAMPO MINADO\n\nDigite um numero:\n1: JOGAR\n2: SAIR\n\n");
        scanf("%d", &valor);
        switch(valor)
    {
    case 1:
    case 2:
        break;
        default:
            whoosh;
            printf("Numero invalido\n");
    }
        return valor;
    }
    
    
    start(int jogo[][DIM], char num[][DIM])
    {
        int x, y;
        int gameOver = 0;
        while(gameOver==0)
        {
    set(jogo);
    whoosh();
        show(jogo, num);
         do
    {
        printf("Linha: ");
        scanf("%d", &x);
        printf("Coluna: ");
        scanf("%d", &y);
        if(x<1 || y<1 || x>9 || y>9)
        printf("Posicao invalida\n\n");}
        while(x<1 || y<1 || x>9 || y>9);
    
    
    }
    jogo[x--][y--]=1;
    if (gameOver==1)
        {printf("Voce ganhou!");
    return 0;}
    else
    {
        printf("Voce perdeu");
        return 0;
    }
    }
    void whoosh()
    {
        int i;
        for (i=0;i<100;i++)
            putchar('\n');
    }
    
    
    void set(int jogo[][DIM])
    {
        int x, y;
        for(x=0;x<DIM;x++)
            for(y=0;y<DIM;y++)
            jogo[x][y] = 0;
    }
    
    
    void show(int jogo[DIM][DIM], char num[DIM][DIM])
    {
        int x, y;
        printf("-------------------\n");
        for(x=0;x<DIM;x++)
         {for(y=0;y<DIM;y++)
         {
             if (y==DIM-DIM)
             printf("|");
             if(jogo[x][y]==0)
                printf(" ");
             else
                printf("%c", num[x][y]);
                printf("|");
             if(y==DIM-1)
                printf("\n-------------------\n");}
    
    
         }
         putchar('\n');
    }
    The printed text is in Portuguese because it's my native language, but the point is after the player inputs the coordinates, their value is supposed to change from 0 to 1 so the squares open when the show function runs, but none of the contents of the squares are showing, so I think the values aren't changing. What am I doing wrong?

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Before I would even test out the code, let me make some observations:

    • Your formating is atrocious!!! Please reformat your code before we comment further
    • Please put braces around the code for all your if(), else(), for(), etc... even if there is only one line of code for the loop or control statement. I can see at least one possible problem caused by this.
    • if your functions don't take parameters, including main, declare/define them as void, "void whoosh(void)", "int main(void)", etc...
    • PLEASE check the return value of scanf() ti insure that it got the value entered correctly!
    • If not done, turn up your warning level to high
    • When all the above is done, re-compile and check any errors or warnings


    Then if you still have problems, come back to us with specific questions.

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by Daniel990 View Post
    Okay, I put the braces and defined the functions as void, how do I check for the return value of scanf() though?
    edit: Nvm, I did it, I'll post again soon about whether there's problems.
    How do you check the return value of any function call?

    What possible values does the scanf() return?

    If you are on Linux. run the following command:
    Code:
    $ man scanf
    Or else check it out online here.

  4. #4
    Registered User
    Join Date
    Apr 2015
    Location
    Maceió, Brazil
    Posts
    4
    Alright, I tried your observations and got rid of warnings after setting to high.
    The code looks like this now:

    Code:
    #include <stdio.h>#define DIM 9
    
    
    int menu(void);
    int start(int jogo[][DIM], char num[][DIM]);
    void whoosh(void);
    void abracadabra(int jogo[][DIM]);
    void sinsalabim(int jogo[][DIM], char num[][DIM]);
    
    
    int main(void)
    {
        int startpoint;
        int jogo[DIM][DIM] = {{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0}};
        char num[DIM][DIM] = {{'0', '1', '1', '2', 'X', '2', '1', '1', '0'}, {'1', '2', 'X', '2', '1', '2', 'X', '2', '1'}, {'X', '2', '1', '1', '0', '1', '1', '2', 'X'}, {'1', '1', '0', '0', '0','0', '0', '1', '1'}, {'1', '1', '2', '1', '1', '0', '0', '0', '0'}, {'1', 'X', '2', 'X', '2', '1', '1', '0', '0'}, {'1', '1', '2', '1', '3', 'X', '2', '0', '0'}, {'0', '1', '1', '1', '2', 'X', '2', '0', '0'}, {'0', '1', 'X', '1', '1', '1', '1', '0', '0'}};
        do
        {
      startpoint = menu();
       if(startpoint == 1)
        {start(jogo, num);}
        }
        while(startpoint!=2);
        return 0;
    }
    
    
    int menu(void)
    {
        int valor;
        printf("\tCAMPO MINADO\n\nDigite um numero:\n1: JOGAR\n2: SAIR\n\n");
        scanf("%d", &valor);
        switch(valor)
    {
    case 1:
    case 2:
        break;
        default:
            whoosh();
            printf("Numero invalido\n");
    }
        return valor;
    }
    
    
    int start(int jogo[][DIM], char num[][DIM])
    {
        int x, y;
        int gameOver = 0;
        while(gameOver==0)
        {
    abracadabra(jogo);
    whoosh();
        sinsalabim(jogo, num);
         do
    {
        printf("Linha: ");
        scanf("%d", &x);
        printf("Coluna: ");
        scanf("%d", &y);
        if(x<1 || y<1 || x>9 || y>9)
        printf("Posicao invalida\n\n");}
        while(x<1 || y<1 || x>9 || y>9);
    
    }
    jogo[x--][y--]=1;
    if (gameOver==1)
        {printf("Voce ganhou!");
    return 0;}
    else
    {
        printf("Voce perdeu");
        return 0;
    }
    }
    
    
    void whoosh(void)
    {
        int i;
        for (i=0;i<100;i++)
            {putchar('\n');}
    }
    
    
    void abracadabra(int jogo[][DIM])
    {
        int x, y;
        for(x=0;x<DIM;x++)
            {for(y=0;y<DIM;y++)
            {jogo[x][y] = 0;}}
    }
    
    
    void sinsalabim(int jogo[DIM][DIM], char num[DIM][DIM])
    {
        int x, y;
        printf("-------------------\n");
        for(x=0;x<DIM;x++)
         {for(y=0;y<DIM;y++)
         {
             if (y==DIM-DIM)
             {printf("|");}
             if(jogo[x][y]==0)
                {printf(" ");}
             else
                {printf("%c", num[x][y]);}
                printf("|");
             if(y==DIM-1)
                {printf("\n-------------------\n");}}
    }
         putchar('\n');
    }
    The scanf() functions are returning the correct values.

    The program still has the same problem, though, the jogo array coordinates aren't changing values to 1 and the numbers aren't showing inside the squares because of that.
    Last edited by Daniel990; 04-26-2015 at 07:27 PM.

  5. #5
    Registered User
    Join Date
    Apr 2015
    Location
    Maceió, Brazil
    Posts
    4
    Bump because I gotta finish this quick

  6. #6
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    As far as i see, the function 'abracadabra' close all boxes.
    You call this function on line 51 and this is inside the while loop.
    So, if you input a row and column, this function reset the field.
    Put this function outside the while loop.

    Second, the change of jogo on line 65 is outside of while loop.
    This should be inside. Move it up to line 63.

    And last, i don't see a contition that set gameOver to 1 or another value.
    You will end up in an infinity loop.
    Other have classes, we are class

  7. #7
    Registered User
    Join Date
    Apr 2015
    Location
    Maceió, Brazil
    Posts
    4
    Yeah, the thing with the gameOver integer isn't completed because I was focusing on making the numbers appear for the moment.
    Doing what you said works, though, thank you!

  8. #8
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    I am glad you have worked out the bugs in your program. For the record, The code below is one example of what I consider a properly formated source code file. Please compare this to what you posted.

    Properly formatting your code helps both you when you are both writing and debugging, and us here when we review your code.

    There are several different styles of formating you can choose from. You can see some of the different styles here. I myself prefer the "Allman style", but you should choose one for yourself and stick with it.

    Your editor may have settings to help format the code as you type. I myself use emacs and it has many options to control the indent style.
    Code:
    #include <stdio.h>
    #define DIM 9
    
    int menu(void);
    int start(int jogo[][DIM], char num[][DIM]);
    void whoosh(void);
    void abracadabra(int jogo[][DIM]);
    void sinsalabim(int jogo[][DIM], char num[][DIM]);
    
    
    int main(void)
    {
       int startpoint;
       int jogo[DIM][DIM] = {{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},
                             {0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},
                             {0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0}};
       char num[DIM][DIM] = {{'0', '1', '1', '2', 'X', '2', '1', '1', '0'},
                             {'1', '2', 'X', '2', '1', '2', 'X', '2', '1'},
                             {'X', '2', '1', '1', '0', '1', '1', '2', 'X'},
                             {'1', '1', '0', '0', '0', '0', '0', '1', '1'},
                             {'1', '1', '2', '1', '1', '0', '0', '0', '0'},
                             {'1', 'X', '2', 'X', '2', '1', '1', '0', '0'},
                             {'1', '1', '2', '1', '3', 'X', '2', '0', '0'},
                             {'0', '1', '1', '1', '2', 'X', '2', '0', '0'},
                             {'0', '1', 'X', '1', '1', '1', '1', '0', '0'}};
       do
       {
          startpoint = menu();
          if(startpoint == 1)
          {
             start(jogo, num);
          }
       }
       while(startpoint!=2);
       return 0;
    }
    
    
    int menu(void)
    {
       int valor;
       printf("\tCAMPO MINADO\n\nDigite um numero:\n1: JOGAR\n2: SAIR\n\n");
       scanf("%d", &valor);
       switch(valor)
       {
          case 1:
          case 2:
             break;
          default:
             whoosh();
             printf("Numero invalido\n");
       }
       return valor;
    }
    
    
    int start(int jogo[][DIM], char num[][DIM])
    {
       int x, y;
       int gameOver = 0;
       while(gameOver==0)
       {
          abracadabra(jogo);
          whoosh();
          sinsalabim(jogo, num);
          do
          {
             printf("Linha: ");
             scanf("%d", &x);
             printf("Coluna: ");
             scanf("%d", &y);
             if(x<1 || y<1 || x>9 || y>9)
             {
                printf("Posicao invalida\n\n");
             }
          }
          while(x<1 || y<1 || x>9 || y>9);
       }
       jogo[x--][y--]=1;
       if (gameOver==1)
       {
          printf("Voce ganhou!");
          return 0;
       }
       else
       {
          printf("Voce perdeu");
          return 0;
       }
    }
    
    
    void whoosh(void)
    {
       int i;
       for (i=0;i<100;i++)
       {
          putchar('\n');
       }
    }
    
    
    void abracadabra(int jogo[][DIM])
    {
       int x, y;
       for(x=0;x<DIM;x++)
       {
          for(y=0;y<DIM;y++)
          {
             jogo[x][y] = 0;
          }
       }
    }
    
    
    void sinsalabim(int jogo[DIM][DIM], char num[DIM][DIM])
    {
       int x, y;
       printf("-------------------\n");
       for(x=0;x<DIM;x++)
       {
          for(y=0;y<DIM;y++)
          {
             if (y==DIM-DIM)
             {
                printf("|");
             }
             if(jogo[x][y]==0)
             {
                printf(" ");
             }
             else
             {
                printf("%c", num[x][y]);
             }
             printf("|");
             if(y==DIM-1)
             {
                printf("\n-------------------\n");
             }
          }
       }
       putchar('\n');
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code problem or compiler problem....?
    By miloki in forum C Programming
    Replies: 4
    Last Post: 03-05-2015, 12:48 AM
  2. Having a problem with my code :(
    By vivec45 in forum C++ Programming
    Replies: 4
    Last Post: 02-09-2011, 06:06 AM
  3. problem in this code?
    By wise_ron in forum C Programming
    Replies: 9
    Last Post: 10-09-2006, 12:08 PM
  4. Problem With Code
    By HAssan in forum C Programming
    Replies: 1
    Last Post: 02-04-2006, 05:36 PM
  5. Code problem
    By dot_rain in forum C Programming
    Replies: 1
    Last Post: 11-21-2003, 04:30 PM