Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions RPSWithErrorMessages/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System;

namespace RockPaperScissors
{
class Program
{
public static void Main()
{
//asks for user input
Console.WriteLine("Enter your move:");
string hand1 = Console.ReadLine().ToLower();

//if player enters invalid hand, throws exception and asks for valid input
while(hand1 != "rock"&&hand1 != "paper"&&hand1 != "scissors"){
try{

if (hand1 != "rock"&&hand1 != "paper"&&hand1 != "scissors")
{
throw new Exception ("Invalid move");
}
}
catch(Exception) {

Console.WriteLine("Invalid move entered please try again");
hand1 = Console.ReadLine().ToLower();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is will work, but its a little less elegant the way you have it written. You are doing the check 2 times for valid input. Once for they condition to throw an exception, and once for the condition to check if there is a valid hand so you can keep asking for input.

Also, the way you have it, a do/While loop would fit better.

}
}
Console.WriteLine(outcome(compareHands(hand1,hand2())));
//prevents program from closing
Console.ReadLine();
}
public static string hand2()
{
//random computer input
Random rnd = new Random();
int computerMove = rnd.Next(1,4);

if (computerMove == 1){
Console.WriteLine("Computer chose rock");
return "rock";
}
else if (computerMove == 2){
Console.WriteLine("Computer chose paper");
return "paper";
}
else{
Console.WriteLine("Computer chose scissors");
return "scissors";
}
}
public static int compareHands(string hand1, string hand2)
{
//Tie condition
if(hand1 == hand2){
return 0;
}
//Win Condition
else if ((hand1 == "rock" && hand2 == "scissors")||(hand1 == "paper" && hand2 == "rock")||(hand1 == "scissors" && hand2 == "paper")){
return 1;
}
//Loss Condition
else{
return 2;
}
}
public static string outcome(int compareHands)
{
//logic to decide outcome
if(compareHands == 0){
return "it's a Tie!";
}
else if(compareHands == 1){
return "You Win!";
}
else{
return "You Lose!";
}
}
}
}
8 changes: 8 additions & 0 deletions RPSWithErrorMessages/RPSWithErrorMessages.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

</Project>