Guest User

Rock_Paper_Scissor C#

a guest
Aug 7th, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.25 KB | None | 0 0
  1. abstract class Participant   //Abstract - no need for instances, Many common functionality
  2.     {
  3.         public int wins;
  4.         float winRate;
  5.  
  6.         public void DisplayWinRate()
  7.         {
  8.             winRate = ((float)wins / Game_Info.gamesPlayed) * 100;  //Win rate percentage
  9.  
  10.             string _winRate = String.Format("win rate: {0}%", winRate.ToString());
  11.             Console.WriteLine(_winRate.PadLeft(0));
  12.         }
  13.  
  14.         public abstract Selection ChooseSelection();   //Every participant needs to have a choice. No choice about that TROLLOLOLOL
  15.     }
  16.     enum Selection
  17.     {
  18.         invalid,
  19.         rock,
  20.         paper,
  21.         scissor
  22.     }
  23.  
  24.     class Computer : Participant // THE TROLL KING ENTERS
  25.     {
  26.  
  27.         //1-3 2
  28.         //1-4 3
  29.  
  30.         Random rand = new Random();
  31.  
  32.         public override Selection ChooseSelection()  //Override the abstract method Choice inside the Participant class
  33.         {
  34.             Selection element;
  35.             element = (Selection)rand.Next(1,Enum.GetNames(typeof(Selection)).Length);
  36.             return element;
  37.         }
  38.  
  39.     }
  40.     class Player : Participant  //the user of the app
  41.     {
  42.  
  43.         public override Selection ChooseSelection()
  44.         {
  45.            
  46.             Selection element;
  47.             string playerChoice = Console.ReadLine().Trim();
  48.             bool validEntry = Enum.TryParse(playerChoice, out element);  //try to turn the string into a enum value
  49.  
  50.                 if (!validEntry)
  51.                 {
  52.                     return Selection.invalid;
  53.                 }
  54.               return element;
  55.            }
  56.         }
  57.  
  58.  
  59.     class Game_Info //The game's current state
  60.     {
  61.         public static int gamesPlayed;  //Made it static - So it does not change on a object reference basis.
  62.  
  63.     }
  64.  
  65.     class Game_Loop
  66.     {
  67.         static void Main()
  68.         {
  69.             Console.BackgroundColor = ConsoleColor.White;
  70.             Console.ForegroundColor = ConsoleColor.Black;
  71.             Console.WindowWidth = 50;
  72.             Console.WindowHeight = 10;
  73.             Computer comp = new Computer();
  74.             Player player = new Player();
  75.             Selection computerSelect;
  76.             Selection playerSelect;
  77.             ConsoleKeyInfo keyPressed;
  78.             bool playAgain;
  79.  
  80.             do  //Runs at least once
  81.             {
  82.  
  83.                 Console.Clear();  //Clears the console window after and before the game is played
  84.  
  85.                 computerSelect = comp.ChooseSelection();
  86.                 playerSelect = player.ChooseSelection();
  87.  
  88.                 Console.Clear();
  89.  
  90.                 while (playerSelect == computerSelect)  // 1/3 chance to 1/2 chance
  91.                 {
  92.                     //Interesting to see how many cycles it takes
  93.                     computerSelect = comp.ChooseSelection();
  94.                 }
  95.                 Console.WriteLine("Player: " + playerSelect);
  96.                 Console.WriteLine("\n" + "Computer: " + computerSelect);
  97.  
  98.                  DetermineWinner(player,comp,playerSelect,computerSelect);  //who will be victorious?
  99.                
  100.                 //Placing a invalid value does not count as a game played.
  101.  
  102.                 Game_Info.gamesPlayed = (playerSelect != Selection.invalid) ? Game_Info.gamesPlayed + 1 : Game_Info.gamesPlayed;
  103.  
  104.                
  105.                 Console.WriteLine("\n" + "Play again? <y/n>".PadLeft(32));
  106.                 Console.WriteLine("\n");
  107.  
  108.                 int resetPosY = Console.CursorTop;
  109.                 int resetPosX = Console.CursorLeft;
  110.  
  111.                 Console.SetCursorPosition(30, 0);  //Displays the winrate UP-LEFT
  112.                 player.DisplayWinRate();
  113.                 Console.SetCursorPosition(30, 2);
  114.                 comp.DisplayWinRate();
  115.                 Console.SetCursorPosition(resetPosX, resetPosY);   //Where the cursor should be
  116.  
  117.                 keyPressed = Console.ReadKey(true);
  118.                 playAgain = keyPressed.KeyChar == 'y';
  119.  
  120.             } while (playAgain);
  121.          }
  122.         public static void DetermineWinner(Player player ,Computer comp,Selection playerSelect,Selection computerChoice)
  123.         {
  124.            
  125.            
  126.  
  127.          if (playerSelect == Selection.rock && computerChoice == Selection.scissor || playerSelect == Selection.paper && computerChoice ==Selection.rock)
  128.          {
  129.              player.wins++;
  130.              Console.WriteLine("\n" + "You won!".PadLeft(30));
  131.  
  132.          }
  133.          else if (playerSelect == Selection.scissor && computerChoice == Selection.rock || playerSelect ==Selection. rock && computerChoice ==Selection.paper)
  134.          {
  135.              comp.wins++;
  136.              Console.WriteLine("\n" + "Computer won!".PadLeft(30));
  137.          }
  138.          else if (playerSelect == Selection.scissor && computerChoice == Selection.paper)
  139.          {
  140.              player.wins++;
  141.              Console.WriteLine("\n" + "You won!".PadLeft(30));
  142.          }
  143.          else if (playerSelect == Selection.paper && computerChoice == Selection.scissor)
  144.          {
  145.              comp.wins++;
  146.              Console.WriteLine("\n" + "Computer won!".PadLeft(30));
  147.          }
  148.          else
  149.          {
  150.              Console.WriteLine("\n" + "invalid value".PadLeft(30));
  151.          }
  152.            
  153.         }
  154.        
  155.     }
Advertisement
Add Comment
Please, Sign In to add comment