From 5bc4c3c3753af21a3366befbc6552b7b39a3ffe5 Mon Sep 17 00:00:00 2001 From: Marvin Rohrbach Date: Fri, 25 May 2018 10:04:52 +0200 Subject: [PATCH] Add map rendering --- CoopSweeper/Program.cs | 63 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/CoopSweeper/Program.cs b/CoopSweeper/Program.cs index 10f3b8f..1fde0b9 100644 --- a/CoopSweeper/Program.cs +++ b/CoopSweeper/Program.cs @@ -1,4 +1,5 @@ using System; +using System.Text; namespace CoopSweeper { @@ -6,7 +7,69 @@ namespace CoopSweeper { static void Main(string[] args) { + string[,] map = new string[,] { + {"X"," ","X"," "}, + {"X"," ","X"," "}, + {"X"," ","X"," "}, + {"X"," ","X"," "} + }; + DrawBorder(2, 2, map.GetLength(0) + 2, map.GetLength(1) + 2); + DrawMap(3,3,RenderMap(map)); + Console.ReadKey(); + } + private static string[] RenderMap(string[,] map) + { + var res = new string[map.GetLength(1)]; + for(var y = 0; y < map.GetLength(1); y++) + { + var linebuilder = new StringBuilder(); + for (var x = 0; x < map.GetLength(0); x++) + { + linebuilder.Append(map[x, y]); + } + res[y] = linebuilder.ToString(); + } + return res; + } + + private static void DrawMap(int posX, int posY, string[] mapLines) + { + for(var y = 0; y < mapLines.Length; y++) + { + Console.SetCursorPosition(posX, posY + y); + Console.Write(mapLines[y]); + } + } + + private static void DrawBorder(int posX, int posY, int width, int height) + { + Console.SetCursorPosition(posX, posY); + var linebuilder = new StringBuilder(); + linebuilder.Append("╔"); + for (var x = 0; x < width - 2; x++) + linebuilder.Append("═"); + linebuilder.Append("╗"); + Console.Write(linebuilder); + for(var y = 1; y < height - 1; y++) + { + Console.SetCursorPosition(posX, posY + y); + Console.Write("║"); + Console.SetCursorPosition(posX + width - 1, posY + y); + Console.Write("║"); + } + Console.SetCursorPosition(posX, posY + height - 1); + linebuilder = new StringBuilder(); + linebuilder.Append("╚"); + for (var x = 0; x < width - 2; x++) + linebuilder.Append("═"); + linebuilder.Append("╝"); + Console.Write(linebuilder); + // ┌──┬──┐ ╔══╦══╗ ╒══╤══╕ ╓──╥──╖ + // │ │ │ ║ ║ ║ │ │ │ ║ ║ ║ + // ├──┼──┤ ╠══╬══╣ ╞══╪══╡ ╟──╫──╢ + // │ │ │ ║ ║ ║ │ │ │ ║ ║ ║ + // └──┴──┘ ╚══╩══╝ ╘══╧══╛ ╙──╨──╜ } } }