From 34b61d633b8c7334558f2bd7688c1e9046b91fb1 Mon Sep 17 00:00:00 2001 From: IgnatSergeev/Desktop Date: Fri, 14 Apr 2023 01:54:39 +0300 Subject: [PATCH 1/5] Finished main task --- C#/forSpbu/Game/EventLoop.cs | 47 ++++++++++++ C#/forSpbu/Game/Game.cs | 136 +++++++++++++++++++++++++++++++++++ C#/forSpbu/Game/Game.csproj | 10 +++ C#/forSpbu/Game/Program.cs | 16 +++++ C#/forSpbu/Game/map.txt | 11 +++ 5 files changed, 220 insertions(+) create mode 100644 C#/forSpbu/Game/EventLoop.cs create mode 100644 C#/forSpbu/Game/Game.cs create mode 100644 C#/forSpbu/Game/Game.csproj create mode 100644 C#/forSpbu/Game/Program.cs create mode 100644 C#/forSpbu/Game/map.txt diff --git a/C#/forSpbu/Game/EventLoop.cs b/C#/forSpbu/Game/EventLoop.cs new file mode 100644 index 0000000..c9fbafd --- /dev/null +++ b/C#/forSpbu/Game/EventLoop.cs @@ -0,0 +1,47 @@ +namespace Game; + +public class EventLoop +{ + public event EventHandler LeftHandler = (_, _) => { }; + public event EventHandler RightHandler = (_, _) => { }; + public event EventHandler UpHandler = (_, _) => { }; + public event EventHandler DownHandler = (_, _) => { }; + public event EventHandler EndHandler = (_, _) => { }; + public event EventHandler StartHandler = (_, _) => { }; + public event EventHandler LoopStartHandler = (_, _) => { }; + + public void Run() + { + var endCondition = false; + StartHandler(this, EventArgs.Empty); + while (true) + { + if (endCondition) + { + break; + } + + LoopStartHandler(this, EventArgs.Empty); + + switch (Console.ReadKey().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; + case ConsoleKey.Enter: + EndHandler(this, EventArgs.Empty); + endCondition = true; + break; + } + } + } +} \ No newline at end of file diff --git a/C#/forSpbu/Game/Game.cs b/C#/forSpbu/Game/Game.cs new file mode 100644 index 0000000..c2df9da --- /dev/null +++ b/C#/forSpbu/Game/Game.cs @@ -0,0 +1,136 @@ +using System.Security.Principal; + +namespace Game; + +public class Game +{ + private int _mapWidth; + private int _mapHeight; + private int _xPosition; + private int _yPosition; + private const string MapPath = "../../../map.txt"; + + private static void DrawMap() + { + Console.Write(File.ReadAllText(MapPath)); + } + + private bool CheckMap() + { + using var fileStream = new StreamReader(MapPath); + + var isLastLine = false; + while (!fileStream.EndOfStream) + { + if (isLastLine) + { + return false; + } + + var line = fileStream.ReadLine(); + if (string.IsNullOrEmpty(line)) + { + return false; + } + + _mapHeight++; + if (_mapWidth == 0) + { + _mapWidth = line.Length; + } + else if (_mapWidth != line.Length) + { + return false; + } + + if (_mapHeight != 1 && line[1] == '_') + { + isLastLine = true; + } + + for (var i = 1; i <= _mapWidth; i++) + { + var correctSymbol = GetCorrectSymbolByCoordinates(i, _mapHeight, isLastLine); + if (correctSymbol != line[i - 1]) + { + return false; + } + } + } + + _mapHeight -= 2; + _mapWidth -= 2; + return true; + } + + private char GetCorrectSymbolByCoordinates(int xCoordinate, int yCoordinate, bool isLastLine) + { + return !isLastLine + ? yCoordinate == 1 + ? (xCoordinate == 1 || xCoordinate == _mapWidth) + ? ' ' + : '_' + : (xCoordinate == 1 || xCoordinate == _mapWidth) + ? '|' + : ' ' + : (xCoordinate == 1 || xCoordinate == _mapWidth) + ? '|' + : '_'; + + } + + public void OnStart(object? sender, EventArgs args) + { + if (!CheckMap()) + { + throw new InvalidMapException(); + } + DrawMap(); + _xPosition = _mapWidth / 2; + _yPosition = _mapHeight / 2; + } + + public void OnLoopStart(object? sender, EventArgs args) + { + Console.SetCursorPosition(_xPosition, _yPosition); + Console.Write('@'); + Console.SetCursorPosition(_xPosition, _yPosition); + } + + public void OnLeft(object? sender, EventArgs args) + { + if (_xPosition > 1) + { + _xPosition--; + } + } + + public void OnRight(object? sender, EventArgs args) + { + if (_xPosition < _mapWidth) + { + _xPosition++; + } + } + + public void OnDown(object? sender, EventArgs args) + { + if (_yPosition < _mapHeight) + { + _yPosition++; + } + } + + public void OnUp(object? sender, EventArgs args) + { + if (_yPosition - 1 > 0) + { + _yPosition--; + } + } + + public static void OnEnd(object? sender, EventArgs args) + { + Console.Clear(); + } +} \ No newline at end of file diff --git a/C#/forSpbu/Game/Game.csproj b/C#/forSpbu/Game/Game.csproj new file mode 100644 index 0000000..2b14c81 --- /dev/null +++ b/C#/forSpbu/Game/Game.csproj @@ -0,0 +1,10 @@ + + + + Exe + net7.0 + enable + enable + + + diff --git a/C#/forSpbu/Game/Program.cs b/C#/forSpbu/Game/Program.cs new file mode 100644 index 0000000..04f50fd --- /dev/null +++ b/C#/forSpbu/Game/Program.cs @@ -0,0 +1,16 @@ +using Game; + +var eventLoop = new EventLoop(); +var game = new Game.Game(); + +eventLoop.StartHandler += game.OnStart; +eventLoop.EndHandler += Game.Game.OnEnd; + +eventLoop.LoopStartHandler += game.OnLoopStart; + +eventLoop.LeftHandler += game.OnLeft; +eventLoop.RightHandler += game.OnRight; +eventLoop.DownHandler += game.OnDown; +eventLoop.UpHandler += game.OnUp; + +eventLoop.Run(); \ No newline at end of file diff --git a/C#/forSpbu/Game/map.txt b/C#/forSpbu/Game/map.txt new file mode 100644 index 0000000..e910a2c --- /dev/null +++ b/C#/forSpbu/Game/map.txt @@ -0,0 +1,11 @@ + ____________________ +| | +| | +| | +| | +| | +| | +| | +| | +| | +|____________________| \ No newline at end of file From b0cf39396c53342fec069ef376920f3e31978459 Mon Sep 17 00:00:00 2001 From: IgnatSergeev/Desktop Date: Fri, 14 Apr 2023 01:54:59 +0300 Subject: [PATCH 2/5] Added custom exception --- C#/forSpbu/Game/CustomExceptions.cs | 5 +++++ C#/forSpbu/forSpbu.sln | 17 +++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 C#/forSpbu/Game/CustomExceptions.cs diff --git a/C#/forSpbu/Game/CustomExceptions.cs b/C#/forSpbu/Game/CustomExceptions.cs new file mode 100644 index 0000000..77ab3f8 --- /dev/null +++ b/C#/forSpbu/Game/CustomExceptions.cs @@ -0,0 +1,5 @@ +namespace Game; + +public class InvalidMapException : Exception +{ +} \ No newline at end of file diff --git a/C#/forSpbu/forSpbu.sln b/C#/forSpbu/forSpbu.sln index 527f400..684fea5 100644 --- a/C#/forSpbu/forSpbu.sln +++ b/C#/forSpbu/forSpbu.sln @@ -10,6 +10,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "03.03", "03.03", "{882A9B9C EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "10.03", "10.03", "{EA6FC7D9-BDFB-49CD-AC00-FC5DDC5274B0}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "31.03", "31.03", "{DF15DDE4-7F0B-4B93-BC7B-8815B1E966B6}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Game", "Game\Game.csproj", "{1239ED0E-09B4-44EB-96F6-4945F308E2AD}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -21,12 +25,17 @@ Global {E007586F-9760-4744-BB25-EDEFD6BA860C}.Release|Any CPU.ActiveCfg = Release|Any CPU {E007586F-9760-4744-BB25-EDEFD6BA860C}.Release|Any CPU.Build.0 = Release|Any CPU {A4F6ADD5-85FD-4F67-8B29-549DDDF6F82E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {A4F6ADD5-85FD-4F67-8B29-549DDDF6F82E}.Debug|Any CPU.Build.0 = Debug|Any CPU - {A4F6ADD5-85FD-4F67-8B29-549DDDF6F82E}.Release|Any CPU.ActiveCfg = Release|Any CPU - {A4F6ADD5-85FD-4F67-8B29-549DDDF6F82E}.Release|Any CPU.Build.0 = Release|Any CPU + {A4F6ADD5-85FD-4F67-8B29-549DDDF6F82E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A4F6ADD5-85FD-4F67-8B29-549DDDF6F82E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A4F6ADD5-85FD-4F67-8B29-549DDDF6F82E}.Release|Any CPU.Build.0 = Release|Any CPU + {1239ED0E-09B4-44EB-96F6-4945F308E2AD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1239ED0E-09B4-44EB-96F6-4945F308E2AD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1239ED0E-09B4-44EB-96F6-4945F308E2AD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1239ED0E-09B4-44EB-96F6-4945F308E2AD}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution - {E007586F-9760-4744-BB25-EDEFD6BA860C} = {D3FCB669-E93F-4F0B-B9C5-6592CE93AC7F} + {E007586F-9760-4744-BB25-EDEFD6BA860C} = {D3FCB669-E93F-4F0B-B9C5-6592CE93AC7F} {A4F6ADD5-85FD-4F67-8B29-549DDDF6F82E} = {D3FCB669-E93F-4F0B-B9C5-6592CE93AC7F} + {1239ED0E-09B4-44EB-96F6-4945F308E2AD} = {DF15DDE4-7F0B-4B93-BC7B-8815B1E966B6} EndGlobalSection EndGlobal From 0722f7359289db1a53b00d3161ca4547f8dceadc Mon Sep 17 00:00:00 2001 From: IgnatSergeev/Desktop Date: Wed, 19 Apr 2023 23:18:15 +0300 Subject: [PATCH 3/5] Remove unused using --- C#/forSpbu/Game/Game.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/C#/forSpbu/Game/Game.cs b/C#/forSpbu/Game/Game.cs index c2df9da..2051ac3 100644 --- a/C#/forSpbu/Game/Game.cs +++ b/C#/forSpbu/Game/Game.cs @@ -1,6 +1,4 @@ -using System.Security.Principal; - -namespace Game; +namespace Game; public class Game { From 6bc3e59374ce66f03b144fe47220bde02902a548 Mon Sep 17 00:00:00 2001 From: IgnatSergeev/laptop Date: Fri, 26 May 2023 17:41:35 +0300 Subject: [PATCH 4/5] Fixed game --- C#/forSpbu/Game/CustomExceptions.cs | 5 -- C#/forSpbu/Game/Game.cs | 96 ++++-------------------- C#/forSpbu/Game/IncorrectMapException.cs | 8 ++ C#/forSpbu/Game/Program.cs | 2 +- C#/forSpbu/Game/map.txt | 2 +- 5 files changed, 24 insertions(+), 89 deletions(-) delete mode 100644 C#/forSpbu/Game/CustomExceptions.cs create mode 100644 C#/forSpbu/Game/IncorrectMapException.cs diff --git a/C#/forSpbu/Game/CustomExceptions.cs b/C#/forSpbu/Game/CustomExceptions.cs deleted file mode 100644 index 77ab3f8..0000000 --- a/C#/forSpbu/Game/CustomExceptions.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace Game; - -public class InvalidMapException : Exception -{ -} \ No newline at end of file diff --git a/C#/forSpbu/Game/Game.cs b/C#/forSpbu/Game/Game.cs index 2051ac3..0354706 100644 --- a/C#/forSpbu/Game/Game.cs +++ b/C#/forSpbu/Game/Game.cs @@ -2,102 +2,34 @@ public class Game { - private int _mapWidth; - private int _mapHeight; + private Map _map; private int _xPosition; private int _yPosition; - private const string MapPath = "../../../map.txt"; - - private static void DrawMap() - { - Console.Write(File.ReadAllText(MapPath)); - } - private bool CheckMap() + public Game(string path) { - using var fileStream = new StreamReader(MapPath); - - var isLastLine = false; - while (!fileStream.EndOfStream) - { - if (isLastLine) - { - return false; - } - - var line = fileStream.ReadLine(); - if (string.IsNullOrEmpty(line)) - { - return false; - } - - _mapHeight++; - if (_mapWidth == 0) - { - _mapWidth = line.Length; - } - else if (_mapWidth != line.Length) - { - return false; - } - - if (_mapHeight != 1 && line[1] == '_') - { - isLastLine = true; - } - - for (var i = 1; i <= _mapWidth; i++) - { - var correctSymbol = GetCorrectSymbolByCoordinates(i, _mapHeight, isLastLine); - if (correctSymbol != line[i - 1]) - { - return false; - } - } - } - - _mapHeight -= 2; - _mapWidth -= 2; - return true; - } - - private char GetCorrectSymbolByCoordinates(int xCoordinate, int yCoordinate, bool isLastLine) - { - return !isLastLine - ? yCoordinate == 1 - ? (xCoordinate == 1 || xCoordinate == _mapWidth) - ? ' ' - : '_' - : (xCoordinate == 1 || xCoordinate == _mapWidth) - ? '|' - : ' ' - : (xCoordinate == 1 || xCoordinate == _mapWidth) - ? '|' - : '_'; - + _map = new Map(path); + _xPosition = _map._playerMapWidth / 2; + _yPosition = _map._playerMapHeight / 2; } public void OnStart(object? sender, EventArgs args) { - if (!CheckMap()) - { - throw new InvalidMapException(); - } - DrawMap(); - _xPosition = _mapWidth / 2; - _yPosition = _mapHeight / 2; + _map.DrawMap(); + Console.SetCursorPosition(0, 0); } public void OnLoopStart(object? sender, EventArgs args) { - Console.SetCursorPosition(_xPosition, _yPosition); + + Console.SetCursorPosition(_xPosition + 1, _yPosition + 1); Console.Write('@'); - Console.SetCursorPosition(_xPosition, _yPosition); + Console.SetCursorPosition(_xPosition + 1, _yPosition + 1); } public void OnLeft(object? sender, EventArgs args) { - if (_xPosition > 1) + if (_xPosition > 0 && _map._playerMap[_yPosition][_xPosition - 1]) { _xPosition--; } @@ -105,7 +37,7 @@ public void OnLeft(object? sender, EventArgs args) public void OnRight(object? sender, EventArgs args) { - if (_xPosition < _mapWidth) + if (_xPosition + 1 < _map._playerMapWidth && _map._playerMap[_yPosition][_xPosition + 1]) { _xPosition++; } @@ -113,7 +45,7 @@ public void OnRight(object? sender, EventArgs args) public void OnDown(object? sender, EventArgs args) { - if (_yPosition < _mapHeight) + if (_yPosition + 1 < _map._playerMapHeight && _map._playerMap[_yPosition + 1][_xPosition]) { _yPosition++; } @@ -121,7 +53,7 @@ public void OnDown(object? sender, EventArgs args) public void OnUp(object? sender, EventArgs args) { - if (_yPosition - 1 > 0) + if (_yPosition > 0 && _map._playerMap[_yPosition - 1][_xPosition]) { _yPosition--; } diff --git a/C#/forSpbu/Game/IncorrectMapException.cs b/C#/forSpbu/Game/IncorrectMapException.cs new file mode 100644 index 0000000..703233a --- /dev/null +++ b/C#/forSpbu/Game/IncorrectMapException.cs @@ -0,0 +1,8 @@ +namespace Game; + +public class IncorrectMapException : Exception +{ + public IncorrectMapException(string message) : base(message) + { + } +} \ No newline at end of file diff --git a/C#/forSpbu/Game/Program.cs b/C#/forSpbu/Game/Program.cs index 04f50fd..fe55e92 100644 --- a/C#/forSpbu/Game/Program.cs +++ b/C#/forSpbu/Game/Program.cs @@ -1,7 +1,7 @@ using Game; var eventLoop = new EventLoop(); -var game = new Game.Game(); +var game = new Game.Game("../../../map.txt"); eventLoop.StartHandler += game.OnStart; eventLoop.EndHandler += Game.Game.OnEnd; diff --git a/C#/forSpbu/Game/map.txt b/C#/forSpbu/Game/map.txt index e910a2c..c934f43 100644 --- a/C#/forSpbu/Game/map.txt +++ b/C#/forSpbu/Game/map.txt @@ -1,6 +1,6 @@ ____________________ | | -| | +| | | | | | | | | From 8fe87ece23479fb711f6c2f981f75e40f26c0204 Mon Sep 17 00:00:00 2001 From: IgnatSergeev/laptop Date: Fri, 26 May 2023 18:10:40 +0300 Subject: [PATCH 5/5] Fixed game --- C#/forSpbu/Game/EventLoop.cs | 7 ++---- C#/forSpbu/Game/Game.cs | 42 +++++++++++++++++++++--------------- C#/forSpbu/Game/Program.cs | 13 ++++++++--- 3 files changed, 37 insertions(+), 25 deletions(-) diff --git a/C#/forSpbu/Game/EventLoop.cs b/C#/forSpbu/Game/EventLoop.cs index c9fbafd..af8db25 100644 --- a/C#/forSpbu/Game/EventLoop.cs +++ b/C#/forSpbu/Game/EventLoop.cs @@ -8,7 +8,6 @@ public class EventLoop public event EventHandler DownHandler = (_, _) => { }; public event EventHandler EndHandler = (_, _) => { }; public event EventHandler StartHandler = (_, _) => { }; - public event EventHandler LoopStartHandler = (_, _) => { }; public void Run() { @@ -20,10 +19,8 @@ public void Run() { break; } - - LoopStartHandler(this, EventArgs.Empty); - - switch (Console.ReadKey().Key) + + switch (Console.ReadKey(true).Key) { case ConsoleKey.LeftArrow: LeftHandler(this, EventArgs.Empty); diff --git a/C#/forSpbu/Game/Game.cs b/C#/forSpbu/Game/Game.cs index 0354706..0df4441 100644 --- a/C#/forSpbu/Game/Game.cs +++ b/C#/forSpbu/Game/Game.cs @@ -1,16 +1,28 @@ -namespace Game; +using System.Security.Principal; + +namespace Game; public class Game { private Map _map; private int _xPosition; private int _yPosition; - - public Game(string path) + private Action _movePlayer; + + public enum Side + { + Right, + Left, + Up, + Down + } + + public Game(string path, Action movePlayer) { _map = new Map(path); - _xPosition = _map._playerMapWidth / 2; - _yPosition = _map._playerMapHeight / 2; + _xPosition = _map.PlayerMapWidth / 2; + _yPosition = _map.PlayerMapHeight / 2; + _movePlayer = movePlayer; } public void OnStart(object? sender, EventArgs args) @@ -18,43 +30,39 @@ public void OnStart(object? sender, EventArgs args) _map.DrawMap(); Console.SetCursorPosition(0, 0); } - - public void OnLoopStart(object? sender, EventArgs args) - { - - Console.SetCursorPosition(_xPosition + 1, _yPosition + 1); - Console.Write('@'); - Console.SetCursorPosition(_xPosition + 1, _yPosition + 1); - } public void OnLeft(object? sender, EventArgs args) { - if (_xPosition > 0 && _map._playerMap[_yPosition][_xPosition - 1]) + if (_xPosition > 0 && _map.PlayerMap[_yPosition][_xPosition - 1]) { + _movePlayer(_xPosition, _yPosition, _xPosition - 1, _yPosition); _xPosition--; } } public void OnRight(object? sender, EventArgs args) { - if (_xPosition + 1 < _map._playerMapWidth && _map._playerMap[_yPosition][_xPosition + 1]) + if (_xPosition + 1 < _map.PlayerMapWidth && _map.PlayerMap[_yPosition][_xPosition + 1]) { + _movePlayer(_xPosition, _yPosition, _xPosition + 1, _yPosition); _xPosition++; } } public void OnDown(object? sender, EventArgs args) { - if (_yPosition + 1 < _map._playerMapHeight && _map._playerMap[_yPosition + 1][_xPosition]) + if (_yPosition + 1 < _map.PlayerMapHeight && _map.PlayerMap[_yPosition + 1][_xPosition]) { + _movePlayer(_xPosition, _yPosition, _xPosition, _yPosition + 1); _yPosition++; } } public void OnUp(object? sender, EventArgs args) { - if (_yPosition > 0 && _map._playerMap[_yPosition - 1][_xPosition]) + if (_yPosition > 0 && _map.PlayerMap[_yPosition - 1][_xPosition]) { + _movePlayer(_xPosition, _yPosition, _xPosition, _yPosition - 1); _yPosition--; } } diff --git a/C#/forSpbu/Game/Program.cs b/C#/forSpbu/Game/Program.cs index fe55e92..78eb9c9 100644 --- a/C#/forSpbu/Game/Program.cs +++ b/C#/forSpbu/Game/Program.cs @@ -1,13 +1,20 @@ using Game; var eventLoop = new EventLoop(); -var game = new Game.Game("../../../map.txt"); +var game = new Game.Game("../../../map.txt", movePlayer); + +void movePlayer(int oldXPosition, int oldYPosition, int newXPosition, int newYPosition) { + Console.SetCursorPosition(oldXPosition + 1, oldYPosition + 1); + Console.Write(' '); + Console.SetCursorPosition(oldXPosition + 1, oldYPosition + 1); + Console.SetCursorPosition(newXPosition + 1, newYPosition + 1); + Console.Write('@'); + Console.SetCursorPosition(newXPosition + 1, newYPosition + 1); +} eventLoop.StartHandler += game.OnStart; eventLoop.EndHandler += Game.Game.OnEnd; -eventLoop.LoopStartHandler += game.OnLoopStart; - eventLoop.LeftHandler += game.OnLeft; eventLoop.RightHandler += game.OnRight; eventLoop.DownHandler += game.OnDown;