Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Oct 2010
    Posts
    277
    Thanks
    10
    Thanked 1 Time in 1 Post

    Help With Simple Game

    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;}
    Last edited by andynov123; 12-04-2011 at 05:24 AM.

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    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.
    OracleGuy

  • #3
    New Coder
    Join Date
    Oct 2011
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Regular Coder
    Join Date
    Oct 2010
    Posts
    277
    Thanks
    10
    Thanked 1 Time in 1 Post
    Nevermind
    Last edited by andynov123; 12-10-2011 at 08:11 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •