Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace Challenge {
- /// <summary> The game class. </summary>
- public class Game {
- /// <summary> The variables. </summary>
- private static Dictionary <string, string> Variables;
- /// <summary> Whether the game is running. </summary>
- private static bool Running;
- /// <summary> The grid. </summary>
- private static bool[][] Grid;
- /// <summary> Waits for the given key to be pressed. </summary>
- /// <param name='key'> The key to wait for. </param>
- private static void WaitForKey(ConsoleKey key)
- {
- Console.WriteLine("Press the '{0}' key to continue.",
- key.ToString());
- while (Console.ReadKey().Key != key)
- ;
- }
- /// <summary> Waits for the Enter key to be pressed. </summary>
- private static void WaitForKey()
- {
- Game.WaitForKey(ConsoleKey.Enter);
- }
- /// <summary> Handles user input. </summary>
- private static void HandleInputs()
- {
- Console.Write("Command (\"help\" for help): ");
- string[] tokens = Console.ReadLine().Split();
- if (tokens.Length < 1)
- return;
- switch (tokens[0].ToLower()) {
- case "get":
- if (tokens.Length < 2) {
- Console.WriteLine("Usage: get <variable>");
- Game.WaitForKey();
- break;
- }
- if (!(Game.Variables.ContainsKey(tokens[1]))) {
- Console.WriteLine("Invalid variable name. Valid names are: ");
- foreach (string key in Variables.Keys)
- Console.WriteLine("\t{0}", key);
- Game.WaitForKey();
- } else {
- Console.WriteLine("'{0}'='{1}'",
- tokens[1], Game.Variables[tokens[1]]);
- Game.WaitForKey();
- }
- break;
- case "set":
- if (tokens.Length < 3) {
- Console.WriteLine("Usage: set <variable> <value>");
- Game.WaitForKey();
- break;
- }
- if (!(Game.Variables.ContainsKey(tokens[1]))) {
- Console.WriteLine("Invalid variable name. Valid names are: ");
- foreach (string key in Variables.Keys)
- Console.WriteLine("\t{0}", key);
- Game.WaitForKey();
- } else {
- Game.Variables[tokens[1]] = tokens[2];
- }
- break;
- case "h":
- case "help":
- case "?":
- Console.WriteLine("Commands:");
- Console.WriteLine("h/help/?\tShow this help.");
- Console.WriteLine("get\t\tGet a variable");
- Console.WriteLine("set\t\tSet a variable");
- Console.WriteLine("quit\t\tQuit game.");
- Game.WaitForKey();
- break;
- case "quit":
- Game.Running = false;
- break;
- }
- }
- /// <summary> Creates the grid. </summary>
- private static void CreateGrid()
- {
- int width, height;
- if (!(int.TryParse(Game.Variables["width"], out width)) || width < 0) {
- Console.WriteLine("Invalid value 'width'='{0}'",
- Game.Variables["width"]);
- Game.WaitForKey();
- return;
- }
- if (!(int.TryParse(Game.Variables["height"], out height)) || height < 0) {
- Console.WriteLine("Invalid value 'height'='{0}'",
- Game.Variables["height"]);
- return;
- }
- if (width == 0 || height == 0)
- return;
- Game.Grid = new bool[height][];
- for (int j = 0; j < height; ++j) {
- Game.Grid[j] = new bool[width];
- for (int i = 0; i < width; ++i) {
- Game.Grid[j][i] = false;
- int index = j * width + i;
- string sw = String.Format("sw{0}", index);
- if (!(Game.Variables.ContainsKey(sw))) {
- Game.Variables.Add(sw, "false");
- } else {
- bool val;
- if (!(bool.TryParse(Game.Variables[sw], out val))) {
- Console.WriteLine("Invalid value '{0}'='{1}'",
- sw, Game.Variables[sw]);
- Game.WaitForKey();
- return;
- }
- Game.Grid[j][i] = val;
- }
- }
- }
- }
- /// <summary> Updates the screen. </summary>
- private static void UpdateScreen()
- {
- Console.Clear();
- if (Game.Grid == null)
- return;
- for (int j = 0; j < Game.Grid.Length; ++j) {
- if (Game.Grid[j] == null)
- return;
- for (int i = 0; i < Game.Grid[j].Length; ++i)
- Console.Write((Game.Grid[j][i]) ? '1' : '0');
- Console.WriteLine();
- }
- }
- /// <summary> The Main function. </summary>
- public static void Main()
- {
- Game.Variables = new Dictionary<string, string>();
- Game.Variables.Add("width", "0");
- Game.Variables.Add("height", "0");
- Game.Running = true;
- while (Game.Running) {
- Game.HandleInputs();
- Game.CreateGrid();
- Game.UpdateScreen();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment