guys, is there any idea how to built paper rock scissor gmae in C in linux?? hm i just finished loop and ongoing in function then my lecturer ask me to built the game as an assignment
Wow thats seem tough huhu he gave me only two weeks lol. As i know C in linux is little bit different from in windows. Amci right?? About the library n so onYou should write what the rules are first. From there, you can determine how you will build the game (e.g. user selects rock, paper, or scissors, randomly generate the response/have a second player select their choice, etc.). From there, you should be able to write some code for a specific design goal.
C is C, except when it's a little sharp or incrementally different. In other words, it's the same.Wow thats seem tough huhu he gave me only two weeks lol. As i know C in linux is little bit different from in windows. Amci right?? About the library n so on
Keep in mind that rand() returns an integer in the range 0 <= rand() <= RAND_MAX, so this code is going to come up scissors a lot!@kamarul amin
Just generate random numbers
For example, symbol = 3 * rand()
If the result is <1, rock
>=1 but <2, paper
>=2 scissor
You can work from there.
Too many programming languages. I'm sure the OP got the gist of it.Keep in mind that rand() returns an integer in the range 0 <= rand() <= RAND_MAX, so this code is going to come up scissors a lot!
No, C uses 0 to RAND_MAX.Too many programming languages. I'm sure the OP got the gist of it.
Some use 0 - 1 for rand(). c uses 0 to ((2^16)-1)
Haha i will try my best.C is C, except when it's a little sharp or incrementally different. In other words, it's the same.
It is not difficult, its just a matter of understanding what is required of you. This is one of the most important parts of programming - defining the requirements. Once you know what is required, you can look into how to implement it. Then, you implement it.
If you don't understand the problem you hope to solve, you can never solve it.
I would have given about three days to do this, so be thankful you don't have me as your instructor.
Wow i thought that rand () only can give weird int. Never know that we can range the random number. Nice tip@kamarul amin
Just generate random numbers
For example, symbol = 3 * rand()
If the result is <1, rock
>=1 but <2, paper
>=2 scissor
You can work from there.
EDIT: Ok, see correction below - if you build this version, pick "Rock" when you play.
Haaaaa help me!!!! my lecturer just only told me about how to display random number by computer timingNo, C uses 0 to RAND_MAX.
RAND_MAX is an implementation specific value that, on most compilers these days, is [(2^31)-1]. Even back when most compilers used 16-bit ints, RAND_MAX was almost never [(2^16)-1] since that is not compatible with 16-bit singed integers, which is almost always what rand() feeds into. So on those compilers it was usually [(2^15)-1], which is the smallest value of RAND_MAX that is allowed by the language specification. RAND_MAX is not even required to be of the form [(2^n)-1] and on implementations that use a better PRNG, RAND_MAX may be forced to be something else, such as a prime number.
Well, you aren't (rather, shouldn't be) using the computer time to create a random number, rather, you should be using srand() to seed the random number generator, basically, you're giving it a starting point from which to generate more numbers that are randomly distributed (almost).my lecturer just only told me about how to display random number by computer timing
i know well about th modulus operatorBefore we get into whether a random number is required, is this supposed to be a single player game? If not, you won't need a random number. If, however, this is supposed to be a single player game, then, yes, a random number is your option.
Well, you aren't (rather, shouldn't be) using the computer time to create a random number, rather, you should be using srand() to seed the random number generator, basically, you're giving it a starting point from which to generate more numbers that are randomly distributed (almost).
Do you know the modulo operator (%) ? You can use this to ensure your results are between 0 and a given number-1. See here for more information on rand().
So far you have not written any code (that we know of), nor have you shown any work whatsoever. Please, show us some work so we can continue.
so, this is my new coding. i do find it how to make random int. thanks for your link up there. hehe but still doesn't complete..Before we get into whether a random number is required, is this supposed to be a single player game? If not, you won't need a random number. If, however, this is supposed to be a single player game, then, yes, a random number is your option.
Well, you aren't (rather, shouldn't be) using the computer time to create a random number, rather, you should be using srand() to seed the random number generator, basically, you're giving it a starting point from which to generate more numbers that are randomly distributed (almost).
Do you know the modulo operator (%) ? You can use this to ensure your results are between 0 and a given number-1. See here for more information on rand().
So far you have not written any code (that we know of), nor have you shown any work whatsoever. Please, show us some work so we can continue.
Two things before I comment on the code:so, this is my new coding. i do find it how to make random int. thanks for your link up there. hehe but still doesn't complete..
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
main()
{
char bl;
int ch1, ch2, x, y, z;
srand(time(NULL));
printf("\nThis is the paper rock scissor game");
printf("\nYou may choose \n'1' for paper\n'2' for rock\n'3' for scissor");
printf("\nPlayer 1 make a choose :");
scanf("%d", &ch1);
ch2=rand()%4;
switch (ch1)
{
case 1: printf("\nThe player 1 has choose paper");
break;
case 2: printf("\nThe player 1 has choose rock");
break;
case 3: printf("\nThe player 1 has choose scissor");
break;
default: printf("\nInvalid input");
break;
}
switch (ch2)
{
case 1: printf("\nThe player 2 has choose paper");
break;
case 2: printf("\nThe player 2 has choose rock");
break;
case 3: printf("\nThe player 2 has choose scissor");
break;
default: printf("\nInvalid input");
break;
}
if(ch1==1 && ch2==2)
printf("\nThe player 2 win the game");
else if(ch1==1 && ch2==3)
printf("\nThe player 2 win the game");
else if(ch1==1 && ch2==1)
printf("\nThe game tie -,-");
else if(ch1==2 && ch2==1)
printf("\nThe player 1 win the game");
else if(ch1==2 && ch2==2)
printf("\nThe game tie");
else if(ch1==2 && ch2==3)
printf("\nThe player 1 win the game");
else if(ch1==3 && ch2==1)
printf("\nThe player 1 win the game");
else if(ch1==3 && ch2==2)
printf("\nThe player 1 win the game");
else if(ch1==3 && ch2==3)
printf("\nThe game tie");
}
Which will generate numbers 0-3. You are expecting 1-3.ch2=rand()%4;