Advertisement

RPS game

Started by January 12, 2002 08:51 PM
6 comments, last by Samurai_x 22 years, 10 months ago
I''m trying to do a simple Rock Paper Scissors game but I cant get the computer to pick a number between 1-3. 1 rock 2 paper 3 scissors can anyone help?
use rand() % 2 + 1
store the return value in a variable, then put statements like if value == 1 then rock, else if value == 2 then paper else scissors,
http://www.dualforcesolutions.comProfessional website designs and development, customized business systems, etc.,
Advertisement
Thanks a lot :D
quote: Original post by mickey
use rand() % 2 + 1
store the return value in a variable, then put statements like if value == 1 then rock, else if value == 2 then paper else scissors,


That would be rand() % 3 + 1 ... and be warned that the low bits of rand() aren''t that random.

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Do you have a better way?
(int)(3 * ((double)rand() / (double)RAND_MAX)) + 1

Slower, due to the * and / though.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
I try that too. It doesnt have to be that fast anyway
I try that too. It doesnt have to be that fast anyway

This topic is closed to new replies.

Advertisement