| Lauren Zaj (2) | |
|
I am having trouble receiving the correct results with my rock-paper-scissors program. It is simply stating the player wins every time, and I cannot find the problem. #include <iostream> #include <ctime> #include <cstdlib> #include <string> #include <iomanip> using namespace std; const string name[4] = { " ", "rock", "paper", "scissors"}; const int size = 10; void getPlayerChoice(int & choice){ cout <<"Pick 1 (rock), 2 (paper), 3 (scissors): "; cin >> choice; while ((choice < 1) || (choice > 3)){ cout << "Invalid choice, try again.\n"; cout <<"Pick 1 (rock), 2 (paper), 3 (scissors): "; cin >> choice; } } int getRoundResult(int computerChoice, int playerChoice){ if(computerChoice == playerChoice) return 3; else if( (playerChoice == 1 && computerChoice == 2) || (playerChoice == 2 && computerChoice == 3) || (playerChoice == 3 && computerChoice == 1) ) return 1; else if ( (playerChoice == 2 && computerChoice == 1) || (playerChoice == 3 && computerChoice == 2) || (playerChoice == 1 && computerChoice == 3) ) return 2; } void showRoundResult(int computerChoice, int playerChoice, int roundResult){ if(roundResult == 3) cout << " I chose " << name[computerChoice] << " too, so we tied.\n\n"; else if (roundResult == 1) {cout << " I chose " << name[computerChoice] << ", so I win! " << name[computerChoice] << " beats " << name[playerChoice] << ".\n\n";} else if (roundResult = 2) {cout << "I chose " << name[computerChoice] << ", so you win! " << name[playerChoice] << " beats " << name[computerChoice] << ".\n\n";} } void showGameResults( const int results[] , const int size){ cout << " Game results\n"; cout << "--------------\n"; cout << left << setw(8) << "Round" << left << setw(10) << "Result" << endl; cout << left << setw(8) << "-----" << left << setw(10) << "------" << endl; for ( int lcv(0) ; lcv < 10; lcv++) {cout << left << setw(8) << (lcv + 1); if( results[lcv] == 3) cout << left << setw(10) << "Tie" << endl; else if ( results[lcv] == 1) cout << left << setw(10) << "Computer win" << endl; else cout << left << setw(10) << "Player win" << endl; } } int main(){ int roundResults[10]; int computerChoice, playerChoice, computerPoints = 0, playerPoints = 0, result, round = 0; srand(time (NULL)); cout << "Let's play Rock-Paper-Scissors!\n"; cout << "We will play 10 rounds.\n\n"; while( round < 10){ computerChoice = 1 + rand() % 3; getPlayerChoice(playerChoice); roundResults[round] = getRoundResult(computerChoice, playerChoice); if(roundResults[round] = 2) playerPoints++; else if (roundResults[round] = 1) computerPoints++; result = roundResults[round]; showRoundResult(computerChoice,playerChoice, result); round++; } showGameResults( roundResults , size); cout << " Let's see how we did : \n"; cout << " You won " << playerPoints << " rounds and I won " << computerPoints << " rounds.\n"; cout << ( size - (computerPoints + playerPoints)) << " rounds were tied\n\n"; if(playerPoints > computerPoints){ cout << " Congratulations! You're the champ!\n\n";} else if(playerPoints < computerPoints){ cout << " Sorry, you lost.\n\n";} else cout << " We Tied!\n\n"; } | |
|
Last edited on
|
|