Rock Paper Scissor Array application C# - c#

Let me start off with what i have so far. Here are my 3 classes:
class Program
{
static void Main(string[] args)
{
Game rps = new Game();
rps.printHeader();
rps.gameStart();
}
}
class GameDetails
{
public string Name;
public int game;
public GameDetails()
{
Name = "unknown";
game = 0;
}
}
class Game
{
string name;
int numPlays;
int game;
GameDetails[] gameArray;
public int NumGames
{
get
{
return numPlays;
}
set
{
numPlays = value;
}
}
public void printHeader()
{
Console.WriteLine("Welcome to rock, paper, scissors");
this.userSettings();
}
private void InitializeArrays()
{
gameArray = new GameDetails[game];
for (int game = 0; game < numPlays; game++)
{
gameArray[game] = new GameDetails();
}
}
public void userSettings()
{
Console.WriteLine("What is your name: ");
name = Console.ReadLine();
Console.WriteLine("How many games would you like to play?: ");
Int32.TryParse(Console.ReadLine(), out numPlays);
while (numPlays < 10 && numPlays % 2 == 0)
{
Console.WriteLine("\nNumber is not odd try again.");
Console.WriteLine("How many games would you like to play?: ");
Int32.TryParse(Console.ReadLine(), out numPlays);
}
}
public void gameStart()
{
for (game = 1; game <= numPlays; game++)
Console.WriteLine("Please choose Rock, Paper, or Scissors");
string userSelection = Console.ReadLine();
Random r = new Random();
int computerSelection = r.Next(4);
if (computerSelection == 1)
{
if (userSelection == "rock")
{
Console.WriteLine("Computer Choice: Rock\n");
Console.WriteLine("Game [{0}] is a tie", game);
}
else if (userSelection == "paper")
{
Console.WriteLine("Computer Choice: Paper\n");
Console.WriteLine("Game[{ 0}] is a tie", game);
}
else if (userSelection == "scissors")
{
Console.WriteLine("Computer Choice: Scissors\n");
Console.WriteLine("Game [{0}] is a tie", game);
}
else
{
Console.WriteLine("You must choose either rock, paper or scissors");
}
}
else if (computerSelection == 2)
{
if (userSelection == "rock")
{
Console.WriteLine("Computer Choice: Paper\n");
Console.WriteLine("You lose game [{0}], papaer beats rock", game);
}
else if (userSelection == "paper")
{
Console.WriteLine("Computer Choice: Scissors\n");
Console.WriteLine("You lose game [{0}], scissors beats paper", game);
}
else if (userSelection == "scissors")
{
Console.WriteLine("Computer Choice: Rock\n");
Console.WriteLine("You lose game [{0}], Rock beats scissors", game);
}
else
{
Console.WriteLine("You must choose either rock, paper or scissors");
}
}
else if (computerSelection == 3)
{
if (userSelection == "rock")
{
Console.WriteLine("The computer chose scissors");
Console.WriteLine("You win game [{0}], rock beats scissors", game);
}
else if (userSelection == "paper")
{
Console.WriteLine("The computer chose rock");
Console.WriteLine("You win game [{0}],paper beats rock", game);
}
else if (userSelection == "scissors")
{
Console.WriteLine("The computer chose paper");
Console.WriteLine("You win game [{0}], scissors beats paper!", game);
}
else
{
Console.WriteLine("You must choose either rock, paper or scissors");
}
Console.ReadLine();
// int arrayIndex = game - 1;
// gameArray[arrayIndex].Result = game;
// }
// }
//public override string ToString()
//{
// string outputString = game + "\n";
// for (int game = 1; game < numPlays; game++)
// {
// int index = game - 1;
// outputString += "Game " + game + ":" + gameArray[index].Result + "\n";
// }
// return outputString;
//}
}
}
}
I need this program to only run for the specified number of games, determine a winner of a best of (number of games chosen) series, and display the results of each game. I am not receiving any errors, but when i run the code, it asks for the name and how many games. After i choose how many games to play, it just displays "Please choose Rock, Paper, or Scissors" the same number of times that i have chosen for the amount of games. How can i make this correctly count the number of games, determine a winner, and display the correct output.

Your for loop for (game = 1; game <= numPlays; game++) is missing the braces around the game method. From how it appears
for (game = 1; game <= numPlays; game++)
Console.WriteLine("Please choose Rock, Paper, or Scissors");
The part Console.WriteLine(..) will run as many times as the numPlays is set.
What you will want to do is wrap the entire method body in the for loop and that should run the game repeatadly for numPlays
public void gameStart()
{
Random r = new Random();
for (game = 1; game <= numPlays; game++)
{
Console.WriteLine("Please choose Rock, Paper, or Scissors");
string userSelection = Console.ReadLine();
int computerSelection = r.Next(4);
if (computerSelection == 1)
{
if (userSelection == "rock")
{
Console.WriteLine("Computer Choice: Rock\n");
Console.WriteLine("Game [{0}] is a tie", game);
}
else if (userSelection == "paper")
{
Console.WriteLine("Computer Choice: Paper\n");
Console.WriteLine("Game[{ 0}] is a tie", game);
}
else if (userSelection == "scissors")
{
Console.WriteLine("Computer Choice: Scissors\n");
Console.WriteLine("Game [{0}] is a tie", game);
}
else
{
Console.WriteLine("You must choose either rock, paper or scissors");
}
}
else if (computerSelection == 2)
{
if (userSelection == "rock")
{
Console.WriteLine("Computer Choice: Paper\n");
Console.WriteLine("You lose game [{0}], papaer beats rock", game);
}
else if (userSelection == "paper")
{
Console.WriteLine("Computer Choice: Scissors\n");
Console.WriteLine("You lose game [{0}], scissors beats paper", game);
}
else if (userSelection == "scissors")
{
Console.WriteLine("Computer Choice: Rock\n");
Console.WriteLine("You lose game [{0}], Rock beats scissors", game);
}
else
{
Console.WriteLine("You must choose either rock, paper or scissors");
}
}
else if (computerSelection == 3)
{
if (userSelection == "rock")
{
Console.WriteLine("The computer chose scissors");
Console.WriteLine("You win game [{0}], rock beats scissors", game);
}
else if (userSelection == "paper")
{
Console.WriteLine("The computer chose rock");
Console.WriteLine("You win game [{0}],paper beats rock", game);
}
else if (userSelection == "scissors")
{
Console.WriteLine("The computer chose paper");
Console.WriteLine("You win game [{0}], scissors beats paper!", game);
}
else
{
Console.WriteLine("You must choose either rock, paper or scissors");
}
Console.ReadLine();
// int arrayIndex = game - 1;
// gameArray[arrayIndex].Result = game;
// }
// }
//public override string ToString()
//{
// string outputString = game + "\n";
// for (int game = 1; game < numPlays; game++)
// {
// int index = game - 1;
// outputString += "Game " + game + ":" + gameArray[index].Result + "\n";
// }
// return outputString;
//}
}
}
}

Related

I need help making this rock paper scissors random [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
** I have already written out what would happen if computer said rock paper or scissors. does anyone know how to make this random? I enjoy coding and want to understand how to do it so please don't just send code if you are good at coding please explain it.**
class Program
{
public static void Main(string[] args)
{
Console.WriteLine(" Hello! I am Computer who are you? ");
Console.Write(" Enter name here: ");
string Name = Console.ReadLine();
Console.WriteLine(" Hello " + Name + " Its nice to meet you! ");
Console.WriteLine("");
Console.WriteLine(" Lets play rock paper scisors ");
Console.WriteLine("");
Console.Write(" Rock... Paper... Scisors... SHOOT! Enter 1,2 or 3: ");
Console.WriteLine("");
string UserChoice = Console.ReadLine();
int userPoints = 0;
int computerPoints = 0;
//this is if coputer chose rock
if (UserChoice == "1")
{
Console.WriteLine("User chose Rock");
Console.WriteLine("Computer chose Rock");
Console.WriteLine("It is a tie.");
}
else if (UserChoice == "2")
{
Console.WriteLine("User chose Paper");
Console.WriteLine("Computer chose Rock");
Console.WriteLine("User wins");
userPoints++;
}
else if (UserChoice == "3")
{
Console.WriteLine("User chose Scissors");
Console.WriteLine("Computer chose Rock");
Console.WriteLine("Computer wins");
computerPoints++;
}
Console.WriteLine($"User has {userPoints} points - Computer has {computerPoints} points");
// this is if computer chose paper
if (UserChoice == "1")
{
Console.WriteLine("User chose Rock");
Console.WriteLine("Computer chose Paper");
Console.WriteLine("Computer wins");
computerPoints++;
Console.Read();
}
else if (UserChoice == "2")
{
Console.WriteLine("User chose Paper");
Console.WriteLine("Computer chose Paper");
Console.WriteLine("It is a tie.");
}
else if (UserChoice == "3")
{
Console.WriteLine("User chose Scissors");
Console.WriteLine("Computer chose Paper");
Console.WriteLine("User wins");
userPoints++;
}
Console.WriteLine($"User has {userPoints} points - Computer has {computerPoints} points");
//this is if computer chose scissors
if (UserChoice == "1")
{
Console.WriteLine("User chose Rock");
Console.WriteLine("Computer chose Scissors");
Console.WriteLine("User wins");
userPoints++;
}
else if (UserChoice == "2")
{
Console.WriteLine("User chose Paper");
Console.WriteLine("Computer chose Scissors");
Console.WriteLine("Computer wins");
computerPoints++;
}
else if (UserChoice == "3")
{
Console.WriteLine("User chose Scissors");
Console.WriteLine("Computer chose Scissors");
Console.WriteLine("It is a tie.");
}
Console.WriteLine($"User has {userPoints} points - Computer has {computerPoints} points");
}
}
So what will be helpful for you here is the Random class. In this instance, you'd want it to give output between 1-3. You'll need to create a Random object and then call the function nextInt. The parameters are the range of random numbers, with the first being inclusive and the second being exclusive (meaning the line below will give random numbers between 1 and 3).
var rand = new Random();
int compChoice = rand.Next(1,4);
This answers your question, just be aware you're taking input as a string i.e. "1", but it will probably be best practice to try parsing it into an integer.
var userChoiceNum = Int32.Parse(UserChoice);
I'd also suggest extracting some of the game logic into a separate function, which you can then call in main.
private function doMatch()
{
/* game logic here */
}

How to display results of rock paper scissors game [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am working on a rock paper scissors console application. I can get the game to run correctly, and display the winner of the individual game. I am unable to get all of the results to display at the end. At the end of the game, it should display the results of all of the games, and it should stop when someone wins enough games. The application currently closes after the last game is played and i cannot figure out why. This is the code i have, there are 3 different classes.
Class 1
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Game rps = new Game();
rps.printHeader();
rps.userSettings();
rps.gameStart();
}
}
}
Class 2
namespace ConsoleApplication1
{
class GameDetails
{
public string Name;
public int game;
public string Result;
public GameDetails()
{
Name = "unknown";
game = 0;
}
public GameDetails(string winner)
{
Result = winner;
}
}
}
and finally Class 3
namespace ConsoleApplication1
{
class Game
{
string name;
string winner;
int numPlays;
int game;
GameDetails[] gameArray;
public int NumGames
{
get
{
return numPlays;
}
set
{
numPlays = value;
}
}
public string Winner
{
get
{
return winner;
}
set
{
winner = value;
}
}
public void printHeader()
{
Console.WriteLine("Welcome to rock, paper, scissors");
this.userSettings();
}
private void InitializeArrays()
{
gameArray = new GameDetails[game];
for (int game = 0; game < numPlays; game++)
{
gameArray[game] = new GameDetails();
}
}
public void userSettings()
{
Console.WriteLine("What is your name: ");
name = Console.ReadLine();
Console.WriteLine("How many games would you like to play?: ");
Int32.TryParse(Console.ReadLine(), out numPlays);
while (numPlays < 10 && numPlays % 2 == 0)
{
Console.WriteLine("\nNumber is not odd try again.");
Console.WriteLine("How many games would you like to play?: ");
Int32.TryParse(Console.ReadLine(), out numPlays);
}
}
public void gameStart()
{
Random r = new Random();
for (game = 1; game <= numPlays; game++)
{
Console.WriteLine("Please choose Rock, Paper, or Scissors");
string userSelection = Console.ReadLine();
int computerSelection = r.Next(4);
if (computerSelection == 1)
{
if (userSelection == "rock")
{
Console.WriteLine("Computer Choice: Rock\n");
Console.WriteLine("Game [{0}] is a tie", game);
}
else if (userSelection == "paper")
{
Console.WriteLine("Computer Choice: Paper\n");
Console.WriteLine("Game[{0}] is a tie", game);
}
else if (userSelection == "scissors")
{
Console.WriteLine("Computer Choice: Scissors\n");
Console.WriteLine("Game [{0}] is a tie", game);
}
else
{
Console.WriteLine("You must choose either rock, paper or scissors");
}
}
else if (computerSelection == 2)
{
if (userSelection == "rock")
{
Console.WriteLine("Computer Choice: Paper\n");
Console.WriteLine("You lose game [{0}], papaer beats rock", game);
}
else if (userSelection == "paper")
{
Console.WriteLine("Computer Choice: Scissors\n");
Console.WriteLine("You lose game [{0}], scissors beats paper", game);
}
else if (userSelection == "scissors")
{
Console.WriteLine("Computer Choice: Rock\n");
Console.WriteLine("You lose game [{0}], Rock beats scissors", game);
}
else
{
Console.WriteLine("You must choose either rock, paper or scissors");
}
}
else if (computerSelection == 3)
{
if (userSelection == "rock")
{
Console.WriteLine("The computer chose scissors");
Console.WriteLine("You win game [{0}], rock beats scissors", game);
}
else if (userSelection == "paper")
{
Console.WriteLine("The computer chose rock");
Console.WriteLine("You win game [{0}],paper beats rock", game);
}
else if (userSelection == "scissors")
{
Console.WriteLine("The computer chose paper");
Console.WriteLine("You win game [{0}], scissors beats paper!", game);
}
else
{
Console.WriteLine("You must choose either rock, paper or scissors");
}
winner = Console.ReadLine();
}
}
}
public override string ToString()
{
int arrayIndex = game - 1;
gameArray[arrayIndex].Result = winner;
string outputString = game + "\n";
for (int game = 1; game < numPlays; game++)
{
int index = game - 1;
outputString += "Game " + game + ":" + gameArray[index].Result + "\n";
}
return outputString;
}
}
}
Hey My Friend i will help at all. But if you Fallow the Comment of #Blorgbeard you won´t need this (Really is an appreciate comment, fallow it).
First You have an error in the class 1
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Game rps = new Game();
rps.printHeader();
rps.userSettings();
rps.gameStart();
}
}
Delete this line "rps.userSettings();" because you allready have this in printHeader() procedure.
The Next Error i catched using "the debugger - try setting a breakpoint and stepping through your code" :) was in this line in the Class 3
int computerSelection = r.Next(4);
Change it for:
int computerSelection = r.Next(1,3);
If you read this https://msdn.microsoft.com/es-es/library/2dx6wyd4(v=vs.110).aspx you will know why is wrong :)
Now the last error i catched and is the reazon because you get close the console, just add this line to all this sentences (Console.ReadLine()):
if (userSelection == "rock")
{
Console.WriteLine("Computer Choice: Rock\n");
Console.WriteLine("Game [{0}] is a tie", game);
Console.ReadLine();
}
I hope this will help you.

Rock Paper Scissors game gone wrong

Console app rock paper scissors game well i have trouble explaining it you just have to test it and see also whenever i press y it's suppose to clear the console and start the game over but that is just 1/2 the problem whenever i type 'rock' it translates it to 'ock' same with paper and scissor it takes out the first letter.
enum Rock_Paper_Scissor
{
rock,
paper,
scissor
}
class Computer
{
Random rand = new Random();
public Rock_Paper_Scissor Choice()
{
Rock_Paper_Scissor element = (Rock_Paper_Scissor)rand.Next(3);
return element;
}
}
class Game_Logic
{
static void Main()
{
Computer Comp = new Computer();
bool PlayAgain = true;
ConsoleKeyInfo input = Console.ReadKey();
Rock_Paper_Scissor ComputerChoice = Comp.Choice();
do
{
string PlayerChoice = Console.ReadLine();
Console.Clear();
while (PlayerChoice == ComputerChoice.ToString())
{
ComputerChoice = Comp.Choice();
}
Console.WriteLine("Player: "+ PlayerChoice);
Console.WriteLine("Computer: " + ComputerChoice);
if (PlayerChoice == "rock" && ComputerChoice.ToString() == "scissor")
Console.WriteLine("You won!");
else if (PlayerChoice == "scissor" && ComputerChoice.ToString() == "rock")
Console.WriteLine("Computer won!");
else if (PlayerChoice == "paper" && ComputerChoice.ToString() == "rock")
Console.WriteLine("You won!");
else if (PlayerChoice == "rock" && ComputerChoice.ToString() == "paper")
Console.WriteLine("Computer won");
else if (PlayerChoice == "scissor" && ComputerChoice.ToString() == "paper")
Console.WriteLine("You won!");
else if (PlayerChoice == "paper" && ComputerChoice.ToString() == "scissor")
Console.WriteLine("Computer won!");
else
{
Console.WriteLine("invalid value");
}
Console.WriteLine("\n"+"Play again? <y/n>");
PlayAgain = input.KeyChar == 'y';
} while (PlayAgain);
}
}
ConsoleKeyInfo input = Console.ReadKey();
Rock_Paper_Scissor ComputerChoice = Comp.Choice();
do
{
string PlayerChoice = Console.ReadLine();
The issue lies with this snippet. Your first letter will be eaten triggering the first Console.Readkey meaning the Console.ReadLine() you are intending to pass the value too will only receive the rest.
The Console.ReadKey() line will do you no good outside the do loop anyway, you may as well remove it and do
///
Console.WriteLine("\n"+"Play again? <y/n>");
} while (Console.ReadKey.KeyChar == 'y');
Directly. Solving both problems.

