Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

1
Need Some Help With This Rock Paper Scissors Program
Post Body

I'm supposed to write a rock paper scissors game using do while loops. I'm almost done but I can't figure out two things:

1) I can't get the program to announce lost games (it announces lost rounds but not lost games). A game is the first to two wins.

2) I am supposed to use a while loop to prevent ties. I thought something like putting "while (playerChoice != cpuChoice)" under the cpuChoice declaration and then putting the rest of the gameplay loop in curly brackets would work but it created an infinite loop.

Here's my code:

/*

Rock, Paper, Scissors

*/

#include <iostream> //for cout and cin

#include <ctime> //seeds random

using namespace std;

int main()

{

cout << "\\n Rock, Paper, Scissors";

cout << "\\n\\nIn this game the player chooses rock(1), paper(2) or scissors(3) and so does the computer."

    << "\\nScissors beats paper, rock beats scissors, paper beats rock."

    <<"\\nFirst to 2 wins takes the round and there are no ties";

srand(time(NULL));

int playerChoice;

int win = 0;

int lose = 0;

int goAgain;

int gameWin = 0;

int gameLoss = 0;

do

{

    cout << "\\n\\nLet's play Rock, Paper, Scissors!" << endl;

    do

    {

        cout << "\\nPress 1 for rock, 2 for paper, 3 for scissors:" << endl;

        cin >> playerChoice;

        cin.ignore();

        int cpuChoice = rand() % 3   1;

        cout << "\\nThe computer chose: " << cpuChoice << endl;

        if (playerChoice == 1 && cpuChoice == 3)

        {

cout << "You chose rock, the computer chose scissors. You win!" << endl;

win ;

        }

        else if (playerChoice == 2 && cpuChoice == 1)

        {

cout << "You chose paper and the computer chose rock. You Win!" << endl;

win ;

        }

        else if (playerChoice == 3 && cpuChoice == 2)

        {

cout << "You chose scissors and the computer chose paper. You win!" << endl;

win ;

        }

        else if (playerChoice == 1 && cpuChoice == 2)

        {

cout << "You chose rock and the computer chose paper. You lost :(" << endl;

lose ;

        }

        else if (playerChoice == 2 && cpuChoice == 3)

        {

cout << "You chose paper and the computer chose scissors. You lost :(" << endl;

lose ;

        }

        else if (playerChoice == 3 && cpuChoice == 1)

        {

cout << "you chose scissors and the computer chose rock. You lost :(" << endl;

lose ;

        }

    } while (win < 2 && lose < 2);

    if (win = 2)

    {

        cout << "Congratulations, you won the game!" << endl;

        gameWin  ;

    }

    else if (lose = 2)

    {

        cout << "Looks like you lost the game :(" << endl;

        gameLoss  ;

    }

    cout << "\\nWant to play again? Y/N";

    cin >> goAgain;

    cin.ignore();

} while (goAgain == 'Y');

return 0;

}

Author
Account Strength
100%
Account Age
7 years
Verified Email
Yes
Verified Flair
No
Total Karma
13,043
Link Karma
3,290
Comment Karma
9,684
Profile updated: 5 days ago
Posts updated: 5 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
5 years ago