diff --git a/ExceptionPractice/ExceptionPractice.csproj b/ExceptionPractice/ExceptionPractice.csproj new file mode 100644 index 00000000..23df6047 --- /dev/null +++ b/ExceptionPractice/ExceptionPractice.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp2.1 + + + diff --git a/ExceptionPractice/Program.cs b/ExceptionPractice/Program.cs new file mode 100644 index 00000000..a9e95829 --- /dev/null +++ b/ExceptionPractice/Program.cs @@ -0,0 +1,29 @@ +using System; + +namespace ExceptionPractice +{ + class Program + { + static void Main(string[] args) + { + try + { + double ans1 = divide(17.0, 0.0); + Console.WriteLine(ans1); + } + catch + { + Console.WriteLine("Oops, there was a problem with the divide function."); + } + + } + public static double divide(double num1, double num2) + { + if (num2 == 0.0) + { + throw new Exception("Cannot divide by 0, Sorry!"); + } + return num1 / num2; + } + } +} diff --git a/RockPaperScissors/RockPaperScissors.cs b/RockPaperScissors/RockPaperScissors.cs index 470ae756..6b59c9ea 100644 --- a/RockPaperScissors/RockPaperScissors.cs +++ b/RockPaperScissors/RockPaperScissors.cs @@ -6,20 +6,136 @@ class Program { public static void Main() { - Console.WriteLine("Enter hand 1:"); - string hand1 = Console.ReadLine().ToLower(); - Console.WriteLine("Enter hand 2:"); - string hand2 = Console.ReadLine().ToLower(); - Console.WriteLine(CompareHands(hand1, hand2)); + if (tests()) + { + Console.WriteLine("Tests Passed!"); + } + else + { + Console.WriteLine("Tests Failed!"); + } + Console.WriteLine("-----------------"); + + string hand1 = null; + + + while (hand1 == null) + { + try + { + playerInput(); + } + catch + { + Console.WriteLine("Enter Rock Paper or Scissor. Come on, you know better than that."); + } + } + + string hand2 = computerInput(); + int compare = CompareHands(hand1, hand2); + + + if (compare == 0) + { + Console.WriteLine("It's a tie!"); + } + else if (compare == 1) + { + Console.WriteLine("You Win, Congrats!"); + } + else if (compare == 2) + { + Console.WriteLine("Computer Wins! Sorry bud."); + } + - // leave this command at the end so your program does not close automatically + //Keep this here to keep code from closing out. Console.ReadLine(); } - - public static string CompareHands(string hand1, string hand2) + + //Get input from user and check if it's valid. + public static string playerInput() + { + + Console.WriteLine("Rock, Paper, Scissors!"); + string hand1 = Console.ReadLine().ToLower(); + + if (hand1 != "rock" && hand1 != "paper" && hand1 != "scissors") + { + throw new Exception("Incorrect Input."); + } + else + { + return hand1; + } + + } + + + //Takes a random number and turns it into the computer's hand. + public static string computerInput() { - // Your code here - return hand1 + ' ' + hand2; + Random rps = new Random(); + int num = rps.Next(0, 3); + string hand2; + + if (num == 0) + { + hand2 = "rock"; + Console.WriteLine("The computer chose rock!"); + } + else if (num == 1) + { + hand2 = "paper"; + Console.WriteLine("The Computer chose paper!"); + } + else if (num == 2) + { + hand2 = "scissors"; + Console.WriteLine("The computer chose scissors!"); + } + else + { + throw new Exception("Error in computer generation."); + } + return hand2; + } + + + public static int CompareHands(string hand1, string hand2) + { + if (hand1 == hand2) + { + return 0; + } + else if (hand1 == "rock" && hand2 == "scissors" || hand1 == "paper" && hand2 == "rock" || hand1 == "scissors" && hand2 == "paper") + { + return 1; + } + else if (hand2 == "rock" && hand1 == "scissors" || hand2 == "paper" && hand1 == "rock" || hand2 == "scissors" && hand1 == "paper") + { + return 2; + } + else + { + throw new Exception("Comparison Error."); + } + } + + public static bool tests() + { + return + CompareHands("rock", "rock") == 0 && + CompareHands("rock", "scissors") == 1 && + CompareHands("rock", "paper") == 2 && + + CompareHands("paper", "paper") == 0 && + CompareHands("paper", "rock") == 1 && + CompareHands("paper", "scissors") == 2 && + + CompareHands("scissors", "scissors") == 0 && + CompareHands("scissors", "paper") == 1 && + CompareHands("scissors", "rock") == 2; } } } diff --git a/textGame/Program.cs b/textGame/Program.cs new file mode 100644 index 00000000..e9cd9656 --- /dev/null +++ b/textGame/Program.cs @@ -0,0 +1,13 @@ +using System; +using System.Threading; + +namespace textGame +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello World!"); + } + } +} diff --git a/week2Practice/Program.cs b/week2Practice/Program.cs new file mode 100644 index 00000000..82e5266a --- /dev/null +++ b/week2Practice/Program.cs @@ -0,0 +1,32 @@ +using System; + +namespace week2Practice +{ + class Program + { + static void Main(string[] args) + { + //ask user for grade + //if number is a grade, print out you made an A + int grade = 0; + string input; + + Console.WriteLine("Enter a Numerical Grade:"); + input = Console.ReadLine(); + grade = Convert.ToInt32(input); + + if(grade >= 90){ + Console.WriteLine("You made an A! Congrats!"); + }else if(grade >= 80){ + Console.WriteLine("You made a B! Almost Perfect."); + }else if(grade >= 70){ + Console.WriteLine("You made a C! Study up."); + }else if(grade >= 60){ + Console.WriteLine("You made a D! You can do better."); + }else{ + Console.WriteLine("You Failed! Try again!"); + } + Console.WriteLine("Thank you for submitting."); + } + } +} diff --git a/week2Practice/week2Practice.csproj b/week2Practice/week2Practice.csproj new file mode 100644 index 00000000..23df6047 --- /dev/null +++ b/week2Practice/week2Practice.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp2.1 + + +