Question

After finding a text-based rock paper scissors app I wrote, I decided to improve the code. Now, I want to write in for the console to clear the screen before returning to the main menu, and before executing a requested app operation. Here is the code:

#include <iostream>
#include <string>
#include <cstdlib>
#include <time.h>
#include <algorithm>

using namespace std;

void scoreCounter(int playerWins, int playerTies, int playerLosses, int compWins, int compTies, int compLosses)
{
    cout << "PLAYER SCORE:\n\n";
    cout << "Wins: " << playerWins << "\n";
    cout << "Losses: " << playerLosses << "\n";
    cout << "Ties: " << playerTies << "\n\n\n";
    cout << "COMPUTER SCORE:\n\n";
    cout << "Wins: " << compWins << "\n";
    cout << "Losses: " << compLosses << "\n";
    cout << "Ties: " << compTies << "\n\n\n"; 
}

void mainMenu()
{
    cout << "Main Menu\n\n";
    cout << "1) Play Rock Paper Scissors\n";
    cout << "2) Display scoreboard\n";
    cout << "3) Exit app\n";
    cout << "\n";
}

void rockPaperScissorsShoot(int playerWins, int playerTies, int playerLosses, int compWins, int compTies, int compLosses) {
    cout << "Rock Paper Scissors Shoot!\n";
    string playerChoice;
    cin >> playerChoice;
    transform(playerChoice.begin(), playerChoice.end(), playerChoice.begin(), ::tolower);
    int n1;
    int n2;
    int n3;
    srand(time(NULL));
        if (playerChoice == "Rock" || playerChoice == "rock") {
            n1 = rand() %3;
            if (n1 == 0) {
                cout << "You win!\n";
                playerWins++;
                compLosses++;
            }
            else if (n1 == 1) {
                cout << "You lose!\n";
                playerLosses++;
                compWins++;
            }
            else if (n1 == 2) {
                cout << "You tied!\n";
                playerTies++;
                compTies++;
            }
        }
        else if (playerChoice == "Paper" || playerChoice == "paper") {
            n2 = rand() %3;
            if (n2 == 0) {
                cout << "You win!\n";
                playerWins++;
                compLosses++;
            }
            else if (n2 == 1) {
                cout << "You lose!\n";
                playerLosses++;
                compWins++;
            }
            else if (n2 == 2) {
                cout << "You tied!\n";
                playerTies++;
                compTies++;
            }
        }
        else if (playerChoice == "Scissors" || playerChoice == "scissors") {
            n3 = rand() %3;
            if (n3 == 0) {
                cout << "You win!\n";
                playerWins++;
                compLosses++;
            }
            else if (n3 == 1) {
                cout << "You lose!\n";
                playerLosses++;
                compWins++;
            }
            else if (n3 == 2) {
                cout << "You tied!\n";
                playerTies++;
                compTies++;
            }
        }
        else {
            cout << "You made an invalid choice. Ending game...\n";
        }
    }

int main()
{
    bool gameOver = 0;
    bool exitProg = 0;
    int playerWins = 0;
    int playerTies = 0;
    int playerLosses = 0;
    int compWins = 0;
    int compTies = 0;
    int compLosses = 0;
    int menuChoice;
    while (exitProg != 1)
    {
        mainMenu();
        cin >> menuChoice;
        switch (menuChoice)
        {
            case 1:
            {
                while (gameOver == 0) {
                    rockPaperScissorsShoot(playerWins, playerTies, playerLosses, compWins, compTies, compLosses);
                    cout << "Would you like to play again? Press Y or N:\n";
                    char yOrN;
                    cin >> yOrN;
                    switch (yOrN) {
                        case 'Y': {
                            cout << "Ok then!\n";
                            break;
                            }
                        case 'y': {
                            cout << "Ok then!\n";
                            break;
                            }
                        case 'N': {
                            cout << "Game over.\n";
                            gameOver = 1;
                            break;
                        }
                        case 'n': {
                            cout << "Game over.\n";
                            gameOver = 1;
                            break;
                        }
                    }
                }
                cout << "\n";
                break;
            }
            case 2:
            {
                scoreCounter(playerWins, playerTies, playerLosses, compWins, compTies, compLosses);
                break;
            }
            case 3:
            {
                exitProg = 1;
                system("PAUSE");
                break;
            }
        }
    }
    return 0;
}

I considered using cin.clear(), but I don't think that will help me if I want to open a different function. What can I do?

Était-ce utile?

La solution

For anyone who finds this question, I use system("CLS"); to clear the console before loading a different operation.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top