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 - #include <stdio.h>
-
-
#include <stdlib.h> // need for srand() function
-
-
#include <time.h> // need for time() function
-
//Game Board
-
//char Borad[60][24];
-
-
//WEAPONS ARRAY
-
-
//10=Rope
-
//20=Revolver
-
//30=Lead pipe
-
//40=candelstick
-
//50=Knife
-
//60=Wrench
-
-
//PEOPLE ARRAY
-
//100=mr.green
-
//200=ms.scarlet
-
//300=Conl.Mustard
-
//400=Prof.Plum
-
//500=Mis.Peacock
-
//600=Mis.White
-
-
-
//PLACE ARRAY
-
//1=Kitchen
-
//2=Hall
-
//3=Poolroom
-
//4=Study
-
//5=Loung
-
//6=Library
-
//7=Conservatory
-
//8=Dining
-
//9=Billiards
-
-
//KILLER ARRAY
-
-
int die=0;
-
char roll ='n';
-
-
int i;
-
-
int x;
-
int p=0;
-
int noc;
-
int q=0;
-
int players[6][9]={0,0,0,0,0,0,0,0,0};
-
int Allcrds[]={100,200,300,400,500,600,10,20,30,40,50,60,1,2,3,4,5,6,7,8,9};
-
int Killer[]={0,0,0};
-
int shuf=0;
-
-
-
int main()
-
{
-
-
-
//number of players
-
//random number for establishing killer
-
// number in for loop to load array
-
-
printf("CLUE\n");
-
printf("written by John Schintone\n");
-
printf("Origonal game delvoped by Hasbro\n");
-
printf("How many players are Going to play :\n");
-
int nop=0;
-
scanf("%d",&nop);
-
srand( time(NULL) ); //time seed to eliminate duplicate start patern
-
-
-
-
-
-
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;
-
-
-
-
if (nop == 6)
-
{
-
noc = 3;
-
}
-
if (nop == 5)
-
{
-
noc = 4;
-
}
-
if (nop == 4)
-
{
-
noc = 5;
-
}
-
if (nop == 3)
-
{
-
noc = 6;
-
}
-
-
-
-
-
-
for ( i = 0 ; i < nop ; i++)
-
{
-
-
for (x = 0 ; x < noc ; x++)
-
{
-
-
// if ((nop == 5) && q ==0 || 1 )
-
// {noc = 4;}
-
// else
-
// {noc =3;}
-
-
-
shuf = rand() % 20 + 0;
-
players[q][x] = Allcrds[shuf];
-
Allcrds[shuf] = 0;
-
while (players[q][x]==0)
-
{
-
shuf = rand() % 20 + 0;
-
players[q][x]=Allcrds[shuf];
-
Allcrds[shuf] = 0;
-
}
-
-
}
-
q++;
-
-
}
-
p=0;
-
while (p < 21)
-
{
-
printf("array %d\n",Allcrds[p]);
-
p++;
-
}
-
-
-
-
//printf ("%d=shuf",shuf); debug check for random number
-
-
-
-
-
-
//test loaded killeraray
-
printf ("The Killer is\n");
-
for (i = 0; i <3 ; i++)
-
{
-
-
printf ("%d\n",Killer[i]);
-
-
}
-
-
//test p1 array
-
-
-
printf ("\n");
-
printf ("P2 \n");
-
printf ("%d\n",players[0][0]);
-
printf ("%d\n",players[0][1]);
-
printf ("%d\n",players[0][2]);
-
printf ("%d\n",players[0][3]);
-
printf ("%d\n",players[0][4]);
-
printf ("%d\n",players[0][5]);
-
printf ("%d\n",players[0][6]);
-
printf ("%d\n",players[0][7]);
-
printf ("%d\n",players[0][8]);
-
-
printf ("\n");
-
printf ("P2 \n");
-
printf ("%d\n",players[1][0]);
-
printf ("%d\n",players[1][1]);
-
printf ("%d\n",players[1][2]);
-
printf ("%d\n",players[1][4]);
-
printf ("%d\n",players[1][5]);
-
printf ("%d\n",players[1][6]);
-
printf ("%d\n",players[1][7]);
-
printf ("%d\n",players[1][8]);
-
-
printf ("\n");
-
printf ("P3 \n");
-
printf ("%d\n",players[2][0]);
-
printf ("%d\n",players[2][1]);
-
printf ("%d\n",players[2][2]);
-
printf ("%d\n",players[2][4]);
-
printf ("%d\n",players[2][5]);
-
printf ("%d\n",players[2][6]);
-
printf ("%d\n",players[2][7]);
-
printf ("%d\n",players[2][8]);
-
-
printf ("\n");
-
printf ("P4 \n");
-
printf ("%d\n",players[3][0]);
-
printf ("%d\n",players[3][1]);
-
printf ("%d\n",players[3][2]);
-
printf ("%d\n",players[3][3]);
-
printf ("%d\n",players[3][4]);
-
printf ("%d\n",players[3][5]);
-
printf ("%d\n",players[3][6]);
-
printf ("%d\n",players[3][7]);
-
printf ("%d\n",players[3][8]);
-
-
printf ("\n");
-
printf ("P5 \n");
-
printf ("%d\n",players[4][0]);
-
printf ("%d\n",players[4][1]);
-
printf ("%d\n",players[4][2]);
-
printf ("%d\n",players[4][3]);
-
printf ("%d\n",players[4][4]);
-
printf ("%d\n",players[4][5]);
-
printf ("%d\n",players[4][6]);
-
printf ("%d\n",players[4][7]);
-
printf ("%d\n",players[4][8]);
-
-
printf ("\n");
-
printf ("P6 \n");
-
printf ("%d\n",players[5][0]);
-
printf ("%d\n",players[5][1]);
-
printf ("%d\n",players[5][2]);
-
printf ("%d\n",players[5][3]);
-
printf ("%d\n",players[5][4]);
-
printf ("%d\n",players[5][5]);
-
printf ("%d\n",players[5][6]);
-
printf ("%d\n",players[5][7]);
-
printf ("%d\n",players[5][8]);
-
-
-
-
-
-
//Roll the die//
-
-
-
-
printf(" Type 'r' to roll the die \n");
-
-
//some weird bug in here compleatly ignores read if only 1 scanf is present???
-
getchar();
-
//scanf("%c", &roll);
-
-
scanf("%c", &roll);
-
-
while (roll == 'r')
-
{
-
die = rand() % 6 + 1;
-
-
printf ("Your Number Is : %d \n", die);
-
roll='n';
-
-
getchar ();
-
getchar ();
-
}
-
-
-
-
-
}
9 2912
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.
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;
boxfish 469
Recognized Expert Contributor
i dont quite understand how the player array would be unitialized it should be 54
elements of "0". No ?
-
int players[6][9]={0,0,0,0,0,0,0,0,0};
-
No, this only initiallizes the first nine; count the zeros.
To initiallize all of them, you can do: -
int players[6][9]={{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}};
-
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.
boxfish 469
Recognized Expert Contributor
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?
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
bump ... help , it defies all reason
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 : )
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. -
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <time.h>
-
-
#define WEAPON_ROPE 10
-
#define WEAPON_REVOLVER 20
-
#define WEAPON_LEADPIPE 30
-
#define WEAPON_CANDLESTICK 40
-
#define WEAPON_KNIFE 50
-
#define WEAPON_WRENCH 60
-
-
#define PEOPLE_MRGREEN 100
-
#define PEOPLE_MSSCARLET 200
-
#define PEOPLE_CONLMUSTARD 300
-
#define PEOPLE_PROFPLUM 400
-
#define PEOPLE_MISPEACOCK 500
-
#define PEOPLE_MISWHITE 600
-
-
#define PLACE_KITCHEN 1
-
#define PLACE_HALL 2
-
#define PLACE_POOLROOM 3
-
#define PLACE_STUDY 4
-
#define PLACE_LOUNG 5
-
#define PLACE_LIBRARY 6
-
#define PLACE_CONSERVATORY 7
-
#define PLACE_DINING 8
-
#define PLACE_BILLIARDS 9
-
-
int main()
-
{
-
int die = 0;
-
int players[6][9] = {{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}};
-
-
int allCards[] = {WEAPON_ROPE, WEAPON_REVOLVER,
-
WEAPON_LEADPIPE, WEAPON_CANDLESTICK,
-
WEAPON_CANDLESTICK, WEAPON_KNIFE,
-
WEAPON_WRENCH,
-
PEOPLE_MRGREEN, PEOPLE_MSSCARLET,
-
PEOPLE_CONLMUSTARD, PEOPLE_CONLMUSTARD,
-
PEOPLE_PROFPLUM, PEOPLE_MISPEACOCK,
-
PEOPLE_MISWHITE,
-
PLACE_KITCHEN, PLACE_HALL,
-
PLACE_POOLROOM, PLACE_STUDY,
-
PLACE_LOUNG, PLACE_LIBRARY,
-
PLACE_CONSERVATORY, PLACE_DINING,
-
PLACE_BILLIARDS};
-
int deckSize = 23; // number of cards in allCards array
-
-
int count;
-
for (count = 0; count < deckSize; ++count)
-
{
-
printf(", %d", allCards[count]);
-
} // End for
-
-
// These three array's are so you can put a card back, if need be...
-
int weaponCards[] = {WEAPON_ROPE, WEAPON_REVOLVER,
-
WEAPON_LEADPIPE, WEAPON_CANDLESTICK,
-
WEAPON_CANDLESTICK, WEAPON_KNIFE,
-
WEAPON_WRENCH};
-
int weaponDeckSize = 7;
-
-
-
int peopleCards[] = {PEOPLE_MRGREEN, PEOPLE_MSSCARLET,
-
PEOPLE_CONLMUSTARD, PEOPLE_CONLMUSTARD,
-
PEOPLE_PROFPLUM, PEOPLE_MISPEACOCK,
-
PEOPLE_MISWHITE};
-
int peopleDeckSize = 7;
-
-
-
int placeCards[] = {PLACE_KITCHEN, PLACE_HALL,
-
PLACE_POOLROOM, PLACE_STUDY,
-
PLACE_LOUNG, PLACE_LIBRARY,
-
PLACE_CONSERVATORY, PLACE_DINING,
-
PLACE_BILLIARDS};
-
int placeDeckSize = 9;
-
-
srand(clock()); // seed rand() using clock() which gives
-
// the current tick your processor is at...
-
-
int killer[3]; // no need to initialize yet. killer[0-2] will initialize
-
-
int deckShuffle = rand() % weaponDeckSize; // picks one number out of the deck
-
killer[0] = weaponCards[deckShuffle];
-
allCards[deckShuffle] = 0; // Card drawn. No longer exists in deck
-
-
deckShuffle = rand() % peopleDeckSize; // picks another random card out of the deck
-
killer[1] = peopleCards[deckShuffle];
-
allCards[deckShuffle + weaponDeckSize] = 0; // Card drawn. No longer exists in deck
-
-
deckShuffle = rand() % placeDeckSize; // randomly picks the last card needed
-
killer[2] = placeCards[deckShuffle];
-
allCards[deckShuffle + weaponDeckSize + peopleDeckSize] = 0; // Card drawn. No longer exists in deck
-
-
-
int numberOfCards = 0;
-
printf("CLUE\n");
-
printf("written by John Schintone\n");
-
printf("Origonal game delvoped by Hasbro\n");
-
-
int numberOfPlayers = 0;
-
while ((numberOfPlayers < 3) || (numberOfPlayers > 6))
-
{
-
printf("How many players are Going to play :\n");
-
printf("[number] > ");
-
scanf("%d",&numberOfPlayers);
-
-
// A very fast if statement which only uses integers/char's
-
switch(numberOfPlayers)
-
{
-
case 6:
-
{
-
numberOfCards = 3;
-
} break;
-
-
case 5:
-
{
-
numberOfCards = 4;
-
} break;
-
-
case 4:
-
{
-
numberOfCards = 5;
-
} break;
-
-
case 3:
-
{
-
numberOfCards = 6;
-
} break;
-
-
default:
-
{
-
printf("You must enter a number between 3 and 6...\n");
-
} // End default
-
} // End switch
-
} // End while
-
-
int index1, index2;
-
// Note: ++index1; is faster than index1++; and will almost always
-
// produce better code (index1++ happens after this statement line.
-
// ++index1 increments index1 before this statement line)
-
for (index1 = 0; index1 < numberOfPlayers; ++index1)
-
{
-
printf("Player %d", index1);
-
for (index2 = 0; index2 < numberOfCards; ++index2)
-
{
-
// Remember that allCards[deckShuffle] == 0 because we removed that
-
// card ages ago... works out well, just don't forget you did that : )
-
while (allCards[deckShuffle] == 0)
-
{
-
deckShuffle = rand() % deckSize;
-
} // End while
-
-
players[index1][index2] = allCards[deckShuffle];
-
allCards[deckShuffle] = 0; // Card removed for after loop...
-
-
printf(", %d", players[index1][index2]);
-
-
switch(players[index1][index2])
-
{
-
case WEAPON_ROPE:
-
{
-
} break;
-
-
// Add more...
-
-
case PEOPLE_MRGREEN:
-
{
-
} break;
-
-
// Add more...
-
-
case PLACE_KITCHEN:
-
{
-
} break;
-
-
// Add more...
-
-
default:
-
{
-
printf("Program has caught player %d cheating...", index1);
-
} // End default
-
} // End switch
-
} // End for
-
-
printf("\n");
-
} // End for
-
-
printf("The killer is %d, with the %d, and in the %d \n\n", killer[0], killer[1], killer[2]);
-
-
printf("Type h for this help... \n");
-
printf("Type e to escape... \n");
-
printf("Type r to roll the die... \n");
-
-
char command = '\0'; // \0 represents zero, or the null character
-
while (command != 'e')
-
{
-
printf("[one character] > ");
-
scanf("%c", &command);
-
-
if (command == 'r')
-
{
-
-
die = rand() % 6 + 1;
-
-
printf("Your number is: %d \n", die);
-
} // end while
-
-
if (command == 'h')
-
{
-
printf("Type h for this help... \n");
-
printf("Type e to escape... \n");
-
printf("Type r to roll the die... \n");
-
} // End if
-
-
printf("\n");
-
} // End while
-
-
return(0); // Success. Program worked ok
-
} // End main() Function
-
-
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
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: EAS |
last post by:
Hey, I'm new to python (and programming in general) so I'll prolly be around
here a lot...
Anyways, I've found out how to make a "guess my number game" where the
player guesses a number between...
|
by: Alex Endl |
last post by:
ok now that i know the random function, i made this guessing game. I get an
error though, and Im new so im not to good at figuring out what its talking
about.
import random
a =...
|
by: jar13861 |
last post by:
Create a game in a 3x3 HTML table that works as follows:
* The goal of the game is to beat the computer by scoring more
points than the computer.
* The game starts when a hidden random number...
|
by: connyledin |
last post by:
Im trying to create a version of the game Wumpus. Mine is called
Belzebub. But im STUCK! And its due tuesday 2 maj. Im panicing! Can
some one help me??
here is the file:...
|
by: raypjr |
last post by:
Hi everyone. I need a little help with some parts of a word guessing game I'm working on. I have some parts done but unsure about others and could use a little advice. Any help is very much...
| |
by: Madmartigan |
last post by:
Hi
I'm a newbie to C# and have been instructed to create a Hangman game in SharpDevelop. I don't want the answer to the full code, just some help along the way.
I have included my code thus...
|
by: YASIN786 |
last post by:
Hi there
M developing an game in the form of an applet which has two buttons: Random and Clear
Random button has to generate random numbers in a textfield labelled start.
Generating random numbers...
|
by: shadowkeeper123 |
last post by:
Hi all I have a small query in preparing this game the game is about shotting the balloons means the whole page will display balloons motion from bottom to up and each balloon will have a particular...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |