Advertisement
jyoung12387

Rock Paper Scissors v1.1

Mar 2nd, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace RockPaperScissorApp
  8. {
  9.     class Program
  10.     {
  11.         static string userChoice = "";
  12.         static string userChoiceInput;
  13.         static string cpuChoice;
  14.         static int gameCount = 0;
  15.         static int winCount = 0;
  16.         static int tieCount = 0;
  17.  
  18.         static void Main(string[] args)
  19.         {
  20.             //Loop to keep the game running
  21.             bool playAgain = true;
  22.             do
  23.             {
  24.  
  25.                 //Loop to see if user input equals Rock, Paper, or Scissors, accept any case
  26.                 bool inputCorrect = false;
  27.                 do
  28.                 {
  29.                     Console.Clear();
  30.                     Console.Write(@"Please enter ""Rock"", ""Paper"", or ""Scissors"" : ");
  31.                     userChoiceInput = Console.ReadLine();
  32.                     userChoice = userChoiceInput.First().ToString().ToUpper() + userChoiceInput.Substring(1).ToLower();
  33.  
  34.                     if ((userChoiceInput.Equals("Rock", StringComparison.OrdinalIgnoreCase)) ||
  35.                     (userChoiceInput.Equals("Paper", StringComparison.OrdinalIgnoreCase)) ||
  36.                     (userChoiceInput.Equals("Scissors", StringComparison.OrdinalIgnoreCase)))
  37.                     {
  38.                         inputCorrect = true;
  39.                     }
  40.                     else
  41.                     {
  42.                         Console.Clear();
  43.                         Console.WriteLine("Input Invalid");
  44.                     }
  45.                 } while (!inputCorrect);
  46.  
  47.                 Console.WriteLine($"You chose {userChoice}");
  48.  
  49.                 //Set Rock, Paper, Scissors array
  50.                 string[] cpuChoose = new string[] { "Rock", "Paper", "Scissors" };
  51.  
  52.                 //Get random cpu choice of rock, paper, or scissors
  53.                 Random rnd = new Random();
  54.                 int cpuIndex = rnd.Next(0, 3);
  55.                 cpuChoice = cpuChoose[cpuIndex];
  56.  
  57.                 Play();
  58.                 DisplayStats();
  59.  
  60.                 Console.WriteLine("");
  61.                 Console.WriteLine(@"Play Again? Enter ""Y"" or ""N"" ");
  62.                 string playAgainInput = Console.ReadLine();
  63.  
  64.                 if(playAgainInput == "N" || playAgainInput =="n")
  65.                 {
  66.                     playAgain = false;
  67.                 }
  68.  
  69.             } while (playAgain);
  70.         }
  71.        
  72.         //Play Round, Display Winner, Display Stats
  73.         static void Play()
  74.         {
  75.             Console.Clear();
  76.             Console.WriteLine("User chooses {0}", userChoice);
  77.             Console.WriteLine("Computer chooses {0}", cpuChoice);
  78.  
  79.             if (userChoice == cpuChoice)
  80.             {
  81.                 Console.WriteLine("Tie");
  82.                 tieCount++;
  83.             }
  84.             else
  85.             {
  86.                 if (userChoice == "Rock" && cpuChoice == "Scissors")
  87.                 {
  88.                     Console.WriteLine("You Win!");
  89.                     winCount++;
  90.                 }
  91.                 if (userChoice == "Rock" && cpuChoice == "Paper")
  92.                 {
  93.                     Console.WriteLine("You Lose");
  94.                 }
  95.                 if (userChoice == "Paper" && cpuChoice == "Rock")
  96.                 {
  97.                     Console.WriteLine("You Win!");
  98.                     winCount++;
  99.                 }
  100.                 if (userChoice == "Paper" && cpuChoice == "Scissors")
  101.                 {
  102.                     Console.WriteLine("You Lose");
  103.                 }
  104.                 if (userChoice == "Scissors" && cpuChoice == "Paper")
  105.                 {
  106.                     Console.WriteLine("You Win!");
  107.                     winCount++;
  108.                 }
  109.                 if (userChoice == "Scissors" && cpuChoice == "Rock")
  110.                 {
  111.                     Console.WriteLine("You Lose");
  112.                 }
  113.             }
  114.             gameCount++;
  115.         }
  116.  
  117.         //Display Stats to user, catch divide by zero
  118.         static void DisplayStats()
  119.         {
  120.             Console.WriteLine("\nGame Count: {0}", gameCount);
  121.             Console.WriteLine("Win Count: {0}", winCount);
  122.             Console.WriteLine("Tie Count: {0}", tieCount);
  123.  
  124.             try
  125.             {
  126.                 double gameCountDouble = (double)gameCount;
  127.                 double winCountDouble = (double)winCount;
  128.                 double winPercent = winCountDouble / ( gameCountDouble - tieCount);
  129.  
  130.                 Console.WriteLine("Win Count: " + winPercent.ToString("P"));
  131.             }
  132.             catch
  133.             {
  134.                 Console.WriteLine("N/A");
  135.             }
  136.         }
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement