-
Notifications
You must be signed in to change notification settings - Fork 0
Hw6 game #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Hw6 game #12
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| ||||||||||||||| | ||
| || || | ||
| || @ || | ||
| || || | ||
| || || | ||
| ||||||||||||||| |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net5.0</TargetFramework> | ||
|
|
||
| <IsPackable>false</IsPackable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="NUnit" Version="3.12.0" /> | ||
| <PackageReference Include="NUnit3TestAdapter" Version="3.16.1" /> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\hw6Game\Hw6Game.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| using System; | ||
|
|
||
| namespace Hw6Game | ||
| { | ||
| /// <summary> | ||
| /// class that generates events | ||
| /// </summary> | ||
| public class EventLoop | ||
| { | ||
| public event EventHandler<EventArgs> LeftHandler = (sender, args) => { }; | ||
|
|
||
| public event EventHandler<EventArgs> RightHandler = (sender, args) => { }; | ||
|
|
||
| public event EventHandler<EventArgs> UpHandler = (sender, args) => { }; | ||
|
|
||
| public event EventHandler<EventArgs> DownHandler = (sender, args) => { }; | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
|
|
||
| public void Run() | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Комментарии к методам и даже событиям тоже нужны, но ладно |
||
| { | ||
| 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; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Hw6Game | ||
| { | ||
| /// <summary> | ||
| /// class that handles events | ||
| /// </summary> | ||
| public class Game | ||
| { | ||
| /// <summary> | ||
| /// enums for moves | ||
| /// </summary> | ||
| public enum Moves | ||
| { | ||
| left, | ||
| right, | ||
| up, | ||
| down | ||
|
Comment on lines
+19
to
+22
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Элементы enum-а в .NET пишутся с заглавной |
||
| } | ||
|
|
||
| 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); | ||
| } | ||
|
Comment on lines
+35
to
+53
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Однострочники пишутся через => |
||
|
|
||
| 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('@'); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,81 @@ | ||||||
| using System; | ||||||
| using System.IO; | ||||||
|
|
||||||
| namespace Hw6Game | ||||||
| { | ||||||
| /// <summary> | ||||||
| /// class working on building the map | ||||||
| /// </summary> | ||||||
| public class Map | ||||||
| { | ||||||
| private struct PlayerCoordinates | ||||||
| { | ||||||
| public int x; | ||||||
| public int y; | ||||||
| } | ||||||
|
|
||||||
| private string[] mapPic; | ||||||
|
|
||||||
| static private PlayerCoordinates player = new (); | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// create a map | ||||||
| /// </summary> | ||||||
| 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] != '@') | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| { | ||||||
| return false; | ||||||
| } | ||||||
| player.x = coord.x; | ||||||
| player.y =coord.y; | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| return true; | ||||||
| } | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// Move player | ||||||
| /// </summary> | ||||||
| /// <param name="step">step direction</param> | ||||||
| /// <returns>true if step is complete</returns> | ||||||
| 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; | ||||||
| } | ||||||
| } | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. expression switch? |
||||||
|
|
||||||
| /// <summary> | ||||||
| /// return player coordinates | ||||||
| /// </summary> | ||||||
| /// <returns>coordinates (x, y)</returns> | ||||||
| public (int, int) GetPlayerCoordinates() | ||||||
| => (player.x, player.y); | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| | ||
| || || | ||
| || || | ||
| || @ || | ||
| || || | ||
| || \\==== ====//================ ========== || | ||
| || \\ // || || || || | ||
| || \\ // || || || || | ||
| || \\ // || || || || | ||
| || \\ // || || || || | ||
| || \\ // || || || || | ||
| || \\// =========|| || || || | ||
| || || || | ||
| || || || | ||
| || ================================ ========== || | ||
| || || | ||
| || || | ||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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(); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net5.0</TargetFramework> | ||
| </PropertyGroup> | ||
|
|
||
| </Project> |
This comment was marked as resolved.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.