Thread: Having problem with a program for Bingo

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    21

    Having problem with a program for Bingo

    Currently having problems with random number generator for the bingo program. It only gets one random number and it doesn't change each time it goes through the while loop. The if function doesn't follow the instruction of changing the cell of the number to asterisk when the random number and cell values match.
    Code:
    // Bingo main.c
    
    #include <stdio.h>
    #include "globals.h"
    #include "card.h"
    #include "randomnumber.h"
    #include "unusednumbers.h"
    #include <conio.h>
    char finitial, linitial, Player1;
    
    int main()
    {
        int iseed;
        printf(" Welcome to the game of Bingo\n");
        
        printf("Enter your initials\n");
        scanf(" %c %c", &finitial, &linitial);
        initRandom();
        newCard();
        Displaycard();
        int count;
        count = 0;
        while (i < 25)
        {
              srand (time (NULL) );
        iseed = rand() % 75 + 1;
        
        printf( "Calling first number :%d\n", iseed);
        if(iseed == MARKED_VAL)
        {
                 printf("  **  ");
                 i = i +1;
                 }
        if(iseed != MARKED_VAL)
        {
                 printf(" The number %d is not on the board\n", iseed);
                 i = i +1;
                 
    }
    
    }
        printf(" Thank you for joining us %c%c\n", finitial, linitial);
        system("Pause");
    }
    Code:
    //card.c
    #include<stdio.h>
    #define MARKED_VAL 0
    int bingoCard[NUMROWS][NUMCOLS];
    
    void newCard(void)
    {
         int nchoice = MULT;
         int row, col;
         int min, max;
         
         for (col = 0; col < NUMCOLS; col++)
         {
             min = (col * nchoice) + 1;
             max = (col + 1) * nchoice;
             
             for (row = 0; row < NUMROWS; row++)
             {
                 bingoCard[row][col] = myRandom(min, max);
             }
         }
         bingoCard[(row / 2)] [(col / 2)] = MARKED_VAL;
    }
                    
    void printCell(int cellValue)
    {
         printf("|");
         if (cellValue == MARKED_VAL)
         {
                       printf("  **  ");
         }
         else
         {
             printf("  %2d  ", cellValue);
         }              
    }
    
    void Displaycard(void)
    {
      int i, j;
      printf("\n Bingo Card for Player 1\n\n");
      printf("+------+------+------+------+------+\n");
      printf("|  B   |  I   |  N   |  G   |  O   |\n");
      printf("+------+------+------+------+------+\n");
      
      for (i = 0; i < NUMROWS; i++)
      {
          for (j = 0; j < NUMCOLS; j++)
          {
              printCell (bingoCard[i][j]);
              }
              printf("|\n+------+------+------+-------+------+\n");
    }
    }
    Code:
    //randomnumbers.c #include <stdlib.h>
    #include <time.h>
    
    void initRandom(void)
    {
         unsigned int iseed = (unsigned int) time(NULL);
         srand (iseed);
    }
         
    int myRandom(int low, int high)
    {
        return (low + rand() % (high - low + 1));
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You should move your myrandom function into the main C source page and dispense with the initRandom all as it doesn't gain you anything in terms of randomness... Try it like this...

    Code:
     srand (time (NULL) );  // only call this once per program run, right at the beginning.
    
    
     int MyRandom(int low, int high)
       { return (rand % (high - low)) + low;

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    21
    With that code placed in the main I get errors of invalid operands to binary %

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Magmadragoon View Post
    With that code placed in the main I get errors of invalid operands to binary %
    OOPS... my bad... should have been...


    Code:
    int MyRandom(int low, int high)
       { return (rand() % (high - low)) + low;}
    ...that's what I get for not proofreading more carefully and that's what you get for doing scoop and poop...

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    21

    Did more work on it

    With putting the random number generator I had linking errors due to code from card.c calling myRandom. So I just kept the way I had before. Still trying to find a way so I can call a random number, mark it on the board, then call the next numbers.

    Code:
    // Bingo main.c
    
    #include <stdio.h>
    #include "globals.h"
    #include "card.h"
    #include "randomnumber.h"
    #include "unusednumbers.h"
    #include "blockChars.h"
    #include <conio.h>
    #include <stdlib.h>
    #include <time.h>
    #define TRUE 1
    #define FALSE 0
    char finitial, linitial, Player1;
    
    int main()
    {
        initRandom();
    /*    srand (time (NULL) );
    
    
     int MyRandom(int low, int high)
       { 
                      return (rand() % (high - low)) + low;
       } */
        int iseed, j, randomn, low, high;
        printf(" Welcome to the game of Bingo\n");
        
        printf("Enter your initials\n");
        scanf(" %c %c", &finitial, &linitial);
        
        newCard();
        Displaycard();
        randomn = myRandom(low, high);
        printf(" Calling first number :%d\n", randomn);
       // printCell (bingoCard[i][j]);
       
           /*while (i < 25)
        {
              srand (time (NULL) );
        iseed = rand() % 75 + 1;
        
        printf( "Calling first number :%d\n", iseed);
        if(bingoCard[i][j] == iseed)
        {
                 printf("  **  ");
                 i = i +1;
                 }
        if(bingoCard[i][j] != iseed)
        {
                 printf(" The number %d is not on the board\n", iseed);
                 i = i +1;
        */
    
       
    
        printf(" Thank you for joining us %c%c\n", finitial, linitial);
        system("Pause");
    }
    Code:
    //card.c
    #include<stdio.h>
    #define MARKED_VAL 0
    int bingoCard[NUMROWS][NUMCOLS];
    
    void newCard(void)
    {
         int nchoice = MULT;
         int row, col;
         int min, max;
         
         for (col = 0; col < NUMCOLS; col++)
         {
             min = (col * nchoice) + 1;
             max = (col + 1) * nchoice;
             
             for (row = 0; row < NUMROWS; row++)
             {
                 bingoCard[row][col] = myRandom(min, max);
             }
         }
         bingoCard[(row / 2)] [(col / 2)] = MARKED_VAL;
    }
                    
    void printCell(int cellValue)
    {
         printf("|");
         if (cellValue == MARKED_VAL)
         {
                       printf("  **  ");
         }
         else
         {
             printf("  %2d  ", cellValue);
         }              
    }
    
    void Displaycard(void)
    {
      int i, j;
      printf("\n Bingo Card for Player 1\n\n");
      printf("+------+------+------+------+------+\n");
      printf("|  B   |  I   |  N   |  G   |  O   |\n");
      printf("+------+------+------+------+------+\n");
      
      for (i = 0; i < NUMROWS; i++)
      {
          for (j = 0; j < NUMCOLS; j++)
          {
              printCell (bingoCard[i][j]);
              }
              printf("|\n+------+------+------+-------+------+\n");
    }
    }
    
    int updateCell(void)
    {
    }
    
    int checkforWin()
    {
    }
    
    int checkRows()
    {
        int i, j;
        for ( i = 0; i < 5; i++)
    	  for( j = 0; j < 5; j++)
    		if (bingoCard[i][0] == bingoCard[i][j]) 
    		  return 1;
    }
    
    int checkColums()
    {
        int i, j;
        for( i = 0; i < 5; i++)
    	for ( j = 0; j < 5; j++)
    	   if (bingoCard[0][j] == bingoCard[i][j]) 
    		 return 1;
    }
    
    int checkDiagnols()
    {
         int i;
         for( i = 0; i < 5; i++)
    	
      if (bingoCard[0][0] == bingoCard[i][i]) 
    	   return 1;
    }

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    21
    I think the problem is with randomn = myRandom(low, high);

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Magmadragoon View Post
    I think the problem is with randomn = myRandom(low, high);
    Well... what error messages is your compiler giving you?

  8. #8
    Registered User
    Join Date
    Feb 2011
    Posts
    21
    I have no errors just when I run the program the random number is over a thousand when I want it to be between 1 and 75

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Magmadragoon View Post
    I have no errors just when I run the program the random number is over a thousand when I want it to be between 1 and 75
    Yeah you do have errors, and I'm betting lots of them... You've put the myRandom function I wrote inside your main function... You can't do that in C.

    This isn't hard...
    Code:
    #include <stdio.h>
    #include <time.h>
    
    int MyRandom(int low, int high)
      { return (rand() % (high - low)) + Low; }
    
    
    int main ( void )
      { int x; // random number
        
        srand(time(NULL));
    
        // lots of other code
    
        x = MyRandom(10,20);
    
        // diagnostic (Random should be between 10 and 20)
        printf("Random = %d\n",x);
    
        // and done
        return 0; }
    Go ahead, create a new project and test it...

  10. #10
    Registered User
    Join Date
    Feb 2011
    Posts
    21
    I see what I was doing I was initially putting the function MyRandom in the main function causing the error. Then for assigning Randomn I was calling low, high instead of setting the bounds for the program.

  11. #11
    Registered User
    Join Date
    Feb 2011
    Posts
    21
    When the random number value is the same as the one already on the Bingo card the number on the card doesn't change.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Magmadragoon View Post
    I see what I was doing I was initially putting the function MyRandom in the main function causing the error. Then for assigning Randomn I was calling low, high instead of setting the bounds for the program.
    Well, you can call it with variables named high and low or x and y or top and bottom or whatever you like... provided they're set to reasonable values before you make the call.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Magmadragoon View Post
    When the random number value is the same as the one already on the Bingo card the number on the card doesn't change.
    Ok... first thing... clean up your code. Now that you get how the random numbers work in C, you need to clean out all the other stuff you tried before you go much further.

    Moreover... this is not a big project... I don't see any reason to split it up into multiple files, moreover there is no logical separation. It's not like you're developing a Network module, a Messaging module and a GUI module here... it's all one thing. (FWIW, I have source pages here that are thousands of lines long.)

    Make your life easier and order your functions on the main .C file above main() so that nothing is called before it's seen, work out the logic problems... minimize the code as much as possible... and you're done.

  14. #14
    Registered User
    Join Date
    Feb 2011
    Posts
    21

    Almost near completion

    The instructor had us do the assignment with the multiple files. Just still can't figure out how to mark each called number with an asterisk on the card.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bingo with words
    By smitty007 in forum C Programming
    Replies: 19
    Last Post: 04-14-2009, 05:14 PM
  2. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  3. Bingo Card Program. Any input?
    By dukebdx12 in forum C++ Programming
    Replies: 5
    Last Post: 02-15-2008, 03:00 PM
  4. Math Equation Program (I can't find the problem with my program!)
    By masked_blueberr in forum C Programming
    Replies: 14
    Last Post: 07-06-2005, 11:53 AM