-
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
Open
MinyazevR
wants to merge
6
commits into
main
Choose a base branch
from
ConsoleGame
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Game #14
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| { | ||
| /// <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; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||||||
|
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.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 | ||||||
| } | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| +------------------------------------------------------------------+ | ||
| | | | | ||
| | +-------------------+ +--------------------+ +---+ +----+ | ||
| | | | | | | | |____| | ||
| +---+ +--+ | | | ||
| | | | | | | +--------+ | ||
| | +-------------------+----+-----------+ +---------+ | | ||
| | | | | | ||
| +----------- +-------------------------------------+ +--------| | ||
| | | | ||
| +------------------------------------------------------------------+ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Надо комментарии