-
November 28th, 2012, 06:53 PM
#1
Assigning values to and extracting them from a vector
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);
}
-
November 29th, 2012, 05:32 AM
#2
Re: Assigning values to and extracting them from a vector
Originally Posted by Slaughtter
I can't figure out how to store the values in the vector properly
Code:
results.push_back(roundResult);
and how to use the vector as an argument to output the result of play.
Code:
showGameResults(results, size, games);
The only remark I have is that you should pass the vector by const reference to showGameResults, rather than by value. Also, an instance of vector already knows its own size.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|