Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
82 changes: 82 additions & 0 deletions hw6Game/hw6Game.Test/MoveTest.cs
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);
}
}
}
6 changes: 6 additions & 0 deletions hw6Game/hw6Game.Test/TestMap.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
|||||||||||||||
|| ||
|| @ ||
|| ||
|| ||
|||||||||||||||
19 changes: 19 additions & 0 deletions hw6Game/hw6Game.Test/hw6Game.Test.csproj
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>
31 changes: 31 additions & 0 deletions hw6Game/hw6Game.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 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
41 changes: 41 additions & 0 deletions hw6Game/hw6Game/EventLoop.cs
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

This comment was marked as resolved.

{
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.


public void Run()
Copy link
Collaborator

Choose a reason for hiding this comment

The 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;
}
}
}
}
}
78 changes: 78 additions & 0 deletions hw6Game/hw6Game/Game.cs
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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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('@');
}
}
}
}
81 changes: 81 additions & 0 deletions hw6Game/hw6Game/Map.cs
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] != '@')
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if (mapPic[coord.y][coord.x ] != ' ' && mapPic[coord.y][coord.x] != '@')
if (mapPic[coord.y][coord.x] != ' ' && mapPic[coord.y][coord.x] != '@')

{
return false;
}
player.x = coord.x;
player.y =coord.y;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
player.y =coord.y;
player.y = coord.y;

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;
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The 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);
}
}
18 changes: 18 additions & 0 deletions hw6Game/hw6Game/Map.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|| ||
|| ||
|| @ ||
|| ||
|| \\==== ====//================ ========== ||
|| \\ // || || || ||
|| \\ // || || || ||
|| \\ // || || || ||
|| \\ // || || || ||
|| \\ // || || || ||
|| \\// =========|| || || ||
|| || ||
|| || ||
|| ================================ ========== ||
|| ||
|| ||
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
19 changes: 19 additions & 0 deletions hw6Game/hw6Game/Program.cs
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();
}
}
}
8 changes: 8 additions & 0 deletions hw6Game/hw6Game/hw6Game.csproj
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>