I am using the following method which works but wondering if there is a better algorithm to perform the test. Is there a better way to do it? Doing this in C# but putting syntax aside, believe the algorithm is going to be the same across OOP languages. Thank you.
public String play(int userInput)
{ //ComputerIn is a randomly generated number between 1-3
ComputerIn = computerInput();
if (ComputerIn == userInput)
return "Draw";
else if (ComputerIn == 1 && userInput == 2)
return "Win";
else if (ComputerIn == 2 && userInput == 3)
return "Win";
else if (ComputerIn == 3 && userInput == 1)
return "Win";
else if (ComputerIn == 1 && userInput == 3)
return "Lose";
else if (ComputerIn == 2 && userInput == 1)
return "Lose";
else
return "Lose";
}
if ((ComputerIn) % 3 + 1 == userInput)
return "Win";
else if ((userInput) % 3 + 1 == ComputerIn)
return "Lose"
else
return "Draw"
If you wrap 3 around to 1 (using %) then the winner is always 1 greater than the loser.
This approach is more natural when you use 0-2, in which case we would use (ComputerIn+1)%3. I came up with my answer by subbing ComputerIn with ComputerIn-1 and UserInput with UserInput-1 and simplifying the expression.
Edit, looking at this question after a long time. As written, if the ComputerIn is not used anywhere else, and is only used to determine win/lose/draw, then this method is actually equivalent to:
if (ComputerIn == 1)
return "Win";
else if (ComputerIn == 2)
return "Lose"
else
return "Draw"
This can even be further simplified to
return new String[]{"Win", "Lose", "Draw"}[ComputerIn-1];
The results from this are entirely indistinguishable. Unless the randomly generated number is exposed to outside of this method. No matter what your input is, there's always 1/3 chance of all possibilities. That is, what you're asking for, is just a complicated way of returning "Win", "Lose", or "Draw" with equal probability.
Here's one of many possible solutions. This will print Win.
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Input userInput = Input.Rock;
Result result = Play(userInput);
Console.WriteLine(Enum.GetName(result.GetType(), result));
Console.ReadKey();
}
static Result Play(Input userInput)
{
Input computer = Input.Scissors;
switch (userInput)
{
case Input.Paper:
switch (computer)
{
case Input.Paper: return Result.Draw;
case Input.Rock: return Result.Win;
case Input.Scissors: return Result.Lose;
default: throw new Exception("Logic fail.");
}
case Input.Rock:
switch (computer)
{
case Input.Paper: return Result.Lose;
case Input.Rock: return Result.Draw;
case Input.Scissors: return Result.Win;
default: throw new Exception("Logic fail.");
}
case Input.Scissors:
switch (computer)
{
case Input.Paper: return Result.Win;
case Input.Rock: return Result.Lose;
case Input.Scissors: return Result.Draw;
default: throw new Exception("Logic fail.");
}
default: throw new Exception("Logic fail.");
}
}
}
enum Input
{
Rock,
Paper,
Scissors
}
enum Result
{
Lose,
Draw,
Win
}
}
This is how I would do it:
public class Program
{
public enum RPSPlay { Rock, Scissors, Paper }
public enum RPSPlayResult { Win, Draw, Loose }
public static readonly int SIZE = Enum.GetValues(typeof(RPSPlay)).Length;
static RPSPlayResult Beats(RPSPlay play, RPSPlay otherPlay)
{
if (play == otherPlay) return RPSPlayResult.Draw;
return ((int)play + 1) % SIZE == (int)otherPlay
? RPSPlayResult.Win
: RPSPlayResult.Loose;
}
static void Main(string[] args)
{
Random rand = new Random();
while (true)
{
Console.Write("Your play ({0}) (q to exit) : ", string.Join(",", Enum.GetNames(typeof(RPSPlay))));
var line = Console.ReadLine();
if (line.Equals("q", StringComparison.OrdinalIgnoreCase))
return;
RPSPlay play;
if (!Enum.TryParse(line, true, out play))
{
Console.WriteLine("Invalid Input");
continue;
}
RPSPlay computerPlay = (RPSPlay)rand.Next(SIZE);
Console.WriteLine("Computer Played {0}", computerPlay);
Console.WriteLine(Beats(play, computerPlay));
Console.WriteLine();
}
}
}
I would prefer to use a static 3x3 matrix to store the possible outcomes. But it is a question of taste, and I am a mathematician.
Here is one-liner that we created at lunchtime.
using System;
public class Rps {
public enum PlayerChoice { Rock, Paper, Scissors };
public enum Result { Draw, FirstWin, FirstLose};
public static Result Match(PlayerChoice player1, PlayerChoice player2) {
return (Result)((player1 - player2 + 3) % 3);
}
public static void Main() {
Rps.Test(Match(PlayerChoice.Rock, PlayerChoice.Rock), Result.Draw);
Rps.Test(Match(PlayerChoice.Paper, PlayerChoice.Paper), Result.Draw);
Rps.Test(Match(PlayerChoice.Scissors, PlayerChoice.Scissors), Result.Draw);
Rps.Test(Match(PlayerChoice.Rock, PlayerChoice.Scissors), Result.FirstWin);
Rps.Test(Match(PlayerChoice.Rock, PlayerChoice.Paper), Result.FirstLose);
Rps.Test(Match(PlayerChoice.Paper, PlayerChoice.Rock), Result.FirstWin);
Rps.Test(Match(PlayerChoice.Paper, PlayerChoice.Scissors), Result.FirstLose);
Rps.Test(Match(PlayerChoice.Scissors, PlayerChoice.Paper), Result.FirstWin);
Rps.Test(Match(PlayerChoice.Scissors, PlayerChoice.Rock), Result.FirstLose);
}
public static void Test(Result sample, Result origin) {
Console.WriteLine(sample == origin);
}
}
\From A java beginner Perspective. User plays with the computer to infinity.
import java.util.Scanner;
public class AlgorithmDevelopmentRockPaperScissors{
public static void main(String[] args){
System.out.println("\n\nHello Eric today we are going to play a game.");
System.out.println("Its called Rock Paper Scissors.");
System.out.println("All you have to do is input the following");
System.out.println("\n 1 For Rock");
System.out.println("\n 2 For Paper");
System.out.println("\n 3 For Scissors");
int loop;
loop = 0;
while (loop == 0){
System.out.println("\n\nWhat do you choose ?");
int userInput;
Scanner input = new Scanner(System.in);
userInput = input.nextInt();
while (userInput > 3 || userInput <= 0 ){ //ensure that the number input by the sure is within range 1-3. if else the loop trap.
System.out.println("Your choice "+userInput+" is not among the choices that are given. Please enter again.");
userInput = input.nextInt();
}
switch (userInput){
case 1:
System.out.println("You Chose Rock.");
break;
case 2:
System.out.println("You Chose Paper.");
break;
case 3:
System.out.println("You Chose Scissors");
break;
default:
System.out.println("Please Choose either of the choices given");
break;
}
int compInput;
compInput = (int)(3*Math.random()+1);
switch (compInput){
case 1:
System.out.println("\nComputer Chooses Rock.");
break;
case 2:
System.out.println("\nComputer Chooses Paper.");
break;
case 3:
System.out.println("\nComputer Chooses Scissors");
break;
}
if (userInput == compInput){
System.out.println(".........................................");
System.out.println("\nYou Both chose the same thing, the game ends DRAW.");
System.out.println(".........................................");
}
if (userInput == 1 && compInput == 2){
System.out.println(".........................................");
System.out.println("\nComputer wins because Paper wraps rock.");
System.out.println(".........................................");
}
if (userInput == 1 && compInput == 3){
System.out.println(".........................................");
System.out.println("\nYou win because Rock breaks Scissors.");
System.out.println(".........................................");
}
if (userInput == 2 && compInput == 1){
System.out.println(".........................................");
System.out.println("\nYou win Because Paper wraps Rock");
System.out.println(".........................................");
}
if (userInput == 2 && compInput == 3){
System.out.println(".........................................");
System.out.println("\nComputer wins because Scissors cut the paper");
System.out.println(".........................................");
}
if (userInput == 3 && compInput == 1){
System.out.println(".........................................");
System.out.println("\nComputer Wins because Rock Breaks Scissors.");
System.out.println(".........................................");
}
if (userInput == 3 && compInput == 2){
System.out.println(".........................................");
System.out.println("\nYou win because scissors cut the paper");
System.out.println(".........................................");
}
}
}
}
A simple JavaScript implementation using sine wave function to calculate result:
<script>
var tab = ["Lose","Draw","Win"];
var dict = ["Paper","Stone","Scissors"];
var i,text = '';
for (i = 0; i < dict.length; i++) {
text += i + '-' + dict[i] + ' ';
}
var val1 = parseInt(prompt(text));
var val2 = Math.floor(Math.random() * 3);
alert('Computer chose: ' + dict[val2]);
result = Math.sin((val1*180+1) - (val2*180+1));
alert(tab[Math.round(result)+1]);
</script>
No if's required, just for fun...
Check it
Observation: The user wins if userInput is only one ahead of computerInput (case of (1,2), (2,3)) or lag two (case of (3,1)).
Conversely, if userInput lags one behind computerInput or two ahead, the user loses.
In the modulo 3, the lag one is the same as the advance two.
(-1 mod 3 == 2 mod 3 == 2)
int decision = (userInput - computerInput + 3) % 3;
String[] answer = {"Draw", "Win", "Lose"};
return answer[decision];
Related
I am working on a project for class. It is my first time coding in C# and have ran into an issue. I need to randomly generate a number (1,2, or 3) for a rock paper scissors game but the program keeps outputting 3 and not a random number. Here is my code. Any suggestions on why this is occurring?
using System;
class elevator{
public static void Main(string[] args){
string response;
// Create an instance of the random class
Random random = new Random();
// Return a random non negative interger. Max is set to 4 as it is exclusive and will set the true max to 3.
int compChoice = random.Next(1, 4);
Console.Write("Do you want to play Rock, Paper, Scissors? ");
response = Console.ReadLine();
response = response.ToUpper();
while(response == "YES"){
// If statements displaying auto generated play for first player.
if(compChoice == 1){
Console.WriteLine("First player <computer> Selection - Rock");
}
if(compChoice == 2){
Console.WriteLine("First player <computer> Selection - Paper");
}
if(compChoice == 3){
Console.WriteLine("First player <computer> Selection - Scissors");
}
// Allow user to make selection
Console.Write("Second Player Selection - <Type 1,2, or 3. Rock = 1, Paper = 2, or Scissors = 3>: ");
int secondPlayer = Convert.ToInt32(Console.ReadLine());
// Determine Winner
if (secondPlayer == 1 & compChoice == 1) {
Console.WriteLine("You both chose rock!");
}
if (secondPlayer == 1 & compChoice == 2) {
Console.WriteLine("Player two wins! Paper covers rock.");
}
if (secondPlayer == 1 & compChoice == 3) {
Console.WriteLine("Player one wins! Rock smashes scissors.");
}
if (secondPlayer == 2 & compChoice == 1) {
Console.WriteLine("Player one wins! Paper covers rock.");
}
if (secondPlayer == 2 & compChoice == 2) {
Console.WriteLine("You both chose paper!");
}
if (secondPlayer == 2 & compChoice == 3) {
Console.WriteLine("Player two wins! Scissors cut paper.");
}
if (secondPlayer == 3 & compChoice == 1) {
Console.WriteLine("Player two wins! Rock smashes scissors.");
}
if (secondPlayer == 3 & compChoice == 2) {
Console.WriteLine("Player one wins! Scissors cut paper.");
}
if (secondPlayer == 3 & compChoice == 3) {
Console.WriteLine("You both chose scissors!");
}
// Ask user if they want to play another round
Console.Write("Do you want to play Rock, Paper, Scissors? ");
response = Console.ReadLine();
// Convert response to all caps
response = response.ToUpper();
}
Console.WriteLine("Thanks for playing!");
}
}
You need to put the random number generation in the loop :
while (response == "YES") {
int compChoice = random.Next(1, 4);
Otherwise, it will generate the number once and take that one all the time
See random.Next as "Random, can I get the next random number", as you did it with the Console.ReadLine() for the second player
I'm trying to compile this code but I get an "not all code paths return a value" in judgeRockPaperScissors(). I need the three methods to work in the main method. I not sure what is wrong. I was having issues with converting int to strings as well. Any help would be great! Thank you!
using System;
using System.Windows.Forms;
class RockPaperScissors
{
static string response;
static string respond;
static string player1Sel;
static int player2Sel;
static int result;
static Random numberGenerator = new Random(); //Generates a random number.
public static void Main(string[] args)
{
Console.Write("Do you want to play Rock, Paper, Scissors?");// User writes yes or anything else.
respond = Console.ReadLine();
respond = respond.ToUpper(); //Makes the responce uppercase.
while (respond == "YES")
{ //Beginning of "while loop".
player1Sel = promptForInput();
player2Sel = generateAutoSelect();
result = judgeRockPaperScissors();
switch (result)
{
case 00:
Console.WriteLine("Draw!");
break;
case 12:
Console.WriteLine("Paper covers rock. Player 2 Wins!");
break;
case 23:
Console.WriteLine("Scissors cut paper. Player 2 Wins!");
break;
case 31:
Console.WriteLine("Rock smashes scissors. Player 2 Wins!");
break;
case 13:
Console.WriteLine("Rock smashes scissors. Player 1 Wins!");
break;
case 21:
Console.WriteLine("Paper covers rock. Player 1 Wins!");
break;
case 32:
Console.WriteLine("Scissors cut paper. Player 1 Wins!");
break;
}// End of switch.
Console.Write("Do you want to play again?");// Where the player decides to play again.
respond = Console.ReadLine();
respond = respond.ToUpper();
} //End of "while loop".
} //End of Main.
private static int judgeRockPaperScissors()
{
throw new NotImplementedException();
}
public static string promptForInput()
{
Console.Write("Player one, make a selection. Type 1=rock, 2=paper, or 3=scissors and press enter: ");
player1Sel = Console.ReadLine();
if (player1Sel == "")
{
MessageBox.Show("You must select a valid choice.");
Environment.Exit(0);
}
else
if (int.Parse(player1Sel) < 1 | int.Parse(response) > 3)
{
MessageBox.Show(response + " - is not a valid choice.");
Environment.Exit(0);
}
return player1Sel;
}// End of promptForInput.
public static int generateAutoSelect()
{
int player2Sel = numberGenerator.Next(1, 4);//Generates random number between 1 and 3.
if (player2Sel == 1)
{
MessageBox.Show("Player2 chose rock.");
}
else
if (player2Sel == 2)
{
MessageBox.Show("Player2 chose paper.");
}
else
if (player2Sel == 3)
{
MessageBox.Show("Player2 chose scissors.");
}
return player2Sel;
} // End of generateAutoSelect.
public static string judgeRockPaperScissors(int player1Sel, int player2Sel)
{
if (player1Sel == player2Sel)
{ return "00"; }
else if (player1Sel == 1 && player2Sel == 2)
{ return "12"; }
else if (player1Sel == 2 && player2Sel == 3)
{ return "23"; }
else if (player1Sel == 3 && player2Sel == 1)
{ return "31"; }
else if (player1Sel == 1 && player2Sel == 3)
{ return "13"; }
else if (player1Sel == 2 && player2Sel == 1)
{ return "21"; }
else if (player1Sel == 3 && player2Sel == 2)
{ return "32"; }
}// End of judgeRockPaperScissors.
} // End of class.
The compiler does not know that you have handled all possible cases in your if/else if blocks because the range of int is far more than 0-2. The easiest way to get your code compiling is to add a generic else block:
...
else
{
throw new ArgumentException("Player selections out of range");
}
Since the input is invalid throw an exception.
On a side note, using strings the way you are is definitely not the right approach.
Update the function to return null if no conditions are met:
public static string judgeRockPaperScissors(int player1Sel, int player2Sel)
{
if (player1Sel == player2Sel)
{ return "00"; }
else if (player1Sel == 1 && player2Sel == 2)
{ return "12"; }
else if (player1Sel == 2 && player2Sel == 3)
{ return "23"; }
else if (player1Sel == 3 && player2Sel == 1)
{ return "31"; }
else if (player1Sel == 1 && player2Sel == 3)
{ return "13"; }
else if (player1Sel == 2 && player2Sel == 1)
{ return "21"; }
else if (player1Sel == 3 && player2Sel == 2)
{ return "32"; }
return null;
}
That way it will always return with something. You'll also want to add a null check on the result variable in your main function to make sure it didn't return null.
I'm making a simple rock paper scissors game that uses keyboard input. I've realised that it's a better thing to type 1 for rock 2 for scissors etc than to write rock paper or scissors, but I want the console to write at the end what each player chose(rock, paper or scissors) instead of 1,2,3.
So when the string that has the number gets correct user input, another string
becomes rock paper or scissors.However, although I'm only using one class and initialising RealChoice1 and RealChoice2 at the beginning of the while, I get the "name doesn't exist in the current context" error for both of them
while (true)
{
string RealChoice2;
string RealChoice1;
while (true)
{
var key1 = System.Console.ReadKey(true);
if (key1.Key == ConsoleKey.Enter)
break;
Choice1 += key1.KeyChar;
}
if (Choice1 == "exit")
return (int)ExitCode.UserInputExit;
else if (Choice1 != "1" && Choice1 != "2" && Choice1 != "3")
{
Choice1 = null;
Console.WriteLine("");
Console.WriteLine("Please write 1, 2 or 3");
}
else if (Choice1 == "1" || Choice1 == "2" || Choice1 == "3")
{
switch (Convert.ToInt32(Choice1))
{
case 1:
RealChoice1 = "Rock";
break;
case 2:
RealChoice1 = "Scissors";
break;
case 3:
RealChoice1 = "Paper";
break;
}
break;
}
} //End of player 1 input
string Choice2 = null;
Console.WriteLine("");
Console.WriteLine("Player2, please type what you choose");
//Player2Input:
while (true)
{
while (true)
{
var key2 = System.Console.ReadKey(true);
if (key2.Key == ConsoleKey.Enter)
break;
Choice2 += key2.KeyChar;
}
if (Choice2 == "exit")
return (int)ExitCode.UserInputExit;
else if (Choice2 != "1" && Choice2 != "2" && Choice2 != "3")
{
Choice2 = null;
Console.WriteLine("");
Console.WriteLine("Please write 1, 2 or 3");
}
else if (Choice2 == "1" || Choice2 == "2" || Choice2 == "3")
{
switch (Convert.ToInt32(Choice2))
{
case 1:
RealChoice2 = "Rock";
break;
case 2:
RealChoice2 = "Scissors";
break;
case 3:
RealChoice2 = "Paper";
break;
}
break;
}
}
You need to declare your variables outside of the while loop if you want them to be available everywhere in the scope of your code. Currently, since you're declaring them within the first while loop, they wouldn't be available where you're using them inside the second while loop.
I recently started making a RPS game console app it worked and I decided to give myself more of a challenge and decided it will be cool if it had a win rate for both computer and player and for some reason it's not showing the win rates on the left side
EDIT: So I got it to show left but the percents remain at 0 any idea how to fix this?
abstract class Participant
{
public int wins;
int _winRate;
public int winRate
{
get
{
return _winRate;
}
set
{
if (value < 0 || value > 100)
{
throw new Exception("value cannot be less than 0 or greater than 100");
}
_winRate = value;
}
}
public abstract string Choice();
public abstract void PrintWinRate();
}
class Computer : Participant
{
string[] Rock_Paper_Scissor = {"rock","paper","scissor"};
Random rand = new Random();
public override string Choice()
{
string element = Rock_Paper_Scissor[rand.Next(3)];
return element;
}
public override void PrintWinRate()
{
winRate = (wins / Game_Logic.gamesPlayed) * 100;
string _winRate = "computer win rate: "+winRate.ToString()+"%".PadRight(10);
Console.WriteLine(_winRate);
}
}
class Player : Participant
{
public override string Choice()
{
string playerChoice = Console.ReadLine().Trim();
return playerChoice;
}
public override void PrintWinRate()
{
winRate = (wins / Game_Logic.gamesPlayed) * 100;
string _winRate = "player win rate: " + winRate.ToString()+"%".PadRight(10);
Console.WriteLine(_winRate);
}
}
class Game_Logic
{
public static int gamesPlayed;
static void Main()
{
Console.BackgroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.Black;
Computer comp = new Computer();
Player player = new Player();
string computerChoice;
string playerChoice;
ConsoleKeyInfo input;
bool playAgain;
int computerWins = comp.wins;
int playerWins = player.wins;
do
{
Console.Clear();
computerChoice = comp.Choice();
playerChoice = player.Choice();
Console.Clear();
while (playerChoice == computerChoice)
{
computerChoice = comp.Choice();
}
Console.WriteLine("Player: "+ playerChoice);
Console.WriteLine("\n"+"Computer: " + computerChoice);
if (playerChoice == "rock" && computerChoice == "scissor" || playerChoice == "paper" && computerChoice == "rock")
{
playerWins++;
Console.WriteLine("\n" + "You won!");
}
else if (playerChoice == "scissor" && computerChoice == "rock" || playerChoice == "rock" && computerChoice == "paper")
{
computerWins++;
Console.WriteLine("\n" + "Computer won!");
}
else if (playerChoice == "scissor" && computerChoice == "paper")
{
playerWins++;
Console.WriteLine("\n" + "You won!");
}
else if (playerChoice == "paper" && computerChoice == "scissor")
{
computerWins++;
Console.WriteLine("\n" + "Computer won!");
}
else
{
Console.WriteLine("\n" + "invalid value");
}
Game_Logic.gamesPlayed++;
Console.WriteLine("\n"+"Play again? <y/n>");
Console.WriteLine("\n");
comp.PrintWinRate();
player.PrintWinRate();
input = Console.ReadKey(true);
playAgain = input.KeyChar == 'y';
} while (playAgain);
}
}
First things first, I think there is a problem with the increment logic, not being able to increment the actual values of participant object. playerWins and computerWins are local variables of your main, and considered as new variable, not just as reference of your participant objects. Change the following lines:
playerWins++ to player.wins++ //Change the actual values of participant
computerWins++ to comp.wins++
Next, when you say left? What format specifically would you want? For now try the following lines:
string _winRate = string.Format("player win rate: {0}%", winRate);
Your game is displaying the scores but instantly clears the screen or exits.
Placing the 'PrintWinRate' calls before 'Console.ReadKey' should give you the desired behaviour!
Like this:
Console.WriteLine("\n" + "Play again? <y/n>");
Console.WriteLine("\n");
comp.PrintWinRate();
player.PrintWinRate();
input = Console.ReadKey(true);
Edit: There also seems to be a bug in the calculation for winRate.
You might want to change your value type for 'wins' from 'int' to 'float' so that it can store the fractional part of the calculation. As it is, when you expect a player to have 50% winRate it will show 0. This is because 'int' data types can not hold fractional values and will cut that part off, leaving the whole number - in this case 0.
This code should be closer to what you want:
winRate = (int)((wins / Game_Logic.gamesPlayed) * 100);
Remember to change 'wins' in your base class 'Participant' to be a float!
This is my first year in a Computer Programming course and one of my current assignments is to create a working Rock, Paper, Scissors game.
I have most of it working but my teacher has said that I have a lot of unnecessary code without actually telling me what it is. I'd like to get this streamline as best as possible as I'm not just satisfied with whatever works.
As well, the second validation of user input (user makes a choice, program runs through and displays who wins then is asked to make a choice again) doesn't work like the first one and I can't figure out what's going wrong. I've been looking at this for the past 2 weeks and can't figure it out so any help is appreciated.
The code that I have is as follows:
namespace Assignment04
{
class Program
{
static void Main(string[] args)
{
// Declare variables
int gamesPlayed = 0, userWins = 0, computerWins = 0, draws = 0, userSelection, computerSelection;
bool inputIsValid = false;
// Prompt user
Console.WriteLine("Welcome to the Rock, Paper, Scissors game!");
Console.WriteLine("1 - Rock");
Console.WriteLine("2 - Paper");
Console.WriteLine("3 - Scissors");
Console.WriteLine("4 - Quit program and view record");
// Create a loop to validate user's selection
do
{
Console.Write("Please make a selection: ");
// loop and test using TryParse()
while (!int.TryParse(Console.ReadLine(), out userSelection))
{
// invalid data type
Console.WriteLine("Invalid input.");
Console.Write("Please make a selection: ");
}
// test if input is within acceptable range
if (userSelection >= 1 && userSelection <= 4)
{
inputIsValid = true;
}
else
{
// valid integer, but out of acceptable range
Console.WriteLine("Number out of range.");
}
} while (!inputIsValid);
// Display user's choice
while (userSelection >= 1 && userSelection <= 3)
{
if (userSelection == 1)
{
Console.WriteLine("\nYou have selected Rock");
gamesPlayed = gamesPlayed + 1;
}
else if (userSelection == 2)
{
Console.WriteLine("\nYou have selected Paper");
gamesPlayed = gamesPlayed + 1;
}
else if (userSelection == 3)
{
Console.WriteLine("\nYou have selected Scissors");
gamesPlayed = gamesPlayed + 1;
}
// Generate computer's choice
Random randomNumber = new Random();
computerSelection = randomNumber.Next(1, 4);
// Display computer's choice
if (computerSelection == 1)
{
Console.WriteLine("Computer has chosen Rock");
}
else if (computerSelection == 2)
{
Console.WriteLine("Computer has chosen Paper");
}
else if (computerSelection == 3)
{
Console.WriteLine("Computer has chosen Scissors");
}
// Calculate and display who wins
if (userSelection == computerSelection)
{
draws = draws + 1;
}
else if (userSelection == 1 && computerSelection == 3 || userSelection == 2 && computerSelection == 1 || userSelection == 3 && computerSelection == 2)
{
userWins = userWins + 1;
}
else if (userSelection == 1 && computerSelection == 2 || userSelection == 2 && computerSelection == 3 || userSelection == 3 && computerSelection == 1)
{
computerWins = computerWins + 1;
}
// Display results and statistics
Console.WriteLine("\nYou have played {0} games with {1} wins, {2} draws, and {3} losses.", gamesPlayed, userWins, draws, computerWins);
do
{
Console.Write("Please make a selection: ");
// loop and test using TryParse()
while (!int.TryParse(Console.ReadLine(), out userSelection))
{
// invalid data type
Console.WriteLine("Invalid input.");
Console.Write("Please make a selection: ");
}
// test if input is within acceptable range
if (userSelection >= 1 && userSelection <= 4)
{
inputIsValid = true;
}
else
{
// valid integer, but out of acceptable range
Console.WriteLine("Number out of range.");
}
} while (!inputIsValid);
}
if (gamesPlayed == 0 && userSelection == 4)
{
Console.WriteLine("\nGoodbye");
}
else if (gamesPlayed > 0 && userSelection == 4)
{
Console.WriteLine("\nGames played = " + gamesPlayed);
Console.WriteLine("User wins = " + userWins);
Console.WriteLine("Computer wins = " + computerWins);
Console.WriteLine("Draws = " + draws);
}
Console.ReadLine();
}
}
}
If you find yourself writing almost the same thing multiple times, you should look for ways to refactor it. Here's an example of unnecessary repeated code:
if (userSelection == 1)
{
Console.WriteLine("\nYou have selected Rock");
gamesPlayed = gamesPlayed + 1;
}
else if (userSelection == 2)
{
Console.WriteLine("\nYou have selected Paper");
gamesPlayed = gamesPlayed + 1;
}
else if (userSelection == 3)
{
Console.WriteLine("\nYou have selected Scissors");
gamesPlayed = gamesPlayed + 1;
}
Can be simplified to:
Console.WriteLine("\nYou have selected " + tools[userSelection - 1]);
gamesPlayed++;
Where tools is defined as:
string[] tools = { "Rock", "Paper", "Scissors" };