This is code for a Rock-Paper-Scissors game I made using some of the OOP concepts I learned in C#. I don't know if I am making the right choices but I am fairly confident. It would be great if I could receive guidance here.
I chose to have a Player
and Computer
class inheriting from an abstract Participant
class. Everything else is control flow which is inside the Game
class which has a struct inside it, GameInfo
, which will store all information regarding global game data.
using System;
enum RockPaperScissors
{
Rock,
Paper,
Scissors
}
abstract class Participant
{
public int wins { get; set; }
float _winRate;
protected RockPaperScissors selection;
protected float winRate
{
get
{
return _winRate;
}
set
{
if (value < 0 || value > 100)
{
throw new Exception("value cannot be less than 0 or greater than 100");
}
_winRate = value;
}
}
public void PrintWinRate()
{
this.winRate = ((float)wins / Game.Info.GamesPlayed) * 100;
string winRate = "win rate: " + this.winRate.ToString() + "%";
Console.WriteLine(winRate.PadLeft(50));
}
public abstract RockPaperScissors Select();
}
class Computer : Participant
{
public override RockPaperScissors Select()
{
Random rand = new Random();
selection = (RockPaperScissors)rand.Next(0, Enum.GetValues(typeof(RockPaperScissors)).Length);
return selection;
}
}
class Player : Participant
{
public override RockPaperScissors Select()
{
bool isValid;
string input;
do
{
Console.Write("Please enter a valid selection: ");
input = Console.ReadLine().Trim();
isValid = Enum.TryParse<RockPaperScissors>(input, true, out selection);
} while (!isValid);
return selection;
}
}
class Game
{
public struct GameInfo
{
public int GamesPlayed;
}
public static GameInfo Info = new GameInfo();
static void Main()
{
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.Black;
Participant comp = new Computer();
Participant player = new Player();
RockPaperScissors computerSelection;
RockPaperScissors playerSelection;
ConsoleKeyInfo input;
bool playAgain;
do
{
Console.Clear();
computerSelection= comp.Select();
playerSelection = player.Select();
Console.Clear();
Console.WriteLine("Player: " + playerSelection);
Console.WriteLine("\n" + "Computer: " + computerSelection);
switch (determineWinner((int)computerSelection, (int)playerSelection))
{
case null:
Console.Write("\n it's a tie");
break;
case true:
Console.Write("\n you won!");
player.wins++;
break;
default:
Console.Write("\n you lost");
comp.wins++;
break;
}
Game.Info.GamesPlayed++;
Console.WriteLine("\n" + "Play again? <y/n>");
Console.WriteLine("\n");
int resetPosY = Console.CursorTop;
int resetPosX = Console.CursorLeft;
Console.SetCursorPosition(30, 0);
player.PrintWinRate();
Console.SetCursorPosition(30, 2);
comp.PrintWinRate();
Console.SetCursorPosition(resetPosX, resetPosY);
input = Console.ReadKey(true);
playAgain = input.KeyChar == 'y';
} while (playAgain);
}
public static bool? determineWinner(int playerSelection, int computerSelection)
{
bool?[,] winMatrix = {
{null, false, true },
{true, null, false },
{false, true, null}
};
if (winMatrix[playerSelection, computerSelection] == null)
return null;
return (winMatrix[playerSelection, computerSelection] == true) ? true : false;
}
}