Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 Selection ChooseSelection(); //Every participant needs to have a choice. No choice about that TROLLOLOLOL
- }
- enum Selection
- {
- invalid,
- rock,
- paper,
- scissor
- }
- class Computer : Participant // THE TROLL KING ENTERS
- {
- //1-3 2
- //1-4 3
- Random rand = new Random();
- public override Selection ChooseSelection() //Override the abstract method Choice inside the Participant class
- {
- Selection element;
- element = (Selection)rand.Next(1,Enum.GetNames(typeof(Selection)).Length);
- return element;
- }
- }
- class Player : Participant //the user of the app
- {
- public override Selection ChooseSelection()
- {
- Selection element;
- string playerChoice = Console.ReadLine().Trim();
- bool validEntry = Enum.TryParse(playerChoice, out element); //try to turn the string into a enum value
- if (!validEntry)
- {
- return Selection.invalid;
- }
- return element;
- }
- }
- class Game_Info //The game's current state
- {
- public static int gamesPlayed; //Made it static - So it does not change on a object reference basis.
- }
- class Game_Loop
- {
- static void Main()
- {
- Console.BackgroundColor = ConsoleColor.White;
- Console.ForegroundColor = ConsoleColor.Black;
- Console.WindowWidth = 50;
- Console.WindowHeight = 10;
- Computer comp = new Computer();
- Player player = new Player();
- Selection computerSelect;
- Selection playerSelect;
- ConsoleKeyInfo keyPressed;
- bool playAgain;
- do //Runs at least once
- {
- Console.Clear(); //Clears the console window after and before the game is played
- computerSelect = comp.ChooseSelection();
- playerSelect = player.ChooseSelection();
- Console.Clear();
- while (playerSelect == computerSelect) // 1/3 chance to 1/2 chance
- {
- //Interesting to see how many cycles it takes
- computerSelect = comp.ChooseSelection();
- }
- Console.WriteLine("Player: " + playerSelect);
- Console.WriteLine("\n" + "Computer: " + computerSelect);
- DetermineWinner(player,comp,playerSelect,computerSelect); //who will be victorious?
- //Placing a invalid value does not count as a game played.
- Game_Info.gamesPlayed = (playerSelect != Selection.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
- keyPressed = Console.ReadKey(true);
- playAgain = keyPressed.KeyChar == 'y';
- } while (playAgain);
- }
- public static void DetermineWinner(Player player ,Computer comp,Selection playerSelect,Selection computerChoice)
- {
- if (playerSelect == Selection.rock && computerChoice == Selection.scissor || playerSelect == Selection.paper && computerChoice ==Selection.rock)
- {
- player.wins++;
- Console.WriteLine("\n" + "You won!".PadLeft(30));
- }
- else if (playerSelect == Selection.scissor && computerChoice == Selection.rock || playerSelect ==Selection. rock && computerChoice ==Selection.paper)
- {
- comp.wins++;
- Console.WriteLine("\n" + "Computer won!".PadLeft(30));
- }
- else if (playerSelect == Selection.scissor && computerChoice == Selection.paper)
- {
- player.wins++;
- Console.WriteLine("\n" + "You won!".PadLeft(30));
- }
- else if (playerSelect == Selection.paper && computerChoice == Selection.scissor)
- {
- comp.wins++;
- Console.WriteLine("\n" + "Computer won!".PadLeft(30));
- }
- else
- {
- Console.WriteLine("\n" + "invalid value".PadLeft(30));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment