Advertisement
Guest User

saunderl

a guest
Jan 14th, 2009
1,479
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.56 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace RockPaperScissors
  5. {
  6.     class Program
  7.     {
  8.         static void Main()
  9.         {
  10.             var rand = new Random();
  11.  
  12.             var HumanChoices = string.Empty;
  13.             char[] GameChoices = {'R', 'P', 'S'};
  14.  
  15.             var HumanWins = 0;
  16.             var ComputerWins = 0;
  17.  
  18.             while (true)
  19.             {
  20.                 Console.WriteLine("Human Totals\tComputer Totals\tHuman\tComputer");
  21.                 Console.Write("------------\t---------------\t-----\t--------    ");
  22.  
  23.                 while (HumanWins < 101 && ComputerWins < 101)
  24.                 {
  25.                     var ComputerChoice = rand.Next(1, int.MaxValue) % 3;
  26.                     var SetOfMovesLength = 2;
  27.  
  28.                     if (HumanChoices.Length >= SetOfMovesLength + 1)
  29.                     {
  30.                         var LastSetOfMoves = HumanChoices.Substring(HumanChoices.Length - SetOfMovesLength);
  31.  
  32.                         int count = 0;
  33.                         for (var i = 0; i <= HumanChoices.Length - LastSetOfMoves.Length - 1; i++)
  34.                         {
  35.                             if (HumanChoices.Substring(i, LastSetOfMoves.Length + 1) == LastSetOfMoves + "0") count++;
  36.                         }
  37.  
  38.                         var NumRocks = count;
  39.  
  40.                         count = 0;
  41.                         for (var i = 0; i <= HumanChoices.Length - LastSetOfMoves.Length - 1; i++)
  42.                         {
  43.                             if (HumanChoices.Substring(i, LastSetOfMoves.Length + 1) == LastSetOfMoves + "1") count++;                            
  44.                         }
  45.  
  46.                         var NumPapers = count;
  47.  
  48.                         count = 0;
  49.                         for (var i = 0; i <= HumanChoices.Length - LastSetOfMoves.Length - 1; i++)
  50.                         {
  51.                             if (HumanChoices.Substring(i, LastSetOfMoves.Length + 1) == LastSetOfMoves + "2")  count++;                            
  52.                         }
  53.  
  54.                         var NumScissors = count;
  55.  
  56.                         var tempMax = NumRocks;
  57.                         var l = new List<int>();
  58.  
  59.                         if (tempMax < NumPapers) tempMax = NumPapers;                        
  60.                         if (tempMax < NumScissors) { tempMax = NumScissors; l.Add(2); }
  61.  
  62.                         if (tempMax == NumPapers)  l.Add(1);                      
  63.                         if (tempMax == NumRocks) l.Add(0);                        
  64.  
  65.                         ComputerChoice = (l[0] + 1) % 3;
  66.  
  67.                         if (l.Count > 1) ComputerChoice = (l[(rand.Next(1, int.MaxValue) % l.Count)] + 1) % 3;
  68.                     }
  69.  
  70.                     //10% chance of a random choice
  71.                     if (rand.Next(1, int.MaxValue) % 10 == 1) ComputerChoice = rand.Next(1, int.MaxValue) % 3;
  72.  
  73.                     Console.Write("Enter Choice(R/P/S) ");
  74.  
  75.                     ConsoleKeyInfo temp;
  76.                     do
  77.                     {
  78.                         temp = Console.ReadKey(true);
  79.                     }
  80.                     while (temp.Key.ToString().ToUpper() != "R" &&
  81.                            temp.Key.ToString().ToUpper() != "P" &&
  82.                            temp.Key.ToString().ToUpper() != "S");
  83.  
  84.                     Console.WriteLine(temp.Key.ToString().ToUpper());
  85.  
  86.                     var HumanChoice = temp.Key.ToString() == "R" ? 0 : temp.Key.ToString() == "P" ? 1 : 2;
  87.  
  88.                     if (HumanChoice == ComputerChoice - 1 || (HumanChoice == 2 && ComputerChoice == 0)) ComputerWins++;
  89.                     else if (HumanChoice - 1 == ComputerChoice || (HumanChoice == 0 && ComputerChoice == 2)) HumanWins++;
  90.  
  91.                     HumanChoices += HumanChoice.ToString();
  92.  
  93.                     Console.Write(HumanWins.ToString().PadLeft(7) + "\t\t" + ComputerWins.ToString().PadLeft(9) + "\t" +
  94.                                   GameChoices[HumanChoice] + "\t" + GameChoices[ComputerChoice] + "\t    ");
  95.                 }
  96.  
  97.                
  98.                 if(HumanWins>ComputerWins) Console.WriteLine("Human Wins!");
  99.                 else Console.WriteLine("Computer Wins!");
  100.                
  101.                 Console.WriteLine();
  102.                 Console.WriteLine("To Play Again Press The Spacebar Or Any Other Key To Quit.");
  103.  
  104.                 if (Console.ReadKey(true).Key.ToString() != "Spacebar") break;
  105.  
  106.                 Console.WriteLine();
  107.  
  108.                 HumanWins = 0;
  109.                 ComputerWins = 0;
  110.             }
  111.         }
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement