built a paper rock scissor game

Thread Starter

kamarul amin

Joined Dec 2, 2014
62
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 :(
 

tshuck

Joined Oct 18, 2012
3,534
You 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.
 

Thread Starter

kamarul amin

Joined Dec 2, 2014
62
You 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.
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
 

tshuck

Joined Oct 18, 2012
3,534
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
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. ;)
 

GopherT

Joined Nov 23, 2012
8,012
@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.
 
Last edited:

WBahn

Joined Mar 31, 2012
26,398
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)
No, 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.
 

WBahn

Joined Mar 31, 2012
26,398
For what he is doing, using the remainder operator (it isn't a modulo operator, but for positive arguments the distinction is moot) will work. In general it introduces a bias into the distribution that favors outcomes less than a certain value over larger ones. Back when RAND_MAX was 32767 on most implementations, that bias started having noticeable effects as soon as the range over which you wanted a random number got to be more than 50 to 100 or so. If you just want one of three values, then the lowest value has about a 0.01% edge over the largest one. For games like this that isn't noticeable, but for anything at all serious that is often big enough to be a problem.
 

Thread Starter

kamarul amin

Joined Dec 2, 2014
62
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. ;)
Haha i will try my best.:);)
 

Thread Starter

kamarul amin

Joined Dec 2, 2014
62
@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.
Wow i thought that rand () only can give weird int. Never know that we can range the random number. Nice tip :) ;)
 

Thread Starter

kamarul amin

Joined Dec 2, 2014
62
No, 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.
Haaaaa help me!!!!:(:( my lecturer just only told me about how to display random number by computer timing
 

tshuck

Joined Oct 18, 2012
3,534
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.
my lecturer just only told me about how to display random number by computer timing
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.
 

Thread Starter

kamarul amin

Joined Dec 2, 2014
62
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.
i know well about th modulus operator :)
yeah i'm about to start of but didnt get any idea where to start. when i do the code, i will post it here. he ask to record the win/lose in HTML possible to learn it within a week. thanks for the idea :D
 

Thread Starter

kamarul amin

Joined Dec 2, 2014
62
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.
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");


}
 

tshuck

Joined Oct 18, 2012
3,534
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");


}
Two things before I comment on the code:
1. Saying it doesn't work doesn't help others understand what it is doing, which is often a big indicator as to where to look for a problem. Explain what it does and any errors you encounter.
2. Please put your code between code tags ([code_][/code_] (without the underscore) - it makes it easier to read without making the page unnecessarily long.

As for the code, you have
ch2=rand()%4;
Which will generate numbers 0-3. You are expecting 1-3.
 
Top