Hello. I am trying to write a program where a human can play rock, paper, scissors with the computer. I'm stuck on the "determine computers choice" part......Here's my code so far...

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main ()
{  
   //  initialize the computer's random number generator
    
    srand(time(0));
    int random_integer = rand();
    cout << random_integer << endl;
    
   //  declare variables
   int R; 
   int P;
   int S; 
   int Q; 
   //  start loop
   { 
       // determine computers choice

       // prompt for, and read, the human's choice
string answer;
    { 
       cout << "Choose Rock, Paper, Scissors or Quit [R, P, S, or Q]: ";
    }     
       // if human wants to quit, break out of loop
        {
          }

       // determine winner
    
      cout << "Computer: "<< ?????? << "Human: " << ??????? << ???????

       // print results 
      
     
// end loop
    }
// end program 
  return 0; 
}

THANKS!

Please use [code]

[/code] tags. It makes code much easier to read.

I think you should use some different coding methods. Try using enums to keep track of rock, paper, and scissors. Then, when you create your random number, make it in the range of 0-3, which will be in line with the enums you created earlier.

Use a single char to keep track of the menu selection. You should then modify the while() loop to keep looping until the char is equal to 'Q'.

Inside the loop use a switch() statement to manage the menu selections, and then you can compare each selection with the computer's choice and print out the result. Additionally, you can use another variable to tally who won, and then once the user exits the while() loop, your program can print out the winning statistics.

Hope this helps

Please use [code]

[/code] tags. It makes code much easier to read.

I think you should use some different coding methods. Try using enums to keep track of rock, paper, and scissors. Then, when you create your random number, make it in the range of 0-3, which will be in line with the enums you created earlier.

Use a single char to keep track of the menu selection. You should then modify the while() loop to keep looping until the char is equal to 'Q'.

Inside the loop use a switch() statement to manage the menu selections, and then you can compare each selection with the computer's choice and print out the result. Additionally, you can use another variable to tally who won, and then once the user exits the while() loop, your program can print out the winning statistics.

Hope this helps

Like this?

int main ()
{  
   //  initialize the computer's random number generator
    
    srand(time(0));
    int i = rand();
    cout << i << endl;
    
   //  declare variables
   enum Choice {R,P,S,Q};

How do I modify the while loop to do what I need it to do?

and I'm sorry, I don't understand what you mean by

tags, give me an example and I'll gladly post that way next time - I'm brand new at this!

Thank you!

Simply put your code in between [code] and [/code]. See this for more information:
http://www.daniweb.com/techtalkforums/announcement8-3.html

I'm not sure why you print out the result of the random number at the beginning of the program... after all, it's supposed to be the computer's secret guess, isn't it?

How do I modify the while loop to do what I need it to do?

What I was thinking was something like this:

char selection;
while (selection != 'Q' && selection != 'q') {
   // get input and other stuff....

   switch selection {

   // ...
   }
}

Hope this helps

Here's a working example i just wrote. I've also used the enum type you guys were talking about, plus i added another of my own. Study this well and u'll c what i was going for. Hope it helps, byee

#include <iostream>
#include <ctime>

using namespace std;

enum Choice { Rock, Paper, Scissors }; // Rock=0, Paper=1, Scissors=2
enum Decision { Draw, Win, Lose }; // Draw=0, Win=1, Lose=2

char DoMenu();
void CheckWinner(int PlayerChoice);

int main()
{  
    //  initialize the computer's random number generator
    srand(time(0));
       
    //  declare variables
    char choice;

    do
    {
        choice = DoMenu();

        switch (choice)
        {
            case 'R': // fall through
            case 'r': cout << "\nPlayer: Rock\n"; CheckWinner(Rock); break;
            case 'P': // fall through
            case 'p': cout << "\nPlayer: Paper\n"; CheckWinner(Paper); break;
            case 'S': // fall through
            case 's': cout << "\nPlayer: Scissors\n"; CheckWinner(Scissors); break;
            default : cout << "\n";
        }

    } while ((choice != 'Q') && (choice != 'q'));


    // end program 
    return 0; 
}

char DoMenu()
{
    char MenuChoice;
        cout << "(R)ock\n";
        cout << "(P)aper\n";
        cout << "(S)cissors\n";
        cout << "(Q)uit\n";
        cout << "----------\n";
        cout << "Choice: ";
    cin >> MenuChoice;
    return MenuChoice;
}

void CheckWinner(int PlayerChoice)
{
    int random = rand()%3;

    switch (random)
    {
        case Rock:       cout << "Computer: Rock\n";       break;
        case Paper:    cout << "Computer: Paper\n";    break;
        case Scissors: cout << "Computer: Scissors\n"; break;
    }

    Decision outcome;

    if (PlayerChoice == Rock)
    {
        switch (random)
        {
            case Rock:       outcome = Draw; break;
            case Paper:    outcome = Lose; break;
            case Scissors: outcome = Win;  break;
        }
    }

    if (PlayerChoice == Paper)
    {
        switch (random)
        {
            case Rock:       outcome = Win;  break;
            case Paper:    outcome = Draw; break;
            case Scissors: outcome = Lose; break;
        }
    }

    if (PlayerChoice == Scissors)
    {
        switch (random)
        {
            case Rock:       outcome = Lose; break;
            case Paper:    outcome = Win;  break;
            case Scissors: outcome = Draw; break;
        }
    }

    switch (outcome)
    {
        case Win:  cout << "Player Wins!\n\n";   break;
        case Lose: cout << "Computer Wins!\n\n"; break;
        case Draw: cout << "Draw\n\n";             break;
    }
}

Of course, i just noticed. The parameter PlayerChoice in the function CheckWinner should be of type Choice but it still works this way as well. Please change it though, it should be

void CheckWinner(Choice PlayerChoice)
{
...

:)

if (PlayerChoice == Rock)
    {
        switch (random){
            case Rock:       outcome = Draw; break;
            case Paper:    outcome = Lose; break;
            case Scissors: outcome = Win;  break;
        }
    }

    if (PlayerChoice == Paper)
    {
        switch (random)
        {
            case Rock:       outcome = Win;  break;
            case Paper:    outcome = Draw; break;
            case Scissors: outcome = Lose; break;
        }
    }

    if (PlayerChoice == Scissors)
    {
        switch (random)
        {
            case Rock:       outcome = Lose; break;
            case Paper:    outcome = Win;  break;
            case Scissors: outcome = Draw; break;
        }
    }

Although that works for determining the winner, I think my method is much better:

if (PlayerChoice == random) {
        // tie!
    }
    else if ((PlayerChoice+1)%3 == random) {
        
        // computer wins!
    }
    else {
        // player wins!
    }

It's merely taking advantage of the fact that the choices are really numbers, and using the difference between the 2 to determine the winner.

Ye sure i mean whatever floats your boat.. There are many ways of solving a problem. We already gave two and agree totally with both! Its a matter of convenience, style, readability and efficiency that will determine which method to use. I believe my code is more readable in the way that its easier to understand what is going on and exactly how. Yours is more efficient as in LOC and processing... We choose and continue, live and let live.. ;)

