diff --git a/74 Rock Scissors Paper/csharp/Choice.cs b/74 Rock Scissors Paper/csharp/Choice.cs new file mode 100644 index 00000000..c981a45a --- /dev/null +++ b/74 Rock Scissors Paper/csharp/Choice.cs @@ -0,0 +1,19 @@ +namespace RockScissorsPaper +{ + public class Choice + { + public string Selector {get; private set; } + public string Name { get; private set; } + internal Choice CanBeat { get; set; } + + public Choice(string selector, string name) { + Selector = selector; + Name = name; + } + + public bool Beats(Choice choice) + { + return choice == CanBeat; + } + } +} diff --git a/74 Rock Scissors Paper/csharp/Choices.cs b/74 Rock Scissors Paper/csharp/Choices.cs new file mode 100644 index 00000000..3026d7cc --- /dev/null +++ b/74 Rock Scissors Paper/csharp/Choices.cs @@ -0,0 +1,42 @@ +using System; + +namespace RockScissorsPaper +{ + public class Choices + { + public static readonly Choice Rock = new Choice("3", "Rock"); + public static readonly Choice Scissors = new Choice("2", "Scissors"); + public static readonly Choice Paper = new Choice("1", "Paper"); + + private static readonly Choice[] _allChoices; + private static readonly Random _random = new Random(); + + static Choices() + { + Rock.CanBeat = Scissors; + Scissors.CanBeat = Paper; + Paper.CanBeat = Rock; + + _allChoices = new[] { Rock, Scissors, Paper }; + } + + public static Choice GetRandom() + { + return _allChoices[_random.Next(_allChoices.GetLength(0))]; + } + + public static bool TryGetBySelector(string selector, out Choice choice) + { + foreach (var possibleChoice in _allChoices) + { + if (string.Equals(possibleChoice.Selector, selector)) + { + choice = possibleChoice; + return true; + } + } + choice = null; + return false; + } + } +} diff --git a/74 Rock Scissors Paper/csharp/Game.cs b/74 Rock Scissors Paper/csharp/Game.cs new file mode 100644 index 00000000..8916a217 --- /dev/null +++ b/74 Rock Scissors Paper/csharp/Game.cs @@ -0,0 +1,58 @@ +using System; +using System.Linq; + +namespace RockScissorsPaper +{ + public class Game + { + public int ComputerWins { get; private set; } + public int HumanWins { get; private set; } + public int TieGames { get; private set; } + + public void PlayGame() + { + var computerChoice = Choices.GetRandom(); + var humanChoice = GetHumanChoice(); + + Console.WriteLine("This is my choice..."); + Console.WriteLine("...{0}", computerChoice.Name); + + if (humanChoice.Beats(computerChoice)) + { + Console.WriteLine("You win!!!"); + HumanWins++; + } + else if (computerChoice.Beats(humanChoice)) + { + Console.WriteLine("Wow! I win!!!"); + ComputerWins++; + } + else + { + Console.WriteLine("Tie game. No winner."); + TieGames++; + } + } + + public void WriteFinalScore() + { + Console.WriteLine(); + Console.WriteLine("Here is the final game score:"); + Console.WriteLine("I have won {0} game(s).", ComputerWins); + Console.WriteLine("You have one {0} game(s).", HumanWins); + Console.WriteLine("And {0} game(s) ended in a tie.", TieGames); + } + + public Choice GetHumanChoice() + { + while (true) + { + Console.WriteLine("3=Rock...2=Scissors...1=Paper"); + Console.WriteLine("1...2...3...What's your choice"); + if (Choices.TryGetBySelector(Console.ReadLine(), out var choice)) + return choice; + Console.WriteLine("Invalid."); + } + } + } +} diff --git a/74 Rock Scissors Paper/csharp/Program.cs b/74 Rock Scissors Paper/csharp/Program.cs new file mode 100644 index 00000000..acd49e4b --- /dev/null +++ b/74 Rock Scissors Paper/csharp/Program.cs @@ -0,0 +1,48 @@ +using System; + +namespace RockScissorsPaper +{ + static class Program + { + static void Main(string[] args) + { + Console.WriteLine("GAME OF ROCK, SCISSORS, PAPER"); + Console.WriteLine("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"); + Console.WriteLine(); + Console.WriteLine(); + Console.WriteLine(); + + var numberOfGames = GetNumberOfGames(); + + var game = new Game(); + for (var gameNumber = 1; gameNumber <= numberOfGames; gameNumber++) { + Console.WriteLine(); + Console.WriteLine("Game number {0}", gameNumber); + + game.PlayGame(); + } + + game.WriteFinalScore(); + + Console.WriteLine(); + Console.WriteLine("Thanks for playing!!"); + } + + static int GetNumberOfGames() + { + while (true) { + Console.WriteLine("How many games"); + if (int.TryParse(Console.ReadLine(), out var number)) + { + if (number < 11 && number > 0) + return number; + Console.WriteLine("Sorry, but we aren't allowed to play that many."); + } + else + { + Console.WriteLine("Sorry, I didn't understand."); + } + } + } + } +} diff --git a/74 Rock Scissors Paper/csharp/RockScissorsPaper.csproj b/74 Rock Scissors Paper/csharp/RockScissorsPaper.csproj new file mode 100644 index 00000000..6e6a5510 --- /dev/null +++ b/74 Rock Scissors Paper/csharp/RockScissorsPaper.csproj @@ -0,0 +1,9 @@ + + + + Exe + net5.0 + RockScissorsPaper + + +