Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.80 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 RandomRacer
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string gameTitle = @"
  14.          :::::::::       :::       ::::    :::   :::::::::    ::::::::      :::   :::
  15.         :+:    :+:    :+: :+:     :+:+:   :+:   :+:    :+:  :+:    :+:    :+:+: :+:+:
  16.        +:+    +:+   +:+   +:+    :+:+:+  +:+   +:+    +:+  +:+    +:+   +:+ +:+:+ +:+
  17.       +#++:++#:   +#++:++#++:   +#+ +:+ +#+   +#+    +:+  +#+    +:+   +#+  +:+  +#+  
  18.      +#+    +#+  +#+     +#+   +#+  +#+#+#   +#+    +#+  +#+    +#+   +#+       +#+  
  19.     #+#    #+#  #+#     #+#   #+#   #+#+#   #+#    #+#  #+#    #+#   #+#       #+#    
  20.    ###    ###  ###     ###   ###    ####   #########    ########    ###       ###    
  21.  
  22.                :::::::::       :::       ::::::::    ::::::::::   :::::::::
  23.               :+:    :+:    :+: :+:    :+:    :+:   :+:          :+:    :+:
  24.              +:+    +:    +:+   +:+   +:+          +:+          +:+    +:+  
  25.             +#++:++#:   +#++:++#++:  +#+          +#++:++#     +#++:++#:    
  26.            +#+    +#+  +#+     +#+  +#+          +#+          +#+    +#+    
  27.           #+#    #+#  #+#     #+#  #+#    #+#   #+#          #+#    #+#    
  28.          ###    ###  ###     ###   ########    ##########   ###    ###      
  29. ";
  30.             Console.WriteLine();
  31.             Console.WriteLine(gameTitle);
  32.             Console.WriteLine();
  33.             Console.WriteLine("\n\t\tPlay against the computer in a very random race game.");
  34.             Console.WriteLine("\t\t       H = human player   C = computer player");
  35.             Console.WriteLine();
  36.             PressAnyKey("\n\t\t\t   Press any key to continue ...");
  37.             ClearScreen();
  38.             Play();
  39.         }
  40.  
  41.         static int wins = 0;
  42.         static int losses = 0;
  43.  
  44.         static void Play()
  45.         {
  46.             string[] numbers = new string[50];
  47.             string[] numbers2 = new string[50];
  48.             bool win = false;
  49.             bool play = true;
  50.             bool tie = false;
  51.            
  52.             string[] player = new string[]
  53.             {
  54.                 "H","-","-","-","-","-","-","-","-","-",
  55.                 "-","-","-","-","-","-","-","-","-","-",
  56.                 "-","-","-","-","-","-","-","-","-","-",
  57.                 "-","-","-","-","-","-","-","-","-","-",
  58.                 "-","-","-","-","-","-","-","-","-","-",
  59.             };
  60.             string[] computer = new string[]
  61.             {
  62.                 "C","-","-","-","-","-","-","-","-","-",
  63.                 "-","-","-","-","-","-","-","-","-","-",
  64.                 "-","-","-","-","-","-","-","-","-","-",
  65.                 "-","-","-","-","-","-","-","-","-","-",
  66.                 "-","-","-","-","-","-","-","-","-","-",
  67.             };
  68.  
  69.             for (int i = 0; i < player.Length; i++)
  70.             {
  71.                 numbers[i] = player[i];
  72.             }
  73.             for (int i = 0; i < computer.Length; i++)
  74.             {
  75.                 numbers2[i] = computer[i];
  76.             }
  77.  
  78.             ConsoleKeyInfo input;
  79.  
  80.  
  81.             int count = 0;
  82.             int value = 0;
  83.             Random random = new Random();
  84.  
  85.             value = random.Next(0, 6);
  86.             Console.WriteLine("\n{0}", string.Join("", numbers));
  87.             Console.WriteLine("\n|---------|---------|---------|---------|---------|");
  88.             Console.WriteLine("\n{0}", string.Join("", numbers2));
  89.  
  90.             while (play)
  91.             {
  92.                 input = Console.ReadKey(true);
  93.                 if (input.Key != ConsoleKey.PrintScreen)
  94.                 {
  95.                     value = random.Next(0, 7);
  96.                     count = 0;
  97.                     while (count != value||play)
  98.                     {
  99.  
  100.                         string temp = numbers[numbers.Length - 1];
  101.  
  102.                         if (temp == "H")
  103.                         {
  104.                             play = false;
  105.                             win = true;
  106.                             continue;
  107.                         }
  108.  
  109.                         for (int i = numbers.Length - 1; i > 0; i--)
  110.                         {
  111.                             numbers[i] = numbers[i - 1];
  112.                         }
  113.                         numbers[0] = temp;
  114.                         count++;
  115.                        
  116.                     }
  117.                     value = random.Next(0, 7);
  118.                     count = 0;
  119.                     while (count != value||play)
  120.                     {
  121.  
  122.                         string temp2 = numbers2[numbers2.Length - 1];
  123.  
  124.                         if (temp2 == "C")
  125.                         {
  126.                             if (win == true)
  127.                             {
  128.                                 tie = true;
  129.                             }
  130.  
  131.                             play = false;
  132.                             continue;
  133.                         }
  134.  
  135.                         for (int i = numbers2.Length - 1; i > 0; i--)
  136.                         {
  137.                             numbers2[i] = numbers2[i - 1];
  138.                         }
  139.                         numbers2[0] = temp2;
  140.                         count++;
  141.                        
  142.                     }
  143.                    
  144.                     Console.Clear();
  145.                     Console.SetCursorPosition(0, 0);
  146.                     Console.WriteLine("\n{0}", string.Join("", numbers));
  147.                     Console.WriteLine("\n|---------|---------|---------|---------|---------|");
  148.                     Console.WriteLine("\n{0}", string.Join("", numbers2));
  149.                    
  150.                 }
  151.             }
  152.  
  153.             playAnother(numbers, numbers2, win, tie);            
  154.         }
  155.  
  156.         static void playAnother(string[] numbers, string[] numbers2, bool win, bool tie)
  157.         {
  158.             //Asks the player if they want to play another game.
  159.             string anotherGame = "y";            
  160.  
  161.             while (true)
  162.             {
  163.                 string outcome;
  164.                 if (tie)
  165.                 {
  166.                     outcome = " YOU TIE!";
  167.                 }
  168.                 else
  169.                 {
  170.                     outcome = win ? " YOU WIN!" : " YOU LOSE!";
  171.                 }
  172.                 Console.Clear();
  173.                 Console.SetCursorPosition(0, 0);
  174.                 Console.WriteLine("\n{0}", string.Join("", numbers) + (outcome));
  175.                 Console.WriteLine("\n|---------|---------|---------|---------|---------|");
  176.                 Console.WriteLine("\n{0}", string.Join("", numbers2));
  177.  
  178.                 Console.Write("\n\tPlay another game? Enter y|n ==> ");
  179.                 anotherGame = Console.ReadLine();
  180.                 if (anotherGame.Equals("y"))
  181.                 {
  182.                     ClearScreen();
  183.  
  184.                     Play();
  185.                 }
  186.                 else if (anotherGame.Equals("n"))
  187.                 {
  188.                     Exit();
  189.                 }
  190.                 else
  191.                 {
  192.                     Console.WriteLine("ERROR: You must enter one of these characters: y, n");
  193.                 }
  194.             }
  195.         }
  196.  
  197.         static void Exit()
  198.         {
  199.             //Exits the game.
  200.             Console.WriteLine();
  201.             Console.WriteLine("\tThank you for playing!");
  202.             Console.WriteLine();
  203.             PressAnyKey("\tPress any key to exit ...");
  204.             Environment.Exit(0);
  205.         }
  206.  
  207.         static void ClearScreen()
  208.         {
  209.             //Clears the screen.
  210.             Console.Clear();
  211.         }
  212.  
  213.         static void PressAnyKey(string prompt)
  214.         {
  215.             //Reads the input when the player is asked to "press any key".
  216.             Console.Write(prompt);
  217.             Console.ReadKey();
  218.         }
  219.     }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement