Rock Paper Scissors

manutd 1 Tallied Votes 275 Views Share

This is a simple rock paper scissors game using random numbers.

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

char RPS (void);
double uniform_deviate ( int seed );
void whowins (char real);
int main()
{
    char letter, quit = 'n';
    srand (time(0));
    while (tolower (quit) != 'y')
    {
        cout << "Enter R for rock, P for paper, and S for scissors: ";
        cin >> letter;
        if (letter == 'R' || letter == 'S' || letter == 'P')
            whowins(letter);
        else
            cout << "Enter a valid letter (R, P, or S) :(" << endl;
        cout << "Do you want to quit (y/n): ";
        cin >> quit;
    }
    cin.get();
	return 0;
}

char RPS (void)
{
    int temp = static_cast<int>(uniform_deviate(rand()) * 2);
    if (temp == 0)
        return 'R';
    else if (temp == 1)
        return 'P';
    else if (temp == 2)
        return 'S';
    else
        cout << "Irreconcilable error with the game :(" << endl;
}
double uniform_deviate ( int seed )
{
  return seed * ( 1.0 / ( RAND_MAX + 1.0 ) );
}

void whowins (char real)
{
    char comp = RPS();
    if (comp == 'R')
    {
        if (real == 'R')
            cout << "Tie! (computer: " << comp << " yours: " << real << ")" << endl;
        else if (real == 'P')
            cout << "You win! (computer: " << comp << " yours: " << real << ")" << endl;
        else
            cout << "You lose! (computer: " << comp << " yours: " << real << ")" << endl;
    }
    else if (comp == 'P')
    {
        if (real == 'P')
            cout << "Tie! (computer: " << comp << " yours: " << real << ")" << endl;
        else if (real == 'S')
            cout << "You win! (computer: " << comp << " yours: " << real << ")" << endl;
        else
            cout << "You lose! (computer: " << comp << " yours: " << real << ")" << endl;
    }
    else if (comp == 'S')
    {
        if (real == 'S')
            cout << "Tie! (computer: " << comp << " yours: " << real << ")" << endl;
        else if (real == 'R')
            cout << "You win! (computer: " << comp << " yours: " << real << ")" << endl;
        else
            cout << "You lose! (computer: " << comp << " yours: " << real << ")" << endl;
    }
    else
        cout << "Internal error :(" << endl;
}