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 */
}
Related
so I am pretty new to C# and after learning some fundamentals I tried making a simple program to check for someone's eligibility using if statements.
The program asks for 3 things: Age, Height, and CollegeDegree
You have to be 18 to go to the next question and you have to be taller than 160cm to go to the CollegeDegree question.
Now the program works fine, however looking at it, I come to realize that there are a lot of nested if statements and the code is just messy and unreadable.
What would I need to do to make it cleaner? I tried just doing it as follows
if
else
if
else
However, that causes the issue that if the first condition is not met, so the age is < 18 it just runs the next if statement instead of making the user be ineligible.
Current Code:
static void Main(string[] args)
{
int ageInput;
int heightInput;
bool hasCollegeDegree;
Console.WriteLine("How old are you?");
ageInput = Convert.ToInt32(Console.ReadLine());
if (ageInput >= 18)
{
Console.WriteLine("You are older than 18");
Console.WriteLine("How tall are you?");
heightInput = Convert.ToInt32(Console.ReadLine());
if (heightInput >= 160)
{
Console.WriteLine("You are taller than 160cm");
Console.WriteLine("Do you have a college degree?");
hasCollegeDegree = Convert.ToBoolean(Console.ReadLine());
if (hasCollegeDegree == true)
{
Console.WriteLine("You have a college degree");
Console.WriteLine("You are eligible");
}
else
{
Console.WriteLine("No College Degree");
Console.WriteLine("You are ineligible");
}
}
else
{
Console.WriteLine("You are too short");
}
}
else
{
Console.WriteLine("You are too young");
}
}
You can exit out of the code using a reverse condition. Example:
Console.WriteLine("How old are you?");
int ageInput = Convert.ToInt32(Console.ReadLine());
if (ageInput < 18)
{
Console.WriteLine("You are too young");
return;
}
Console.WriteLine("You are older than 18");
Console.WriteLine("How tall are you?");
int heightInput = Convert.ToInt32(Console.ReadLine());
if (heightInput < 160)
{
Console.WriteLine("You are too short");
return;
}
Console.WriteLine("You are taller than 160cm");
Console.WriteLine("Do you have a college degree?");
bool hasCollegeDegree = Convert.ToBoolean(Console.ReadLine());
if (!hasCollegeDegree)
{
Console.WriteLine("No College Degree");
Console.WriteLine("You are ineligible");
return;
}
Console.WriteLine("You have a college degree");
Console.WriteLine("You are eligible");
I suggest extracting methods to read integer:
private static int ReadInteger(string question) {
while (true) {
if (!string.IsNullOrWhiteSpace(question))
Console.WriteLine(question);
if (int.TryParse(Console.ReadLine(), out int result))
return result;
Console.WriteLine("Sorry, not a valid integer; please, try again.");
}
}
and boolean:
private static HashSet<string> s_Yes =
new HashSet<string>(StringComparer.OrdinalIgnoreCase) {
"Y", "Yes", "T", "True", "OK"
};
private static HashSet<string> s_No =
new HashSet<string>(StringComparer.OrdinalIgnoreCase) {
"N", "No", "F", "False"
};
private static bool ReadBoolean(string question) {
while (true) {
if (!string.IsNullOrWhiteSpace(question))
Console.WriteLine(question);
string input = Console.ReadLine().Trim();
if (s_Yes.Contains(input))
return true;
if (s_No.Contains(input))
return false;
Console.WriteLine("Sorry, not a valid value; please, try again.");
}
}
then you can put
if (ReadInteger("How old are you?") < 18) {
Console.WriteLine("You are too young");
return;
}
Console.WriteLine("You are older than 18");
if (ReadInteger("How tall are you?") < 160) {
Console.WriteLine("You are too short");
return;
}
Console.WriteLine("You are taller than 160cm");
if (!ReadBoolean("Do you have a college degree (Y/N)?")) {
Console.WriteLine("No College Degree");
Console.WriteLine("You are ineligible");
return;
}
Console.WriteLine("You have a college degree");
Console.WriteLine("You are eligible");
A disadvantage with the answers so far using return is that the method is exited and therefore you can't have any code below that point in the same method.
A different approach is to use a COMPOUND boolean statement and combine all the conditions into one boolean expression:
if (ageInput >= 18 && heightInput >= 160 && hasCollegeDegree)
{
Console.WriteLine("Congratulations! You meet the criteria.");
}
else
{
Console.WriteLine("You do not meet the criteria.");
}
This style of check might be useful if you have many different combinations that you need to check for. So the user enters all the information up front and then you can have a whole series of different compound checks to see which combinations they satisfy. Maybe you're checking to see which grants from a list that the user qualifies for.
See Boolean logical operators for more information on &&, ||, and !.
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
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;
//}
}
}
}
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.
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