Created
March 18, 2019 14:50
-
-
Save DivinityArcane/b67d22556d5d047f4b2b798ccb072305 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace ConsoleAppTest01 | |
{ | |
class Program | |
{ | |
static readonly Random RNG = new Random(); | |
enum Choice | |
{ | |
Invalid = -1, | |
Rock, | |
Paper, | |
Scissors | |
} | |
enum State | |
{ | |
Win = 0, | |
Loss, | |
Draw | |
} | |
static State Check(Choice user, Choice ai) | |
{ | |
if (user == Choice.Rock) | |
{ | |
if (ai == Choice.Paper) return State.Loss; | |
else if (ai == Choice.Scissors) return State.Win; | |
} | |
else if (user == Choice.Paper) | |
{ | |
if (ai == Choice.Rock) return State.Win; | |
else if (ai == Choice.Scissors) return State.Loss; | |
} | |
else if (user == Choice.Scissors) | |
{ | |
if (ai == Choice.Rock) return State.Loss; | |
else if (ai == Choice.Paper) return State.Win; | |
} | |
return State.Draw; | |
} | |
static Choice ProcessInput(string input) | |
{ | |
string proc = input.ToLower().Trim(); | |
if (proc == "rock") return Choice.Rock; | |
else if (proc == "paper") return Choice.Paper; | |
else if (proc == "scissors") return Choice.Scissors; | |
else return Choice.Invalid; | |
} | |
static Choice RandomChoice() | |
{ | |
int rand = RNG.Next(300); | |
if (rand <= 100) return Choice.Rock; | |
else if (rand <= 200) return Choice.Paper; | |
else return Choice.Scissors; | |
} | |
static void Main(string[] args) | |
{ | |
string input = String.Empty; | |
State results; | |
Choice user, ai; | |
int wins = 0, losses = 0; | |
Console.WriteLine("Let's play a simple game of Rock, Paper, Scissors!"); | |
Console.WriteLine("When you're done, simply input 'quit' or close this window!"); | |
while (input != "quit") | |
{ | |
Console.Write("Pick a choice between rock, paper and scissors: "); | |
input = Console.ReadLine(); | |
user = ProcessInput(input); | |
ai = RandomChoice(); | |
results = Check(user, ai); | |
if (user == Choice.Invalid) | |
{ | |
Console.WriteLine("Come on now, that's not a valid option..."); | |
continue; | |
} | |
Console.WriteLine("You chose {0} while the computer chose {1}...", user, ai); | |
if (results == State.Win) | |
{ | |
Console.WriteLine("You won!"); | |
wins++; | |
} | |
else if (results == State.Loss) | |
{ | |
Console.WriteLine("The computer won!"); | |
losses++; | |
} | |
else | |
{ | |
Console.WriteLine("It's a draw! Nobody wins."); | |
} | |
Console.WriteLine("That puts it at {0} for you and {1} for the computer. How about a rematch?", wins, losses); | |
} | |
Console.WriteLine("Done already? Alright, see you later!"); | |
Console.Write("Press any key to close this window."); | |
Console.ReadKey(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment