Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Revenge of Rock, Paper, Scissors! Revisit your Rock, Paper, Scissors game from l

ID: 3575824 • Letter: R

Question

Revenge of Rock, Paper, Scissors!

Revisit your Rock, Paper, Scissors game from last week, using principles of modularity to improve the code. Everything else should stay the same, but you're going to make the code a lot more readable and maintainable by making use of the following methods:

//public static int promptForInput(string prompt)
//Prompts for user input, displaying "<prompt>". User response will be passed back to calling program
//BLANK input yields error window & prog exit; Otherwise, return 1 = 'rock', 2 = 'paper', 3 = 'scissors',
// 4 = 'yes', 5 = other than previous 1-4;


//public static int generateAutoSelect()
//Generates a uniformly distributed random integer between 1 and 3, inclusive, as an automated
//game selection. For each integer generated, the method displays text in the console window
//corresponding with the selection of 1 - rock, 2 - paper, 3 - scissors


//public static void judgeRockPaperScissors(int player1Sel, int player2Sel)
//Takes two integer inputs - player1Sel and player2Sel - and displays the result of a single
//Rock, Paper, Scissors game round (declare winner and print appropriate game statement,
//ie. "rock smashes scissors") assuming the integers are each in the range 1-3, where
//1 = rock, 2 = paper, 3 = scissors.

Because you'll be creating a function for all user input, you will continue with the assumptions from previous assignments - checking for different combos of capital letters in "yes" response; if user doesn't answer request to play then display a pop-up window; etc. - but you'll find that ALL possibilities of erroneous user input are addressed in this program, just by nature of writing the input function, standalone. Our previous homework had allowed the user to input incorrect things and make the program crash.

HINT: TryParse() can help you implement the functionality in a more streamlined fashion.

Here is my previous code:

using System;

namespace CSharpApplication
{
class RockPaperScissors
{
static void Main(string[] args)
{
//choices of player 1 and player 2
string player1Choice, player2Choice;
//process choices of players
Random r = new Random();
string[] options = {"rock", "scissors", "paper"};
while (true)
{
Console.WriteLine("Do you want to play rock, paper or scissors?");
string choice = Console.ReadLine();
if (!choice.Equals("yes"))
{
System.Environment.Exit(1);
}

  
int i = r.Next(0, 2);
player1Choice = options[i];
Console.WriteLine("First Player (Computer) Selection :"+ player1Choice);
Console.WriteLine("Second Player Selection (1-rock 2-scissors 3-paper) :");
int ch = Convert.ToInt32(Console.ReadLine());
if (ch == 1)
{
player2Choice = "rock";
}
else if (ch == 2)
{
player2Choice = "scissors";
}
else if (ch == 3)
{
player2Choice = "paper";
}
else
{
Console.WriteLine("You must choose either rock, paper or scissors!.");
continue;
}

//the game's logic starts base on the player's choices.
if (player2Choice == player1Choice)
{
Console.WriteLine("its a draw!.");
}
else if (player1Choice == "rock" & player2Choice == "scissors")
{
Console.WriteLine("rock beats scissors.");
Console.WriteLine("Player 1 wins!.");
}
else if (player1Choice == "rock" & player2Choice == "paper")
{
Console.WriteLine("Paper engulfs rock.");
Console.WriteLine("Player 2 wins!.");
}
else if (player1Choice == "paper" & player2Choice == "scissors")
{
Console.WriteLine("Scissors cuts paper.");
Console.WriteLine("Player 2 wins!.");
}
else if (player1Choice == "paper" & player2Choice == "rock")
{
Console.WriteLine("Paper catches rock.");
Console.WriteLine("Player 1 wins!.");
}
else if (player1Choice == "scissors" & player2Choice == "rock")
{
Console.WriteLine("Rock destroys scissors.");
Console.WriteLine("Player 2 wins!.");
}
else if (player1Choice == "scissors" & player2Choice == "paper")
{
Console.WriteLine("Scissors cuts paper.");
Console.WriteLine("Player 1 wins!.");
}
}// while end
}
}
}

Explanation / Answer

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Test { static void Main(string[] args) { Console.WriteLine("choose rock,paper or scissors"); string userChoice = Console.ReadLine(); Random r = new Random(); int computerChoice = r.Next(4); if (computerChoice == 1) { if (userChoice == "rock") { Console.WriteLine("The computer chose rock"); Console.WriteLine("It is a tie "); } else if (userChoice == "paper") { Console.WriteLine("The computer chose paper"); Console.WriteLine("It is a tie "); } else if (userChoice == "scissors") { Console.WriteLine("The computer chose scissors"); Console.WriteLine("It is a tie "); } else { Console.WriteLine("You must choose rock,paper or scissors!"); } } else if (computerChoice == 2) { if (userChoice == "rock") { Console.WriteLine("The computer chose paper"); Console.WriteLine("Sorry you lose,paper beat rock"); } else if (userChoice == "paper") { Console.WriteLine("The computer chose scissors"); Console.WriteLine("Sorry you lose,scissors beat paper "); } else if (userChoice == "scissors") { Console.WriteLine("The computer chose rock"); Console.WriteLine("Sorry you lose,rock beats scissors"); } else { Console.WriteLine("You must choose rock,paper or scissors!"); } } else if (computerChoice == 3) { if (userChoice == "rock") { Console.WriteLine("The computer chose scissors"); Console.WriteLine("You win,rock beats scissors"); } else if (userChoice == "paper") { Console.WriteLine("The computer chose rock"); Console.WriteLine("You win,paper beats rock"); } else if (userChoice == "scissors") { Console.WriteLine("The computer chose paper"); Console.WriteLine("You win,scissors beat paper"); } else { Console.WriteLine("You must choose rock,paper or scissors!"); } } Console.ReadLine(); } } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote