Mazing.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. #nullable enable
  2. using System.Text;
  3. namespace UICatalog.Scenarios;
  4. [ScenarioMetadata ("A Mazing", "Illustrates how to make a basic maze game.")]
  5. [ScenarioCategory ("Drawing")]
  6. [ScenarioCategory ("Mouse and Keyboard")]
  7. [ScenarioCategory ("Games")]
  8. public class Mazing : Scenario
  9. {
  10. private Window? _top;
  11. private MazeGenerator? _m;
  12. private List<Point>? _potions;
  13. private List<Point>? _goblins;
  14. private string? _message;
  15. private bool _dead;
  16. public override void Main ()
  17. {
  18. Application.Init ();
  19. _top = new ();
  20. _m = new ();
  21. GenerateNpcs ();
  22. // Define the keys for movement
  23. _top.KeyBindings.Add (Key.CursorLeft, Command.Left);
  24. _top.KeyBindings.Add (Key.CursorRight, Command.Right);
  25. _top.KeyBindings.Add (Key.CursorUp, Command.Up);
  26. _top.KeyBindings.Add (Key.CursorDown, Command.Down);
  27. // Changing the key-bindings of a View is not allowed, however,
  28. // by default, Runnable doesn't bind any of our movement keys, so
  29. // we can take advantage of the CommandNotBound event to handle them
  30. //
  31. // An alternative implementation would be to create a Runnable subclass that
  32. // calls AddCommand/KeyBindings.Add in the constructor. See the Snake game scenario
  33. // for an example.
  34. _top.CommandNotBound += TopCommandNotBound;
  35. _top.DrawingContent += (s, _) =>
  36. {
  37. if (s is not Runnable top)
  38. {
  39. return;
  40. }
  41. // Build maze
  42. var lc = new LineCanvas (_m.BuildWallLinesFromMaze ());
  43. // Print maze
  44. foreach (KeyValuePair<Point, Rune> p in lc.GetMap ())
  45. {
  46. top.Move (p.Key.X, p.Key.Y);
  47. top.AddRune (p.Value);
  48. }
  49. // Draw objects
  50. top.Move (_m.Start.X, _m.Start.Y);
  51. top.AddStr ("s");
  52. top.Move (_m.End.X, _m.End.Y);
  53. top.AddStr ("e");
  54. top.Move (_m.Player.X, _m.Player.Y);
  55. top.SetAttribute (new (Color.Cyan, top.GetAttributeForRole (VisualRole.Normal).Background));
  56. top.AddStr (_dead ? "x" : "@");
  57. // Draw goblins
  58. foreach (Point goblin in _goblins!)
  59. {
  60. top.Move (goblin.X, goblin.Y);
  61. top.SetAttribute (new (Color.Red, top.GetAttributeForRole (VisualRole.Normal).Background));
  62. top.AddStr ("G");
  63. }
  64. // Draw potions
  65. foreach (Point potion in _potions!)
  66. {
  67. top.Move (potion.X, potion.Y);
  68. top.SetAttribute (new (Color.Yellow, top.GetAttributeForRole (VisualRole.Normal).Background));
  69. top.AddStr ("p");
  70. }
  71. // Draw UI
  72. top.SetAttribute (top.GetAttributeForRole (VisualRole.Normal));
  73. var g = new Gradient ([new (Color.Red), new (Color.BrightGreen)], [10]);
  74. top.Move (_m.MazeWidth + 1, 0);
  75. top.AddStr ("Name: Sir Flibble");
  76. top.Move (_m.MazeWidth + 1, 1);
  77. top.AddStr ("HP:");
  78. for (var i = 0; i < _m.PlayerHp; i++)
  79. {
  80. top.Move (_m.MazeWidth + 1 + "HP:".Length + i, 1);
  81. top.SetAttribute (new (g.GetColorAtFraction (i / 20f)));
  82. top.AddRune ('█');
  83. }
  84. top.SetAttribute (top.GetAttributeForRole (VisualRole.Normal));
  85. if (!string.IsNullOrWhiteSpace (_message))
  86. {
  87. top.Move (_m.MazeWidth + 2, 2);
  88. top.AddStr (_message);
  89. }
  90. };
  91. Application.Run (_top);
  92. _top.Dispose ();
  93. Application.Shutdown ();
  94. }
  95. private void GenerateNpcs ()
  96. {
  97. _goblins = _m?.GenerateSpawnLocations (3, []); // Generate 3 goblins
  98. _potions = _m?.GenerateSpawnLocations (3, _goblins!); // Generate 3 potions
  99. }
  100. private void TopCommandNotBound (object? sender, CommandEventArgs e)
  101. {
  102. if (_dead)
  103. {
  104. return;
  105. }
  106. Point newPos = _m!.Player;
  107. Command? command = e.Context?.Command;
  108. if (command == Command.Left)
  109. {
  110. newPos = _m.Player with { X = _m.Player.X - 1 };
  111. }
  112. if (command == Command.Right)
  113. {
  114. newPos = _m.Player with { X = _m.Player.X + 1 };
  115. }
  116. if (command == Command.Up)
  117. {
  118. newPos = _m.Player with { Y = _m.Player.Y - 1 };
  119. }
  120. if (command == Command.Down)
  121. {
  122. newPos = _m.Player with { Y = _m.Player.Y + 1 };
  123. }
  124. // Only move if in bounds and it's a path
  125. if (newPos.X >= 0 && newPos.X < _m._maze.GetLength (1) && newPos.Y >= 0 && newPos.Y < _m._maze.GetLength (0) && _m._maze [newPos.Y, newPos.X] == 0)
  126. {
  127. _m.Player = newPos;
  128. // Check if player is on a goblin
  129. if (_goblins!.Contains (_m.Player))
  130. {
  131. _message = "You fight a goblin!";
  132. _m.PlayerHp -= 5; // Decrease player's HP when attacked
  133. // Remove the goblin
  134. _goblins.Remove (_m.Player);
  135. // Check if player is dead
  136. if (_m.PlayerHp <= 0)
  137. {
  138. _message = "You died!";
  139. Application.TopRunnableView!.SetNeedsDraw (); // trigger redraw
  140. _dead = true;
  141. return; // Stop further action if dead
  142. }
  143. }
  144. else if (_potions!.Contains (_m.Player))
  145. {
  146. _message = "You drink a health potion!";
  147. _m.PlayerHp = Math.Min (20, _m.PlayerHp + 5); // increase player's HP when drinking potion
  148. // Remove the potion
  149. _potions.Remove (_m.Player);
  150. }
  151. else
  152. {
  153. _message = string.Empty;
  154. }
  155. Application.TopRunnableView!.SetNeedsDraw (); // trigger redraw
  156. }
  157. // Optional win condition:
  158. if (_m.Player == _m.End)
  159. {
  160. var hp = _m.PlayerHp;
  161. _m = new (); // Generate a new maze
  162. _m.PlayerHp = hp;
  163. GenerateNpcs ();
  164. Application.TopRunnableView!.SetNeedsDraw (); // trigger redraw
  165. }
  166. }
  167. }
  168. internal class MazeGenerator
  169. {
  170. private const int WIDTH = 20;
  171. private const int HEIGHT = 10;
  172. public int [,] _maze;
  173. public Random Rand { get; } = new ();
  174. public Point Start { get; }
  175. public Point End { get; }
  176. public Point Player { get; set; }
  177. public int PlayerHp { get; set; } = 20;
  178. // Private accessors for width and height
  179. public int MazeWidth => WIDTH * 2 + 1;
  180. public int MazeHeight => HEIGHT * 2 + 1;
  181. public MazeGenerator ()
  182. {
  183. int w = WIDTH * 2 + 1;
  184. int h = HEIGHT * 2 + 1;
  185. _maze = new int [h, w];
  186. // Fill with walls
  187. for (var y = 0; y < h; y++)
  188. for (var x = 0; x < w; x++)
  189. {
  190. _maze [y, x] = 1;
  191. }
  192. // Start carving from a random odd cell
  193. int startX = Rand.Next (WIDTH) * 2 + 1;
  194. int startY = Rand.Next (HEIGHT) * 2 + 1;
  195. Carve (new (startX, startY));
  196. // Set random entrance
  197. Start = GetRandomEdgePoint (w, h, true);
  198. _maze [Start.Y, Start.X] = 0;
  199. Player = Start;
  200. // Set random exit (ensure it's not same as entrance)
  201. End = GetRandomEdgePoint (w, h, false, Start.X, Start.Y);
  202. _maze [End.Y, End.X] = 0;
  203. }
  204. public List<StraightLine> BuildWallLinesFromMaze ()
  205. {
  206. List<StraightLine> lines = new ();
  207. int h = _maze.GetLength (0);
  208. int w = _maze.GetLength (1);
  209. // Horizontal lines
  210. for (var y = 0; y < h; y++)
  211. {
  212. var x = 0;
  213. while (x < w)
  214. {
  215. if (_maze [y, x] == 1)
  216. {
  217. int startX = x;
  218. while (x < w && _maze [y, x] == 1)
  219. {
  220. x++;
  221. }
  222. int length = x - startX;
  223. if (length > 1)
  224. {
  225. lines.Add (new (new (startX, y), length, Orientation.Horizontal, LineStyle.Single));
  226. }
  227. }
  228. else
  229. {
  230. x++;
  231. }
  232. }
  233. }
  234. // Vertical lines
  235. for (var x = 0; x < w; x++)
  236. {
  237. var y = 0;
  238. while (y < h)
  239. {
  240. if (_maze [y, x] == 1)
  241. {
  242. int startY = y;
  243. while (y < h && _maze [y, x] == 1)
  244. {
  245. y++;
  246. }
  247. int length = y - startY;
  248. lines.Add (new (new (x, startY), length, Orientation.Vertical, LineStyle.Single));
  249. }
  250. else
  251. {
  252. y++;
  253. }
  254. }
  255. }
  256. return lines;
  257. }
  258. public List<Point> GenerateSpawnLocations (int count, List<Point> exclude)
  259. {
  260. // Create a new copy of the list so we can track exclusions
  261. exclude = exclude.ToList ();
  262. List<Point> locations = new ();
  263. for (var i = 0; i < count; i++)
  264. {
  265. Point point;
  266. do
  267. {
  268. point = new (Rand.Next (1, WIDTH * 2), Rand.Next (1, HEIGHT * 2));
  269. }
  270. // Ensure the spawn point is not in the exclusion list and it's an open space (not a wall)
  271. while (exclude.Contains (point) || _maze [point.Y, point.X] != 0);
  272. exclude.Add (point); // Mark this location as occupied
  273. locations.Add (point); // Add the location to the list
  274. }
  275. return locations;
  276. }
  277. private void Carve (Point p)
  278. {
  279. _maze [p.Y, p.X] = 0;
  280. int [] [] dirs =
  281. {
  282. [0, -2],
  283. [0, 2],
  284. [-2, 0],
  285. [2, 0]
  286. };
  287. Shuffle (dirs);
  288. foreach (int [] dir in dirs)
  289. {
  290. int nx = p.X + dir [0], ny = p.Y + dir [1];
  291. if (nx > 0 && ny > 0 && nx < WIDTH * 2 && ny < HEIGHT * 2 && _maze [ny, nx] == 1)
  292. {
  293. _maze [p.Y + dir [1] / 2, p.X + dir [0] / 2] = 0;
  294. Carve (new (nx, ny));
  295. }
  296. }
  297. }
  298. private void Shuffle (int [] [] array)
  299. {
  300. for (int i = array.Length - 1; i > 0; i--)
  301. {
  302. int j = Rand.Next (i + 1);
  303. int [] temp = array [i];
  304. array [i] = array [j];
  305. array [j] = temp;
  306. }
  307. }
  308. private Point GetRandomEdgePoint (int w, int h, bool isEntrance, int avoidX = -1, int avoidY = -1)
  309. {
  310. List<Point> candidates = [];
  311. for (var i = 1; i < h - 1; i += 2)
  312. {
  313. candidates.Add (new (0, i)); // Left edge
  314. candidates.Add (new (w - 1, i)); // Right edge
  315. }
  316. for (var i = 1; i < w - 1; i += 2)
  317. {
  318. candidates.Add (new (i, 0)); // Top edge
  319. candidates.Add (new (i, h - 1)); // Bottom edge
  320. }
  321. // Remove one if same as entrance
  322. if (!isEntrance)
  323. {
  324. candidates.RemoveAll (p => p.X == avoidX && p.Y == avoidY);
  325. }
  326. return candidates [Rand.Next (candidates.Count)];
  327. }
  328. }