By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,608 Members | 3,801 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,608 IT Pros & Developers. It's quick & easy.

Clue game Woes : ( Random runtime hangs

xpun
P: 39
hey all
So I decided to be a little ambitious over the summer and create the game of clue in c. I figured it would give my self a GOOD refresher of the language and further understand functions arrays objects etc..

Any way, I encountered a strange logic error and kinda put it to the side until now.

This code is supposed to role the die, chose the killer and deal the cards to the players depending upon the # of player. However it only seems to work intermittently, it will hang when you enter the number of players one time, the next it will work.

This is obviously some inefficient code, but (what i assume to be ) logically sound.

In any event im scratching my head on this one
~John
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. #include <stdlib.h>    // need for srand() function
  4.  
  5. #include <time.h>    // need for time() function
  6. //Game Board
  7. //char Borad[60][24];    
  8.  
  9. //WEAPONS ARRAY
  10.  
  11. //10=Rope
  12. //20=Revolver    
  13. //30=Lead pipe    
  14. //40=candelstick
  15. //50=Knife
  16. //60=Wrench
  17.  
  18. //PEOPLE ARRAY
  19. //100=mr.green
  20. //200=ms.scarlet
  21. //300=Conl.Mustard
  22. //400=Prof.Plum
  23. //500=Mis.Peacock
  24. //600=Mis.White
  25.  
  26.  
  27. //PLACE ARRAY
  28. //1=Kitchen
  29. //2=Hall
  30. //3=Poolroom
  31. //4=Study
  32. //5=Loung
  33. //6=Library
  34. //7=Conservatory
  35. //8=Dining
  36. //9=Billiards
  37.  
  38. //KILLER ARRAY
  39.  
  40. int die=0;
  41. char roll ='n';
  42.  
  43. int i;
  44.  
  45.   int x;
  46. int p=0; 
  47. int noc;
  48. int q=0;
  49. int players[6][9]={0,0,0,0,0,0,0,0,0};
  50. int Allcrds[]={100,200,300,400,500,600,10,20,30,40,50,60,1,2,3,4,5,6,7,8,9};
  51. int Killer[]={0,0,0};
  52.  int shuf=0; 
  53.  
  54.  
  55. int main()
  56.  {
  57.  
  58.  
  59.               //number of players
  60.                //random number for establishing killer
  61.               // number in for loop to load array
  62.  
  63.  printf("CLUE\n");
  64.  printf("written by John Schintone\n"); 
  65.  printf("Origonal game delvoped by Hasbro\n");
  66.  printf("How many players are Going to play :\n");
  67.  int nop=0;
  68.  scanf("%d",&nop);
  69.  srand( time(NULL) );                //time seed to eliminate duplicate start patern
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  shuf = rand() % 6 + 0;
  76.  Killer[0]= Allcrds[shuf];
  77.  Allcrds[shuf]=0;
  78.  
  79.  shuf = rand () % 6 + 6;
  80.  Killer[1]= Allcrds[shuf];
  81.  Allcrds[shuf]=0;
  82.  
  83.  shuf = rand() % 9 + 12; 
  84.  Killer[2] = Allcrds[shuf];
  85.  Allcrds[shuf]=0;  
  86.  
  87.  
  88.  
  89.      if (nop == 6) 
  90.       {
  91.         noc = 3;
  92.          }
  93.     if (nop == 5)
  94.       {
  95.         noc = 4;
  96.     }
  97.       if (nop == 4)
  98.       {
  99.        noc = 5;
  100.       }
  101.    if (nop == 3)
  102.      {
  103.       noc = 6;
  104.       }
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  for ( i = 0 ; i < nop ; i++)
  111.  {  
  112.  
  113.    for (x = 0 ; x < noc ; x++)
  114.    {  
  115.  
  116.     //  if ((nop == 5) && q ==0 || 1 ) 
  117. //        {noc = 4;}
  118. //        else
  119. //    {noc =3;}
  120.  
  121.  
  122.      shuf = rand() % 20 + 0;
  123.    players[q][x] = Allcrds[shuf];
  124.     Allcrds[shuf] = 0;
  125.     while (players[q][x]==0)
  126.     {
  127.      shuf = rand() % 20 + 0; 
  128.      players[q][x]=Allcrds[shuf];
  129.       Allcrds[shuf] = 0;
  130.     }
  131.  
  132.   }
  133.   q++;
  134.  
  135.     }
  136.       p=0;
  137.         while (p < 21)
  138.   {
  139.   printf("array %d\n",Allcrds[p]);
  140. p++;
  141. }
  142.  
  143.  
  144.  
  145. //printf ("%d=shuf",shuf);           debug check for random number
  146.  
  147.  
  148.  
  149.  
  150.  
  151.    //test loaded killeraray
  152.    printf ("The Killer is\n");
  153. for (i = 0; i <3 ; i++)
  154. {
  155.  
  156.    printf ("%d\n",Killer[i]);
  157.  
  158. }
  159.  
  160. //test p1 array
  161.  
  162.  
  163.   printf ("\n");    
  164.   printf ("P2 \n");
  165.   printf ("%d\n",players[0][0]);
  166.   printf ("%d\n",players[0][1]);
  167.   printf ("%d\n",players[0][2]);
  168.   printf ("%d\n",players[0][3]);
  169.   printf ("%d\n",players[0][4]);
  170.   printf ("%d\n",players[0][5]);
  171.   printf ("%d\n",players[0][6]);
  172.   printf ("%d\n",players[0][7]);
  173.   printf ("%d\n",players[0][8]);
  174.  
  175.   printf ("\n");
  176.   printf ("P2 \n");
  177.   printf ("%d\n",players[1][0]);
  178.   printf ("%d\n",players[1][1]);
  179.   printf ("%d\n",players[1][2]);
  180.   printf ("%d\n",players[1][4]);
  181.   printf ("%d\n",players[1][5]);
  182.   printf ("%d\n",players[1][6]);
  183.   printf ("%d\n",players[1][7]);
  184.   printf ("%d\n",players[1][8]);
  185.  
  186.     printf ("\n");
  187.   printf ("P3 \n");
  188.   printf ("%d\n",players[2][0]);
  189.   printf ("%d\n",players[2][1]);
  190.   printf ("%d\n",players[2][2]);
  191.   printf ("%d\n",players[2][4]);
  192.   printf ("%d\n",players[2][5]);
  193.   printf ("%d\n",players[2][6]);
  194.   printf ("%d\n",players[2][7]);
  195.   printf ("%d\n",players[2][8]);
  196.  
  197.   printf ("\n");
  198.   printf ("P4 \n");
  199.   printf ("%d\n",players[3][0]);
  200.   printf ("%d\n",players[3][1]);
  201.   printf ("%d\n",players[3][2]);
  202.   printf ("%d\n",players[3][3]);
  203.   printf ("%d\n",players[3][4]);
  204.   printf ("%d\n",players[3][5]);
  205.   printf ("%d\n",players[3][6]);
  206.   printf ("%d\n",players[3][7]);
  207.   printf ("%d\n",players[3][8]);
  208.  
  209.   printf ("\n");
  210.   printf ("P5 \n");
  211.   printf ("%d\n",players[4][0]);
  212.   printf ("%d\n",players[4][1]);
  213.   printf ("%d\n",players[4][2]);
  214.   printf ("%d\n",players[4][3]);
  215.   printf ("%d\n",players[4][4]);
  216.   printf ("%d\n",players[4][5]);
  217.   printf ("%d\n",players[4][6]);
  218.   printf ("%d\n",players[4][7]);
  219.   printf ("%d\n",players[4][8]);
  220.  
  221.    printf ("\n");
  222.   printf ("P6 \n");
  223.   printf ("%d\n",players[5][0]);
  224.   printf ("%d\n",players[5][1]);
  225.   printf ("%d\n",players[5][2]);
  226.   printf ("%d\n",players[5][3]);
  227.   printf ("%d\n",players[5][4]);
  228.   printf ("%d\n",players[5][5]);
  229.   printf ("%d\n",players[5][6]);
  230.   printf ("%d\n",players[5][7]);
  231.   printf ("%d\n",players[5][8]);
  232.  
  233.  
  234.  
  235.  
  236.  
  237. //Roll the die//    
  238.  
  239.  
  240.  
  241. printf(" Type 'r' to roll the die \n");
  242.  
  243.     //some weird bug in here compleatly ignores read if only 1 scanf is present???
  244.     getchar();
  245.     //scanf("%c", &roll);
  246.  
  247.     scanf("%c", &roll);
  248.  
  249.     while (roll == 'r')
  250.     {
  251.     die = rand() % 6 + 1;
  252.  
  253.         printf ("Your Number Is :  %d \n", die);
  254.         roll='n';
  255.  
  256.         getchar ();
  257.         getchar ();
  258.     }
  259.  
  260.  
  261.  
  262.  
  263. }
Sep 15 '08 #1
Share this Question
Share on Google+
9 Replies


100+
P: 424
For starters:
  • Line 49: players has 6x9=54 elements, but you only initialize 9 of them.
  • Lines 75, 79, 83, 122, 127:
    Are you sure the value of shuf doesn't fall outsize the index range of Killer? You might add print the value of shuf to make sure it is behaves like you expect it to. I see you do this on line 156 but you should do it each time you change shuf.
    Also, don't you need to re-seed your random number generator to ensure that successive calls to rand() yield different numbers?
  • Lines 123, 165-231: values of players[][] are being used uninitialized for the reason explained above.
  • Line 244: What is the purpose of getchar() here? You have a scanf to read user input straight afterwards.
  • Line 256,257: Shouldn't this be scanf("%c", &roll); ? Otherwise the loop will always exit after one pass since roll will never be changed from 'n' after line 254.
Sep 15 '08 #2

xpun
P: 39
For starters:
  • Line 49: players has 6x9=54 elements, but you only initialize 9 of them.
  • Lines 75, 79, 83, 122, 127:
    Are you sure the value of shuf doesn't fall outsize the index range of Killer? You might add print the value of shuf to make sure it is behaves like you expect it to. I see you do this on line 156 but you should do it each time you change shuf.
    Also, don't you need to re-seed your random number generator to ensure that successive calls to rand() yield different numbers?
  • Lines 123, 165-231: values of players[][] are being used uninitialized for the reason explained above.
  • Line 244: What is the purpose of getchar() here? You have a scanf to read user input straight afterwards.
  • Line 256,257: Shouldn't this be scanf("%c", &roll); ? Otherwise the loop will always exit after one pass since roll will never be changed from 'n' after line 254.
i dont quite understand how the player array would be unitialized it should be 54
elements of "0". No ?

The killer code works fine, the index never falls out of range of killer because it is set. I am just grabbing items out of Allcards randomly.

shuf = rand() % 6 + 0;
Killer[0 ]= Allcrds[shuf];
Allcrds[shuf]=0;

shuf = rand () % 6 + 6;
Killer[1]= Allcrds[shuf];
Allcrds[shuf]=0;

shuf = rand() % 9 + 12;
Killer[2 ] = Allcrds[shuf];
Allcrds[shuf]=0;
Sep 15 '08 #3

boxfish
Expert 100+
P: 469
i dont quite understand how the player array would be unitialized it should be 54
elements of "0". No ?
Expand|Select|Wrap|Line Numbers
  1. int players[6][9]={0,0,0,0,0,0,0,0,0};
  2.  
No, this only initiallizes the first nine; count the zeros.
To initiallize all of them, you can do:
Expand|Select|Wrap|Line Numbers
  1. int players[6][9]={{0,0,0,0,0,0,0,0,0},
  2.                    {0,0,0,0,0,0,0,0,0},
  3.                    {0,0,0,0,0,0,0,0,0},
  4.                    {0,0,0,0,0,0,0,0,0},
  5.                    {0,0,0,0,0,0,0,0,0},
  6.                    {0,0,0,0,0,0,0,0,0}};
  7.  
But it would probably be better to do this with a loop.

As for killer, I don't have perfect vision, but I don't see it going out of range either.
Sep 15 '08 #4

boxfish
Expert 100+
P: 469
I'm running this code through the debugger, and the debugger's not making sense to me. It says:
q = 0
x = 3
players[0] = {0, 0, 0, 0, 0, 0, 0, 0, 0}
players[q] = {3, 0, 0, 0, 0, 0, 0, 0, 0}
players[q][x] = 60
How can players[0] not be the same as players[q] if q = 0? I'm using the Dev-C++ debugger. Does this make sense to anyone else?
Sep 15 '08 #5

xpun
P: 39
I'm running this code through the debugger, and the debugger's not making sense to me. It says:
q = 0
x = 3
players[0] = {0, 0, 0, 0, 0, 0, 0, 0, 0}
players[q] = {3, 0, 0, 0, 0, 0, 0, 0, 0}
players[q][x] = 60
How can players[0] not be the same as players[q] if q = 0? I'm using the Dev-C++ debugger. Does this make sense to anyone else?

Exactly my point , I've tryed the same thing, all I wind up with is a WTF expression on my face lol

by the way, my reply above wasn't meant to be satyrical
Sep 16 '08 #6

xpun
P: 39
bump ... help , it defies all reason
Sep 23 '08 #7

100+
P: 110
I love your program and the ideas you have used. I can tell this isn't a school project, but one you are doing for fun on your own. My favorite type of projects : )

Stepping through your code, you had a few mistakes when it comes to the language. The biggest for me was your variable naming. Try to make the names mildly lengthy. No variable, even in C should be 2 characters or less (unless you are dealing with processor registers).

xpun setup your array for players very well... For a huge multi-dimensional array, I would use a loop, but this is small enough xpun's method is a better way to initialize it.

I also like to see #define's when setting up data variables. #defines are C's way of doing enum's in C++ (I prefer #defines even in C++ over enum's in most cases).

As for your choice of compiler... I like dev-cpp over Visual C++ Express, such as you are using. But it has an outdated mingw compiler. This will cause you problems when opening and saving files (bug in the libraries/headers). But there is an easy fix.

Download Codeblocks ide. It's open source/gpl. Written in C++. It can download packages from dev-cpp repository. And it's a little more complicated. But it can use about any compiler imaginable.

Download the Codeblocks with MinGW included. Then in Dev-Cpp, go to Tools->Compiler and change the directories of gcc, g++, make, and gprof to the ones located in the Codeblocks' Mingw bin directory.

Dev-Cpp now fixed, and another ide to become familiar with : )
Sep 24 '08 #8

