fork download
  1. using System;
  2. using System.Text;
  3.  
  4.  
  5. namespace Memory_Maze
  6. {
  7. class Program
  8. {
  9. static char[,] map = new char[8, 8];
  10.  
  11. static Random random = new Random();
  12.  
  13. struct Point
  14. {
  15. public int x;
  16. public int y;
  17. }
  18.  
  19.  
  20. static Point player;
  21.  
  22.  
  23.  
  24.  
  25.  
  26. static void showStartText()
  27. {
  28. Console.Write("~~~Memory Maze~~~\n\n");
  29.  
  30. Console.Write("How good is your short term memory? Time to put it to the test!\n\n");
  31.  
  32. Console.Write("Instructions: You will be shown a map. The 'S' is the start, and the 'F' is the end.\n\n");
  33.  
  34. Console.Write("You will be shown the maze for a few seconds, and then you must try to navigate from start to finish.\n\n");
  35.  
  36. Console.Write("To move in a direction, hit that arrow key. If you run into a wall, you lose.\n\n");
  37. }
  38.  
  39. static void generateMap()
  40. {
  41. for (int x = 0; x < map.GetLength(0); ++x)
  42. for (int y = 0; y < map.GetLength(1); ++y)
  43. {
  44. map[x, y] = 'W';
  45. }
  46.  
  47. int tunnelX = 1;
  48. int tunnelY = 1;
  49.  
  50. map[tunnelX, tunnelY] = 'S';
  51.  
  52. while (true)
  53. {
  54. if (random.Next(0, 2) == 1)
  55. {
  56. if (tunnelX + 1 < map.GetLength(0) - 1)
  57. {
  58. ++tunnelX;
  59.  
  60. map[tunnelX, tunnelY] = ' ';
  61.  
  62. continue;
  63. }
  64. else
  65. {
  66. if (tunnelY + 1 < map.GetLength(1) - 1)
  67. {
  68. ++tunnelY;
  69.  
  70. map[tunnelX, tunnelY] = ' ';
  71. }
  72. else
  73. {
  74. map[tunnelX, tunnelY] = 'F';
  75.  
  76. break;
  77. }
  78. }
  79. }
  80. else
  81. {
  82. if (tunnelY + 1 < map.GetLength(1) - 1)
  83. {
  84. ++tunnelY;
  85.  
  86. map[tunnelX, tunnelY] = ' ';
  87.  
  88. continue;
  89. }
  90. else
  91. {
  92. if (tunnelX + 1 < map.GetLength(0) - 1)
  93. {
  94. ++tunnelX;
  95.  
  96. map[tunnelX, tunnelY] = ' ';
  97. }
  98. else
  99. {
  100. map[tunnelX, tunnelY] = 'F';
  101.  
  102. break;
  103. }
  104. }
  105. }
  106. }
  107. }
  108.  
  109. static void drawMap()
  110. {
  111. for (int x = map.GetLength(0) - 1; x >= 0; --x)
  112. {
  113. for (int y = 0; y < map.GetLength(1); ++y)
  114. {
  115. switch (map[x, y])
  116. {
  117. case 'W':
  118. Console.ForegroundColor = ConsoleColor.Red;
  119. break;
  120.  
  121. case 'P':
  122. Console.ForegroundColor = ConsoleColor.Green;
  123. break;
  124.  
  125. case 'S':
  126. Console.ForegroundColor = ConsoleColor.Cyan;
  127. break;
  128.  
  129. case 'F':
  130. Console.ForegroundColor = ConsoleColor.Magenta;
  131. break;
  132.  
  133. }
  134.  
  135. Console.Write(map[x, y]);
  136. }
  137.  
  138. Console.Write("\n");
  139. }
  140. }
  141.  
  142. static bool quit()
  143. {
  144. while (true)
  145. {
  146. Console.ForegroundColor = ConsoleColor.Red;
  147.  
  148. Console.Write("Would you like to play again? y/n\n");
  149.  
  150. Console.ForegroundColor = ConsoleColor.Green;
  151.  
  152. string input = Console.ReadLine();
  153.  
  154. Console.ForegroundColor = ConsoleColor.Gray;
  155.  
  156. if (input.ToLowerInvariant() == "y")
  157. return false;
  158. else if (input.ToLowerInvariant() == "n")
  159. return true;
  160. else
  161. {
  162. Console.Write("\n\nInvalid input\n\n");
  163.  
  164. continue;
  165. }
  166. }
  167. }
  168.  
  169. static bool movePlayer()
  170. {
  171. var key = Console.ReadKey();
  172.  
  173. if (key.Key == ConsoleKey.RightArrow)
  174. {
  175. ++player.y;
  176.  
  177. if (map[player.x, player.y] == 'W')
  178. return false;
  179. else
  180. return true;
  181. }
  182. else if (key.Key == ConsoleKey.UpArrow)
  183. {
  184. ++player.x;
  185.  
  186. if (map[player.x, player.y] == 'W')
  187. return false;
  188. else
  189. return true;
  190. }
  191.  
  192. return true;
  193. }
  194.  
  195. static bool won()
  196. {
  197. return map[player.x, player.y] == 'F';
  198. }
  199.  
  200.  
  201.  
  202.  
  203.  
  204. static void Main(string[] args)
  205. {
  206. Program program = new Program();
  207.  
  208. showStartText();
  209.  
  210. Console.Write("Press enter to continue.");
  211.  
  212. Console.ReadLine();
  213.  
  214. Console.Clear();
  215.  
  216.  
  217. while (true)
  218. {
  219. Console.Clear();
  220.  
  221. generateMap();
  222.  
  223. player.x = 1;
  224. player.y = 1;
  225.  
  226. drawMap();
  227.  
  228. Console.ForegroundColor = ConsoleColor.Gray;
  229.  
  230. Console.Write("\n\nSeconds left: 6");
  231.  
  232. Console.ForegroundColor = ConsoleColor.Green;
  233.  
  234. for (int i = 6; i >= 0; --i)
  235. {
  236. Console.Write("\b");
  237.  
  238. Console.Write(i);
  239.  
  240. System.Threading.Thread.Sleep(1000);
  241. }
  242.  
  243. Console.ForegroundColor = ConsoleColor.Gray;
  244.  
  245. Console.Clear();
  246.  
  247. while (true)
  248. {
  249. Console.Clear();
  250.  
  251. Console.Write("Press an arrow key to go that direction\n\n");
  252.  
  253. if (movePlayer())
  254. {
  255. if (won())
  256. {
  257. Console.Write("\bCongratulations, you won!\n\n");
  258.  
  259. if (quit())
  260. return;
  261. else
  262. break;
  263. }
  264.  
  265. continue;
  266. }
  267. else
  268. {
  269. Console.Write("\bYou ran into a wall.\n\n");
  270.  
  271. if (quit())
  272. return;
  273. else
  274. break;
  275. }
  276. }
  277.  
  278.  
  279. }
  280.  
  281. }
  282. }
  283. }
  284.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty