I wrote a code that play rock, paper, scissors in with the user in C programming
ID: 3801321 • Letter: I
Question
I wrote a code that play rock, paper, scissors in with the user in C programming and put it below. How would I use my rock, paper, scissors code to do the rest of the 4 bullet points in C Programming in the image posted? Please if possible screenshot how it would look like with it compiled. Thanks.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int userMove;
int computerMove;
srand(time(NULL));
printf("This is a rock, paper, and scissor game. Please enter a move(1=Rock, 2 =Paper, 3=Scissors):");
scanf("%d", &userMove);
printf(" Computer chooses ");
computerMove=1+rand()%3;
if(computerMove==1)
{
printf("Rock! ");
}
else if(computerMove==2)
{
printf("Paper! ");
}
else if(computerMove==3)
{
printf("Scissor! ");
}
else printf("Tie!");
//{
if ((userMove==1)&&(computerMove==1))
{
printf(" Tie!");
}
else if ((userMove==1)&&(computerMove==2))
{ printf(" User Wins!");
}
else if ((userMove==1)&&(computerMove==3))
{ printf(" Computer Wins!");
}
else if((userMove==2)&&(computerMove==1))
{ printf(" User Wins!");
}
else if((userMove==2)&&(computerMove==2))
{ printf(" Tie!");
}
else if((userMove==2)&&(computerMove==3))
{ printf(" Computer Wins!");
}
else if((userMove==3)&&(computerMove==1))
{ printf(" Computer Wins!");
}
else if((userMove==3)&&(computerMove==2))
{ printf(" User Wins!");
}
else if((userMove==3)&&(computerMove==3))
{ printf(" Tie!");
}
else
{ printf("You have select an invalid selection.");
}
}
Goals The goals for you to accomplish during class today are as follows: 1. Write your first function! Start with the code you wrote that plays Rock, Paper, Scissors with the user. Update this code to make a FUNCTION called ComputerMove that displays the computers move (randomly selected 1, 2, or 3) and returns it Write another FUNCTION called UserMove that asks the user for their move and returns their selection (1, 2, or 3) In your main(), call each function once and display the winner (or tie) Make sure your code compiles and runsExplanation / Answer
#include<iostream>
#include<string>
#include <ctime>
#include <cstdlib>
#include <cctype>
using namespace std;
char computer()
{
char computer;
computer = (rand() % 3);
if (computer == 0)
computer = 'R';
else if (computer == 1)
computer = 'P';
else computer = 'S';
return computer;
} // end of computer funtion
char player()
{
char Lplayer;
cout << "Welcome to Rock Paper Scissors! Please select a move: R = Rock, P = Paper, S = Scissors, and Q = Quit. " << endl;
cin >> Lplayer;
cin.ignore(1000, 10);
if (Lplayer == "0")
Lplayer = "R";
else if (Lplayer == "1")
Lplayer == "P";
else if (Lplayer == "2")
Lplayer == "S";
else Lplayer == "Q";
return Lplayer;
}
char playersel;
char computersel;
void winner (char playersel, char computersel)
{
if (playersel == 0)
{
if (computersel == 0)
cout << "It's a tie! ";
else if (computersel == 1)
cout << "Paper beats rock! Sorry, you lose! ";
else if (computersel == 2)
cout << "Rock beats scissors! You win! ";
}
else if (playersel == 1)
{
if (computersel == 0)
cout << "It's a tie! ";
else if (computersel == 1)
cout << "Paper beats rock! You win! ";
else if (computersel == 2)
cout << "Scissors beat paper! Sorry, you lose! ";
}
else if (playersel == 2)
{
if (computersel == 0)
cout << "It's a tie! ";
else if (computersel == 1)
cout << "Scissors beat paper! You win! ";
else if (computersel == 2)
cout << "Rock beats scissors! Sorry, you lose! ";
}
}
int main()
{
srand(time(0));
char getplayer;
char getcomputer;
while (true)
{
getplayer = player();
getcomputer = computer();
if (toupper(player) == 'Q') break;
winner(getplayer,getcomputer);
}
return 0;
}