Skip to content
Open

Game #14

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
21 changes: 19 additions & 2 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: Build

on: [push, pull_request]
on: [push]

jobs:
build:
build-Windows:

runs-on: windows-latest

Expand All @@ -20,4 +20,21 @@ jobs:
- name: Test
run: $slnList = Get-ChildItem $foo.FullName -Recurse -Filter '*.sln'; foreach ($file in $slnList) {dotnet test $file.FullName}

build-Ubuntu:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Build
uses: actions/setup-dotnet@v1
with:
dotnet-version: '6.x'
- name: Restore
run: for f in $(find . -name "*.sln"); do dotnet restore $f; done
- name: Build
run: for f in $(find . -name "*.sln"); do dotnet build $f; done
- name: Test
run: for f in $(find . -name "*.sln"); do dotnet test $f; done


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
69 changes: 69 additions & 0 deletions Game/Game/Game/EventLoop.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
namespace Game;

using System;

/// <summary>
/// A class representing an event handler loop
/// </summary>
public class EventLoop

Choose a reason for hiding this comment

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

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

{
/// <summary>
/// Handler for the left move event
/// </summary>
public event EventHandler<EventArgs> LeftHandler = (sender, args) => { };

/// <summary>
/// Handler for the right move event
/// </summary>
public event EventHandler<EventArgs> RightHandler = (sender, args) => { };

/// <summary>
/// Handler for the up move event
/// </summary>
public event EventHandler<EventArgs> UpHandler = (sender, args) => { };

/// <summary>
/// Handler for the down move event
/// </summary>
public event EventHandler<EventArgs> DownHandler = (sender, args) => { };

/// <summary>
/// Event handler loop
/// </summary>
/// <param name="EndOfСycleСondition">Delegate loop exit condition</param>
public void Run(Func<bool> EndOfСycleСondition)
{
while (!EndOfСycleСondition())
{
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;
}
}
}
}
}

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

/// <summary>
/// The class responsible for the logic of the game
/// </summary>
public class Game
{
private readonly Action<int, int> controlFunction;
private int currentPositionOnX;
private int currentPositionOnY;
private readonly string[] map;

/// <summary>
/// Сonstructor of Game class
/// </summary>
/// <param name="initialPositionOnX">Initial position on x</param>
/// <param name="initialPositionOnY">Initial position on y</param>
/// <param name="pathToFile">Path to file</param>
/// <param name="action">Control function</param>
public Game(int initialPositionOnX, int initialPositionOnY, string pathToFile, Action <int, int> action)

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.controlFunction = action;
PrintMap(this.map);
action(currentPositionOnX, currentPositionOnY);
DrawCharacter();
}

private static void DrawCharacter() => Console.WriteLine("@");

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 == '_';

private void ChangePlayerPosition(Func<int, int, (int, int)> func)
{
var (newPositionOnX, newPositionOnY) = func(currentPositionOnX, currentPositionOnY);
controlFunction(newPositionOnX, newPositionOnY);
if (IsWall(map[newPositionOnY][newPositionOnX]))
{
controlFunction(currentPositionOnX, currentPositionOnY);
return;
}

Console.Write("@");
controlFunction(currentPositionOnX, currentPositionOnY);
Console.WriteLine(" ");
controlFunction(newPositionOnX, newPositionOnY);
(currentPositionOnX, currentPositionOnY) = (newPositionOnX, newPositionOnY);
}

/// <summary>
/// Function for changing the player's position one step to the left
/// </summary>
public void OnLeft(object? sender, EventArgs args) => ChangePlayerPosition((x, y) => (x - 1, y));

/// <summary>
/// Function for changing the player's position one step to the right
/// </summary>
public void OnRight(object? sender, EventArgs args) => ChangePlayerPosition((x, y) => (x + 1, y));

/// <summary>
/// Function for changing the player's position one step up
/// </summary>
public void Up(object? sender, EventArgs args) => ChangePlayerPosition((x, y) => (x, y - 1));

/// <summary>
/// Function for changing the player's position one step down
/// </summary>
public void Down(object? sender, EventArgs args) => ChangePlayerPosition((x, y) => (x, y + 1));

/// <summary>
/// Function for getting the player's position
/// </summary>
/// <returns>Player position</returns>
public (int, int) PlayerPosition() => (currentPositionOnX, currentPositionOnY);

/// <summary>
/// A function that determines whether the game is completed or not
/// </summary>
/// <returns>True if passed</returns>
public bool IsTheEndOfTheGame() => (currentPositionOnX, currentPositionOnY) == (68, 4); // Magic 68 and 4 are the coordinates of the point you need to reach in order to win
}
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 @@
+------------------------------------------------------------------+
| | |
| +-------------------+ +--------------------+ +---+ +----+
| | | | | | | |____|
+---+ +--+ | |
| | | | | | +--------+
| +-------------------+----+-----------+ +---------+ |
| | | |
+----------- +-------------------------------------+ +--------|
| |
+------------------------------------------------------------------+
9 changes: 9 additions & 0 deletions Game/Game/Game/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Game;

var eventLoop = new EventLoop();
var game = new Game.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(game.IsTheEndOfTheGame);
Loading