Skip to content
Open

Game #13

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
31 changes: 31 additions & 0 deletions 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.4.33403.182
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Game", "Game\Game.csproj", "{9B96D5C7-8B64-463D-9AA8-E25AA17831C8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestsForGame", "TestsForGame\TestsForGame.csproj", "{8F9ECC7B-3D85-4839-930E-90A426CD0E34}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{9B96D5C7-8B64-463D-9AA8-E25AA17831C8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9B96D5C7-8B64-463D-9AA8-E25AA17831C8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9B96D5C7-8B64-463D-9AA8-E25AA17831C8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9B96D5C7-8B64-463D-9AA8-E25AA17831C8}.Release|Any CPU.Build.0 = Release|Any CPU
{8F9ECC7B-3D85-4839-930E-90A426CD0E34}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8F9ECC7B-3D85-4839-930E-90A426CD0E34}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8F9ECC7B-3D85-4839-930E-90A426CD0E34}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8F9ECC7B-3D85-4839-930E-90A426CD0E34}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {44167783-B5A0-436E-A141-633477AFAD18}
EndGlobalSection
EndGlobal
117 changes: 117 additions & 0 deletions Game/Game/EventLoop.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
namespace Game;

public delegate void ArrowHandler(int startPositionLeft, int startPositionTop, WorkWithConsole data, ref List<((int, int), char)> forTests);

Choose a reason for hiding this comment

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

Обожечтоэто.

  • Делегаты не нужны, используйте Action
  • Код специально для тестов в боевом коде никто не пишет (без крайней нужды, а тут нет крайней нужды)
  • Как-то это суперсложно, этот делегад вообще должен быть без параметров


/// <summary>
/// Event handler implementation class
/// </summary>
public static class EventLoop
{
/// <summary>
/// Event Handler
/// </summary>
/// <param name="left">Function for moving to the left</param>
/// <param name="right">Function for moving to the right</param>
/// <param name="up">Function for moving to the up</param>
/// <param name="down">Function for moving to the down</param>
/// <param name="fileWithMap">File with map</param>
/// <param name="data">The selected type for testing or normal operation</param>
/// <param name="forTests">A list intended for entering results for tests ONLY</param>
/// <param name="listKeys">List of commands for tests ONLY</param>
/// <exception cref="InvalidMapException">Incorrectly set map</exception>
/// <exception cref="NullReferenceException">Checking that the read card line is not empty</exception>
public static void Run(ArrowHandler left, ArrowHandler right, ArrowHandler up, ArrowHandler down,
string fileWithMap, WorkWithConsole data, ref List<((int, int), char)> forTests, List<Char> listKeys = null)

Choose a reason for hiding this comment

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

Предполагалось, что тут будут использоваться события, а не явная передача делегатов, как в 2005-м году

Choose a reason for hiding this comment

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

И nullability надо поправить

{
int length = 0;
int width = 0;
var file = new StreamReader(fileWithMap);

Choose a reason for hiding this comment

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

EventLoop не должен ничего знать про карту

Choose a reason for hiding this comment

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

И using надо

int sizeAtSymbols = 0;
int sizeSpaces = 0;
bool isFirst = true;
while (!file.EndOfStream)
{
if (!isFirst)
{
if (width == 1 && sizeAtSymbols != length)
{
throw new InvalidMapException();
}
else if (width != 1 && sizeAtSymbols != 2 || sizeSpaces != length - sizeAtSymbols)
{
throw new InvalidMapException();
}
}
var line = file.ReadLine();
++width;
if (line == null)
{
throw new ArgumentException();
}
int size = line.Count(x => x == '@');
if (length != 0 && length != line.Length)
{
throw new InvalidMapException();
}
length = line.Length;
sizeAtSymbols = line.Count(x => x == '@');
sizeSpaces = line.Count(x => x == ' ');
isFirst = false;
Console.WriteLine(line);
}
if (sizeAtSymbols != length)
{
throw new InvalidMapException();
}

if (width < 3 || length < 3)
{
throw new InvalidMapException();
}
data.SetCursorPosition(length / 2, width / 2, ref forTests);
data.Print('@', ref forTests);
int startPositionLeft = length / 2;
int startPositionTop = width / 2;
data.SetCursorPosition(startPositionLeft, startPositionTop, ref forTests);
var checkType = new PrintInList();
while (true)
{
if (data.GetType() == checkType.GetType() && listKeys.Count == 0)
{
return;
}
var key = data.Comparison(listKeys);
switch (key)
{
case "left":
if (startPositionLeft != 1)
{
left(startPositionLeft, startPositionTop, data, ref forTests);
--startPositionLeft;
}
break;
case "right":
if (startPositionLeft != length - 2)
{
right(startPositionLeft, startPositionTop, data, ref forTests);
++startPositionLeft;
}
break;
case "up":
if (startPositionTop != 1)
{
up(startPositionLeft, startPositionTop, data, ref forTests);
--startPositionTop;
}
break;
case "down":
if (startPositionTop != width - 2)
{
down(startPositionLeft, startPositionTop, data, ref forTests);
++startPositionTop;
}
break;
}
}
}
}
67 changes: 67 additions & 0 deletions Game/Game/Game.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
namespace Game;

/// <summary>
/// A class that implements actions in the game
/// </summary>
public static class Game
{
/// <summary>
/// Move to the Left
/// </summary>
/// <param name="startPositionLeft">X coordinate</param>
/// <param name="startPositionTop">Y coordinate</param>
/// <param name="data">Selected type: testing or normal launch</param>
/// <param name="forTests">The list of entering results for tests ONLY</param>
public static void OnLeft(int startPositionLeft, int startPositionTop, WorkWithConsole data, ref List<((int, int), char)> forTests)
{
data.Print(' ', ref forTests);
data.SetCursorPosition(startPositionLeft - 1, startPositionTop, ref forTests);
data.Print('@', ref forTests);
data.SetCursorPosition(startPositionLeft - 1, startPositionTop, ref forTests);
}

/// <summary>
/// Move to the Right
/// </summary>
/// <param name="startPositionLeft">X coordinate</param>
/// <param name="startPositionTop">Y coordinate</param>
/// <param name="data">Selected type: testing or normal launch</param>
/// <param name="forTests">The list of entering results for tests ONLY</param>
public static void OnRight(int startPositionLeft, int startPositionTop, WorkWithConsole data, ref List<((int, int), char)> forTests)
{
data.Print(' ', ref forTests);
data.SetCursorPosition(startPositionLeft + 1, startPositionTop, ref forTests);
data.Print('@', ref forTests);
data.SetCursorPosition(startPositionLeft + 1, startPositionTop, ref forTests);
}

/// <summary>
/// Move to the Up
/// </summary>
/// <param name="startPositionLeft">X coordinate</param>
/// <param name="startPositionTop">Y coordinate</param>
/// <param name="data">Selected type: testing or normal launch</param>
/// <param name="forTests">The list of entering results for tests ONLY</param>
public static void OnUp(int startPositionLeft, int startPositionTop, WorkWithConsole data, ref List<((int, int), char)> forTests)
{
data.Print(' ', ref forTests);
data.SetCursorPosition(startPositionLeft, startPositionTop - 1, ref forTests);
data.Print('@', ref forTests);
data.SetCursorPosition(startPositionLeft, startPositionTop - 1, ref forTests);
}

/// <summary>
/// Move to the Down
/// </summary>
/// <param name="startPositionLeft">X coordinate</param>
/// <param name="startPositionTop">Y coordinate</param>
/// <param name="data">Selected type: testing or normal launch</param>
/// <param name="forTests">The list of entering results for tests ONLY</param>
public static void OnDown(int startPositionLeft, int startPositionTop, WorkWithConsole data, ref List<((int, int), char)> forTests)
{
data.Print(' ', ref forTests);
data.SetCursorPosition(startPositionLeft, startPositionTop + 1, ref forTests);
data.Print('@', ref forTests);
data.SetCursorPosition(startPositionLeft, startPositionTop + 1, ref forTests);
}

Choose a reason for hiding this comment

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

Копипаст суть ересь

}
10 changes: 10 additions & 0 deletions 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>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
29 changes: 29 additions & 0 deletions Game/Game/Interface.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace Game;

/// <summary>
/// The interface for the game, namely for tests and normal startup
/// </summary>
interface GameInterface
{
/// <summary>
/// For a normal launch, it prints to the console for tests, it is included in a special list
/// </summary>
/// <param name="symbol">Symbol for printing</param>
/// <param name="forTests">The list is necessary ONLY for tests</param>
void Print(char symbol, ref List<((int, int), char)> forTests);

/// <summary>
/// For normal work, it puts the cursor in the console at the specified coordinates, for tests it fixes cursor changes in a special list
/// </summary>
/// <param name="positionLeft">X coordinate</param>
/// <param name="PositionTop">Y coordinates</param>
/// <param name="forTests">The list is for tests ONLY changes are being made here</param>
void SetCursorPosition(int positionLeft, int PositionTop, ref List<((int, int), char)> forTests);

/// <summary>
/// Different comparison for tests and for a normal run
/// </summary>
/// <param name="listKeys">List of commands for tests only</param>
/// <returns>Returns a string: left up right down</returns>
string Comparison(List<char> listKeys = null);
}
6 changes: 6 additions & 0 deletions Game/Game/InvalidMapException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Game;

/// <summary>
/// Throws exception then incorrect input map
/// </summary>
public class InvalidMapException : Exception {}

Choose a reason for hiding this comment

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

Надо комментарий

34 changes: 34 additions & 0 deletions Game/Game/PrintInConsole.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace Game;

/// <summary>
/// Implements an interface for tests
/// </summary>
public class PrintInConsole : WorkWithConsole
{
public override void Print(char symbol, ref List<((int, int), char)> forTests)
{
Console.Write(symbol);
}

public override void SetCursorPosition(int positionLeft, int PositionTop, ref List<((int, int), char)> forTests)
{
Console.SetCursorPosition(positionLeft, PositionTop);
}

public override string Comparison(List<char> listKeys = null)
{
var key = Console.ReadKey(true);
switch (key.Key)
{
case ConsoleKey.LeftArrow:
return "left";
case ConsoleKey.RightArrow:
return "right";
case ConsoleKey.UpArrow:
return "up";
case ConsoleKey.DownArrow:
return "down";
}
return "another";
}
}
48 changes: 48 additions & 0 deletions Game/Game/PrintInList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
namespace Game;

/// <summary>
/// Implements the interface for the console
/// </summary>
public class PrintInList:WorkWithConsole
{
public override void Print(char symbol, ref List<((int, int), char)> forTests)
{
if (forTests == null)
{
throw new NullReferenceException();
}
forTests.Add(((-1, -1), symbol));
}

public override void SetCursorPosition(int positionLeft, int PositionTop, ref List<((int, int), char)> forTests)
{
if (forTests == null)
{
throw new NullReferenceException();
}
forTests.Add(((positionLeft, PositionTop), '\0'));
}

public override string Comparison(List<char> listKeys = null)
{
if (listKeys == null)
{
throw new NullReferenceException();
}
var key = listKeys.First();
listKeys.RemoveAt(0);
switch (key)
{
case '%':
return "left";
case '&':
return "up";
case '\'':
return "right";
case '(':
return "down";
}
return "another";

}
}
33 changes: 33 additions & 0 deletions Game/Game/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace Game;

public class Program
{
public static void Main()

Choose a reason for hiding this comment

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

Это не нужно

{
char symbol = (char)38;
Console.WriteLine("Input your string with file path");
var filePath = Console.ReadLine();
Console.Clear();
List<((int, int), char)> list = new List<((int, int), char)>();
try
{
EventLoop.Run(
new ArrowHandler(Game.OnLeft),
new ArrowHandler(Game.OnRight),
new ArrowHandler(Game.OnUp),
new ArrowHandler(Game.OnDown),
filePath,
new PrintInConsole(),
ref list
);
}
catch (InvalidMapException)
{
Console.WriteLine("Incorrect map");
}
catch (NullReferenceException)
{
Console.WriteLine("Problems with file or reading lines in file");
}
}
}
Loading