♥milo Member
Posts : 3 Join date : 2011-07-05
| Subject: Help With Simple Game Sat Dec 10, 2011 4:27 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 - Code:
-
if (randNum == randNum1){ printf("Tie Game\n\n");} else if (randNum ==array[0]&& randNum1 == array1[2]){ printf ("Player 1 Wins!"); }
Heres all of it - Code:
-
#include <stdlib.h> #include<stdio.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";
srand((unsigned)(time(0))); printf ("Welcome to Rock Paper Scissors\n"); system ("pause"); do{ 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); fflush(stdin); if(playAgain == '2') {
printf("Goodbye"); } else if (playAgain == '1'); else{ system ("cls"); printf("\t\tInvalid Entry\n\t\t");
} }while (playAgain != '1' && playAgain != '2'); //system ("Pause"); system ("cls"); }while (playAgain != '2');
return 0;}
|
|
Saged Super Moderator
Posts : 5 Join date : 2011-07-05
| Subject: Re: Help With Simple Game Sat Dec 10, 2011 4:28 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: - Code:
-
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. |
|
CrazyGamer Member
Posts : 1 Join date : 2011-07-05
| Subject: Re: Help With Simple Game Sat Dec 10, 2011 4:28 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. |
|
Sponsored content
| Subject: Re: Help With Simple Game | |
| |
|