Please write a c++ code for my pseudo code. I am take a class that is teaching m
ID: 3722952 • Letter: P
Question
Please write a c++ code for my pseudo code. I am take a class that is teaching me flow charts and pseudocode and I would like to see the actual code so that I can start preparing myself for next semester’s classes. Here is the pseudo code:
// main module
Module main()
// Local variables
Declare Integer computer, player
Declare String playerChoice, computerChoice
// Get computer number
Set computer = Rand(1, 3)
// Get player choice
Call getNumber(player)
// Show winner
Call showWinner(computer, player)
End Module
// The getNumber module gets an integer
Module getNumber(Integer Ref inputAnswer)
Declare String Choice
Display “Enter rock, paper, or scissors: “
Input Choice
// validate choice
While toLower(Choice) != “rock” AND toLower(Choice) != “paper” AND toLower(Choice) != “scissors”
Display “Enter rock, paper, or scissors: “
Display “Enter a valid quantity.”
Input Choice
End While
If Choice == rock” Then
Set inputAnser = 1
Endif
If Choice == paper” Then
Set inputAnser = 2
Endif
If Choice == scissors” Then
Set inputAnser = 3
Endif
End Module
// The showWinner module shows if number is a prime
Module showWinner(Integer computer, player)
Declare Integer which
Display “Computer chose “, whichChoice(computer)
If computer == player Then
Display “Player made same choice. Play again.”
Else
Set which = whoWon(computer, player)
If which == 1 Then
Display “Computer wins.”
Else
If which == 2 Then
Display “Player wins.”
Else
Display “No winner.”
End If
End If
End If
End Module
// The whichChoice function displays choice
Function String whichChoice (Integer number)
If number == 1 Then
Return “rock”
Else
If number == 2 Then
Return “paper”
Else
Return “scissors
End If
End Function
// The whoWon function selects winner
Function Integer whoWon(computer, player)
// 1 = rock, 2 = paper, 3 = scissors
// rock smashes scissors
// scissors cuts paper
// paper wraps rock
// both choose same no winner
If computer == player then
Return 0
End If
// test computer chose rock
If computer == 1 then
If player == 2 then
Return 2 // paper wraps rock
End If
Else
If player == 3 then
Return 1 // rock smashes scissors
End If
End If
// test computer chose paper
If computer == 2 then
If player == 1 then
Return 1 // paper wraps rock
End If
Else
If player == 3 then
Return 2 // scissors cuts paper
End If
End If
// test computer chose scissors
If computer == 3 then
If player == 1 then
Return 2 // rock smashes scissors
End If
Else
If player == 2 then
Return 1 // scissors cuts paper
End If
End If
Return 0
End Function
Output should look like this:
Type selection, 'rock', 'paper' OR 'scissors' - 'q' to QUIT
rok
Sorry, wrong input...
Type selection, 'rock', 'paper' OR 'scissors' - 'q' to QUIT
ROCK
Sorry, wrong input...
Type selection, 'rock', 'paper' OR 'scissors' - 'q' to QUIT
rock
LOSE - User: rock Computer: paper
Type selection, 'rock', 'paper' OR 'scissors' - 'q' to QUIT
rock
TIE - User: rock Computer: rock
Type selection, 'rock', 'paper' OR 'scissors' - 'q' to QUIT
rock
WIN - User: rock Computer: scissors
Type selection, 'rock', 'paper' OR 'scissors' - 'q' to QUIT
q
Come Back Soon
Explanation / Answer
//as per output.
//In algorithm if player choses ROCK, it conver into lowercase then it compared. in output ROCK is not accepted...
#include <iostream>
#include <cstdlib>
#include <string>
#include <time.h>
using namespace std;
//The getNumber module gets an integer
void getNumber(int *inputAnswer)
{
string Choice;
cout << "Type selection, 'rock', 'paper' OR 'scissors' - 'q' to QUIT" << endl;
cin >> Choice;
if (Choice == "q")
{
cout << "Come Back Soon" << endl;
exit(1);
}
while (Choice != "rock" && Choice != "paper" && Choice != "scissors")
{
cout << "Sorry, wrong input..." << endl;
cout << "Type selection, 'rock', 'paper' OR 'scissors' - 'q' to QUIT" << endl;
cin >> Choice;
}
if (Choice == "rock")
*inputAnswer = 1;
if (Choice == "paper")
*inputAnswer = 2;
if (Choice == "scissors")
*inputAnswer = 3;
}
//The whichChoice function displays choice
string whichChoice(int number)
{
if (number == 1)
return "rock";
else if (number == 2)
return "paper";
else
return "scissors";
}
// The whoWon function selects winner
int whoWon(int computer, int player)
{
// 1 = rock, 2 = paper, 3 = scissors
// rock smashes scissors
// scissors cuts paper
// paper wraps rock
// both choose same no winner
if (computer == player)
return 0;
// test computer chose rock
if (computer == 1)
if (player == 2)
return 2;
else
{
if (player == 3)
return 1;
}
// test computer chose paper
if (computer == 2)
if (player == 1)
return 1;
else
{
if (player == 3)
return 2;
}
// test computer chose scissors
if (computer == 3)
if (player == 1)
return 2;
else
{
if (player == 2)
return 1;
}
return 0;
}
//The showWinner module shows if number is a prime
void showWinner(int computer, int player)
{
int which;
if (computer == player)
cout << "TIE";
else
{
which = whoWon(computer, player);
if (which == 1)
cout << "LOSE";
else if (which == 2)
cout << "WIN";
else
cout << "TIE";
}
cout << " - User:" << whichChoice(player) << " Computer: " << whichChoice(computer) << endl;
}
int main()
{
//to generate random number every time
srand(time(NULL));
int computer, player;
string playerChoice, computerChoice;
while (1)
{
computer = (rand() % 3) + 1;
getNumber(&player);
showWinner(computer, player);
}
return 0;
}