Hey im new here.
i Need help with this game
Unfortunatly i have C#
Can you convert to C#?

Hey im new here.
i Need help with this game
Unfortunatly i have C#
Can you convert to C#?

Why? So you can hand it this assignment with 0% effort?

For one thing, it's unlikely that any of us are just going to convert this code to C# for you.
http://www.daniweb.com/techtalkforums/announcement8-2.html

You're just piggy-backing another thread, and for another, this is the wrong forum. I don't know a bit of C#, so that's why I post here. Perhaps a moderator will split this thread for you.

Here is another C++ implementation.
-GNU-Compiled*

#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
char cpuhand();
int referee(char,char);

int main(){
	srand(time(NULL));
	char hand,cpu;int score(0);bool again(true);
	while(again){
		cout<<endl<<"Select (R,P,S,X) for hand:";cin>>hand;cpu=cpuhand();
		if(hand=='X')again=false;
		cout<<"Player throws: "<<hand<<endl<<"CPU throws: "<<cpu<<endl;
		score+=referee(hand,cpu);
	}
	cout<<endl<<"Final Score: "<<score<<endl;
	return 0;
}

char cpuhand(){
	int num_eval;
	char cpuhand;
	num_eval=rand()%3+1;
	switch (num_eval){
		case 1:cpuhand='R';break;
		case 2:cpuhand='P';break;
		case 3:cpuhand='S';break;
	}
	return cpuhand;
}

int referee(char hand, char cpu){
	if(hand=='R'){
		if(cpu=='R'){
			cout<<endl<<"---Draw---"<<endl;
			return 0;
		}else if(cpu=='P'){
			cout<<endl<<"---Lose---"<<endl;
			return -1;
		}else if(cpu=='S'){
			cout<<endl<<"---Win---"<<endl;
			return 1;}
	}else if(hand=='P'){
		if(cpu=='R'){
			cout<<endl<<"---Win---"<<endl;
			return 1;
		}else if(cpu=='P'){
			cout<<endl<<"---Draw---"<<endl;
			return 0;
		}else if(cpu=='S'){
			cout<<endl<<"---Lose---"<<endl;
			return -1;}
	}else if(hand=='S'){
		if(cpu=='R'){
			cout<<endl<<"---Lose---"<<endl;
			return -1;
		}else if(cpu=='P'){
			cout<<endl<<"---Win---"<<endl;
			return 1;
		}else if(cpu=='S'){
			cout<<endl<<"---Draw---"<<endl;
			return 0;}
	}
	cout<<"GAME OVER."<<endl;return 0;
}

how can i put a code to output the score in three rounds.?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.