Merge pull request #58 from jessemcdowell/feature/74-csharp

Implement Rock Scissors Paper (Game 74) in C#
This commit is contained in:
Jeff Atwood
2021-02-20 14:41:32 -08:00
committed by GitHub
5 changed files with 176 additions and 0 deletions

View File

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

View File

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

View File

@@ -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.");
}
}
}
}

View File

@@ -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.");
}
}
}
}
}

View File

@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<RootNamespace>RockScissorsPaper</RootNamespace>
</PropertyGroup>
</Project>