-
Rock, Paper, Scissors
Any idea why this isn't working quite right? I'm stumped.
#include <stdio.h>
/*
ROCK 1
PAPER 2
SCISSOR 3
*/
int user_choice = 0;
int computer_choice = 0;
void do_get_input()
{
printf("Please choose 1, 2, or 3.\nRock being 1, Paper being 2 and Scissors being 3.\n");
scanf("%d", &user_choice);
if (user_choice != '1' || user_choice != '2' || user_choice != '3')
{
printf("Your choice was invalid. Choose again.\n");
scanf("%d", &user_choice);
}
}
void do_who_wins()
{
rand(computer_choice)%4;
if (computer_choice == '1' && user_choice == '1')
{
printf("You tied.\n");
}
else if (computer_choice == '1' && user_choice == '2')
{
printf("You win!\n");
}
else if (computer_choice == '1' && user_choice == '3')
{
printf("You loose!\n");
}
else if (computer_choice == '2' && user_choice == '1')
{
printf("You loose!\n");
}
else if (computer_choice == '2' && user_choice == '2')
{
printf("You tie.\n");
}
else if (computer_choice == '2' && user_choice == '3')
{
printf("You win!\n");
}
else if (computer_choice == '3' && user_choice == '1')
{
printf("You win!\n");
}
else if (computer_choice == '3' && user_choice == '2')
{
printf("You loose!\n");
}
else if (computer_choice == '3' && user_choice == '3')
{
printf("You tie.\n");
}else{
printf("Bug: Do_who_wins");
}
}
int main()
{
do_get_input();
do_who_wins();
return 0;
}
-
When you're reading input into an integer any conditional statement should compare this value with an integer. You have -
computer_choice == '1'
'1' is a character. You want -
computer_choice == 1
Also to test for validity of input you have -
if (user_choice != '1' || user_choice != '2' || user_choice != '3')
The compound statement under this will be executed if the choice is not '1' or the choice is not '2' or the choice is not '3'. Therefore if you entered '1', the the choice wouldn't be '2' or '3' so the above statement would be true. You want -
if (user_choice != 1 && user_choice !=2 && user_choice != 3)
I'm not sure want compiler you're using, but most implementations of the random functions require a seed, otherwise you'll get the same results everytime you'll execute the program so you may have to change this.
-
-
An initial value that the randomise function uses to produce random numbers. The standard function rand() uses an initial default seed of 1. Therefore if you don't provide your own you'll get the same series of random numbers generated everytime you run the program. The most popular method of seeding the random number generator is to use the time, as unless your program is run at exactly the same time, you'll get a different series of random numbers everytime it's run. To do this you'd do -
srand((unsigned)time(0));
-
So looking through my program. How would I use this srand function and assign it to the computer_choice variable? I'm thinking
srand(computer_choice)time(0);
?
--Twigg
-
The FAQ has examples of the syntax required to use srand() and rand().
-
Try this:
void do_who_wins()
{
computer_choice = rand()%3 + 1;
.
.
.
int main()
{
srand((unsigned) time(NULL));
do_get_input();
do_who_wins();
return 0;
}
-
Ok, I've finished everything and it seems to work. If you see any problems in the code could you tell me?
[QUOTE]
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/*
ROCK 1
PAPER 2
SCISSOR 3
*/
int rand(void);
//Global Int's
int cnt = 0;
int score = 0;
int user_choice = 0;
int computer_choice = 0;
void do_get_input()
{
printf("Enter 1 for Rock, 2 for Paper or 3 for Scissors.\n");
scanf("%d", &user_choice);
if (user_choice != 1 && user_choice != 2 && user_choice != 3)
{
printf("Your choice was invalid. Choose again.\n");
scanf("%d", &user_choice);
}
}
void do_who_wins()
{
srand(time(NULL));
computer_choice = rand()%3;
if (computer_choice == 0 && user_choice == 1)
{
printf("You: Rock | Computer: Rock\n");
printf("You tied.\n");
}
else if (computer_choice == 0 && user_choice == 2)
{
printf("You: Paper | Computer: Rock\n");
printf("You win!\n");
score++;
}
else if (computer_choice == 0 && user_choice == 3)
{
printf("You: Scissors | Computer: Rock\n");
printf("You loose!\n");
cnt++;
}
else if (computer_choice == 1 && user_choice == 1)
{
printf("You: Rock | Computer: Paper\n");
printf("You loose!\n");
cnt++;
}
else if (computer_choice == 1 && user_choice == 2)
{
printf("You: Paper | Computer: Paper\n");
printf("You tie.\n");
cnt++;
}
else if (computer_choice == 1 && user_choice == 3)
{
printf("You: Scissors | Computer: Paper\n");
printf("You win!\n");
score++;
cnt++;
}
else if (computer_choice == 2 && user_choice == 1)
{
printf("You: Rock | Computer: Scissors\n");
printf("You win!\n");
score++;
cnt++;
}
else if (computer_choice == 2 && user_choice == 2)
{
printf("You: Paper | Computer: Scissors\n");
printf("You loose!\n");
cnt++;
}
else if (computer_choice == 3 && user_choice == 3)
{
printf("You: Scissors | Computer: Scissors\n");
printf("You tie.\n");
cnt++;
}else{
printf("Bug: Win Table");
}
}
int main()
{
char answer;
while (cnt < 25)
{
do_get_input();
do_who_wins();
while ( getchar() != '\n' );
printf("Play again?(Y/N)\n");
scanf("%c", &answer);
answer = toupper(answer);
if (answer == 'N')
{
printf("You scored %d out of %d.\n", score, cnt);
return 0;
}else{
continue;
}
}
printf("You scoured %d out of %d.\n", score, cnt);
return 0;
}
[\QUOTE]
-Twigg
-
Best to only seed the srand() once. I had problems because my PC was so fast the time was the same for all the srand calls.
Create a static var to tell if you have seeded the srand() and don't do it a second time
Code:
static int iCount=0;
if(iCount==0)
{
iCount++;
srand(time(NULL));
}
computer_choice = rand()%3;
if (computer_choice == 0 && user_choice == 1)
There are better ways to do this but this will give you an easy solution.
-
a better way to do this :
void do_who_wins(void)
{
int Array[]={-2,1,2,-1,0};
int computer_choice,i;
char *Message[]={"You win!\n","You loose!\n","You tie.\n","Bug: win table\n"};
char *ChoiceName[]={"Rock","Paper","Scissors"};
if(user_choice>3||user_choice<1){
printf("bad choice\n");
return;
}
computer_choice=rand()%3+1;
for(i=0;i<sizeof(Array)/sizeof(int);i++)
if((user_choice-computer_choice)==Array[i]){
printf("You: %s | Computer: %s\n",ChoiceName[user_choice-1],ChoiceName[computer_choice-1]);
if(i<2)printf(Message[0]);
else if(i<4)printf(Message[1]);
else if(i==4)printf(Message[2]);
else printf(Message[3]);
break;
}
}