100+
P: 110
I don't mean to intrude on your code, but merely contribute. Seeing as you programmed this much so far, I am sure you aren't going to just copy & use this code (well, it's your code anyways). But if you decide to, please use any code I've added.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. #define WEAPON_ROPE        10
  7. #define WEAPON_REVOLVER    20
  8. #define WEAPON_LEADPIPE    30
  9. #define WEAPON_CANDLESTICK 40
  10. #define WEAPON_KNIFE       50
  11. #define WEAPON_WRENCH      60
  12.  
  13. #define PEOPLE_MRGREEN     100
  14. #define PEOPLE_MSSCARLET   200
  15. #define PEOPLE_CONLMUSTARD 300
  16. #define PEOPLE_PROFPLUM    400
  17. #define PEOPLE_MISPEACOCK  500
  18. #define PEOPLE_MISWHITE    600
  19.  
  20. #define PLACE_KITCHEN      1
  21. #define PLACE_HALL         2
  22. #define PLACE_POOLROOM     3
  23. #define PLACE_STUDY        4
  24. #define PLACE_LOUNG        5
  25. #define PLACE_LIBRARY      6
  26. #define PLACE_CONSERVATORY 7
  27. #define PLACE_DINING       8
  28. #define PLACE_BILLIARDS    9
  29.  
  30. int main()
  31. {
  32.     int die = 0;
  33.     int players[6][9] = {{0, 0, 0, 0, 0, 0},
  34.                          {0, 0, 0, 0, 0, 0},
  35.                          {0, 0, 0, 0, 0, 0},
  36.                          {0, 0, 0, 0, 0, 0},
  37.                          {0, 0, 0, 0, 0, 0},
  38.                          {0, 0, 0, 0, 0, 0},
  39.                          {0, 0, 0, 0, 0, 0},
  40.                          {0, 0, 0, 0, 0, 0},
  41.                          {0, 0, 0, 0, 0, 0}};
  42.  
  43.     int allCards[] = {WEAPON_ROPE, WEAPON_REVOLVER,
  44.                       WEAPON_LEADPIPE, WEAPON_CANDLESTICK,
  45.                       WEAPON_CANDLESTICK, WEAPON_KNIFE,
  46.                       WEAPON_WRENCH,
  47.                       PEOPLE_MRGREEN, PEOPLE_MSSCARLET,
  48.                       PEOPLE_CONLMUSTARD, PEOPLE_CONLMUSTARD,
  49.                       PEOPLE_PROFPLUM, PEOPLE_MISPEACOCK,
  50.                       PEOPLE_MISWHITE,
  51.                       PLACE_KITCHEN, PLACE_HALL,
  52.                       PLACE_POOLROOM, PLACE_STUDY,
  53.                       PLACE_LOUNG, PLACE_LIBRARY,
  54.                       PLACE_CONSERVATORY, PLACE_DINING,
  55.                       PLACE_BILLIARDS};
  56.     int deckSize = 23; // number of cards in allCards array
  57.  
  58.     int count;
  59.     for (count = 0; count < deckSize; ++count)
  60.     {
  61.         printf(", %d", allCards[count]);
  62.     } // End for
  63.  
  64.   // These three array's are so you can put a card back, if need be...
  65.     int weaponCards[] = {WEAPON_ROPE, WEAPON_REVOLVER,
  66.                          WEAPON_LEADPIPE, WEAPON_CANDLESTICK,
  67.                          WEAPON_CANDLESTICK, WEAPON_KNIFE,
  68.                          WEAPON_WRENCH};
  69.     int weaponDeckSize = 7;
  70.  
  71.  
  72.     int peopleCards[] = {PEOPLE_MRGREEN, PEOPLE_MSSCARLET,
  73.                          PEOPLE_CONLMUSTARD, PEOPLE_CONLMUSTARD,
  74.                          PEOPLE_PROFPLUM, PEOPLE_MISPEACOCK,
  75.                          PEOPLE_MISWHITE};
  76.     int peopleDeckSize = 7;
  77.  
  78.  
  79.     int placeCards[] = {PLACE_KITCHEN, PLACE_HALL,
  80.                         PLACE_POOLROOM, PLACE_STUDY,
  81.                         PLACE_LOUNG, PLACE_LIBRARY,
  82.                         PLACE_CONSERVATORY, PLACE_DINING,
  83.                         PLACE_BILLIARDS};
  84.     int placeDeckSize = 9;
  85.  
  86.     srand(clock()); // seed rand() using clock() which gives
  87.                     //   the current tick your processor is at...
  88.  
  89.     int killer[3]; // no need to initialize yet.  killer[0-2] will initialize
  90.  
  91.     int deckShuffle = rand() % weaponDeckSize; // picks one number out of the deck
  92.     killer[0] = weaponCards[deckShuffle];
  93.     allCards[deckShuffle] = 0; // Card drawn.  No longer exists in deck
  94.  
  95.     deckShuffle = rand() % peopleDeckSize; // picks another random card out of the deck
  96.     killer[1] = peopleCards[deckShuffle];
  97.     allCards[deckShuffle + weaponDeckSize] = 0; // Card drawn.  No longer exists in deck
  98.  
  99.     deckShuffle = rand() % placeDeckSize; // randomly picks the last card needed
  100.     killer[2] = placeCards[deckShuffle];
  101.     allCards[deckShuffle + weaponDeckSize + peopleDeckSize] = 0; // Card drawn.  No longer exists in deck
  102.  
  103.  
  104.     int numberOfCards = 0;
  105.     printf("CLUE\n");
  106.     printf("written by John Schintone\n");
  107.     printf("Origonal game delvoped by Hasbro\n");
  108.  
  109.     int numberOfPlayers = 0;
  110.     while ((numberOfPlayers < 3) || (numberOfPlayers > 6))
  111.     {
  112.         printf("How many players are Going to play :\n");
  113.         printf("[number] > ");
  114.         scanf("%d",&numberOfPlayers);
  115.  
  116.         // A very fast if statement which only uses integers/char's
  117.         switch(numberOfPlayers)
  118.         {
  119.             case 6:
  120.             {
  121.                 numberOfCards = 3;
  122.             } break;
  123.  
  124.             case 5:
  125.             {
  126.                 numberOfCards = 4;
  127.             } break;
  128.  
  129.             case 4:
  130.             {
  131.                 numberOfCards = 5;
  132.             } break;
  133.  
  134.             case 3:
  135.             {
  136.                 numberOfCards = 6;
  137.             } break;
  138.  
  139.             default:
  140.             {
  141.                 printf("You must enter a number between 3 and 6...\n");
  142.             } // End default
  143.         } // End switch
  144.     } // End while
  145.  
  146.     int index1, index2;
  147.     // Note: ++index1; is faster than index1++; and will almost always
  148.     //         produce better code (index1++ happens after this statement line.
  149.     //         ++index1 increments index1 before this statement line)
  150.     for (index1 = 0; index1 < numberOfPlayers; ++index1)
  151.     {
  152.         printf("Player %d", index1);
  153.         for (index2 = 0; index2 < numberOfCards; ++index2)
  154.         {
  155.             // Remember that allCards[deckShuffle] == 0 because we removed that
  156.             //   card ages ago...  works out well, just don't forget you did that : )
  157.             while (allCards[deckShuffle] == 0)
  158.             {
  159.                 deckShuffle = rand() % deckSize;
  160.             } // End while
  161.  
  162.             players[index1][index2] = allCards[deckShuffle];
  163.             allCards[deckShuffle] = 0; // Card removed for after loop...
  164.  
  165.             printf(", %d", players[index1][index2]);
  166.  
  167.             switch(players[index1][index2])
  168.             {
  169.                 case WEAPON_ROPE:
  170.                 {
  171.                 } break;
  172.  
  173.                 // Add more...
  174.  
  175.                 case PEOPLE_MRGREEN:
  176.                 {
  177.                 } break;
  178.  
  179.                 // Add more...
  180.  
  181.                 case PLACE_KITCHEN:
  182.                 {
  183.                 } break;
  184.  
  185.                 // Add more...
  186.  
  187.                 default:
  188.                 {
  189.                     printf("Program has caught player %d cheating...", index1);
  190.                 } // End default
  191.             } // End switch
  192.         } // End for
  193.  
  194.         printf("\n");
  195.     } // End for
  196.  
  197.     printf("The killer is %d, with the %d, and in the %d \n\n", killer[0], killer[1], killer[2]);
  198.  
  199.     printf("Type h for this help... \n");
  200.     printf("Type e to escape... \n");
  201.     printf("Type r to roll the die... \n");
  202.  
  203.     char command = '\0';  // \0 represents zero, or the null character
  204.     while (command != 'e')
  205.     {
  206.         printf("[one character] > ");
  207.         scanf("%c", &command);
  208.  
  209.         if (command == 'r')
  210.         {
  211.  
  212.           die = rand() % 6 + 1;
  213.  
  214.           printf("Your number is: %d \n", die);
  215.         } // end while
  216.  
  217.         if (command == 'h')
  218.         {
  219.             printf("Type h for this help... \n");
  220.             printf("Type e to escape... \n");
  221.             printf("Type r to roll the die... \n");
  222.         } // End if
  223.  
  224.         printf("\n");
  225.     } // End while
  226.  
  227.     return(0); // Success.  Program worked ok
  228. } // End main() Function
  229.  
  230.  
And the formatted code (ClueGame.c and ClueGame.cbp) is at ClueGame.

The .cbp file is the project file for codeblocks. And codeblocks can open dev-cpp projects just as easily : )

Hopefully helpful,

TamusJRoyce
Sep 24 '08 #9

xpun
P: 39
WOW, thanks very much for all the advice and positive comments, looks like i have allot to read up on. I just started up school again so I'm gonna have to work on this during the weekends when free. I actually first wrote this code in XCode on my mac when I encountered all the problems. Hopefully i can start this coming weekend. your code is commented very well i can actually follow what your doing!!

Thanks again
ill get back to you soon

John Schintone
Sep 30 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.