Guest User

Untitled

a guest
Apr 19th, 2012
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Challenge {
  5.     /// <summary> A pair of objects. </summary>
  6.     class Pair <T, U> {
  7.         /// <summary> The first object. </summary>
  8.         public T First;
  9.         /// <summary> The second object. </summary>
  10.         public U Second;
  11.  
  12.         /// <summary> Constructor. </summary>
  13.         /// <param name='first'> The first object. </param>
  14.         /// <param name='second'> The second object. </param>
  15.         public Pair(T first, U second)
  16.         {
  17.             this.First = first;
  18.             this.Second = second;
  19.         }
  20.     }
  21.  
  22.     /// <summary> The game class. </summary>
  23.     public class Game {
  24.         /// <summary> The variables. </summary>
  25.         private static Dictionary <string, string> Variables;
  26.         /// <summary> Whether the game is running. </summary>
  27.         private static bool Running;
  28.         /// <summary> The grid. </summary>
  29.         private static bool[][] Grid;
  30.  
  31.         /// <summary> Waits for the given key to be pressed. </summary>
  32.         /// <param name='key'> The key to wait for. </param>
  33.         private static void WaitForKey(ConsoleKey key)
  34.         {
  35.             Console.WriteLine("Press the '{0}' key to continue.",
  36.                             key.ToString());
  37.             while (Console.ReadKey().Key != key)
  38.                 ;
  39.         }
  40.  
  41.         /// <summary> Waits for the Enter key to be pressed. </summary>
  42.         private static void WaitForKey()
  43.         {
  44.             Game.WaitForKey(ConsoleKey.Enter);
  45.         }
  46.  
  47.         /// <summary> Handles user input. </summary>
  48.         private static void HandleInputs()
  49.         {
  50.             Console.Write("> ");
  51.             string[] tokens = Console.ReadLine().Split();
  52.             if (tokens.Length < 1)
  53.                 return;
  54.             switch (tokens[0].ToLower()) {
  55.             case "get":
  56.                 if (tokens.Length < 2) {
  57.                     Console.WriteLine("Usage: get <variable>");
  58.                     Game.WaitForKey();
  59.                     break;
  60.                 }
  61.                 if (!(Game.Variables.ContainsKey(tokens[1]))) {
  62.                     Console.WriteLine("Invalid variable name. Valid names are: ");
  63.                     foreach (string key in Variables.Keys)
  64.                         Console.WriteLine("\t{0}", key);
  65.                     Game.WaitForKey();
  66.                 } else {
  67.                     Console.WriteLine("'{0}'='{1}'",
  68.                     tokens[1], Game.Variables[tokens[1]]);
  69.                     Game.WaitForKey();
  70.                 }
  71.                 break;
  72.             case "set":
  73.                 if (tokens.Length < 3) {
  74.                     Console.WriteLine("Usage: set <variable> <value>");
  75.                     Game.WaitForKey();
  76.                     break;
  77.                 }
  78.                 if (!(Game.Variables.ContainsKey(tokens[1]))) {
  79.                     Console.WriteLine("Invalid variable name. Valid names are: ");
  80.                     foreach (string key in Variables.Keys)
  81.                         Console.WriteLine("\t{0}", key);
  82.                     Game.WaitForKey();
  83.                 } else {
  84.                     Game.Variables[tokens[1]] = tokens[2];
  85.                 }
  86.                 break;
  87.             case "h":
  88.             case "help":
  89.             case "?":
  90.                 Console.WriteLine("Commands:");
  91.                 Console.WriteLine("h/help/?\tShow this help.");
  92.                 Console.WriteLine("get\t\tGet a variable");
  93.                 Console.WriteLine("set\t\tSet a variable");
  94.                 Console.WriteLine("quit\t\tQuit game.");
  95.                 Game.WaitForKey();
  96.                 break;
  97.             case "quit":
  98.                 Game.Running = false;
  99.                 break;
  100.             }
  101.         }
  102.  
  103.         /// <summary> Creates the grid. </summary>
  104.         private static void CreateGrid()
  105.         {
  106.             int width, height;
  107.             if (!(int.TryParse(Game.Variables["width"], out width)) || width < 0) {
  108.                 Console.WriteLine("Invalid value 'width'='{0}'",
  109.                             Game.Variables["width"]);
  110.                 Game.WaitForKey();
  111.                 return;
  112.             }
  113.             if (!(int.TryParse(Game.Variables["height"], out height)) || height < 0) {
  114.                 Console.WriteLine("Invalid value 'height'='{0}'",
  115.                             Game.Variables["height"]);
  116.                 return;
  117.             }
  118.             if (width == 0 || height == 0)
  119.                 return;
  120.             Game.Grid = new bool[height][];
  121.             for (int j = 0; j < height; ++j) {
  122.                 Game.Grid[j] = new bool[width];
  123.                 for (int i = 0; i < width; ++i) {
  124.                     Game.Grid[j][i] = false;
  125.                     int index = j * width + i;
  126.                     string sw = String.Format("sw{0}", index);
  127.                     if (!(Game.Variables.ContainsKey(sw))) {
  128.                         Game.Variables.Add(sw, "false");
  129.                     } else {
  130.                         bool val;
  131.                         if (!(bool.TryParse(Game.Variables[sw], out val))) {
  132.                             Console.WriteLine("Invalid value '{0}'='{1}'",
  133.                             sw, Game.Variables[sw]);
  134.                             Game.WaitForKey();
  135.                             return;
  136.                         }
  137.                         Game.Grid[j][i] = val;
  138.                     }
  139.                 }
  140.             }
  141.         }
  142.  
  143.         /// <summary> Updates the screen. </summary>
  144.         private static void UpdateScreen()
  145.         {
  146.             Console.Clear();
  147.             if (Game.Grid == null)
  148.                 return;
  149.             for (int j = 0; j < Game.Grid.Length; ++j) {
  150.                 if (Game.Grid[j] == null)
  151.                     return;
  152.                 for (int i = 0; i < Game.Grid[j].Length; ++i)
  153.                     Console.Write((Game.Grid[j][i]) ? '1' : '0');
  154.                 Console.WriteLine();
  155.             }
  156.         }
  157.  
  158.         /// <summary> The Main function. </summary>
  159.         public static void Main()
  160.         {
  161.             Game.Variables = new Dictionary<string, string>();
  162.             Game.Variables.Add("width", "0");
  163.             Game.Variables.Add("height", "0");
  164.             Game.Running = true;
  165.             while (Game.Running) {
  166.                 Game.HandleInputs();
  167.                 Game.CreateGrid();
  168.                 Game.UpdateScreen();
  169.             }
  170.  
  171.         }
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment