View difference between Paste ID: E7M5e0hN and 0UgfTpk7
SHOW: | | - or go back to the newest paste.
1
using System;
2
using System.Collections.Generic;
3
4
namespace Challenge {
5
	/// <summary> The game class. </summary>
6
	public class Game {
7
		/// <summary> The variables. </summary>
8
		private static Dictionary <string, string> Variables;
9
		/// <summary> Whether the game is running. </summary>
10
		private static bool Running;
11
		/// <summary> The grid. </summary>
12
		private static bool[][] Grid;
13
14
		/// <summary> Waits for the given key to be pressed. </summary>
15
		/// <param name='key'> The key to wait for. </param>
16
		private static void WaitForKey(ConsoleKey key)
17
		{
18
			Console.WriteLine("Press the '{0}' key to continue.",
19
							key.ToString());
20
			while (Console.ReadKey().Key != key)
21
				;
22
		}
23
24
		/// <summary> Waits for the Enter key to be pressed. </summary>
25
		private static void WaitForKey()
26
		{
27
			Game.WaitForKey(ConsoleKey.Enter);
28
		}
29
30
		/// <summary> Handles user input. </summary>
31
		private static void HandleInputs()
32
		{
33-
			Console.Write("> ");
33+
			Console.Write("Command (\"help\" for help): ");
34
			string[] tokens = Console.ReadLine().Split();
35
			if (tokens.Length < 1)
36
				return;
37
			switch (tokens[0].ToLower()) {
38
			case "get":
39
				if (tokens.Length < 2) {
40
					Console.WriteLine("Usage: get <variable>");
41
					Game.WaitForKey();
42
					break;
43
				}
44
				if (!(Game.Variables.ContainsKey(tokens[1]))) {
45
					Console.WriteLine("Invalid variable name. Valid names are: ");
46
					foreach (string key in Variables.Keys)
47
						Console.WriteLine("\t{0}", key);
48
					Game.WaitForKey();
49
				} else {
50
					Console.WriteLine("'{0}'='{1}'",
51
					tokens[1], Game.Variables[tokens[1]]);
52
					Game.WaitForKey();
53
				}
54
				break;
55
			case "set":
56
				if (tokens.Length < 3) {
57
					Console.WriteLine("Usage: set <variable> <value>");
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
					Game.Variables[tokens[1]] = tokens[2];
68
				}
69
				break;
70
			case "h":
71
			case "help":
72
			case "?":
73
				Console.WriteLine("Commands:");
74
				Console.WriteLine("h/help/?\tShow this help.");
75
				Console.WriteLine("get\t\tGet a variable");
76
				Console.WriteLine("set\t\tSet a variable");
77
				Console.WriteLine("quit\t\tQuit game.");
78
				Game.WaitForKey();
79
				break;
80
			case "quit":
81
				Game.Running = false;
82
				break;
83
			}
84
		}
85
86
		/// <summary> Creates the grid. </summary>
87
		private static void CreateGrid()
88
		{
89
			int width, height;
90
			if (!(int.TryParse(Game.Variables["width"], out width)) || width < 0) {
91
				Console.WriteLine("Invalid value 'width'='{0}'",
92
							Game.Variables["width"]);
93
				Game.WaitForKey();
94
				return;
95
			}
96
			if (!(int.TryParse(Game.Variables["height"], out height)) || height < 0) {
97
				Console.WriteLine("Invalid value 'height'='{0}'",
98
							Game.Variables["height"]);
99
				return;
100
			}
101
			if (width == 0 || height == 0)
102
				return;
103
			Game.Grid = new bool[height][];
104
			for (int j = 0; j < height; ++j) {
105
				Game.Grid[j] = new bool[width];
106
				for (int i = 0; i < width; ++i) {
107
					Game.Grid[j][i] = false;
108
					int index = j * width + i;
109
					string sw = String.Format("sw{0}", index);
110
					if (!(Game.Variables.ContainsKey(sw))) {
111
						Game.Variables.Add(sw, "false");
112
					} else {
113
						bool val;
114
						if (!(bool.TryParse(Game.Variables[sw], out val))) {
115
							Console.WriteLine("Invalid value '{0}'='{1}'",
116
							sw, Game.Variables[sw]);
117
							Game.WaitForKey();
118
							return;
119
						}
120
						Game.Grid[j][i] = val;
121
					}
122
				}
123
			}
124
		}
125
126
		/// <summary> Updates the screen. </summary>
127
		private static void UpdateScreen()
128
		{
129
			Console.Clear();
130
			if (Game.Grid == null)
131
				return;
132
			for (int j = 0; j < Game.Grid.Length; ++j) {
133
				if (Game.Grid[j] == null)
134
					return;
135
				for (int i = 0; i < Game.Grid[j].Length; ++i)
136
					Console.Write((Game.Grid[j][i]) ? '1' : '0');
137
				Console.WriteLine();
138
			}
139
		}
140
141
		/// <summary> The Main function. </summary>
142
		public static void Main()
143
		{
144
			Game.Variables = new Dictionary<string, string>();
145
			Game.Variables.Add("width", "0");
146
			Game.Variables.Add("height", "0");
147
			Game.Running = true;
148
			while (Game.Running) {
149
				Game.HandleInputs();
150
				Game.CreateGrid();
151
				Game.UpdateScreen();
152
			}
153
154
		}
155
	}
156
}