-
Notifications
You must be signed in to change notification settings - Fork 0
Game #14
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: main
Are you sure you want to change the base?
Game #14
Changes from 2 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,31 @@ | ||
| | ||
| Microsoft Visual Studio Solution File, Format Version 12.00 | ||
| # Visual Studio Version 17 | ||
| VisualStudioVersion = 17.1.32228.430 | ||
| MinimumVisualStudioVersion = 10.0.40219.1 | ||
| Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Game", "Game\Game.csproj", "{5C94A5A6-4A89-4028-87EB-872F3053657A}" | ||
| EndProject | ||
| Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GameTest", "GameTest\GameTest.csproj", "{194D762F-14D0-4A7D-993D-FB324AAC0E1D}" | ||
| EndProject | ||
| Global | ||
| GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
| Debug|Any CPU = Debug|Any CPU | ||
| Release|Any CPU = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
| {5C94A5A6-4A89-4028-87EB-872F3053657A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {5C94A5A6-4A89-4028-87EB-872F3053657A}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {5C94A5A6-4A89-4028-87EB-872F3053657A}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {5C94A5A6-4A89-4028-87EB-872F3053657A}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| {194D762F-14D0-4A7D-993D-FB324AAC0E1D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {194D762F-14D0-4A7D-993D-FB324AAC0E1D}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {194D762F-14D0-4A7D-993D-FB324AAC0E1D}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {194D762F-14D0-4A7D-993D-FB324AAC0E1D}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(SolutionProperties) = preSolution | ||
| HideSolutionNode = FALSE | ||
| EndGlobalSection | ||
| GlobalSection(ExtensibilityGlobals) = postSolution | ||
| SolutionGuid = {C9167AF3-096A-436F-9070-46CD6A12D020} | ||
| EndGlobalSection | ||
| EndGlobal |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| namespace Game; | ||
| using System; | ||
|
|
||
| public class EventLoop | ||
|
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. Надо комментарии |
||
| { | ||
| 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) => { }; | ||
|
|
||
| public void Run() | ||
| { | ||
| // Magic 68 and 4 are the coordinates of the point you need to reach in order to win | ||
| while (Console.GetCursorPosition() != (68, 4)) | ||
|
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. Это часть игровой логики, про которую EventLoop как раз не имеет права знать |
||
| { | ||
| 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; | ||
| } | ||
| case ConsoleKey.Escape: | ||
| { | ||
| return; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,79 @@ | ||||||||||||||||
| namespace Game; | ||||||||||||||||
|
|
||||||||||||||||
| public class Game | ||||||||||||||||
| { | ||||||||||||||||
| private readonly Action<int, int> action; | ||||||||||||||||
|
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. Это далеко не лучшее имя поля и параметра конструктора. action что? :) |
||||||||||||||||
| private int currentPositionOnX; | ||||||||||||||||
| private int currentPositionOnY; | ||||||||||||||||
| private readonly string[] map; | ||||||||||||||||
|
|
||||||||||||||||
| public Game(int initialPositionOnX, int initialPositionOnY, string pathToFile, Action <int, int> action) | ||||||||||||||||
|
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
|
||||||||||||||||
| { | ||||||||||||||||
| currentPositionOnX = initialPositionOnX; | ||||||||||||||||
| currentPositionOnY = initialPositionOnY; | ||||||||||||||||
| map = File.ReadAllLines(pathToFile); | ||||||||||||||||
| this.action = action; | ||||||||||||||||
| PrintMap(this.map); | ||||||||||||||||
| action(currentPositionOnX, currentPositionOnY); | ||||||||||||||||
| DrawCharacter(); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
| private static void DrawCharacter() | ||||||||||||||||
|
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
|
||||||||||||||||
| { | ||||||||||||||||
| Console.WriteLine("@"); | ||||||||||||||||
| } | ||||||||||||||||
|
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 static void PrintMap(string[] map) | ||||||||||||||||
| { | ||||||||||||||||
| for (int i = 0; i < map.Length; i++) | ||||||||||||||||
| { | ||||||||||||||||
| for (int j = 0; j < map[i].Length; j++) | ||||||||||||||||
| { | ||||||||||||||||
| Console.Write(map[i][j]); | ||||||||||||||||
| } | ||||||||||||||||
| Console.WriteLine(); | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| private static bool IsWall(char x) => x == '|' || x == '+' || x == '-' || x == '_'; | ||||||||||||||||
|
|
||||||||||||||||
| public void Move(Func<int, int, (int, int)> func) | ||||||||||||||||
|
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. Тут тоже, стоило бы именовать функцию так, чтобы это как-то отражало её назначение |
||||||||||||||||
| { | ||||||||||||||||
| var (newPositionOnX, newPositionOnY) = func(currentPositionOnX, currentPositionOnY); | ||||||||||||||||
| action(newPositionOnX, newPositionOnY); | ||||||||||||||||
| if (IsWall(map[newPositionOnY][newPositionOnX])) | ||||||||||||||||
| { | ||||||||||||||||
| action(currentPositionOnX, currentPositionOnY); | ||||||||||||||||
| return; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| Console.Write("@"); | ||||||||||||||||
| action(currentPositionOnX, currentPositionOnY); | ||||||||||||||||
| Console.WriteLine(" "); | ||||||||||||||||
| action(newPositionOnX, newPositionOnY); | ||||||||||||||||
| (currentPositionOnX, currentPositionOnY) = (newPositionOnX, newPositionOnY); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| public void OnLeft(object? sender, EventArgs args) | ||||||||||||||||
| { | ||||||||||||||||
| Move((x, y) => (x - 1, y)); | ||||||||||||||||
| } | ||||||||||||||||
|
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
Это просто делегация, так что даже по смыслу надо писать через |
||||||||||||||||
|
|
||||||||||||||||
| public void OnRight(object? sender, EventArgs args) | ||||||||||||||||
| { | ||||||||||||||||
| Move((x, y) => (x + 1, y)); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| public void Up(object? sender, EventArgs args) | ||||||||||||||||
| { | ||||||||||||||||
| Move((x, y) => (x, y - 1)); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| public void Down(object? sender, EventArgs args) | ||||||||||||||||
| { | ||||||||||||||||
| Move((x, y) => (x, y + 1)); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| public (int, int) PlayerPosition() => (currentPositionOnX, currentPositionOnY); | ||||||||||||||||
| } | ||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net6.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| +------------------------------------------------------------------+ | ||
| | | | | ||
| | +-------------------+ +--------------------+ +---+ +----+ | ||
| | | | | | | | |____| | ||
| +---+ +--+ | | | ||
| | | | | | | +--------+ | ||
| | +-------------------+----+-----------+ +---------+ | | ||
| | | | | | ||
| +----------- +-------------------------------------+ +--------| | ||
| | | | ||
| +------------------------------------------------------------------+ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| namespace Game; | ||
|
|
||
| public class Program | ||
| { | ||
| static void Main(string[] args) | ||
| { | ||
|
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. Это ненужный с появлением topp-level-операторов код |
||
| var eventLoop = new EventLoop(); | ||
|
|
||
| var game = new Game(2, 9, "..//..//..//Game.txt", Console.SetCursorPosition); | ||
| eventLoop.LeftHandler += game.OnLeft; | ||
| eventLoop.RightHandler += game.OnRight; | ||
| eventLoop.UpHandler += game.Up; | ||
| eventLoop.DownHandler += game.Down; | ||
| eventLoop.Run(); | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,126 @@ | ||||||||||||||||||
| namespace GameTest; | ||||||||||||||||||
|
|
||||||||||||||||||
| using NUnit.Framework; | ||||||||||||||||||
| using System.IO; | ||||||||||||||||||
| using System; | ||||||||||||||||||
|
|
||||||||||||||||||
| public class Tests | ||||||||||||||||||
| { | ||||||||||||||||||
| private Game.Game game = new(0 ,0, "..//..//..//..//Game//Game.txt", (x, y) => { } ); | ||||||||||||||||||
|
|
||||||||||||||||||
| [SetUp] | ||||||||||||||||||
| public void Setup() | ||||||||||||||||||
| { | ||||||||||||||||||
| game = new Game.Game(2, 9, "..//..//..//..//Game//Game.txt", (x, y) => { }); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // It is known in advance that there is no wall on the left in the position | ||||||||||||||||||
| [Test] | ||||||||||||||||||
| public void ShouldPlayerPositionOnXEqualPreviousPositionOnXMinus1WhenOnLeft() | ||||||||||||||||||
| { | ||||||||||||||||||
| var (x, y) = game.PlayerPosition(); | ||||||||||||||||||
| game.OnLeft(this, EventArgs.Empty); | ||||||||||||||||||
| var (z, t) = game.PlayerPosition(); | ||||||||||||||||||
| Assert.AreEqual(z, x - 1); | ||||||||||||||||||
| Assert.AreEqual(t, y); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // It is known in advance that there is no wall on the right in the position | ||||||||||||||||||
| [Test] | ||||||||||||||||||
| public void ShouldPlayerPositionOnXEqualPreviousPositionOnXPlus1WhenOnRight() | ||||||||||||||||||
| { | ||||||||||||||||||
| var (x, y) = game.PlayerPosition(); | ||||||||||||||||||
| game.OnRight(this, EventArgs.Empty); | ||||||||||||||||||
| var (z, t) = game.PlayerPosition(); | ||||||||||||||||||
| Assert.AreEqual(z, x + 1); | ||||||||||||||||||
| Assert.AreEqual(t, y); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // It is known in advance that there is no wall on the up in the position | ||||||||||||||||||
| [Test] | ||||||||||||||||||
| public void ShouldPlayerPositionOnYEqualPreviousPositionOnYMinus1WhenUp() | ||||||||||||||||||
| { | ||||||||||||||||||
| for (int i = 0; i < 11; i++) | ||||||||||||||||||
| { | ||||||||||||||||||
| game.OnRight(this, EventArgs.Empty); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| var (x, y) = game.PlayerPosition(); | ||||||||||||||||||
| game.Up(this, EventArgs.Empty); | ||||||||||||||||||
| var (z, t) = game.PlayerPosition(); | ||||||||||||||||||
| Assert.AreEqual(z, x); | ||||||||||||||||||
| Assert.AreEqual(t, y - 1); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // It is known in advance that there is no wall on the down in the position | ||||||||||||||||||
| [Test] | ||||||||||||||||||
| public void ShouldPlayerPositionOnYEqualPreviousPositionOnYPlus1WhenDown() | ||||||||||||||||||
| { | ||||||||||||||||||
| for (int i = 0; i < 11; i++) | ||||||||||||||||||
| { | ||||||||||||||||||
| game.OnRight(this, EventArgs.Empty); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| game.Up(this, EventArgs.Empty); | ||||||||||||||||||
| var (x, y) = game.PlayerPosition(); | ||||||||||||||||||
| game.Down(this, EventArgs.Empty); | ||||||||||||||||||
| var (z, t) = game.PlayerPosition(); | ||||||||||||||||||
| Assert.AreEqual(z, x); | ||||||||||||||||||
| Assert.AreEqual(t, y + 1); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| // It is known in advance that in the current position there will be a wall on the left | ||||||||||||||||||
| [Test] | ||||||||||||||||||
| public void ShouldPlayerPositionOnXEqualPreviousPositionOnXWhenOnLeft() | ||||||||||||||||||
| { | ||||||||||||||||||
| game.OnLeft(this, EventArgs.Empty); | ||||||||||||||||||
| var (x, y) = game.PlayerPosition(); | ||||||||||||||||||
| game.OnLeft(this, EventArgs.Empty); | ||||||||||||||||||
| var (z, t) = game.PlayerPosition(); | ||||||||||||||||||
| Assert.AreEqual(z, x); | ||||||||||||||||||
| Assert.AreEqual(t, y); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // It is known in advance that in the current position there will be a wall on the right | ||||||||||||||||||
| [Test] | ||||||||||||||||||
| public void ShouldPlayerPositionOnXEqualPreviousPositionOnXWhenOnRight() | ||||||||||||||||||
| { | ||||||||||||||||||
| for (int i = 0; i < 65; i ++) | ||||||||||||||||||
| { | ||||||||||||||||||
| game.OnRight(this, EventArgs.Empty); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| var (x, y) = game.PlayerPosition(); | ||||||||||||||||||
| game.OnRight(this, EventArgs.Empty); | ||||||||||||||||||
| var (z, t) = game.PlayerPosition(); | ||||||||||||||||||
| Assert.AreEqual(z, x); | ||||||||||||||||||
| Assert.AreEqual(t, y); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // It is known in advance that in the current position there will be a wall on the up | ||||||||||||||||||
| [Test] | ||||||||||||||||||
| public void ShouldPlayerPositionOnXEqualPreviousPositionOnXWhenUp() | ||||||||||||||||||
| { | ||||||||||||||||||
| var (x, y) = game.PlayerPosition(); | ||||||||||||||||||
| game.Up(this, EventArgs.Empty); | ||||||||||||||||||
| var (z, t) = game.PlayerPosition(); | ||||||||||||||||||
| Assert.AreEqual(z, x); | ||||||||||||||||||
| Assert.AreEqual(t, y); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // It is known in advance that in the current position there will be a wall on the down | ||||||||||||||||||
| [Test] | ||||||||||||||||||
| public void ShouldPlayerPositionOnXEqualPreviousPositionOnXWhenDown() | ||||||||||||||||||
| { | ||||||||||||||||||
| var (x, y) = game.PlayerPosition(); | ||||||||||||||||||
| game.Down(this, EventArgs.Empty); | ||||||||||||||||||
| var (z, t) = game.PlayerPosition(); | ||||||||||||||||||
| Assert.AreEqual(z, x); | ||||||||||||||||||
| Assert.AreEqual(t, y); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| } | ||||||||||||||||||
|
Comment on lines
+121
to
+138
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
|
||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net6.0</TargetFramework> | ||
| <Nullable>enable</Nullable> | ||
|
|
||
| <IsPackable>false</IsPackable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" /> | ||
| <PackageReference Include="NUnit" Version="3.13.2" /> | ||
| <PackageReference Include="NUnit3TestAdapter" Version="4.0.0" /> | ||
| <PackageReference Include="coverlet.collector" Version="3.1.0" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\Game\Game.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.