Skip to content

Instantly share code, notes, and snippets.

@Kilowhisky
Created January 22, 2021 23:06
Show Gist options
  • Save Kilowhisky/5d48242725280a03f448053d9a776380 to your computer and use it in GitHub Desktop.
Save Kilowhisky/5d48242725280a03f448053d9a776380 to your computer and use it in GitHub Desktop.
Rock Paper Scissors program build in C#
using System;
using System.Collections.Generic;
public class Program
{
public static List<WinEnum> PlayerStats = new List<WinEnum>();
public static void Main()
{
Console.WriteLine("Welcome to my great game");
Console.WriteLine("I bet you can't beat me");
do
{
// Make sure the user input is valid and acceptable
MovesEnum? userPlay = null;
do {
Console.WriteLine(@"Pick your play
0 = Rock
1 = Paper
2 = Scisors");
var input = Console.ReadLine();
int play;
if (int.TryParse(input, out play) && Enum.IsDefined(typeof(MovesEnum), play))
{
userPlay = (MovesEnum)play;
} else {
Console.WriteLine("Thats not right, try again");
}
} while (!userPlay.HasValue);
// Generate the computer play
var random = new Random();
var computerPlay = (MovesEnum)random.Next((int)MovesEnum.Rock, (int)MovesEnum.Scisors + 1);
// Figure out if you won
var winStat = FirstPlayerWon(userPlay.Value, computerPlay);
// Add to the users score card
PlayerStats.Add(winStat);
// Tell the user they suck at playing
Console.WriteLine(string.Format("Congratulations you {0}, you played {1}, computer played {2}", winStat, userPlay, computerPlay));
Console.WriteLine(string.Format("Your player history so far: {0}", string.Join(", ",PlayerStats)));
Console.WriteLine("Thanks for playing, try again? (Y/N)");
} while(Console.ReadLine().ToUpperInvariant() == "Y");
}
public static WinEnum FirstPlayerWon(MovesEnum computerMove, MovesEnum userMove)
{
switch(computerMove)
{
case MovesEnum.Rock:
if (userMove == MovesEnum.Scisors) return WinEnum.Won;
else if (userMove == MovesEnum.Rock) return WinEnum.Tied;
else return WinEnum.Lost;
case MovesEnum.Paper:
if (userMove == MovesEnum.Scisors) return WinEnum.Lost;
else if (userMove == MovesEnum.Rock) return WinEnum.Won;
else return WinEnum.Tied;
case MovesEnum.Scisors:
if (userMove == MovesEnum.Scisors) return WinEnum.Tied;
else if (userMove == MovesEnum.Rock) return WinEnum.Lost;
else return WinEnum.Won;
default:
return WinEnum.Lost;
}
}
public enum MovesEnum {
Rock = 0,
Paper,
Scisors
}
public enum WinEnum {
Won,
Lost,
Tied
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment