Skip to content
Open

Game #14

Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions Game/Game/Game.sln
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
47 changes: 47 additions & 0 deletions Game/Game/Game/EventLoop.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
namespace Game;
using System;
Comment on lines +1 to +3
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
namespace Game;
using System;
namespace Game;

using System;


public class EventLoop
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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))
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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;
}
}
}
}
}

79 changes: 79 additions & 0 deletions Game/Game/Game/Game.cs
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;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public Game(int initialPositionOnX, int initialPositionOnY, string pathToFile, Action <int, int> action)
public Game(int initialPositionOnX, int initialPositionOnY, string pathToFile, Action<int, int> action)

{
currentPositionOnX = initialPositionOnX;
currentPositionOnY = initialPositionOnY;
map = File.ReadAllLines(pathToFile);
this.action = action;
PrintMap(this.map);
action(currentPositionOnX, currentPositionOnY);
DrawCharacter();
}


private static void DrawCharacter()
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
private static void DrawCharacter()
}
private static void DrawCharacter()

{
Console.WriteLine("@");
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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));
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public void OnLeft(object? sender, EventArgs args)
{
Move((x, y) => (x - 1, y));
}
public void OnLeft(object? sender, EventArgs args)
=> Move((x, y) => (x - 1, y));

Это просто делегация, так что даже по смыслу надо писать через =>


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);
}
10 changes: 10 additions & 0 deletions Game/Game/Game/Game.csproj
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>
11 changes: 11 additions & 0 deletions Game/Game/Game/Game.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
+------------------------------------------------------------------+
| | |
| +-------------------+ +--------------------+ +---+ +----+
| | | | | | | |____|
+---+ +--+ | |
| | | | | | +--------+
| +-------------------+----+-----------+ +---------+ |
| | | |
+----------- +-------------------------------------+ +--------|
| |
+------------------------------------------------------------------+
17 changes: 17 additions & 0 deletions Game/Game/Game/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Game;

public class Program
{
static void Main(string[] args)
{
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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();
}
}

126 changes: 126 additions & 0 deletions Game/Game/GameTest/GameTest.cs
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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
}
}
}

21 changes: 21 additions & 0 deletions Game/Game/GameTest/GameTest.csproj
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>