Advertisement
Guest User

Untitled

a guest
Jul 26th, 2014
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.81 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Level3
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             string login = IntroScreen();
  13.             bool exitProgram = false;
  14.  
  15.             // Create menu items used by sub menus.
  16.             StatisticsItem stats = new StatisticsItem();
  17.             MenuItem mainMenu = new MenuItem("Main Menu");
  18.             MenuItem restart = new MenuItem("New Simulation");
  19.             do
  20.             {
  21.                 Console.CursorVisible = false;
  22.                 Console.Clear();
  23.                 int selection = MainMenu(login, stats);
  24.                 if (selection == 0) // Start game.
  25.                 {
  26.                     int montyHallSelection = 0;
  27.                     MenuItem[] montyHallMenu = { mainMenu, restart };
  28.                     do
  29.                     {
  30.                         MontyHall(stats);
  31.                         montyHallSelection = Menu(montyHallMenu, montyHallSelection, 1, 14, " >");
  32.                     } while (montyHallSelection == 1);                    
  33.                 }
  34.                 else if (selection == 1) // View stats.
  35.                 {
  36.                     stats.PrintStats();
  37.                     MenuItem[] statsMenu = { mainMenu };
  38.                     Menu(statsMenu, 0, 1, 14, " >");
  39.                 }
  40.                 else // Exit program.
  41.                 {
  42.                     exitProgram = true;
  43.                 }
  44.             } while (exitProgram == false);
  45.         }
  46.        
  47.         static string IntroScreen()
  48.         {
  49.             string introMessage = "MH3v4 simulator activated.  Security clearance required for further access.";
  50.             string loginMessage = "\nLogin:";
  51.             string error1 = "Error: login must be between one and twelve characters.";            
  52.             Console.WriteLine(introMessage);
  53.             Console.WriteLine(loginMessage);
  54.             Console.SetCursorPosition(loginMessage.Length, 2);
  55.             string login = Console.ReadLine();
  56.  
  57.             if (login == null)
  58.             {
  59.                 login = "Azrael";
  60.             }
  61.             else
  62.             {
  63.                 // Accept only valid input.
  64.                 while (login.Length > 12 || login.Length < 1)
  65.                 {
  66.                     ClearLine(loginMessage.Length, 2);
  67.                     ClearLine(0, 4);
  68.                     Console.WriteLine(error1);
  69.                     Console.SetCursorPosition(loginMessage.Length, 2);
  70.                     login = Console.ReadLine();
  71.                 }
  72.             }
  73.  
  74.             return login;
  75.         } // This screen only runs once.
  76.  
  77.         static int MainMenu(string login, StatisticsItem stats)
  78.         {
  79.             if (login == "Roy") // Easter egg.
  80.             {
  81.                 Console.WriteLine("Welcome, Captain Roy. Please select an option.");
  82.             }
  83.             else
  84.             {
  85.                 Console.WriteLine("Welcome, Ensign " + login + ". Please select an option.");
  86.             }
  87.  
  88.             // Create the main menu options.
  89.             MenuItem startOption = new MenuItem("Start Simulation");
  90.             MenuItem statsOption = new MenuItem("View Statistics");
  91.             MenuItem exitOption = new MenuItem("Exit Program");
  92.             MenuItem[] mainMenu = { startOption, statsOption, exitOption };
  93.            
  94.             // Stats do not exist if the user has not played the game.
  95.             if (stats.TotalCount < 1)
  96.             {
  97.                 statsOption.GreyStatus = true;
  98.             }
  99.                        
  100.             return Menu(mainMenu, 0, 1, 12, " >");
  101.         }
  102.  
  103.         static void MontyHall(StatisticsItem stats)
  104.         {
  105.             // Creating various menu items.
  106.             MenuItem doorMenu1 = new MenuItem("Hatch 1");
  107.             MenuItem doorMenu2 = new MenuItem("Hatch 2");
  108.             MenuItem doorMenu3 = new MenuItem("Hatch 3");
  109.  
  110.             MenuItem[] doorMenu = { doorMenu1, doorMenu2, doorMenu3 };
  111.  
  112.             // Winning door is created.
  113.             Console.Clear();
  114.             Random rnd = new Random();
  115.             int winningDoor = rnd.Next(0, 3);
  116.  
  117.             // Strings found here!  They say stuff, like how the user is going to die.
  118.             string introMessage = "You are the only survivor aboard a ship adrift in interstellar space.  Life support systems have failed.  A nearby computer console estimates you will run out of oxygen in fifteen minutes.  Fortunately, your ship is equipped with three warp capable escape pods.";
  119.             string introMessage2 = "When you reach the escape pods, you find two of the three pods have already been launched.  However, the sensors are damaged: the indicator lights over each of the hatches are all dark, which would normally indicate all three hatches have escape pods docked.  Engaging the manual override will disconnect the hatch from the sensor grid, forcing the sensor system to correctly turn on one, and only one, indicator light above the other two hatches.";
  120.             string introPrompt = "Which hatch do you choose?";
  121.            
  122.             string switchPrompt = "Will you open the hatch you've previously chosen?";
  123.  
  124.             string winMessage = "You open the hatch to reveal a cockpit on the other side.  Quickly you jump in and start the ignition sequence, plotting a course for the nearest starbase.";
  125.             string loseMessage = "You open the hatch only to be sucked out into space.  In your last moments you can see that Hatch " + (winningDoor + 1) + " has an escape pod docked.  You are dead!";
  126.            
  127.  
  128.             // User is asked to select a door.  It doesn't really matter which one they pick, though.    
  129.             WriteWrappedString(introMessage, Console.WindowWidth - 1);
  130.             WriteWrappedString("\n\n" + introMessage2, Console.WindowWidth - 1);
  131.             WriteWrappedString("\n\n" + introPrompt, Console.WindowWidth - 1);
  132.             int chosenDoor = Menu(doorMenu, 0, 1, 14, " >");
  133.  
  134.  
  135.             // Generate the revealed door.            
  136.             int revealedDoor = chosenDoor;
  137.             int switchDoor = revealedDoor;
  138.             for (int i = 0; revealedDoor == chosenDoor || revealedDoor == winningDoor; i++)
  139.             {
  140.                 revealedDoor = i;
  141.             }
  142.             for (int i = 0; switchDoor == revealedDoor || switchDoor == chosenDoor; i++)
  143.             {
  144.                 switchDoor = i;
  145.             }
  146.  
  147.             // Second block of string stuff and menu items.  Uses variables that had to be declared first.
  148.             string switchMessage = "You engage the manual override on Hatch " + (chosenDoor + 1) + ", and a sensor suddenly flickers back on, revealing that Escape Pod " + (revealedDoor + 1) + " has been launched.  However, the other sensor is still malfunctioning.";
  149.             MenuItem switchMenu1 = new MenuItem("Yes, keep Hatch " + (chosenDoor + 1) + ".");
  150.             MenuItem switchMenu2 = new MenuItem("No, use Hatch " + (switchDoor + 1) + ".");
  151.             MenuItem[] switchMenu = { switchMenu1, switchMenu2 };
  152.  
  153.             // User is asked if they're sure they want to keep their door (if they're dummies).
  154.             Console.Clear();
  155.             WriteWrappedString(switchMessage, Console.WindowWidth - 1);
  156.             WriteWrappedString("\n\n" + switchPrompt, Console.WindowWidth - 1);
  157.             int changeDoor = Menu(switchMenu, 0, 1, 14, " >");
  158.  
  159.             // Switch the doors.
  160.             if (changeDoor == 1)
  161.             {
  162.                     chosenDoor = switchDoor;
  163.             }
  164.  
  165.             // Win/Lose messages.  Stats are created here.
  166.             Console.Clear();
  167.             if (chosenDoor == winningDoor)
  168.             {
  169.                 WriteWrappedString(winMessage, Console.WindowWidth - 1);
  170.             }
  171.             else
  172.             {
  173.                 WriteWrappedString(loseMessage, Console.WindowWidth - 1);
  174.             }
  175.             stats.UpdateStats(winningDoor, chosenDoor, changeDoor);          
  176.         }
  177.  
  178.         static int Menu(MenuItem[] menuItems, int selection, int lcol, int urow, string cursor)
  179.             {
  180.                 int menuLength = menuItems.Length;
  181.  
  182.                 // Draw menu.
  183.                 // If selection is set to a greyed item, cursor is not drawn.
  184.                 for (int i = 0; i != menuLength; i++)
  185.                 {
  186.                     Console.SetCursorPosition(lcol, i + urow);
  187.                     if (i == selection && menuItems[selection].GreyStatus == false)
  188.                     {
  189.                         Console.WriteLine(cursor + " " + menuItems[i].Title);
  190.                     }
  191.                     else
  192.                     {
  193.                         Console.WriteLine("  " + menuItems[i].Title);
  194.                     }
  195.                 }
  196.  
  197.                 // Acccept keyboard input and update menu.
  198.                 while (true)
  199.                 {
  200.                     ConsoleKeyInfo cki;
  201.                     cki = Console.ReadKey();
  202.                     int greyOffset = 0;
  203.  
  204.                     ClearLine(lcol, selection + urow);
  205.                     if (cki.Key == ConsoleKey.Enter)
  206.                     {
  207.                         return selection;
  208.                     }
  209.                     else if (cki.Key == ConsoleKey.DownArrow)
  210.                     {
  211.                         Console.WriteLine("  " + menuItems[selection].Title);
  212.                         selection++;
  213.                         greyOffset = 1;
  214.                     }
  215.                     else if (cki.Key == ConsoleKey.UpArrow)
  216.                     {
  217.                         Console.WriteLine("  " + menuItems[selection].Title);
  218.                         selection--;
  219.                         greyOffset = -1;
  220.                     }
  221.  
  222.                     if (selection < 0)
  223.                     {
  224.                         selection = menuLength - 1;
  225.                     }
  226.                     else if (selection > menuLength - 1)
  227.                     {
  228.                         selection = 0;
  229.                     }
  230.  
  231.                     // Skips over greyed input.
  232.                     // If all options are greyed, cursor will not be drawn.
  233.                     // But it will be stuck in an infinite loop!
  234.                     while (menuItems[selection].GreyStatus == true)
  235.                     {
  236.                         selection = selection + greyOffset;
  237.                         if (selection < 0)
  238.                         {
  239.                             selection = menuLength - 1;
  240.                         }
  241.                         else if (selection > menuLength - 1)
  242.                         {
  243.                             selection = 0;
  244.                         }
  245.                     }
  246.  
  247.                     // Update menu.
  248.                     ClearLine(0, urow + menuItems.Length); // Removes character printed by silly users.
  249.                     ClearLine(lcol, selection + urow);
  250.                     Console.WriteLine(cursor + " " + menuItems[selection].Title);
  251.                 }
  252.             }
  253.                
  254.         static void ClearLine(int lcol, int urow)
  255.         {
  256.             Console.SetCursorPosition(lcol, urow);
  257.             Console.Write(new string(' ', Console.WindowWidth));
  258.             Console.SetCursorPosition(lcol, urow);
  259.         } // Necessary for Menu to function.
  260.  
  261.         static void WriteWrappedString(string text, int lineWidth)
  262.         {
  263.             string[] words = text.Split(' ');
  264.             string wrappedText = words[0];
  265.             bool lineChange = false;
  266.             for (int i = 1; i < words.Length; i++)
  267.             {
  268.                 if (wrappedText.Length + words[i].Length < lineWidth)
  269.                 {
  270.                     wrappedText = wrappedText + ' ' + words[i];
  271.                 }
  272.                 else
  273.                 {
  274.                     Console.WriteLine(wrappedText);
  275.                     if (wrappedText.Length == lineWidth)
  276.                     {
  277.                         Console.CursorTop--;
  278.                         lineChange = true;
  279.                     }
  280.                     else
  281.                     {
  282.                         lineChange = false;
  283.                     }
  284.                     wrappedText = words[i];
  285.                 }
  286.             }
  287.             if (lineChange == true)
  288.             {
  289.                 Console.CursorTop++;
  290.             }
  291.             Console.Write(wrappedText);
  292.         } // Does not word wrap a string!  Only prints a string in a word wrapped fashion.
  293.     }
  294.  
  295.     public class MenuItem
  296.         {
  297.            
  298.             public bool GreyStatus { get; set; } // If GreyStatus is true, then the menu item won't be selectable.
  299.             public string Title { get; set; } // The Title is the name the user sees on the menu.
  300.  
  301.             public MenuItem(string title)
  302.             {
  303.                 this.Title = title;
  304.                 GreyStatus = false;
  305.             }
  306.  
  307.             public void SetTitle(string newTitle)
  308.             {
  309.                 Title = newTitle;
  310.             }
  311.  
  312.             public void SetGreyed(bool setGrey)
  313.             {
  314.                 GreyStatus = setGrey;
  315.             }
  316.         }
  317.  
  318.     public class StatisticsItem
  319.     {
  320.         public int WinCount { get; set; }
  321.         public int LoseCount { get; private set; }
  322.         public float TotalCount { get; set; }
  323.  
  324.         public int ChangeDoorWinCount { get; private set; }
  325.         public int ChangeDoorLoseCount { get; private set; }
  326.         public float ChangeDoorTotalCount { get; private set; }
  327.  
  328.         public int KeepDoorWinCount { get; private set; }
  329.         public int KeepDoorLoseCount { get; private set; }
  330.         public float KeepDoorTotalCount { get; private set; }
  331.  
  332.        
  333.         public void UpdateStats(int winningDoor, int selection, int changeDoor)
  334.         {
  335.             TotalCount++;
  336.             if (changeDoor == 1 && winningDoor == selection)
  337.             {
  338.                 ChangeDoorWinCount++;
  339.             }
  340.             else if (changeDoor == 1 && winningDoor != selection)
  341.             {
  342.                 ChangeDoorLoseCount++;
  343.             }
  344.             if (selection == winningDoor)
  345.             {
  346.                 WinCount++;
  347.             }
  348.             LoseCount = (int) TotalCount - WinCount;
  349.  
  350.             ChangeDoorTotalCount = ChangeDoorLoseCount + ChangeDoorWinCount;
  351.             KeepDoorLoseCount = LoseCount - ChangeDoorLoseCount;
  352.             KeepDoorWinCount = WinCount - ChangeDoorWinCount;
  353.             KeepDoorTotalCount = (int) TotalCount - ChangeDoorTotalCount;
  354.  
  355.  
  356.         } // Heavy stat lifting done here, wear your hardhat!
  357.  
  358.         public void PrintStats()
  359.         {
  360.  
  361.             // Do the math only when needed.
  362.             float winPercent = (float)Math.Round(100*WinCount / TotalCount, 2);
  363.             float changeDoorWinPercent = (float)Math.Round(100*ChangeDoorWinCount / (ChangeDoorTotalCount), 2);
  364.             float keepDoorWinPercent = (float)Math.Round(100*KeepDoorWinCount / (KeepDoorTotalCount), 2);
  365.  
  366.             Console.Clear();
  367.             Console.WriteLine("Total simulations: " + TotalCount);
  368.             Console.WriteLine("Total successful: " + WinCount);
  369.             Console.WriteLine("Total failed: " + LoseCount);
  370.             Console.WriteLine("\nTotal successful switching: " + ChangeDoorWinCount);
  371.             Console.WriteLine("Total failed switching: " + ChangeDoorLoseCount);
  372.             Console.WriteLine("\nTotal successful keeping: " + KeepDoorWinCount);
  373.             Console.WriteLine("Total failed keeping: " + KeepDoorLoseCount);
  374.  
  375.             Console.WriteLine("\nSuccess rate: " + winPercent + "%");
  376.             if (ChangeDoorTotalCount != 0)
  377.             {
  378.                 Console.WriteLine("Success rate switching: " + changeDoorWinPercent + "%");
  379.             }
  380.             if (KeepDoorTotalCount != 0)
  381.             {
  382.                 Console.WriteLine("Success rate keeping: " + keepDoorWinPercent + "%");
  383.             }
  384.         }        
  385.     }
  386. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement