diff --git a/hw6Game/hw6Game.Test/MoveTest.cs b/hw6Game/hw6Game.Test/MoveTest.cs new file mode 100644 index 0000000..8399276 --- /dev/null +++ b/hw6Game/hw6Game.Test/MoveTest.cs @@ -0,0 +1,82 @@ +using NUnit.Framework; +using System; + +namespace Hw6Game.Test +{ + public class Tests + { + private Map map; + + [SetUp] + public void Setup() + { + map = new Map("..//..//..//TestMap.txt"); + } + + [Test] + public void MoveLeftTest() + { + (int, int) oldCoordinates = map.GetPlayerCoordinates(); + map.Move(Game.Moves.left); + (int, int) newCoordinates = map.GetPlayerCoordinates(); + oldCoordinates.Item1--; + Assert.AreEqual(oldCoordinates.Item1, newCoordinates.Item1); + Assert.AreEqual(oldCoordinates.Item2, newCoordinates.Item2); + } + + [Test] + public void MoveRightTest() + { + (int, int) oldCoordinates = map.GetPlayerCoordinates(); + map.Move(Game.Moves.right); + (int, int) newCoordinates = map.GetPlayerCoordinates(); + oldCoordinates.Item1++; + Assert.AreEqual(oldCoordinates.Item1, newCoordinates.Item1); + Assert.AreEqual(oldCoordinates.Item2, newCoordinates.Item2); + } + + [Test] + public void MoveUpTest() + { + (int, int) oldCoordinates = map.GetPlayerCoordinates(); + map.Move(Game.Moves.up); + (int, int) newCoordinates = map.GetPlayerCoordinates(); + oldCoordinates.Item2--; + Assert.AreEqual(oldCoordinates.Item1, newCoordinates.Item1); + Assert.AreEqual(oldCoordinates.Item2, newCoordinates.Item2); + } + + [Test] + public void MoveDownTest() + { + (int, int) oldCoordinates = map.GetPlayerCoordinates(); + map.Move(Game.Moves.down); + (int, int) newCoordinates = map.GetPlayerCoordinates(); + oldCoordinates.Item2++; + Assert.AreEqual(oldCoordinates.Item1, newCoordinates.Item1); + Assert.AreEqual(oldCoordinates.Item2, newCoordinates.Item2); + } + + [Test] + public void MoveLeftToWallTest() + { + map.Move(Game.Moves.left); + (int, int) oldCoordinates = map.GetPlayerCoordinates(); + map.Move(Game.Moves.left); + (int, int) newCoordinates = map.GetPlayerCoordinates(); + Assert.AreEqual(oldCoordinates.Item1, newCoordinates.Item1); + Assert.AreEqual(oldCoordinates.Item2, newCoordinates.Item2); + } + + [Test] + public void MoveUpToWallTest() + { + map.Move(Game.Moves.up); + (int, int) oldCoordinates = map.GetPlayerCoordinates(); + map.Move(Game.Moves.up); + (int, int) newCoordinates = map.GetPlayerCoordinates(); + Assert.AreEqual(oldCoordinates.Item1, newCoordinates.Item1); + Assert.AreEqual(oldCoordinates.Item2, newCoordinates.Item2); + } + } +} \ No newline at end of file diff --git a/hw6Game/hw6Game.Test/TestMap.txt b/hw6Game/hw6Game.Test/TestMap.txt new file mode 100644 index 0000000..d7a5a17 --- /dev/null +++ b/hw6Game/hw6Game.Test/TestMap.txt @@ -0,0 +1,6 @@ +||||||||||||||| +|| || +|| @ || +|| || +|| || +||||||||||||||| \ No newline at end of file diff --git a/hw6Game/hw6Game.Test/hw6Game.Test.csproj b/hw6Game/hw6Game.Test/hw6Game.Test.csproj new file mode 100644 index 0000000..31977d7 --- /dev/null +++ b/hw6Game/hw6Game.Test/hw6Game.Test.csproj @@ -0,0 +1,19 @@ + + + + net5.0 + + false + + + + + + + + + + + + + diff --git a/hw6Game/hw6Game.sln b/hw6Game/hw6Game.sln new file mode 100644 index 0000000..46fe308 --- /dev/null +++ b/hw6Game/hw6Game.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31205.134 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Hw6Game", "hw6Game\Hw6Game.csproj", "{515F0DB5-A660-4F14-AA87-D5BF71260215}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "hw6Game.Test", "hw6Game.Test\hw6Game.Test.csproj", "{D99F3186-0BF2-43AB-88CF-4535AC3B0D76}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {515F0DB5-A660-4F14-AA87-D5BF71260215}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {515F0DB5-A660-4F14-AA87-D5BF71260215}.Debug|Any CPU.Build.0 = Debug|Any CPU + {515F0DB5-A660-4F14-AA87-D5BF71260215}.Release|Any CPU.ActiveCfg = Release|Any CPU + {515F0DB5-A660-4F14-AA87-D5BF71260215}.Release|Any CPU.Build.0 = Release|Any CPU + {D99F3186-0BF2-43AB-88CF-4535AC3B0D76}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D99F3186-0BF2-43AB-88CF-4535AC3B0D76}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D99F3186-0BF2-43AB-88CF-4535AC3B0D76}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D99F3186-0BF2-43AB-88CF-4535AC3B0D76}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {6BFE6769-C343-495E-959C-9FF9C1D95D9B} + EndGlobalSection +EndGlobal diff --git a/hw6Game/hw6Game/EventLoop.cs b/hw6Game/hw6Game/EventLoop.cs new file mode 100644 index 0000000..9f62ec2 --- /dev/null +++ b/hw6Game/hw6Game/EventLoop.cs @@ -0,0 +1,41 @@ +using System; + +namespace Hw6Game +{ + /// + /// class that generates events + /// + public class EventLoop + { + public event EventHandler LeftHandler = (sender, args) => { }; + + public event EventHandler RightHandler = (sender, args) => { }; + + public event EventHandler UpHandler = (sender, args) => { }; + + public event EventHandler DownHandler = (sender, args) => { }; + + public void Run() + { + while (true) + { + var key = Console.ReadKey(true); + switch (key.Key) + { + case ConsoleKey.LeftArrow: + LeftHandler(this, EventArgs.Empty); + break; + case ConsoleKey.RightArrow: + RightHandler(this, EventArgs.Empty); + break; + case ConsoleKey.UpArrow: + UpHandler(this, EventArgs.Empty); + break; + case ConsoleKey.DownArrow: + DownHandler(this, EventArgs.Empty); + break; + } + } + } + } +} diff --git a/hw6Game/hw6Game/Game.cs b/hw6Game/hw6Game/Game.cs new file mode 100644 index 0000000..1912917 --- /dev/null +++ b/hw6Game/hw6Game/Game.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Hw6Game +{ + /// + /// class that handles events + /// + public class Game + { + /// + /// enums for moves + /// + public enum Moves + { + left, + right, + up, + down + } + + private Map map; + + private (int, int) coordinates; + + public Game(string filePath) + { + map = new Map(filePath); + coordinates = map.GetPlayerCoordinates(); + } + + public void OnLeft(object sender, EventArgs args) + { + Move(Moves.left); + } + + public void OnRight(object sender, EventArgs args) + { + Move(Moves.right); + } + + public void OnDown(object sender, EventArgs args) + { + Move(Moves.down); + } + + public void OnUp(object sender, EventArgs args) + { + Move(Moves.up); + } + + private (int, int) GetNewCoordinates((int, int) coordinates, Moves move) => + move switch + { + Moves.left => (coordinates.Item1 + 1, coordinates.Item2), + Moves.right => (coordinates.Item1 - 1, coordinates.Item2), + Moves.down => (coordinates.Item1, coordinates.Item2 - 1), + Moves.up => (coordinates.Item1, coordinates.Item2 + 1), + _ => (coordinates.Item1, coordinates.Item2) + }; + + private void Move(Moves move) + { + if (map.Move(move)) + { + var oldCoordinates = map.GetPlayerCoordinates(); + coordinates = GetNewCoordinates(map.GetPlayerCoordinates(), move); + Console.SetCursorPosition(coordinates.Item1, coordinates.Item2); + Console.Write(' '); + Console.SetCursorPosition(oldCoordinates.Item1, oldCoordinates.Item2); + Console.Write('@'); + } + } + } +} \ No newline at end of file diff --git a/hw6Game/hw6Game/Map.cs b/hw6Game/hw6Game/Map.cs new file mode 100644 index 0000000..32db20a --- /dev/null +++ b/hw6Game/hw6Game/Map.cs @@ -0,0 +1,81 @@ +using System; +using System.IO; + +namespace Hw6Game +{ + /// + /// class working on building the map + /// + public class Map + { + private struct PlayerCoordinates + { + public int x; + public int y; + } + + private string[] mapPic; + + static private PlayerCoordinates player = new (); + + /// + /// create a map + /// + public Map(string filePath) + { + var map = File.ReadAllLines(filePath); + for (int i = 0; i < map.Length; ++i) + { + var index = map[i].IndexOf("@"); + if (index != -1) + { + player.x = index; + player.y = i; + } + Console.WriteLine(map[i]); + } + map[player.y].Replace('@', ' '); + mapPic = map; + } + + private bool CheckMove((int x, int y) coord) + { + if (mapPic[coord.y][coord.x ] != ' ' && mapPic[coord.y][coord.x] != '@') + { + return false; + } + player.x = coord.x; + player.y =coord.y; + return true; + } + + /// + /// Move player + /// + /// step direction + /// true if step is complete + public bool Move(Game.Moves move) + { + switch (move) + { + case Game.Moves.left: + return CheckMove((player.x - 1, player.y)); + case Game.Moves.right: + return CheckMove((player.x + 1, player.y)); + case Game.Moves.down: + return CheckMove((player.x, player.y + 1)); + case Game.Moves.up: + return CheckMove((player.x, player.y - 1)); + default: + return false; + } + } + + /// + /// return player coordinates + /// + /// coordinates (x, y) + public (int, int) GetPlayerCoordinates() + => (player.x, player.y); + } +} \ No newline at end of file diff --git a/hw6Game/hw6Game/Map.txt b/hw6Game/hw6Game/Map.txt new file mode 100644 index 0000000..4bb1671 --- /dev/null +++ b/hw6Game/hw6Game/Map.txt @@ -0,0 +1,18 @@ +|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| +|| || +|| || +|| @ || +|| || +|| \\==== ====//================ ========== || +|| \\ // || || || || +|| \\ // || || || || +|| \\ // || || || || +|| \\ // || || || || +|| \\ // || || || || +|| \\// =========|| || || || +|| || || +|| || || +|| ================================ ========== || +|| || +|| || +|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| \ No newline at end of file diff --git a/hw6Game/hw6Game/Program.cs b/hw6Game/hw6Game/Program.cs new file mode 100644 index 0000000..5d89cb4 --- /dev/null +++ b/hw6Game/hw6Game/Program.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; + +namespace Hw6Game +{ + class Program + { + static void Main() + { + var eventLoop = new EventLoop(); + var game = new Game("..//..//..//Map.txt"); + eventLoop.LeftHandler += game.OnLeft; + eventLoop.RightHandler += game.OnRight; + eventLoop.UpHandler += game.OnUp; + eventLoop.DownHandler += game.OnDown; + eventLoop.Run(); + } + } +} diff --git a/hw6Game/hw6Game/hw6Game.csproj b/hw6Game/hw6Game/hw6Game.csproj new file mode 100644 index 0000000..169b775 --- /dev/null +++ b/hw6Game/hw6Game/hw6Game.csproj @@ -0,0 +1,8 @@ + + + + Exe + net5.0 + + + \ No newline at end of file