From 1ad513e504ec386203fd29fa427489e431d80e87 Mon Sep 17 00:00:00 2001 From: MikePuzanov <89803250877@mail.ru> Date: Mon, 19 Apr 2021 02:05:11 +0300 Subject: [PATCH 1/7] code Game, Map. tests are still waiting --- hw6Game/hw6Game.Test/ActoinTest.cs | 29 +++++++++ hw6Game/hw6Game.Test/TestMap.txt | 6 ++ hw6Game/hw6Game.Test/hw6Game.Test.csproj | 19 ++++++ hw6Game/hw6Game.sln | 31 ++++++++++ hw6Game/hw6Game/EventLoop.cs | 35 +++++++++++ hw6Game/hw6Game/Game.cs | 70 ++++++++++++++++++++++ hw6Game/hw6Game/Map.cs | 75 ++++++++++++++++++++++++ hw6Game/hw6Game/Map.txt | 18 ++++++ hw6Game/hw6Game/Program.cs | 20 +++++++ hw6Game/hw6Game/hw6Game.csproj | 8 +++ 10 files changed, 311 insertions(+) create mode 100644 hw6Game/hw6Game.Test/ActoinTest.cs create mode 100644 hw6Game/hw6Game.Test/TestMap.txt create mode 100644 hw6Game/hw6Game.Test/hw6Game.Test.csproj create mode 100644 hw6Game/hw6Game.sln create mode 100644 hw6Game/hw6Game/EventLoop.cs create mode 100644 hw6Game/hw6Game/Game.cs create mode 100644 hw6Game/hw6Game/Map.cs create mode 100644 hw6Game/hw6Game/Map.txt create mode 100644 hw6Game/hw6Game/Program.cs create mode 100644 hw6Game/hw6Game/hw6Game.csproj diff --git a/hw6Game/hw6Game.Test/ActoinTest.cs b/hw6Game/hw6Game.Test/ActoinTest.cs new file mode 100644 index 0000000..c0c9ba4 --- /dev/null +++ b/hw6Game/hw6Game.Test/ActoinTest.cs @@ -0,0 +1,29 @@ +using NUnit.Framework; +using System; + +namespace hw6Game.Test +{ + public class Tests + { + private EventLoop eventLoop; + + private Game game; + + [SetUp] + public void Setup() + { + eventLoop = new EventLoop(); + game = new Game("..//..//..//TestMap.txt"); + } + + [Test] + public void MoveLeftTest() + { + (int, int) oldCoordinates = game.GetCoordinates(); + game.OnLeft(ConsoleKey.LeftArrow, EventArgs.Empty); + (int, int) newCoordinates = game.GetCoordinates(); + Assert.AreEqual(oldCoordinates.Item1 + 1, 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..f12373e --- /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..25b99b9 --- /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..0d6ec04 --- /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("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "hw6Game.Test", "hw6Game.Test\hw6Game.Test.csproj", "{622B2E63-5AC4-4CD1-9D3E-699C1C5B0EFE}" +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 + {622B2E63-5AC4-4CD1-9D3E-699C1C5B0EFE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {622B2E63-5AC4-4CD1-9D3E-699C1C5B0EFE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {622B2E63-5AC4-4CD1-9D3E-699C1C5B0EFE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {622B2E63-5AC4-4CD1-9D3E-699C1C5B0EFE}.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..aa2a3cb --- /dev/null +++ b/hw6Game/hw6Game/EventLoop.cs @@ -0,0 +1,35 @@ +using System; + +namespace hw6Game +{ + 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..77aeee8 --- /dev/null +++ b/hw6Game/hw6Game/Game.cs @@ -0,0 +1,70 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace hw6Game +{ + public class Game + { + private Map map; + + private (int, int) coordinates; + + public (int, int) GetCoordinates() + => (coordinates.Item1, coordinates.Item2); + + public Game(string filePath) + { + map = new Map(filePath); + coordinates = map.GetCoordinates(); + } + + public void OnLeft(object sender, EventArgs args) + { + Move("left"); + } + + public void OnRight(object sender, EventArgs args) + { + Move("right"); + } + + public void OnDown(object sender, EventArgs args) + { + Move("down"); + } + + public void OnUp(object sender, EventArgs args) + { + Move("up"); + } + + private void Move(string step) + { + if (map.Move(step)) + { + coordinates = map.GetCoordinates(); + switch (step) + { + case "left": + Console.SetCursorPosition(coordinates.Item1 + 1, coordinates.Item2); + break; + case "right": + Console.SetCursorPosition(coordinates.Item1 - 1, coordinates.Item2); + break; + case "down": + Console.SetCursorPosition(coordinates.Item1, coordinates.Item2 - 1); + break; + case "up": + Console.SetCursorPosition(coordinates.Item1, coordinates.Item2 + 1); + break; + } + Console.Write(' '); + Console.SetCursorPosition(coordinates.Item1, coordinates.Item2); + Console.Write('@'); + } + } + } +} diff --git a/hw6Game/hw6Game/Map.cs b/hw6Game/hw6Game/Map.cs new file mode 100644 index 0000000..71466e5 --- /dev/null +++ b/hw6Game/hw6Game/Map.cs @@ -0,0 +1,75 @@ +using System; +using System.IO; + +namespace hw6Game +{ + public class Map + { + private struct PlayerCoordination + { + public int x; + public int y; + } + + static private string[] mapPic; + + static private PlayerCoordination player = new PlayerCoordination(); + + 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; + } + + public bool Move(string step) + { + switch (step) + { + case "left": + if (mapPic[player.y][player.x - 1] != ' ' && mapPic[player.y][player.x - 1] != '@') + { + return false; + } + player.x -= 1; + return true; + case "right": + if (mapPic[player.y][player.x + 1] != ' ' && mapPic[player.y][player.x + 1] != '@') + { + return false; + } + player.x += 1; + return true; + case "down": + if (mapPic[player.y + 1][player.x] != ' ' && mapPic[player.y + 1][player.x] != '@') + { + return false; + } + player.y += 1; + return true; + case "up": + if (mapPic[player.y - 1][player.x] != ' ' && mapPic[player.y - 1][player.x] != '@') + { + return false; + } + player.y -= 1; + return true; + default: + return false; + } + } + + public (int, int) GetCoordinates() + => (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..faaa6a8 --- /dev/null +++ b/hw6Game/hw6Game/Program.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; + +namespace hw6Game +{ + class Program + { + static void Main(string[] args) + { + 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..2082704 --- /dev/null +++ b/hw6Game/hw6Game/hw6Game.csproj @@ -0,0 +1,8 @@ + + + + Exe + net5.0 + + + From 05363ff361a45a3ecfea4aa74f9c24af603f74ea Mon Sep 17 00:00:00 2001 From: MikePuzanov <89803250877@mail.ru> Date: Mon, 19 Apr 2021 21:36:42 +0300 Subject: [PATCH 2/7] add tests --- hw6Game/hw6Game.Test/ActoinTest.cs | 29 ---------- hw6Game/hw6Game.Test/MoveTest.cs | 85 ++++++++++++++++++++++++++++++ hw6Game/hw6Game.Test/TestMap.txt | 2 +- hw6Game/hw6Game.sln | 10 ++-- hw6Game/hw6Game/EventLoop.cs | 1 - hw6Game/hw6Game/Game.cs | 7 +-- hw6Game/hw6Game/Map.cs | 14 ++++- hw6Game/hw6Game/Program.cs | 3 +- hw6Game/hw6Game/hw6Game.csproj | 10 ++-- 9 files changed, 112 insertions(+), 49 deletions(-) delete mode 100644 hw6Game/hw6Game.Test/ActoinTest.cs create mode 100644 hw6Game/hw6Game.Test/MoveTest.cs diff --git a/hw6Game/hw6Game.Test/ActoinTest.cs b/hw6Game/hw6Game.Test/ActoinTest.cs deleted file mode 100644 index c0c9ba4..0000000 --- a/hw6Game/hw6Game.Test/ActoinTest.cs +++ /dev/null @@ -1,29 +0,0 @@ -using NUnit.Framework; -using System; - -namespace hw6Game.Test -{ - public class Tests - { - private EventLoop eventLoop; - - private Game game; - - [SetUp] - public void Setup() - { - eventLoop = new EventLoop(); - game = new Game("..//..//..//TestMap.txt"); - } - - [Test] - public void MoveLeftTest() - { - (int, int) oldCoordinates = game.GetCoordinates(); - game.OnLeft(ConsoleKey.LeftArrow, EventArgs.Empty); - (int, int) newCoordinates = game.GetCoordinates(); - Assert.AreEqual(oldCoordinates.Item1 + 1, newCoordinates.Item1); - Assert.AreEqual(oldCoordinates.Item2, newCoordinates.Item2); - } - } -} \ No newline at end of file diff --git a/hw6Game/hw6Game.Test/MoveTest.cs b/hw6Game/hw6Game.Test/MoveTest.cs new file mode 100644 index 0000000..c1c9096 --- /dev/null +++ b/hw6Game/hw6Game.Test/MoveTest.cs @@ -0,0 +1,85 @@ +using NUnit.Framework; +using System; + +namespace hw6Game.Test +{ + public class Tests + { + private EventLoop eventLoop; + + private Map map; + + [SetUp] + public void Setup() + { + eventLoop = new EventLoop(); + map = new Map("..//..//..//TestMap.txt"); + } + + [Test] + public void MoveLeftTest() + { + (int, int) oldCoordinates = map.GetPlayerCoordinates(); + map.Move("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("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("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("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("left"); + (int, int) oldCoordinates = map.GetPlayerCoordinates(); + map.Move("left"); + (int, int) newCoordinates = map.GetPlayerCoordinates(); + Assert.AreEqual(oldCoordinates.Item1, newCoordinates.Item1); + Assert.AreEqual(oldCoordinates.Item2, newCoordinates.Item2); + } + + [Test] + public void MoveUpToWallTest() + { + map.Move("up"); + (int, int) oldCoordinates = map.GetPlayerCoordinates(); + map.Move("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 index f12373e..d7a5a17 100644 --- a/hw6Game/hw6Game.Test/TestMap.txt +++ b/hw6Game/hw6Game.Test/TestMap.txt @@ -1,6 +1,6 @@ ||||||||||||||| -|| @ || || || +|| @ || || || || || ||||||||||||||| \ No newline at end of file diff --git a/hw6Game/hw6Game.sln b/hw6Game/hw6Game.sln index 0d6ec04..96d3755 100644 --- a/hw6Game/hw6Game.sln +++ b/hw6Game/hw6Game.sln @@ -5,7 +5,7 @@ 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("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "hw6Game.Test", "hw6Game.Test\hw6Game.Test.csproj", "{622B2E63-5AC4-4CD1-9D3E-699C1C5B0EFE}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "hw6Game.Test", "hw6Game.Test\hw6Game.Test.csproj", "{D99F3186-0BF2-43AB-88CF-4535AC3B0D76}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -17,10 +17,10 @@ Global {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 - {622B2E63-5AC4-4CD1-9D3E-699C1C5B0EFE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {622B2E63-5AC4-4CD1-9D3E-699C1C5B0EFE}.Debug|Any CPU.Build.0 = Debug|Any CPU - {622B2E63-5AC4-4CD1-9D3E-699C1C5B0EFE}.Release|Any CPU.ActiveCfg = Release|Any CPU - {622B2E63-5AC4-4CD1-9D3E-699C1C5B0EFE}.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 diff --git a/hw6Game/hw6Game/EventLoop.cs b/hw6Game/hw6Game/EventLoop.cs index aa2a3cb..e5d0ef6 100644 --- a/hw6Game/hw6Game/EventLoop.cs +++ b/hw6Game/hw6Game/EventLoop.cs @@ -31,5 +31,4 @@ public void Run() } } } - } diff --git a/hw6Game/hw6Game/Game.cs b/hw6Game/hw6Game/Game.cs index 77aeee8..bbfd620 100644 --- a/hw6Game/hw6Game/Game.cs +++ b/hw6Game/hw6Game/Game.cs @@ -12,13 +12,10 @@ public class Game private (int, int) coordinates; - public (int, int) GetCoordinates() - => (coordinates.Item1, coordinates.Item2); - public Game(string filePath) { map = new Map(filePath); - coordinates = map.GetCoordinates(); + coordinates = map.GetPlayerCoordinates(); } public void OnLeft(object sender, EventArgs args) @@ -45,7 +42,7 @@ private void Move(string step) { if (map.Move(step)) { - coordinates = map.GetCoordinates(); + coordinates = map.GetPlayerCoordinates(); switch (step) { case "left": diff --git a/hw6Game/hw6Game/Map.cs b/hw6Game/hw6Game/Map.cs index 71466e5..5a4bab9 100644 --- a/hw6Game/hw6Game/Map.cs +++ b/hw6Game/hw6Game/Map.cs @@ -15,6 +15,9 @@ private struct PlayerCoordination static private PlayerCoordination player = new PlayerCoordination(); + /// + /// create a map + /// public Map(string filePath) { var map = File.ReadAllLines(filePath); @@ -32,6 +35,11 @@ public Map(string filePath) mapPic = map; } + /// + /// Move player + /// + /// step direction + /// true if step is complete public bool Move(string step) { switch (step) @@ -69,7 +77,11 @@ public bool Move(string step) } } - public (int, int) GetCoordinates() + /// + /// 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/Program.cs b/hw6Game/hw6Game/Program.cs index faaa6a8..b4cfa68 100644 --- a/hw6Game/hw6Game/Program.cs +++ b/hw6Game/hw6Game/Program.cs @@ -5,7 +5,7 @@ namespace hw6Game { class Program { - static void Main(string[] args) + static void Main() { var eventLoop = new EventLoop(); var game = new Game("..//..//..//Map.txt"); @@ -15,6 +15,5 @@ static void Main(string[] args) eventLoop.DownHandler += game.OnDown; eventLoop.Run(); } - } } diff --git a/hw6Game/hw6Game/hw6Game.csproj b/hw6Game/hw6Game/hw6Game.csproj index 2082704..169b775 100644 --- a/hw6Game/hw6Game/hw6Game.csproj +++ b/hw6Game/hw6Game/hw6Game.csproj @@ -1,8 +1,8 @@ - - Exe - net5.0 - + + Exe + net5.0 + - + \ No newline at end of file From 5772532d87b3a8395ba7801ea77299c01f40bad7 Mon Sep 17 00:00:00 2001 From: MikePuzanov <89803250877@mail.ru> Date: Tue, 18 May 2021 02:58:46 +0300 Subject: [PATCH 3/7] delete useless lines --- hw6Game/hw6Game.Test/MoveTest.cs | 3 +-- hw6Game/hw6Game/EventLoop.cs | 9 ++++++++- hw6Game/hw6Game/Game.cs | 5 ++++- hw6Game/hw6Game/Map.cs | 11 +++++++---- hw6Game/hw6Game/Program.cs | 2 +- 5 files changed, 21 insertions(+), 9 deletions(-) diff --git a/hw6Game/hw6Game.Test/MoveTest.cs b/hw6Game/hw6Game.Test/MoveTest.cs index c1c9096..f190ada 100644 --- a/hw6Game/hw6Game.Test/MoveTest.cs +++ b/hw6Game/hw6Game.Test/MoveTest.cs @@ -1,7 +1,7 @@ using NUnit.Framework; using System; -namespace hw6Game.Test +namespace Hw6Game.Test { public class Tests { @@ -12,7 +12,6 @@ public class Tests [SetUp] public void Setup() { - eventLoop = new EventLoop(); map = new Map("..//..//..//TestMap.txt"); } diff --git a/hw6Game/hw6Game/EventLoop.cs b/hw6Game/hw6Game/EventLoop.cs index e5d0ef6..8a61f6d 100644 --- a/hw6Game/hw6Game/EventLoop.cs +++ b/hw6Game/hw6Game/EventLoop.cs @@ -1,13 +1,20 @@ using System; -namespace hw6Game +namespace Hw6Game { + /// + /// + /// 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) diff --git a/hw6Game/hw6Game/Game.cs b/hw6Game/hw6Game/Game.cs index bbfd620..40bada6 100644 --- a/hw6Game/hw6Game/Game.cs +++ b/hw6Game/hw6Game/Game.cs @@ -4,8 +4,11 @@ using System.Text; using System.Threading.Tasks; -namespace hw6Game +namespace Hw6Game { + /// + /// + /// public class Game { private Map map; diff --git a/hw6Game/hw6Game/Map.cs b/hw6Game/hw6Game/Map.cs index 5a4bab9..7e4e8ef 100644 --- a/hw6Game/hw6Game/Map.cs +++ b/hw6Game/hw6Game/Map.cs @@ -1,19 +1,22 @@ using System; using System.IO; -namespace hw6Game +namespace Hw6Game { + /// + /// + /// public class Map { - private struct PlayerCoordination + private struct PlayerCoordinates { public int x; public int y; } - static private string[] mapPic; + private string[] mapPic; - static private PlayerCoordination player = new PlayerCoordination(); + static private PlayerCoordinates player = new PlayerCoordinates(); /// /// create a map diff --git a/hw6Game/hw6Game/Program.cs b/hw6Game/hw6Game/Program.cs index b4cfa68..5d89cb4 100644 --- a/hw6Game/hw6Game/Program.cs +++ b/hw6Game/hw6Game/Program.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; -namespace hw6Game +namespace Hw6Game { class Program { From 39505b4cad8ff29bd4e5aeca2e06ee31355b2185 Mon Sep 17 00:00:00 2001 From: MikePuzanov <89803250877@mail.ru> Date: Tue, 18 May 2021 02:59:42 +0300 Subject: [PATCH 4/7] delete useless lines --- hw6Game/hw6Game.Test/hw6Game.Test.csproj | 2 +- hw6Game/hw6Game.sln | 4 ++-- hw6Game/hw6Game/EventLoop.cs | 2 +- hw6Game/hw6Game/Map.cs | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/hw6Game/hw6Game.Test/hw6Game.Test.csproj b/hw6Game/hw6Game.Test/hw6Game.Test.csproj index 25b99b9..31977d7 100644 --- a/hw6Game/hw6Game.Test/hw6Game.Test.csproj +++ b/hw6Game/hw6Game.Test/hw6Game.Test.csproj @@ -13,7 +13,7 @@ - + diff --git a/hw6Game/hw6Game.sln b/hw6Game/hw6Game.sln index 96d3755..46fe308 100644 --- a/hw6Game/hw6Game.sln +++ b/hw6Game/hw6Game.sln @@ -3,9 +3,9 @@ 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}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Hw6Game", "hw6Game\Hw6Game.csproj", "{515F0DB5-A660-4F14-AA87-D5BF71260215}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "hw6Game.Test", "hw6Game.Test\hw6Game.Test.csproj", "{D99F3186-0BF2-43AB-88CF-4535AC3B0D76}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "hw6Game.Test", "hw6Game.Test\hw6Game.Test.csproj", "{D99F3186-0BF2-43AB-88CF-4535AC3B0D76}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/hw6Game/hw6Game/EventLoop.cs b/hw6Game/hw6Game/EventLoop.cs index 8a61f6d..24f4be7 100644 --- a/hw6Game/hw6Game/EventLoop.cs +++ b/hw6Game/hw6Game/EventLoop.cs @@ -3,7 +3,7 @@ namespace Hw6Game { /// - /// + /// класс, генерирующий события /// public class EventLoop { diff --git a/hw6Game/hw6Game/Map.cs b/hw6Game/hw6Game/Map.cs index 7e4e8ef..0d5d169 100644 --- a/hw6Game/hw6Game/Map.cs +++ b/hw6Game/hw6Game/Map.cs @@ -4,7 +4,7 @@ namespace Hw6Game { /// - /// + /// класс, работающий над построений карты /// public class Map { From bd6436a85ce19d93b06d08bd875fe13566203071 Mon Sep 17 00:00:00 2001 From: MikePuzanov <89803250877@mail.ru> Date: Tue, 25 May 2021 00:11:10 +0300 Subject: [PATCH 5/7] fix mistakes --- hw6Game/hw6Game/EventLoop.cs | 2 +- hw6Game/hw6Game/Game.cs | 33 +++++++++++++++------------------ hw6Game/hw6Game/Map.cs | 2 +- 3 files changed, 17 insertions(+), 20 deletions(-) diff --git a/hw6Game/hw6Game/EventLoop.cs b/hw6Game/hw6Game/EventLoop.cs index 24f4be7..9f62ec2 100644 --- a/hw6Game/hw6Game/EventLoop.cs +++ b/hw6Game/hw6Game/EventLoop.cs @@ -3,7 +3,7 @@ namespace Hw6Game { /// - /// класс, генерирующий события + /// class that generates events /// public class EventLoop { diff --git a/hw6Game/hw6Game/Game.cs b/hw6Game/hw6Game/Game.cs index 40bada6..e0b0725 100644 --- a/hw6Game/hw6Game/Game.cs +++ b/hw6Game/hw6Game/Game.cs @@ -7,7 +7,7 @@ namespace Hw6Game { /// - /// + /// class that handles events /// public class Game { @@ -41,28 +41,25 @@ public void OnUp(object sender, EventArgs args) Move("up"); } + private (int, int) GetNewCoordinates((int, int) coordinates, string step) => + step switch + { + "left" => (coordinates.Item1 + 1, coordinates.Item2), + "right" => (coordinates.Item1 - 1, coordinates.Item2), + "down" => (coordinates.Item1, coordinates.Item2 - 1), + "up" => (coordinates.Item1, coordinates.Item2 + 1), + _ => (coordinates.Item1, coordinates.Item2) + }; + private void Move(string step) { if (map.Move(step)) { - coordinates = map.GetPlayerCoordinates(); - switch (step) - { - case "left": - Console.SetCursorPosition(coordinates.Item1 + 1, coordinates.Item2); - break; - case "right": - Console.SetCursorPosition(coordinates.Item1 - 1, coordinates.Item2); - break; - case "down": - Console.SetCursorPosition(coordinates.Item1, coordinates.Item2 - 1); - break; - case "up": - Console.SetCursorPosition(coordinates.Item1, coordinates.Item2 + 1); - break; - } - Console.Write(' '); + var oldCoordinates = map.GetPlayerCoordinates(); + coordinates = GetNewCoordinates(map.GetPlayerCoordinates(), step); Console.SetCursorPosition(coordinates.Item1, coordinates.Item2); + Console.Write(' '); + Console.SetCursorPosition(oldCoordinates.Item1, oldCoordinates.Item2); Console.Write('@'); } } diff --git a/hw6Game/hw6Game/Map.cs b/hw6Game/hw6Game/Map.cs index 0d5d169..9310694 100644 --- a/hw6Game/hw6Game/Map.cs +++ b/hw6Game/hw6Game/Map.cs @@ -4,7 +4,7 @@ namespace Hw6Game { /// - /// класс, работающий над построений карты + /// class working on building the map /// public class Map { From 4a57d66fc7b4164842934386156626917fd52252 Mon Sep 17 00:00:00 2001 From: MikePuzanov <89803250877@mail.ru> Date: Tue, 25 May 2021 00:23:38 +0300 Subject: [PATCH 6/7] delete line --- hw6Game/hw6Game/Game.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hw6Game/hw6Game/Game.cs b/hw6Game/hw6Game/Game.cs index e0b0725..0d7881c 100644 --- a/hw6Game/hw6Game/Game.cs +++ b/hw6Game/hw6Game/Game.cs @@ -64,4 +64,4 @@ private void Move(string step) } } } -} +} \ No newline at end of file From 97ac6c325602a5a07c34392cb86a22faefe2c1dd Mon Sep 17 00:00:00 2001 From: MikePuzanov <89803250877@mail.ru> Date: Fri, 4 Jun 2021 00:09:30 +0300 Subject: [PATCH 7/7] fix code --- hw6Game/hw6Game.Test/MoveTest.cs | 18 +++++------ hw6Game/hw6Game/Game.cs | 37 ++++++++++++++-------- hw6Game/hw6Game/Map.cs | 53 +++++++++++++------------------- 3 files changed, 54 insertions(+), 54 deletions(-) diff --git a/hw6Game/hw6Game.Test/MoveTest.cs b/hw6Game/hw6Game.Test/MoveTest.cs index f190ada..8399276 100644 --- a/hw6Game/hw6Game.Test/MoveTest.cs +++ b/hw6Game/hw6Game.Test/MoveTest.cs @@ -5,8 +5,6 @@ namespace Hw6Game.Test { public class Tests { - private EventLoop eventLoop; - private Map map; [SetUp] @@ -19,7 +17,7 @@ public void Setup() public void MoveLeftTest() { (int, int) oldCoordinates = map.GetPlayerCoordinates(); - map.Move("left"); + map.Move(Game.Moves.left); (int, int) newCoordinates = map.GetPlayerCoordinates(); oldCoordinates.Item1--; Assert.AreEqual(oldCoordinates.Item1, newCoordinates.Item1); @@ -30,7 +28,7 @@ public void MoveLeftTest() public void MoveRightTest() { (int, int) oldCoordinates = map.GetPlayerCoordinates(); - map.Move("right"); + map.Move(Game.Moves.right); (int, int) newCoordinates = map.GetPlayerCoordinates(); oldCoordinates.Item1++; Assert.AreEqual(oldCoordinates.Item1, newCoordinates.Item1); @@ -41,7 +39,7 @@ public void MoveRightTest() public void MoveUpTest() { (int, int) oldCoordinates = map.GetPlayerCoordinates(); - map.Move("up"); + map.Move(Game.Moves.up); (int, int) newCoordinates = map.GetPlayerCoordinates(); oldCoordinates.Item2--; Assert.AreEqual(oldCoordinates.Item1, newCoordinates.Item1); @@ -52,7 +50,7 @@ public void MoveUpTest() public void MoveDownTest() { (int, int) oldCoordinates = map.GetPlayerCoordinates(); - map.Move("down"); + map.Move(Game.Moves.down); (int, int) newCoordinates = map.GetPlayerCoordinates(); oldCoordinates.Item2++; Assert.AreEqual(oldCoordinates.Item1, newCoordinates.Item1); @@ -62,9 +60,9 @@ public void MoveDownTest() [Test] public void MoveLeftToWallTest() { - map.Move("left"); + map.Move(Game.Moves.left); (int, int) oldCoordinates = map.GetPlayerCoordinates(); - map.Move("left"); + map.Move(Game.Moves.left); (int, int) newCoordinates = map.GetPlayerCoordinates(); Assert.AreEqual(oldCoordinates.Item1, newCoordinates.Item1); Assert.AreEqual(oldCoordinates.Item2, newCoordinates.Item2); @@ -73,9 +71,9 @@ public void MoveLeftToWallTest() [Test] public void MoveUpToWallTest() { - map.Move("up"); + map.Move(Game.Moves.up); (int, int) oldCoordinates = map.GetPlayerCoordinates(); - map.Move("up"); + map.Move(Game.Moves.up); (int, int) newCoordinates = map.GetPlayerCoordinates(); Assert.AreEqual(oldCoordinates.Item1, newCoordinates.Item1); Assert.AreEqual(oldCoordinates.Item2, newCoordinates.Item2); diff --git a/hw6Game/hw6Game/Game.cs b/hw6Game/hw6Game/Game.cs index 0d7881c..1912917 100644 --- a/hw6Game/hw6Game/Game.cs +++ b/hw6Game/hw6Game/Game.cs @@ -11,6 +11,17 @@ namespace Hw6Game /// public class Game { + /// + /// enums for moves + /// + public enum Moves + { + left, + right, + up, + down + } + private Map map; private (int, int) coordinates; @@ -23,40 +34,40 @@ public Game(string filePath) public void OnLeft(object sender, EventArgs args) { - Move("left"); + Move(Moves.left); } public void OnRight(object sender, EventArgs args) { - Move("right"); + Move(Moves.right); } public void OnDown(object sender, EventArgs args) { - Move("down"); + Move(Moves.down); } public void OnUp(object sender, EventArgs args) { - Move("up"); + Move(Moves.up); } - private (int, int) GetNewCoordinates((int, int) coordinates, string step) => - step switch + private (int, int) GetNewCoordinates((int, int) coordinates, Moves move) => + move switch { - "left" => (coordinates.Item1 + 1, coordinates.Item2), - "right" => (coordinates.Item1 - 1, coordinates.Item2), - "down" => (coordinates.Item1, coordinates.Item2 - 1), - "up" => (coordinates.Item1, coordinates.Item2 + 1), + 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(string step) + private void Move(Moves move) { - if (map.Move(step)) + if (map.Move(move)) { var oldCoordinates = map.GetPlayerCoordinates(); - coordinates = GetNewCoordinates(map.GetPlayerCoordinates(), step); + coordinates = GetNewCoordinates(map.GetPlayerCoordinates(), move); Console.SetCursorPosition(coordinates.Item1, coordinates.Item2); Console.Write(' '); Console.SetCursorPosition(oldCoordinates.Item1, oldCoordinates.Item2); diff --git a/hw6Game/hw6Game/Map.cs b/hw6Game/hw6Game/Map.cs index 9310694..32db20a 100644 --- a/hw6Game/hw6Game/Map.cs +++ b/hw6Game/hw6Game/Map.cs @@ -16,7 +16,7 @@ private struct PlayerCoordinates private string[] mapPic; - static private PlayerCoordinates player = new PlayerCoordinates(); + static private PlayerCoordinates player = new (); /// /// create a map @@ -38,43 +38,34 @@ public Map(string filePath) 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(string step) + public bool Move(Game.Moves move) { - switch (step) + switch (move) { - case "left": - if (mapPic[player.y][player.x - 1] != ' ' && mapPic[player.y][player.x - 1] != '@') - { - return false; - } - player.x -= 1; - return true; - case "right": - if (mapPic[player.y][player.x + 1] != ' ' && mapPic[player.y][player.x + 1] != '@') - { - return false; - } - player.x += 1; - return true; - case "down": - if (mapPic[player.y + 1][player.x] != ' ' && mapPic[player.y + 1][player.x] != '@') - { - return false; - } - player.y += 1; - return true; - case "up": - if (mapPic[player.y - 1][player.x] != ' ' && mapPic[player.y - 1][player.x] != '@') - { - return false; - } - player.y -= 1; - return true; + 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; }