Paper,Rock,Scissors game (c#)

I'm working on a simple paper,rock,scissors game (c#),I made everything except the part where the user can type (for example) yes or no and play again or quit.How can I do this?
This is my current code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Do you 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();
}
}
}
Help!...
Remove the last ReadLine, put the entire logic in a while block as follows:
bool keepPlaying = true;
while (keepPlaying) {
//game logic here
Console.WriteLine("New game? y/n");
ConsoleKeyInfo cki = Console.ReadKey(); //wait for player to press a key
keepPlaying = cki.KeyChar == 'y'; //continue only if y was pressed
}
Hope this works for you.
class Program
{
static void Main(string[] args)
{
bool continuegame = false;
Random r = new Random();
int computerChoice;
do
{
Console.WriteLine("\nDo you choose rock,paper or scissors");
string userChoice = Console.ReadLine();
computerChoice = r.Next(3);
if (userChoice.ToLower().Equals("rock") || userChoice.ToLower().Equals("paper") || userChoice.ToLower().Equals("scissors"))
{
if (computerChoice == 1)
{
ComputerChoicePaper(userChoice);
}
else if (computerChoice == 2)
{
ComputerChoicePaper(userChoice);
}
else if (computerChoice == 3)
{
ComputerChoicePaper(userChoice);
}
}
else
{
Console.WriteLine("\nInvalid Input !!! please enter valid input, rock paper scissors");
}
Console.WriteLine("\nDo you want to continue? y/n");
var enteredk = Console.ReadKey();
//Console.();
continuegame = enteredk.Key.ToString().ToLower() == "y";
} while (continuegame);
}
public static void ComputerChoicePaper(string userSelection)
{
if (userSelection.Equals("rock")|| userSelection.Equals("scissors"))
{
Console.WriteLine("\nComputer Choice paper");
Console.WriteLine("You won!! Worray");
}
else if (userSelection.Equals("paper"))
{
Console.WriteLine("\nComputer Choice {0} as well its, a draw :P", userSelection);
}
}
public void ComputerChoiceRock(string userSelection)
{
if (userSelection.Equals("paper") || userSelection.Equals("scissors"))
{
Console.WriteLine("\nComputer Choice rock");
Console.WriteLine("\nYou lost!! Computer won");
}
else if (userSelection.Equals("rock"))
{
Console.WriteLine("\nComputer Choice {0} as well its, a draw :P", userSelection);
}
}
public void ComputerChoiceScissors(string userSelection)
{
Console.WriteLine("\nComputer Choice scissors");
if (userSelection.Equals("rock"))
{
Console.WriteLine("\nYou won!! Worray");
}
else if(userSelection.Equals("paper"))
{
Console.WriteLine("\nYou lost!! Computer won");
}
else if (userSelection.Equals("scissors"))
{
Console.WriteLine("\nComputer Choice {0} as well its, a draw :P", userSelection);
}
}
}
You can wrap everything in a do while structure:
class Program
{
static void Main(string[] args)
{
do
{
Console.WriteLine("Do you choose rock,paper or scissors");
string userChoice = Console.ReadLine();
// Rest of your code
}while(Console.ReadLine() == "yes");
}
}
This will execute your code once, and then once again every time the user inputs yes.
With that, you don't need any private variables and you'll be sure the game will be executed at least once. If you want to prompt the user (something alone the lines of "play again?"), you can do so at the end of your existing code, before the }while part.
Firstly, when you call r.Next(4); you program has a chance to generate a number from 0 to 3. So the possibilities computerChoice for are 0, 1, 2, 3... Your program does not have any code handling a computerChoice when it is equal to 0 so it just abruptly closes. To solve this first change r.Next(4); to r.Next(3); which will give the possibilities of 0, 1, 2. Just change else if (computerChoice == 3) to `else if (computerChoice == 0)'. This will fix your program. Secondly, as the other answers have said it would be wise to put a loop around your entire program so you can play RPS multiple times.
-mnursey

Validation of second user input isn't working properly and I'm told I have a lot of unnecessary coding

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" };

Categories