Question for Rock Paper Scissors game program - Page 2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 34

Thread: Question for Rock Paper Scissors game program

  1. #16
    Join Date
    Aug 2009
    Posts
    437

    Re: Question for Rock Paper Scissors game program

    I'm with 2kaud. Throw it in. It adds complexity (though not much, really). I'd just be sure to output and comment the code with the rules. Not everyone may know about that variation of the game.

  2. #17
    Join Date
    Jan 2013
    Posts
    71

    Re: Question for Rock Paper Scissors game program

    Update:

    I'm not any closer than I was yesterday!
    No...Im not posting my code. lol
    I'm embarrassed this stuff isnt sticking

  3. #18
    Join Date
    Aug 2009
    Posts
    437

    Re: Question for Rock Paper Scissors game program

    You should post what you have, explain where you are stuck or what is confusing. That way we can help you.

  4. #19
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    11,980

    Re: Question for Rock Paper Scissors game program

    Quote Originally Posted by psfign View Post
    Update:

    I'm not any closer than I was yesterday!
    No...Im not posting my code. lol
    I'm embarrassed this stuff isnt sticking
    There are several structural approaches, one of which is something like
    Code:
    if(UserInput == 'R')
    {
        if(ComputerGuess == 1)
            ...
        else
        if(ComputerGuess == 2)
        ...
    }
    else
    if(UserInput == 'S')
    {
        ...
    }
    etc.

    You may want to use switch statements instead of if. Either will work.

    More than that and you'll need to post your code.

  5. #20
    Join Date
    Jan 2013
    Posts
    71

    Re: Question for Rock Paper Scissors game program

    well this is what i have so far. It wont cout the answer. Im stuck..

    Code:
    #include <stdlib.h>
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <time.h.>
    using namespace std;
    char userChoice(void);
    void winOrLose(int,int);
    char getAns(void);
    int play(void);
    int main()
    {
    	char ans;
    	char choice;
    	int num;
    	int uChoice;
    	srand(time(NULL));
    
    	cout << "ROCK PAPER SCISSORS.\n\n\n"
    		 << "Play against your computer.\n\n"
    		 << "  Make your selection and\n"
    		 << "    the computer will randomly choose as well,\n"
    		 << "    and then the game will be scored.'\n\n"
    		 << "Rules:\n\n"
    		 << "  PAPER covers ROCK\n"
    		 << "  ROCK smashes SCISSORS\n"
    		 << "  SCISSORS cut PAPER\n\n";
    		system ("pause");
            
    	do
    		{
    			system ("cls");
    		 	num = 1 + rand( ) % 3;
    		 	uChoice = userChoice();
    		 	winOrLose(choice,num); 
    		 	ans = getAns();
    		}
     while(ans=='Y');
       return 0;
    }
    char userChoice(void)
    {
    	
    	char choice;
    	int uChoice;
    	do
    	{
    		cout <<  "Enter r, p, or s" ;
    		cin >> choice; cin.clear(); cin.ignore(10,'\n');
    		cout<<endl<<endl;
    	}
    while (choice != 'N');
    {
    
    if(choice == 'r')
    {
    	uChoice = 1;
    }		
    if(choice == 'p')
    {
    	uChoice = 2;	
    }	
    if(choice == 's')
    {
    	uChoice = 3;
    }
    }
    return uChoice;
    }
    char getAns(void)
    {
     	char ans;
        do
    	{
             ans='N';
             cout<<"Do you want to play again? ";
          	 cin>>ans; 
             ans=toupper(ans);
        }
    	 while(ans!='Y'&& ans!='N');
    	 return ans;
    }
    void winOrLose(int uChoice, int num)
    {					
    		if (uChoice == num)
    			{
    				cout << "Computer wins - PAPER covers ROCK " << endl;
    			}
    			else
    			{
    				cout << setw(35) << " " << "DRAW" << endl;	
    			}
    			cout<<endl<<endl;
    
    						
    }

  6. #21
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,001

    Re: Question for Rock Paper Scissors game program

    OK. Look at this

    Code:
    do
    	{
    		cout <<  "Enter r, p, or s" ;
    		cin >> choice; cin.clear(); cin.ignore(10,'\n');
    		cout<<endl<<endl;
    	}
    while (choice != 'N');
    {
    The while belongs to the do loop. So for the code in red the do loop is repeated until the user enters an 'N' - which I don't think is what you mean.

    The green { is probably left over from a while loop but now just serves as the start of a compound statement - and can removed togther with its partner.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  7. #22
    Join Date
    Jan 2013
    Posts
    71

    Re: Question for Rock Paper Scissors game program

    Thanks! I think i have an issue with my winOrLose function too though.

    Code:
    #include <stdlib.h>
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <time.h.>
    using namespace std;
    char userChoice(void);
    char getChoice(void);
    void winOrLose(int,int);
    char getAns(void);
    int play(void);
    int main()
    {
    	char ans;
    	char choice;
    	int num;
    	int uChoice;
    	srand(time(NULL));
    
    	cout << "ROCK PAPER SCISSORS.\n\n\n"
    		 << "Play against your computer.\n\n"
    		 << "  Make your selection and\n"
    		 << "    the computer will randomly choose as well,\n"
    		 << "    and then the game will be scored.'\n\n"
    		 << "Rules:\n\n"
    		 << "  PAPER covers ROCK\n"
    		 << "  ROCK smashes SCISSORS\n"
    		 << "  SCISSORS cut PAPER\n\n";
    		system ("pause");
            
    	do
    		{
    			system ("cls");
    		 	num = 1 + rand( ) % 3;
    		 	uChoice = userChoice();
    		 	winOrLose(uChoice,num); 
    		 	ans = getAns();
    		}
     while(ans=='Y');
       return 0;
    }
    char userChoice(void)
    {
    	
    	char choice;
    	int uChoice;
    	do
    	{
    		cout <<  "Enter r, p, or s" ;
    		cin >> choice; cin.clear(); cin.ignore(10,'\n');
    		cout<<endl<<endl;
    	}
    while (choice != 'r'&& choice != 'p'&& choice != 's');
    return choice;
    
    if(choice == 'r')
    {
    	uChoice = 1;
    }		
    if(choice == 'p')
    {
    	uChoice = 2;	
    }	
    if(choice == 's')
    {
    	uChoice = 3;
    }
    
    return uChoice;
    }
    char getAns(void)
    {
     	char ans;
        do
    	{
             ans='N';
             cout<<"Do you want to play again? ";
          	 cin>>ans; 
             ans=toupper(ans);
        }
    	 while(ans!='Y'&& ans!='N');
    	 return ans;
    }
    void winOrLose(int uChoice, int num)
    {					
    	if (uChoice == 1 && num == 2)
    			{
    			cout << "Computer wins - PAPER covers ROCK " << endl;	
    			}
    	else if (uChoice == 2 && num == 1)
    			{
    			cout << "User wins - PAPER covers ROCK" << endl;		
    			}
    	else if (uChoice == 3 && num == 1)
    			{
    			cout << "Computer wins - ROCK smashes SCISSOR" << endl;
    			}
    	else if (uChoice == 3 && num == 2)
    			{
    			cout << "User wins - SCISSORS cut PAPER" << endl;
    			}
    	else if (uChoice == 1 && num == 3)
    			{
    			cout << "User wins - ROCK smashes SCISSORS" << endl;
    			}
    	else if (uChoice == 2 && num == 3)
    			{
    			cout << "Computer wins - SCISSORS cut PAPER" << endl;
    			}
    			else
    			{
    				cout << "Draw";
    			}
    	cout<<endl<<endl;
    
    						
    }
    Last edited by psfign; April 28th, 2013 at 11:42 AM.

  8. #23
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,001

    Re: Question for Rock Paper Scissors game program

    Code:
    while (choice != 'r'&& choice != 'p'&& choice != 's');
    return choice;
    Why the return here? You are now not converting choice to uchoice so this statement is not needed.

    userChoice should also return an int and not a char.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  9. #24
    Join Date
    Jan 2013
    Posts
    71

    Re: Question for Rock Paper Scissors game program

    That got it! Thanks! Now, all i have to do is tally up wins/losses

  10. #25
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,001

    Re: Question for Rock Paper Scissors game program

    Quote Originally Posted by psfign View Post
    That got it! Thanks! Now, all i have to do is tally up wins/losses
    That's easy! But you should really learn how to debug your programs using the debugger once they compile but don't work as expected.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  11. #26
    Join Date
    Jan 2013
    Posts
    71

    Re: Question for Rock Paper Scissors game program

    I always have a problem with keeping score total. How do I return the wins/losses/draws to keep score each time. So am I correct that I cant return the score in a void funtion like this? Do i need to do a int function?

    Code:
    void winOrLose(int uChoice, int num)
    {		
    int wins = 0;
    	int losses = 0;
    	int draws = 0;			
    	if (uChoice == 1 && num == 2)
    			{
    				system ("cls");
    				losses++;
    			cout << "Player:   ROCK\n\n"
    				 << "Computer: PAPER\n\n"
    			     << "Computer wins - PAPER covers ROCK \n\n"
    				 << " Wins: "<< wins << " Losses: " << losses << " Draws: " << draws << endl;	
    			}
    	else if (uChoice == 2 && num == 1)
    			{
    				system ("cls");
    				wins++;
    			cout << "Player:   PAPER\n\n"
    				 << "Computer: ROCK\n\n"
    			     << "User wins - PAPER covers ROCK\n\n"
    				 << " Wins: "<< wins << " Losses: " << losses << " Draws: " << draws << endl;		
    			}
    	else if (uChoice == 3 && num == 1)
    			{
    				system ("cls");
    				losses++;
    			cout << "Player:   SCISSORS\n\n"
    				 << "Computer: ROCK\n\n"
    			     << "Computer wins - ROCK smashes SCISSOR\n\n"
    			     << " Wins: "<< wins << " Losses: " << losses << " Draws: " << draws << endl;
    			}
    	else if (uChoice == 3 && num == 2)
    			{
    				system ("cls");
    				wins++;
    			cout << "Player:   SCISSORS\n\n"
    				 << "Computer: PAPER\n\n"
    			     << "User wins - SCISSORS cut PAPER\n\n"
    			     << " Wins: "<< wins << " Losses: " << losses << " Draws: " << draws << endl;
    			}
    	else if (uChoice == 1 && num == 3)
    			{
    				system ("cls");
    				wins++;
    			cout << "Player:   ROCK\n\n"
    				 << "Computer: SCISSORS\n\n"
    			     << "User wins - ROCK smashes SCISSORS\n\n"
    			     << " Wins: "<< wins << " Losses: " << losses << " Draws: " << draws << endl;
    			}
    	else if (uChoice == 2 && num == 3)
    			{
    				system ("cls");
    				losses++;
    			cout << "Player:   ROCK\n\n"
    				 << "Computer: SCISSORS\n\n"
    			     << "Computer wins - SCISSORS cut PAPER\n\n"
    			     << " Wins: "<< wins << " Losses: " << losses << " Draws: " << draws << endl;
    			}
    			else if (uChoice == 1 && num == 1)
    			{
    				system ("cls");
    				draws++;
    			cout << "Player:   ROCK\n\n"
    				 << "Computer: ROCK\n\n"
    				 << "Tie\n\n"
    				 << " Wins: "<< wins << " Losses: " << losses << " Draws: " << draws << endl;
    			}
    			else if (uChoice == 2 && num == 2)
    			{
    				system ("cls");
    				draws++;
    			cout << "Player:   PAPER\n\n"
    				 << "Computer: PAPER\n\n"
    				 << "Tie\n\n"
    				 << " Wins: "<< wins << " Losses: " << losses << " Draws: " << draws << endl;
    			}
    			else if (uChoice == 3 && num == 3)
    			{
    				system ("cls");
    				draws++;
    			cout << "Player:   SCISSORS\n\n"
    				 << "Computer: SCISSORS\n\n"
    				 << "Tie\n\n"
    				 << " Wins: "<< wins << " Losses: " << losses << " Draws: " << draws << endl;
    			}
    			cout<<endl<<endl;
    }

  12. #27
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,001

    Re: Question for Rock Paper Scissors game program

    You do it something like this

    Code:
    int cwin = 0,
        closs = 0,
        uwin = 0,
        uloss = 0;
    ....
    
    if (uChoice == 1 && num == 2) {
    	cout << "Computer wins - PAPER covers ROCK " << endl;	
            cwin++;
            uloss++;
    }.....
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  13. #28
    Join Date
    Jan 2013
    Posts
    71

    Re: Question for Rock Paper Scissors game program

    i have that but it resets after every turn

  14. #29
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,001

    Re: Question for Rock Paper Scissors game program

    Where are you defining the variables? They should be global.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  15. #30
    Join Date
    Jan 2013
    Posts
    71

    Re: Question for Rock Paper Scissors game program

    Code:
    #include <stdlib.h>
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <time.h.>
    using namespace std;
    int userChoice(void);
    char getChoice(void);
    void winOrLose(int,int);
    char getAns(void);
    int play(void);
    int main()
    {
    	char ans;
    	char choice;
    	int num;
    	int uChoice;
    	int wins = 0;
    	int losses = 0;
    	int draws = 0;
    	srand(time(NULL));
    
    	cout << "ROCK PAPER SCISSORS.\n\n\n"
    		 << "Play against your computer.\n\n"
    		 << "  Make your selection and\n"
    		 << "    the computer will randomly choose as well,\n"
    		 << "    and then the game will be scored.'\n\n"
    		 << "Rules:\n\n"
    		 << "  PAPER covers ROCK\n"
    		 << "  ROCK smashes SCISSORS\n"
    		 << "  SCISSORS cut PAPER\n\n";
    		system ("pause");
            
    	do
    		{
    			system ("cls");
    		 	num = 1 + rand( ) % 3;
    		 	uChoice = userChoice();
    		 	winOrLose(uChoice,num); 
    		 	ans = getAns();
    		}
     while(ans=='Y');
       return 0;
    }
    int userChoice(void)
    {
    	
    	char choice;
    	int uChoice;
    	do
    	{
    		cout << "Enter your choice\n"
    			 << "R -Rock\n"
    			 << "P - Paper\n"
    			 << "S - Scissors\n";
    		cin >> choice; cin.clear(); cin.ignore(10,'\n');
    		cout<<endl<<endl;
    	}
    while (choice != 'r'&& choice != 'p'&& choice != 's');
    
    
    if(choice == 'r')
    {
    	uChoice = 1;
    }		
    if(choice == 'p')
    {
    	uChoice = 2;	
    }	
    if(choice == 's')
    {
    	uChoice = 3;
    }
    
    return uChoice;
    }
    char getAns(void)
    {
     	char ans;
        do
    	{
             ans='N';
             cout<<"Do you want to play again? ";
          	 cin>>ans; 
             ans=toupper(ans);
        }
    	 while(ans!='Y'&& ans!='N');
    	 return ans;
    }
    void winOrLose(int uChoice, int num)
    {		
    int wins = 0;
    	int losses = 0;
    	int draws = 0;			
    	if (uChoice == 1 && num == 2)
    			{
    				system ("cls");
    				losses++;
    			cout << "Player:   ROCK\n\n"
    				 << "Computer: PAPER\n\n"
    			     << "Computer wins - PAPER covers ROCK \n\n"
    				 << " Wins: "<< wins << " Losses: " << losses << " Draws: " << draws << endl;
    				 	
    			}
    	else if (uChoice == 2 && num == 1)
    			{
    				system ("cls");
    				wins++;
    			cout << "Player:   PAPER\n\n"
    				 << "Computer: ROCK\n\n"
    			     << "User wins - PAPER covers ROCK\n\n"
    				 << " Wins: "<< wins << " Losses: " << losses << " Draws: " << draws << endl;		
    			}
    	else if (uChoice == 3 && num == 1)
    			{
    				system ("cls");
    				losses++;
    			cout << "Player:   SCISSORS\n\n"
    				 << "Computer: ROCK\n\n"
    			     << "Computer wins - ROCK smashes SCISSOR\n\n"
    			     << " Wins: "<< wins << " Losses: " << losses << " Draws: " << draws << endl;
    			}
    	else if (uChoice == 3 && num == 2)
    			{
    				system ("cls");
    				wins++;
    			cout << "Player:   SCISSORS\n\n"
    				 << "Computer: PAPER\n\n"
    			     << "User wins - SCISSORS cut PAPER\n\n"
    			     << " Wins: "<< wins << " Losses: " << losses << " Draws: " << draws << endl;
    			}
    	else if (uChoice == 1 && num == 3)
    			{
    				system ("cls");
    				wins++;
    			cout << "Player:   ROCK\n\n"
    				 << "Computer: SCISSORS\n\n"
    			     << "User wins - ROCK smashes SCISSORS\n\n"
    			     << " Wins: "<< wins << " Losses: " << losses << " Draws: " << draws << endl;
    			}
    	else if (uChoice == 2 && num == 3)
    			{
    				system ("cls");
    				losses++;
    			cout << "Player:   ROCK\n\n"
    				 << "Computer: SCISSORS\n\n"
    			     << "Computer wins - SCISSORS cut PAPER\n\n"
    			     << " Wins: "<< wins << " Losses: " << losses << " Draws: " << draws << endl;
    			}
    			else if (uChoice == 1 && num == 1)
    			{
    				system ("cls");
    				draws++;
    			cout << "Player:   ROCK\n\n"
    				 << "Computer: ROCK\n\n"
    				 << "Tie\n\n"
    				 << " Wins: "<< wins << " Losses: " << losses << " Draws: " << draws << endl;
    			}
    			else if (uChoice == 2 && num == 2)
    			{
    				system ("cls");
    				draws++;
    			cout << "Player:   PAPER\n\n"
    				 << "Computer: PAPER\n\n"
    				 << "Tie\n\n"
    				 << " Wins: "<< wins << " Losses: " << losses << " Draws: " << draws << endl;
    			}
    			else if (uChoice == 3 && num == 3)
    			{
    				system ("cls");
    				draws++;
    			cout << "Player:   SCISSORS\n\n"
    				 << "Computer: SCISSORS\n\n"
    				 << "Tie\n\n"
    				 << " Wins: "<< wins << " Losses: " << losses << " Draws: " << draws << endl;
    			}
    			cout<<endl<<endl;
    
    
     	
    }

Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center