I just began my C# debut and was just wondering if this Rock-Paper-Scissor console app is good and flexible.
abstract class Participant //Abstract - no need for instances, Many common functionality
{
public int wins;
float winRate;
public void DisplayWinRate()
{
winRate = ((float)wins / Game_Info.gamesPlayed) * 100; //Win rate percentage
string _winRate = String.Format("win rate: {0}%", winRate.ToString());
Console.WriteLine(_winRate.PadLeft(0));
}
public abstract R_P_S Choice(); //Every participant needs to have a choice. No choice about that TROLLOLOLOL
}
enum R_P_S
{
invalid,
rock,
paper,
scissor
}
class Computer : Participant // THE TROLL KING ENTERS
{
//1-3 2
//1-4 3
Random rand = new Random();
public override R_P_S Choice() //Override the abstract method Choice inside the Participant class
{
R_P_S element;
element = (R_P_S)rand.Next(1,Enum.GetNames(typeof(R_P_S)).Length);
return element;
}
}
class Player : Participant // DEEEEEEEZ NUUUTS
{
public override R_P_S Choice()
{
R_P_S element;
string playerChoice = Console.ReadLine().Trim();
bool validEntry = Enum.TryParse(playerChoice, out element);
if (!validEntry)
{
return R_P_S.invalid;
}
return element;
}
}
struct Game_Info //The game's current state
{
public static int gamesPlayed; //Made it static - So it does not change on a object reference basis.
}
static class Settings
{
public static int space = 28;
}
class Game_Loop
{
static void Main()
{
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.Black;
Console.WindowWidth = 50;
Computer comp = new Computer();
Player player = new Player();
R_P_S computerChoice;
R_P_S playerChoice;
ConsoleKeyInfo input;
bool playAgain;
do //Runs at least once
{
Console.Clear(); //Clears the console window after and before the game is played
computerChoice = comp.Choice();
playerChoice = player.Choice();
Console.Clear();
while (playerChoice == computerChoice) // 1/3 chance to 1/2 chance
{
//Interesting to see how many cycles it takes
computerChoice = comp.Choice();
}
Console.WriteLine("Player: " + playerChoice);
Console.WriteLine("\n" + "Computer: " + computerChoice);
determineWinner(player,comp,playerChoice,computerChoice);
//Placing a invalid value does not count as a game played.
Game_Info.gamesPlayed = (playerChoice != R_P_S.invalid) ? Game_Info.gamesPlayed + 1 : Game_Info.gamesPlayed;
Console.WriteLine("\n" + "Play again? <y/n>".PadLeft(32));
Console.WriteLine("\n");
int resetPosY = Console.CursorTop;
int resetPosX = Console.CursorLeft;
Console.SetCursorPosition(30, 0); //Displays the winrate UP-LEFT
player.DisplayWinRate();
Console.SetCursorPosition(30, 2);
comp.DisplayWinRate();
Console.SetCursorPosition(resetPosX, resetPosY); //Where the cursor should be
input = Console.ReadKey(true);
playAgain = input.KeyChar == 'y';
} while (playAgain);
}
public static void determineWinner(Player player ,Computer comp,R_P_S playerChoice,R_P_S computerChoice)
{
R_P_S rock = R_P_S.rock;
R_P_S scissor = R_P_S.scissor;
R_P_S paper = R_P_S.paper;
int space = Settings.space;
if (playerChoice == rock && computerChoice == scissor || playerChoice == paper && computerChoice == rock)
{
player.wins++;
Console.WriteLine("\n" + "You won!".PadLeft(space));
}
else if (playerChoice == scissor && computerChoice == rock || playerChoice == rock && computerChoice == paper)
{
comp.wins++;
Console.WriteLine("\n" + "Computer won!".PadLeft(space + 2));
}
else if (playerChoice == scissor && computerChoice == paper)
{
player.wins++;
Console.WriteLine("\n" + "You won!".PadLeft(space));
}
else if (playerChoice == paper && computerChoice == scissor)
{
comp.wins++;
Console.WriteLine("\n" + "Computer won!".PadLeft(space + 2));
}
else
{
Console.WriteLine("\n" + "invalid value".PadLeft(space + 2));
}
}
}