View Full Version : Help With Simple Game

12-04-2011, 05:22 AM
I'm making a rock paper scissors game but I'm having problems having it display if player 1 wins. I know you are probably gonna say that I can't compare an integer with a character but I can't think of any way to rewrite that. If you could fix this line of code how would you?
The line of code I'm talking about is

if (randNum == randNum1){
printf("Tie Game\n\n");}
else if (randNum ==array[0]&& randNum1 == array1[2]){
printf ("Player 1 Wins!");

Heres all of it

#include <stdlib.h>
#include <time.h>

int main(){
char array[3][20] = {"Rock", "Paper", "Scissors"};
char array1[3][20] = {"Rock", "Paper", "Scissors"};
int randNum=0;
int randNum1=0;
char playAgain;
int option=0;
char player1[20]="Andy";
char player2[20]="Spencer";

printf ("Welcome to Rock Paper Scissors\n");
system ("pause");
randNum = rand() % 3;
randNum1 = rand() % 3;
printf("\n%s %s %s Shoot:\n\n", array[0], array[1], array[2]);
printf("\n\t\t%s: %s\n", player1 ,array[randNum]);
printf("\t\t%s: %s\n\n", player2, array1[randNum1]);
if (randNum == randNum1){//line of code to look at.
printf("Tie Game\n\n");}
else if (randNum ==array[0]&& randNum1 == array1[2]){
printf ("Player 1 Wins!");

do {
printf("Do you want to play again?\n1 = Yes\n2 = No(Exit the Program)\n Your Choice: ");

scanf("%c", &playAgain);
if(playAgain == '2') {

else if (playAgain == '1');
system ("cls");
printf("\t\tInvalid Entry\n\t\t");

}while (playAgain != '1' && playAgain != '2');
//system ("Pause");
system ("cls");
}while (playAgain != '2');

return 0;}

12-05-2011, 03:59 AM
Well for starters the code you posted doesn't even compile. You have two extra closing braces before the inner do while loop.

However past that the reason it never says Player 1 wins is because you are comparing the random number against the address of the string at array[0] and array1[2]. Which even if it was comparing the value inside the string, I'm not sure how that would work.

Take a look at this, this works:

if (randNum == randNum1)
printf("Tie Game\n\n");
else if (randNum == 0 && randNum1 == 2)
printf ("Player 1 Wins!");

Since the random number is the index into the array of the thing they picked you compare against that.

To make your code a whole lot easier to understand I would highly recommend using constants to represent the index values. (e.g. rock, paper scissors)

Also why do you have array and array1 which have the exact same values? Seems like you only need one of them.

12-10-2011, 03:55 AM
I don't know how many times I have to say this...but you guys really need to get into the habit of putting COMMENTS in your code so others can read it more easily. Otherwise it just makes my job harder to help you.

12-10-2011, 07:46 PM