Thread: RandomTrouble

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    11

    RandomTrouble

    I am trying figure out how to generate random choices by the computer. I think I know what I'm doing in theory but I need someone to point me in the right direction as far as coding. I know the random number generator works, so I want to set it up so that if the random number is 0 then computerChoice is going to be equal to word "Paper", if the number is 1, then computerChoice is equal to "Rock"..and so on and so forth. I didn't think it would work to just set the variable equal to the word, I gave it a try and sure enough it didn't work. Could someone tell me how I can make computerChoice equal to a certain word?


    Code:
    #include <stdlib.h>
    #include <time.h>
    #include <iostream.h>
    
    int main()
    {
    
    char computerChoice[10]=""
     srand((unsigned)time(NULL)); //Casts time's return to unsigned
      int d=rand()%3;  //Should be more random now  
    
    if(d==0)
    {
    cout<<"Computer choice: Paper"<<endl;
    computerChoice="Paper";
    }
    if(d==1)cout<<"Computer choice: Rock"<<endl;
    
    if(d==2)cout<<"Computer choice: Scissors"<<endl;
    
      cout<<d;
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Instead of using char arrays, you could use the string class.
    Code:
    #include <cstdlib>
    #include <ctime>
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    
       string computerChoice;
       srand((unsigned)time(NULL)); //Casts time's return to unsigned
       int d=rand()%3;  //Should be more random now  
    
       if (d==0)
       {
          cout<<"Computer choice: Paper"<<endl;
          computerChoice="Paper";
       }
       else if (d==1)
       {
          cout<<"Computer choice: Rock"<<endl;
          computerChoice="Rock";
       }
    
       else if (d==2)
       {
          cout<<"Computer choice: Scissors"<<endl;
          computerChoice="Scissors";
       }
       cout<<d;
       return 0;
    }

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    11
    Hmm...that works for this portion of the game, but further in the game I use strcmp, i get a error message that says "function call 'strcmp({1val} char[15],{1val} std:basic_string"....ect. Im guessing you can't compare a char array to a string array. Do i have to define all the variables as strings?..or is there a different statement to compare these things?


    (just snippet of the code)
    Code:
    	if((strcmp(playerChoice,computerChoice))==0)
    	{
    	cout<<"Tie"<<endl;
    
    	}
    
    	if ((strcmp(playerChoice,computerChoice))==2)
    	{
    	cout<< " paper beats rock, computer wins this round"<<endl;
    	}
    
    	if((strcmp(playerChoice,computerChoice))==3)
    	{
    	cout<<"scissors over paper,player wins this round "<<endl;
    	}

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >or is there a different statement to compare these things?
    Well, you could do this:
    Code:
        if((strcmp(playerChoice,computerChoice.c_str()))==0) // Strings match

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    11
    ok, I tested the if statements that contain the string comparisons in order to determine whether the computer or player wins and it worked fine, yet when I put it in the program everything works up to that part, the player chooses a choice and then it shows the computer choice but it never prints the statement that says who won that round. I haven't gotten any error message..is there something wrong in my coding?

    Code:
    #include<iostream>
    #include<math.h>
    #include<string>
    #include<stdlib.h>
    #include<cstdlib>
    #include<ctime>
    
    int checkLoop;
    char yesOrno[10]="";		
    
    using namespace std;
    
    int main()
    {
    
    
    while(checkLoop == 0){   				
    	
    		//intro screen
    		cout << "                                                                             " << endl;
    		cout << "            Rock          Paper         Scissors                             " << endl;
    		cout << " ____________________________________________________________________________" << endl;
    		cout << "| This is a game of rock, paper, scissors. You will the best of three rounds |" << endl;
    		cout << "|  against the computer. To pick either rock, paper, or scissors simply      | " << endl;
    		cout << "|  type in your choice and hit enter. The computer will randomly pick its    | " << endl;
    		cout << "|  choice. At the end of the game type y to play again & n to end the game.  | " << endl;
    		cout << "|               Do you understand?  please enter y or n                      | " << endl;
    		cout << "|____________________________________________________________________________| " << endl; 
    		
    		//user input
    		cin.getline(yesOrno, 10);
    	
    if((strcmp(yesOrno,"n")==0)||(strcmp(yesOrno,"y")==0))			//if the user input is a y or n go into this other loop
    	{	
    		if(strcmp(yesOrno,"y")==0)
    			{
    				checkLoop=1;									
    				system("CLS");						
    				cout<< "Get ready to start the game"<<endl;
    				}
    			else												//if the user input is no repeat the directions
    			
    			{
    				system("CLS");
    				cout<< "Read the directions again";
    				checkLoop=0;									//makes this loop repeat until y or n entered
    			}	
    				}	 
    else
    	{ 
    	 				
    	 	cout<< " Enter a y or n "<<endl;						//if the user enters something other than a y or n print this statement										
    	 	cout<<endl;
    	 	cout<<endl;	
    	 	}
    
    	}
    
    
    
     
    string computerChoice;
    
    char playerChoice[20]="";
    
    cout << "Input player choice."<<endl;
    cin.getline(playerChoice,10);
    
    
    
    
    if((strcmp(playerChoice,"paper")==0)||(strcmp(playerChoice,"rock")==0)||(strcmp(playerChoice,"scissors")==0))
    
    {
    
       srand((unsigned)time(NULL)); 
       int d=rand()%3;  
       
       if (d==0)
       {
          cout<<"Computer choice: Paper"<<endl;
          computerChoice=="Paper";
       }
        if (d==1)
       {
          cout<<"Computer choice: Rock"<<endl;
          computerChoice=="Rock";
       }
    
       if (d==2)
       {
          cout<<"Computer choice: Scissors"<<endl;
          computerChoice=="Scissors";
       }
       cout<<d;
      
    //this is the part that doesn't show up 
    
    	if((strcmp(playerChoice,computerChoice.c_str()))==0)cout<<"Tie"<<endl;
    	
    	if ((strcmp(playerChoice,computerChoice.c_str()))==2)cout<< " paper beats rock, computer wins this round"<<endl;
    	
    	if((strcmp(playerChoice,computerChoice.c_str()))==3)cout<<"scissors over paper,player wins this round "<<endl;
    		
    	if((strcmp(playerChoice,computerChoice.c_str()))==-2)cout<<"paper beats rock,player wins this round"<<endl;
    	
    	if((strcmp(playerChoice,computerChoice.c_str()))==-3)cout<<"scissors beats paper, computer wins"<<endl;
    	
    	if ((strcmp(playerChoice,computerChoice.c_str()))==1)cout<<"rock beats scissors, computer wins this round"<<endl;
    	
    	if ((strcmp(playerChoice,computerChoice.c_str()))==-1)cout<<"rock beats scissors, player wins this round"<<endl;
    	
    }	
    
    else
    
    {
    	cout<<"you made a mistake in typing"<<endl;
    }
    
     
    return 0;
    
    }

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >is there something wrong in my coding?

    First, = and == are different operators:
    = is assignment
    == is checking for equality

    So:
    > computerChoice=="Paper";
    > computerChoice=="Rock";
    > computerChoice=="Scissors";

    These lines does basically does nothing. They check for equality, but don't use the result. I think you want assignment here.

    Second, "Rock" and "rock" are not equal. So you must make sure everything is either uppercase or lowercase, or convert the player's choice to one case, then match the computer choice with that case.

    With these changes, I think it will work.

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Code:
      if((strcmp(playerChoice,computerChoice.c_str()))==  0) // Strings match
    Before we run too far with this, let's consider:
    Code:
    if(computerChoice == playerChoice)
    Just had to stick in my 2 cents.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by Hunter2
    Code:
      if((strcmp(playerChoice,computerChoice.c_str()))==  0) // Strings match
    Before we run too far with this, let's consider:
    Code:
    if(computerChoice == playerChoice)
    Just had to stick in my 2 cents.
    How about that! I have just learned a new trick, thanks Hunter.

Popular pages Recent additions subscribe to a feed