PROBLEM: paper rock scissors

I've copy/pasted my work-in-progress code below for the game ROCK PAPER SCISSORS.

objective: user inputs R (rock), P (paper), S (scissors) or Q (quit.
computer is then to automatically play the game with user.
the winner is determined, etc.

problem: how do i get to generate the computer to play with the user?!

limits/restrictions: i am currently in a beginner's c++ class. the furthest we've gone are Subprograms, Voids, Value-Returning Functions. Teacher wants us to use such tools to create the game.

The program below runs and compiles properly, but of course, the computer doesn't output anything because i have no idea how to come about including the computer to think for its own.

I was hoping someone could go through my program and see what my current problems are. I'm sure I'm going to run through a few as I further create/write this program, but for now, will someone lend a hand so i can go forward with this? =) THANK YOU for the help, in advance.

...KristiNE...


PS: this is an online hybrid class, so asking the teacher is pointless. it takes weeks for him to give me a response. =(

once again, thank you.


_________________________________________


#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;


void results();
char human();
char computer();


char human()
{
char humchoice;

while (true)
{
cout << " " << endl;
cout << "Choose [R-Rock, P-Paper, S-Scissors, Q-Quit]: ";
cin >> humchoice;

if (humchoice == 'Q' || humchoice == 'q') break;
if (humchoice == 'R' || humchoice == 'r') break;
if (humchoice == 'P' || humchoice == 'p') break;
if (humchoice == 'S' || humchoice == 's') break;
cout << " INVALID ENTRY. Try again..." << endl;
}
return humchoice;
} // human


char computer()
{
int compchoice = rand()%3;


return compchoice;
}


void results()
{

}



int main ()
{

// initialize the computer's random number generator
srand((unsigned)time(0));


// declare variables
char humchoice;
char compchoice;

// start loop
while (true)
{

// determine computer's choice
compchoice = computer();

// prompt for, and read, the human's choice
humchoice = human();

// if human wants to quit, break out of loop
if (humchoice == 'Q' || humchoice == 'q') break;


// print results
cout << "Computer: " << compchoice << endl;
cout << "Human: " << humchoice << endl;


// end loop
}

// end program


cin.ignore();
cin.get();
return 0;
} // main
Last edited on
Your human() function returns essentially 'R', 'P', or 'S' or the lowercase equivalents.
Your computer() function returns the integers 0, 1, or 2 (in a char variable).

You probably want to make the functions return something similar.

At your current class level, I would suggest making three constants:

const int Rock = 0;
const int Paper = 1;
const int Scissors = 2;

and making human() and computer() return one of those.

As for result(), you just have to write a bunch of if() statements.

Rock beats scissors; scissors beats paper, paper beats rock and
ties are ties.

So result() should take as parameters both the human's choice and
the computer's choice and test all the cases.

Here's two examples:

1
2
3
4
if( humchoice == compchoice )
    cout << "It's a tie!\n";
else if( humchoice == Rock && compchoice == Scissors )
   cout << "Human wins!\n";


i see. ..

where should i implement the "const int rock = ..." stuff?

i am assuming like below?

1
2
3
4
5
6
7
8
9
10
char computer()
{
int compchoice = rand()%3;

const int Rock = 0;
const int Paper = 1;
const int Scissors =2;

return compchoice;
}


how do i make them return those values?

i noticed that when i compile my program (without your suggestions), the computer results in giving me smiley faces such as this...

Choose [R-Rock, P-Paper, S-Scissors, Q-Quit]: R
Computer: ☻
Human: R

im assuming it is returning the integers as characters, like you said.

any suggestions?

thank you.
Put the constants near the top of your program and make them global so that you can use them in several functions.

Then, change your human() function to return Rock if the user pressed R or r, etc. (The function should return an int instead of a char). Example:

1
2
3
4
5
6
int human() {
    // ... get user input ...
    if( choice == 'r' || choice == 'R' )
       return Rock;
    // etc.
}


Then, change your computer() function to do the same (make it return int also).
You _could_ leave the function as is (ie, return rand() % 3) because you
know that the three valid values are 0, 1, and 2 (for Rock, Paper, Scissors).

Next, write a function to output the choice, similar to this:

1
2
3
4
5
void print_choice( int choice ) {
    if( choice == Rock ) 
        cout << "Rock";
    // etc.
}


Last edited on
Topic archived. No new replies allowed.