I am writing a program to play rock paper scissors, and I am trying to get a vector to store the result of a given round of play, and output it as a declaration as to who won. I can't figure out how to store the values in the vector properly, and how to use the vector as an argument to output the result of play.
Code:#include <iostream> #include <iomanip> #include <ctime> #include <vector> #include <cstdlib> #include <string> using namespace std; const string name[4] = {" ", "rock", "paper", "scissors"}; const string roundResult[3] = {"computer win", "player win", "tie"}; void getPlayerChoice(int & choice) { cout << "Pick 1 (rock), 2 (paper), or 3 (scissors): "; cin >> choice; if (choice <=3 && choice >=1) choice = choice; else cout << "invalid choice, try again\n" << endl; } 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 2; } else if ((playerChoice == 2 && computerChoice == 1) || (playerChoice == 3 && computerChoice == 2) || (playerChoice == 1 && computerChoice == 3)) {return 1; } } 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(vector<int> results, const int size, int games) { cout << "Game Results\n" << endl; cout << "------------\n"; cout << "Round" << setw(3) << "Result\n"; for (games = 0; games < size; games++) { cout << games + 1 << setw(7) << roundResult->at(games) << endl; } } int main() { int games, choice(0), computerChoice, playerChoice, roundResult; const int size = 10; vector<int> results; srand(int(time(NULL))); cout << "CPS 150 Assignment 10 by -----------\n"; cout << "\nLet's play Rock-Paper-Scissors!" << endl; cout << "We will play 10 rounds.\n" << endl; for (games = 0; games < size; games++) { computerChoice = 1 + rand() % 3; getPlayerChoice(choice); playerChoice = choice; roundResult = getRoundResult(playerChoice, computerChoice); results.push_back(roundResult); showRoundResult(computerChoice, playerChoice, roundResult); } showGameResults(results, size, games); }


Reply With Quote
Bookmarks