my current code: #include #include using namespace std; int userschoice, compcho
ID: 3765982 • Letter: M
Question
my current code:
#include
#include
using namespace std;
int userschoice, compchoice, wincode;
int getUserChoice();//select rock paper or scissors. return 1 for rock 2 for paper 3 for scissors.
int getComputerChoice();//returns the games choice and displays winner.
void determineWinner(int, int);//determines winner...
void displayChoice(int);
void displayChoice(int choice)
{
int ROCK = 1;
int PAPER = 2;
int SCISSORS = 3;
if (choice == ROCK)
cout << "Rock ";
else if (choice == PAPER)
cout << "Paper ";
else if (choice == SCISSORS)
cout << "Scissors ";
else
cout << "Error condition detected! ";
}
int getUserChoice(int userschoice, int)
{
int userschoice, Rock = 1, Paper = 2, Scissors = 3, Lizard = 4, Spock = 5;
cout << "Please enter your choice: 1-Rock, 2-Paper, 3-Scissors;";
cin >> userschoice;
if ( userschoice == 1 )
{
cout << "You Chose: Rock" << endl;
}
else if ( userschoice == 2 )
{
cout << "You Chose: Paper" << endl;
}
else if ( userschoice == 3 )
{
cout << "You Chose: Scissors" << endl;
}
int getComputerChoice()
{
I usually go to the computer lab to get help. However, they are closed until monday. please help. thank you
Copy of Tony Gaddis-Starting Out with C++from Control Structures to Objects-Addison-Wesley (2014).pdf - Adobe Acrobat Reader DC File Edit View Window Help Copy of Tony Gaddi...x Sign In Home ools 23. Prime Number List Use the isPrime function that you wrote in Programming Challenge 22 in a program that stores a list of all the prime numbers from 1 through 100 in a file. 24. Rock, Paper, Scissors Game Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. 1. When the program begins, a random number in the range of 1 through 3 is gener- ated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. (Don't display the computer's choice yet.) 2. The user enters his or her choice of "rock", "paper", or "scissors" at the keyboard. (You can use a menu if you prefer.) 3. The computer's choice is displayed. 4. A winner is selected according to the following rules: If one player chooses rock and the other player chooses scissors, then rock wins. (The rock smashes the scissors.) wins. (Scissors cuts paper.) (Paper wraps rock.) If one player chooses scissors and the other player chooses paper, then scissors If one player chooses paper and the other player chooses rock, then paper wins. e If both players make the same choice, the game must be played again to deter- mine the winner. Be sure to divide the program into functions that perform each major task. 6:38 PM 11/25/2015 Search the web and WindowsExplanation / Answer
PROGRAM FOR ROCK,,PAPER,SCISSROS..
#include <iostream>
#include <string>
#include <array>
#include <cstdlib>
#include <ctime>
#include <stdexcept>
#include <utility>
enum Chance { ROCK=1, PAPER, SCISSORS, LIZARD, SPOCK };
enum Outcome { WON, LOSE, DRAW };
typedef std::pair<Chance, Chance> PlayerMoves;
const std::array<PlayerMoves, 10> possibleMoves =
{ std::make_pair(SCISSORS, PAPER),
std::make_pair(PAPER, ROCK),
std::make_pair(ROCK, LIZARD),
std::make_pair(LIZARD, SPOCK),
std::make_pair(SPOCK, SCISSORS),
std::make_pair(SCISSORS, LIZARD),
std::make_pair(LIZARD, PAPER),
std::make_pair(PAPER, SPOCK),
std::make_pair(SPOCK, ROCK),
std::make_pair(ROCK, SCISSORS)
};
std::istream& operator>>(std::istream& in,Chance& chance)
{
int val;
if (in >> val)
chance = static_cast<Chance>(val);
else
throw std::logic_error("invalid chance");
return in;
}
std::ostream& operator<<(std::ostream& out, Chance const& chance)
{
switch (chance)
{
case ROCK: return out << "rock";
case PAPER: return out << "paper";
case SCISSORS: return out << "scissors";
case LIZARD: return out << "lizard";
case SPOCK: return out << "Spock";
default: throw std::logic_error("invalid chance");
}
}
Chance getPlayerChance()
{
std::cout << " (1) -> Rock ";
std::cout << "(2) -> Paper ";
std::cout << "(3) -> Scissors ";
std::cout << "(4) -> Lizard ";
std::cout << "(5) -> Spock ";
Chance chance;
do
{
std::cout << "Now your turne: ";
std::cin >> chance;
} while (chance < ROCK || chance > SPOCK);
return chance;
}
Chance getOpponentChance()
{
return static_cast<Chance>(1 + std::rand() % 5);
}
Outcome determineOutcome(PlayerMoves const& playerMoves)
{
if (playerMoves.first == playerMoves.second)
return DRAW;
for (auto iter = possibleMoves.cbegin(); iter != possibleMoves.cend(); ++iter)
{
if (playerMoves == *iter)
return WON;
}
return LOSE;
}
int main()
{
std::srand(static_cast<unsigned int>(std::time(nullptr)));
std::cout << "Rock, Paper, Scissors, Lizard, Spock ";
unsigned int playerScore = 0;
unsigned int opponentScore = 0;
for (;;)
{
Chance playerChance = getPlayerChance();
ChanceopponentChance = getOpponentChance();
PlayerMoves playerMoves = std::make_pair(playerChance, opponentChance);
std::cout << " You've picked : " << playerChance;
std::cout << " Computer picked: " << opponentChance;
Outcome outcome = determineOutcome(playerMoves);
switch (outcome)
{
case WON:
std::cout << " You won! Point added to you.";
playerScore++;
break;
case LOSE:
std::cout << " You lose! Point added to opponent.";
opponentScore++;
break;
case DRAW:
std::cout << " It's a draw! No points added.";
break;
default:
throw std::logic_error(" invalid outcome");
break;
}
std::cout << " Play again (Y/N)? ";
char w;
std::cin >> w;
if (w == 'n' || w == 'N') break;
}
std::cout << " Final Scores: ";
std::cout << "Player : " << playerScore;
std::cout << " Opponent: " << opponentScore;
}
PROGRAM FOR PRIME NUMBERS:
#include<iostream>
using namespace std;
void main()
{
int n ,i,j,prime;
cout<<"Enter the upper limit :";
cin>>n ;
cout<<"Prime numbers till "<<n <<" are :2, ";
for(i=2;i<=n ;i++)
{
prime=1;
for(j=2;j<i;j++)
{
if(i%j==0)
{
prime=0;
break;
}
}
if(prime==1)
cout<<i<<", ";
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.