Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace RockPaperScissorApp
- {
- class Program
- {
- static string userChoice = "";
- static string userChoiceInput;
- static string cpuChoice;
- static int gameCount = 0;
- static int winCount = 0;
- static int tieCount = 0;
- static void Main(string[] args)
- {
- //Loop to keep the game running
- bool playAgain = true;
- do
- {
- //Loop to see if user input equals Rock, Paper, or Scissors, accept any case
- bool inputCorrect = false;
- do
- {
- Console.Clear();
- Console.Write(@"Please enter ""Rock"", ""Paper"", or ""Scissors"" : ");
- userChoiceInput = Console.ReadLine();
- userChoice = userChoiceInput.First().ToString().ToUpper() + userChoiceInput.Substring(1).ToLower();
- if ((userChoiceInput.Equals("Rock", StringComparison.OrdinalIgnoreCase)) ||
- (userChoiceInput.Equals("Paper", StringComparison.OrdinalIgnoreCase)) ||
- (userChoiceInput.Equals("Scissors", StringComparison.OrdinalIgnoreCase)))
- {
- inputCorrect = true;
- }
- else
- {
- Console.Clear();
- Console.WriteLine("Input Invalid");
- }
- } while (!inputCorrect);
- Console.WriteLine($"You chose {userChoice}");
- //Set Rock, Paper, Scissors array
- string[] cpuChoose = new string[] { "Rock", "Paper", "Scissors" };
- //Get random cpu choice of rock, paper, or scissors
- Random rnd = new Random();
- int cpuIndex = rnd.Next(0, 3);
- cpuChoice = cpuChoose[cpuIndex];
- Play();
- DisplayStats();
- Console.WriteLine("");
- Console.WriteLine(@"Play Again? Enter ""Y"" or ""N"" ");
- string playAgainInput = Console.ReadLine();
- if(playAgainInput == "N" || playAgainInput =="n")
- {
- playAgain = false;
- }
- } while (playAgain);
- }
- //Play Round, Display Winner, Display Stats
- static void Play()
- {
- Console.Clear();
- Console.WriteLine("User chooses {0}", userChoice);
- Console.WriteLine("Computer chooses {0}", cpuChoice);
- if (userChoice == cpuChoice)
- {
- Console.WriteLine("Tie");
- tieCount++;
- }
- else
- {
- if (userChoice == "Rock" && cpuChoice == "Scissors")
- {
- Console.WriteLine("You Win!");
- winCount++;
- }
- if (userChoice == "Rock" && cpuChoice == "Paper")
- {
- Console.WriteLine("You Lose");
- }
- if (userChoice == "Paper" && cpuChoice == "Rock")
- {
- Console.WriteLine("You Win!");
- winCount++;
- }
- if (userChoice == "Paper" && cpuChoice == "Scissors")
- {
- Console.WriteLine("You Lose");
- }
- if (userChoice == "Scissors" && cpuChoice == "Paper")
- {
- Console.WriteLine("You Win!");
- winCount++;
- }
- if (userChoice == "Scissors" && cpuChoice == "Rock")
- {
- Console.WriteLine("You Lose");
- }
- }
- gameCount++;
- }
- //Display Stats to user, catch divide by zero
- static void DisplayStats()
- {
- Console.WriteLine("\nGame Count: {0}", gameCount);
- Console.WriteLine("Win Count: {0}", winCount);
- Console.WriteLine("Tie Count: {0}", tieCount);
- try
- {
- double gameCountDouble = (double)gameCount;
- double winCountDouble = (double)winCount;
- double winPercent = winCountDouble / ( gameCountDouble - tieCount);
- Console.WriteLine("Win Count: " + winPercent.ToString("P"));
- }
- catch
- {
- Console.WriteLine("N/A");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement