mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-22 23:26:40 -08:00
Merge pull request #58 from jessemcdowell/feature/74-csharp
Implement Rock Scissors Paper (Game 74) in C#
This commit is contained in:
19
74 Rock Scissors Paper/csharp/Choice.cs
Normal file
19
74 Rock Scissors Paper/csharp/Choice.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
42
74 Rock Scissors Paper/csharp/Choices.cs
Normal file
42
74 Rock Scissors Paper/csharp/Choices.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
58
74 Rock Scissors Paper/csharp/Game.cs
Normal file
58
74 Rock Scissors Paper/csharp/Game.cs
Normal 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.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
48
74 Rock Scissors Paper/csharp/Program.cs
Normal file
48
74 Rock Scissors Paper/csharp/Program.cs
Normal 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.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
9
74 Rock Scissors Paper/csharp/RockScissorsPaper.csproj
Normal file
9
74 Rock Scissors Paper/csharp/RockScissorsPaper.csproj
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net5.0</TargetFramework>
|
||||||
|
<RootNamespace>RockScissorsPaper</RootNamespace>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
Reference in New Issue
Block a user