CSharp · Console
using System;
public enum Action { Rock, Paper, Scissor };
public enum Result { Tie, PlayerWon, ComputerWon }
public class Program {
public static Random rand;
public static void Main() {
// static variable used in GetComputerChoice
rand = new Random();
int playerPoints = 0;
int computerPoints = 0;
Console.WriteLine("How many rounds would you like to play?");
int rounds = GetDesiredRounds();
for(var round = 1; round <= rounds; round++) {
Console.WriteLine("Round {0} Begins", round);
Console.WriteLine("Which hand do you choose(Rock, Paper, Scissor)? ");
var playerAction = GetPlayerAction();
Console.WriteLine("You picked: {0}", playerAction.ToString());
var computerAction = GetComputerAction();
Console.WriteLine("Your opponent picked: {0}", computerAction.ToString());
switch(CalculateResult(playerAction, computerAction)) {
case Result.PlayerWon:
Console.WriteLine("You won the round! You gained a point.");
playerPoints++;
break;
case Result.ComputerWon:
Console.WriteLine("Computer won the round! Computer gained a point.");
computerPoints++;
break;
case Result.Tie:
Console.WriteLine("Round tied. You and the computer gained a point.");
playerPoints++;
computerPoints++;
break;
}
Console.WriteLine();
}
Console.WriteLine("Results - Player {0}, Computer {1}", playerPoints, computerPoints);
if (playerPoints == computerPoints) {
Console.WriteLine("Tie Game");
} else {
bool isPlayerWinner = playerPoints > computerPoints;
Console.WriteLine("{0} won the game!", isPlayerWinner ? "Player" : "Computer");
}
}
public static int GetDesiredRounds() {
int result;
do {
var input = Console.ReadLine();
if(Int32.TryParse(input, out result)) {
return result;
} else {
Console.WriteLine("Invalid input. Please input a number.");
}
} while(true);
}
public static Action GetPlayerAction() {
Action result;
do {
var input = Console.ReadLine();
// Second param tells to ignore case of string
if(Action.TryParse(input, true, out result)) {
return result;
} else {
Console.WriteLine("Invalid action {0}. Please input 'Rock', 'Paper' or 'Scissors'.", input);
}
} while(true);
}
public static Action GetComputerAction() {
// Gets a random number between 1 (Action.Rock) and 4 (Action.Scissor + 1)
// This will result in the values 1 (Rock), 2 (Paper), or 3 (Scissor)
return (Action)rand.Next((int)Action.Rock, (int)Action.Scissor + 1);
}
public static Result CalculateResult(Action p, Action c) {
switch(p) {
case Action.Rock:
switch (c) {
case Action.Rock: return Result.Tie;
case Action.Paper: return Result.ComputerWon;
case Action.Scissor: return Result.PlayerWon;
}
break;
case Action.Paper:
switch (c) {
case Action.Rock: return Result.PlayerWon;
case Action.Paper: return Result.Tie;
case Action.Scissor: return Result.ComputerWon;
}
break;
case Action.Scissor:
switch (c) {
case Action.Rock: return Result.ComputerWon;
case Action.Paper: return Result.PlayerWon;
case Action.Scissor: return Result.Tie;
}
break;
}
throw new Exception(string.Format("Unhandled action pair occured: {0}, {1}", p, c));
}
}