Rock Paper Scissors game not working as expected

14 year old kid here. I have bad experience using parse methods. Cause i don't really know how they work most of the time. People want me to be using enums instead of string when i am making my RPS game but when a user enters a invalid value it just turns into rock.

 abstract class Participant //Abstract - no need for instances, Many common functionality { public int wins; float winRate; public void DisplayWinRate() { winRate = (wins / Game_Info.gamesPlayed) * 100; //Win rate percentage string _winRate =String.Format("win rate: {0}%",winRate.ToString()); Console.WriteLine(_winRate.PadLeft(0)); } public abstract Rock_Paper_Scissor Choice(); //Every participant needs to have a choice } enum Rock_Paper_Scissor { rock, paper, scissor } class Computer:Participant { Random rand = new Random(); public override Rock_Paper_Scissor Choice() //Override the abstract method Choice inside the Participant class { Rock_Paper_Scissor element; element = (Rock_Paper_Scissor)rand.Next(Enum.GetNames(typeof(Rock_Paper_Scissor)).Length); return element; } } class Player:Participant { public override Rock_Paper_Scissor Choice() { string playerChoice = Console.ReadLine().Trim(); // Take out the whitespaces around the player's input Rock_Paper_Scissor element; bool parse = Enum.TryParse(playerChoice, out element); 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. } 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(); string computerChoice; string playerChoice; ConsoleKeyInfo input; int space = 28; bool playAgain; bool invalidValue = false; do //Runs at least once { Console.Clear(); //Clears the console window after and before the game is played computerChoice = comp.Choice().ToString(); playerChoice = player.Choice().ToString(); Console.Clear(); while (playerChoice == computerChoice) // 1/3 chance to 1/2 chance { //Interesting to see how many cycles it takes computerChoice = comp.Choice().ToString(); } Console.WriteLine("Player: "+ playerChoice); Console.WriteLine("\n"+"Computer: " + computerChoice); 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)); invalidValue = true; } //Placing a invalid value does not count as a game played. Game_Info.gamesPlayed = (!invalidValue) ? Game_Info.gamesPlayed + 1 : Game_Info.gamesPlayed; invalidValue = false; 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); } } 

by MatriXcel via /r/csharp

Leave a Reply