private static void UpdateScores(Player player, Opponent opponent, bool isPlayer) { if (isPlayer) { player.Wins++; opponent.Losses++; } else { opponent.Wins++; player.Losses++; } }
/// <summary> /// Entry point to start the game. /// </summary> /// <param name="player"><see cref="Player"/></param> /// <param name="opponent"><see cref="Opponent"/></param> public static void Startmatch(Player player, Opponent opponent) { Console.Clear(); Console.WriteLine($"Make a choice by typing the number associated to your choice:\n1 -> Rock (Defaulted)\n2 -> Paper\n3 -> Scissors\n"); player.Choice = PlayerChoice(Console.ReadKey().Key); Console.WriteLine(player.Choice); while (true) { Console.Clear(); Console.WriteLine($"You chose {player.Choice}, are you sure? (1 - yes / 2 - no)"); if (Console.ReadKey().Key == ConsoleKey.D1) { opponent.RandomizeChoice(); Task.Delay(300); Console.Clear(); Console.WriteLine($"You: { player.Choice }"); Console.WriteLine($"--------------------"); Console.WriteLine($"Opponent: { opponent.Choice }\n"); CompareChoices(player, opponent); Console.WriteLine($"\nScore:"); Console.WriteLine($"You - { player.Wins }"); Console.WriteLine($"Opponent - { opponent.Wins }"); Console.WriteLine($"\nWould you like to play another match? (1 - yes / 2 - quit)"); while (true) { if (Console.ReadKey().Key == ConsoleKey.D1) { Startmatch(player, opponent); break; } else if (Console.ReadKey().Key == ConsoleKey.D2) { Environment.Exit(0); } } } else { Startmatch(player, opponent); } } }
/// <summary> /// Logic to compare the choices of the player and computer /// to determine the winner of the match. /// </summary> /// <param name="player"><see cref="Player"/></param> /// <param name="opponent"><see cref="Opponent"/></param> public static void CompareChoices(Player player, Opponent opponent) { //Player wins with Rock > Scissors if (player.Choice == Choices.Rock && opponent.Choice == Choices.Scissors) { Console.WriteLine($"Your { player.Choice} beat your Opponent's { opponent.Choice }!"); UpdateScores(player, opponent, true); } //Player wins Paper > Rock else if (player.Choice == Choices.Paper && opponent.Choice == Choices.Rock) { Console.WriteLine($"Your { player.Choice} beat your Opponent's { opponent.Choice }!"); UpdateScores(player, opponent, true); } //Player wins Scissors > Paper else if (player.Choice == Choices.Scissors && opponent.Choice == Choices.Paper) { Console.WriteLine($"Your { player.Choice} beat your Opponent's { opponent.Choice }!"); UpdateScores(player, opponent, true); } //Computer wins Rock > Scissors else if (opponent.Choice == Choices.Rock && player.Choice == Choices.Scissors) { Console.WriteLine($"Your Opponent's { opponent.Choice } beat your { player.Choice }"); UpdateScores(player, opponent, false); } //Computer wins Paper > Rock else if (opponent.Choice == Choices.Paper && player.Choice == Choices.Rock) { Console.WriteLine($"Your Opponent's { opponent.Choice } beat your { player.Choice }"); UpdateScores(player, opponent, false); } //Computer wins Scissors > Paper else if (opponent.Choice == Choices.Scissors && player.Choice == Choices.Paper) { Console.WriteLine($"Your Opponent's { opponent.Choice } beat your { player.Choice }"); UpdateScores(player, opponent, false); } //Player and opponent Tie else { Console.WriteLine($"You and your Opponent chose {player.Choice}, resulting in a tie!"); } }
static void Main(string[] args) { Player player = new Player(); Opponent opponent = new Opponent(); Console.WriteLine($"Welcome to the Rock Paper Scissor Game!"); Console.WriteLine($"Press the space button when you are ready..."); while (true) { Console.WriteLine("If you are ready, press any key to contine\nOr press Escape to quit."); if (Console.ReadKey().Key != ConsoleKey.Escape) { GamePlay.Startmatch(player, opponent); break; } else { Environment.Exit(0); } } }
static void Main(string[] args) { //Create the player object Player p1 = new Player(); p1.GetPlayerName(); p1.NeedRules(); //Create the Opponent object Opponent o1 = new Opponent(); //start the game Game game = new Game(); do { string playerDecision = p1.PlayerSelection(); string opponentDecision = o1.OpponentSelection(); Console.WriteLine("You chose {0}", playerDecision); Console.WriteLine("The Horde of Hamsters chose {0}", opponentDecision); game.WhoWins(playerDecision, opponentDecision); } while (game.PlayAgain()); }
static void Main(string[] args) { // Instantiate the player and opponent objects with a temporary name Player mainPlayer = new Player(""); Opponent aiEnemy = new Opponent(); // Greet and request a valid name Console.WriteLine("Welcome to Rock Paper Scissors. What is your name?"); mainPlayer.RequestName(); // Main game loop in a do while() to ensure it runs at least once do { // If not yet defined, ask the player how many round they would like to play, validate mainPlayer.RequestNumberOfRounds(); // Request player chooses a hand mainPlayer.RequestPlayerHand(); // Generate a random opponent hand aiEnemy.GenerateHand(); // Decide win/lose/tie string roundResult = aiEnemy.DetermineRound(mainPlayer.Hand); // Let the player object act upon win/lose/tie mainPlayer.ProcessRound(roundResult); // If no rounds remaining, display results and ask the player if they would like to play again. if (mainPlayer.RoundsRemaining == 0) { mainPlayer.DisplayResults(); mainPlayer.PromptToReplay(); } } while (mainPlayer.RoundsRemaining > 0); }