- Notifications
You must be signed in to change notification settings - Fork162
Monster Maze#98
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
gjat wants to merge2 commits intodotnet:mainChoose a base branch fromgjat:main
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.
Uh oh!
There was an error while loading.Please reload this page.
Open
Monster Maze#98
Changes fromall commits
Commits
Show all changes
2 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| name: Monster Maze Build | ||
| on: | ||
| push: | ||
| paths: | ||
| - 'Projects/MonsterMaze/**' | ||
| - '!**.md' | ||
| pull_request: | ||
| paths: | ||
| - 'Projects/MonsterMaze/**' | ||
| - '!**.md' | ||
| workflow_dispatch: | ||
| jobs: | ||
| build: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v3 | ||
| - uses: actions/setup-dotnet@v3 | ||
| with: | ||
| dotnet-version: 8.0.x | ||
| - run: dotnet build "Projects\MonsterMaze\MonsterMaze.csproj" --configuration Release |
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
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,37 @@ | ||
| public enum EntityAction | ||
| { | ||
| None, | ||
| Left, | ||
| Up, | ||
| Right, | ||
| Down, | ||
| Quit | ||
| } | ||
| public static class EntityActionExtensions | ||
| { | ||
| public static EntityAction FromConsoleKey(ConsoleKeyInfo key) | ||
| { | ||
| return key.Key switch | ||
| { | ||
| ConsoleKey.LeftArrow => EntityAction.Left, | ||
| ConsoleKey.RightArrow => EntityAction.Right, | ||
| ConsoleKey.UpArrow => EntityAction.Up, | ||
| ConsoleKey.DownArrow => EntityAction.Down, | ||
| ConsoleKey.Escape => EntityAction.Quit, | ||
| _ => key.KeyChar switch { | ||
| 'a' => EntityAction.Left, | ||
| 'A' => EntityAction.Left, | ||
| 'w' => EntityAction.Up, | ||
| 'W' => EntityAction.Up, | ||
| 'd' => EntityAction.Right, | ||
| 'D' => EntityAction.Right, | ||
| 's' => EntityAction.Down, | ||
| 'S' => EntityAction.Down, | ||
| 'q' => EntityAction.Quit, | ||
| 'Q' => EntityAction.Quit, | ||
| _ => EntityAction.None | ||
| } | ||
| }; | ||
| } | ||
| } |
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,26 @@ | ||
| public static class GameUtils | ||
| { | ||
| public static char[,] ConvertToCharMaze(bool[,] maze, char wallCharacter = '#') | ||
| { | ||
| var result = new char[maze.GetLength(0), maze.GetLength(1)]; | ||
| for (int i = 0; i < maze.GetLength(0); i++) | ||
| { | ||
| for (int j = 0; j < maze.GetLength(1); j++) | ||
| { | ||
| result[i, j] = maze[i, j] ? ' ' : wallCharacter; | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
| public static bool WaitForEscapeOrSpace() | ||
| { | ||
| var key = Console.ReadKey(true).Key; | ||
| while(key != ConsoleKey.Spacebar && key != ConsoleKey.Escape) | ||
| { | ||
| key = Console.ReadKey(true).Key; | ||
| } | ||
| return key == ConsoleKey.Escape; | ||
| } | ||
| } | ||
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,28 @@ | ||
| public struct MazePoint(int x, int y) | ||
| { | ||
| public int X { get; set; } = x; | ||
| public int Y { get; set; } = y; | ||
| public override bool Equals(object? obj) | ||
| { | ||
| if(obj is not MazePoint) | ||
| return false; | ||
| var other = (MazePoint)obj; | ||
| return other.X == X && other.Y == Y; | ||
| } | ||
| public static bool operator ==(MazePoint left, MazePoint right) | ||
| { | ||
| return left.Equals(right); | ||
| } | ||
| public static bool operator !=(MazePoint left, MazePoint right) | ||
| { | ||
| return !(left == right); | ||
| } | ||
| public override readonly int GetHashCode() | ||
| { | ||
| return HashCode.Combine(X, Y); | ||
| } | ||
| } |
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,140 @@ | ||
| public class MazeRecursiveGenerator | ||
| { | ||
| public enum MazeMode { | ||
| OnePath, | ||
| FilledDeadEnds, | ||
| Loops | ||
| }; | ||
| private static readonly (int, int)[] Directions = { (0, -1), (1, 0), (0, 1), (-1, 0) }; // Up, Right, Down, Left | ||
| private static Random random = new Random(); | ||
| public static bool[,] GenerateMaze(int width, int height, MazeMode mazeMode = MazeMode.OnePath) | ||
| { | ||
| if (width % 2 == 0 || height % 2 == 0) | ||
| throw new ArgumentException("Width and height must be odd numbers for a proper maze."); | ||
| bool[,] maze = new bool[width, height]; // by default, everything is a wall (cell value == false) | ||
| // Start the maze generation | ||
| GenerateMazeRecursive(maze, 1, 1); | ||
| // Make sure the entrance and exit are open | ||
| maze[0, 1] = true; // Entrance | ||
| maze[width - 1, height - 2] = true; // Exit | ||
| if(mazeMode == MazeMode.FilledDeadEnds) | ||
| FillDeadEnds(maze); | ||
| else if(mazeMode == MazeMode.Loops) | ||
| RemoveDeadEnds(maze); | ||
| return maze; | ||
| } | ||
| private static void GenerateMazeRecursive(bool[,] maze, int x, int y) | ||
| { | ||
| maze[x, y] = true; | ||
| // Shuffle directions | ||
| var shuffledDirections = ShuffleDirections(); | ||
| foreach (var (dx, dy) in shuffledDirections) | ||
| { | ||
| int nx = x + dx * 2; | ||
| int ny = y + dy * 2; | ||
| // Check if the new position is within bounds and not visited | ||
| if (IsInBounds(maze, nx, ny) && !maze[nx, ny]) | ||
| { | ||
| // Carve a path | ||
| maze[x + dx, y + dy] = true; | ||
| GenerateMazeRecursive(maze, nx, ny); | ||
| } | ||
| } | ||
| } | ||
| private static List<(int, int)> ShuffleDirections() | ||
| { | ||
| var directions = new List<(int, int)>(Directions); | ||
| for (int i = directions.Count - 1; i > 0; i--) | ||
| { | ||
| int j = random.Next(i + 1); | ||
| (directions[i], directions[j]) = (directions[j], directions[i]); | ||
| } | ||
| return directions; | ||
| } | ||
| private static bool IsInBounds(bool[,] maze, int x, int y) | ||
| { | ||
| return x > 0 && y > 0 && x < maze.GetLength(0) - 1 && y < maze.GetLength(1) - 1; | ||
| } | ||
| private static void FillDeadEnds(bool[,] maze) | ||
| { | ||
| bool removed; | ||
| do | ||
| { | ||
| removed = false; | ||
| for (int x = 1; x < maze.GetLength(0) - 1; x++) | ||
| { | ||
| for (int y = 1; y < maze.GetLength(1) - 1; y++) | ||
| { | ||
| if (maze[x, y]) // If it's a path | ||
| { | ||
| int neighbors = 0; | ||
| foreach (var (dx, dy) in Directions) | ||
| { | ||
| if (maze[x + dx, y + dy]) | ||
| neighbors++; | ||
| } | ||
| if (neighbors <= 1) // If it's a dead end | ||
| { | ||
| maze[x, y] = false; | ||
| removed = true; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } while (removed); | ||
| } | ||
| private static void RemoveDeadEnds(bool[,] maze) | ||
| { | ||
| bool removed; | ||
| do | ||
| { | ||
| removed = false; | ||
| for (int x = 1; x < maze.GetLength(0) - 1; x++) | ||
| { | ||
| for (int y = 1; y < maze.GetLength(1) - 1; y++) | ||
| { | ||
| if (maze[x, y]) // If it's a path | ||
| { | ||
| int neighbors = 0; | ||
| foreach (var (dx, dy) in Directions) | ||
| { | ||
| if (maze[x + dx, y + dy]) | ||
| neighbors++; | ||
| } | ||
| if (neighbors <= 1) // If it's a dead end | ||
| { | ||
| // Pick a random neighbor to keep open | ||
| var shuffledDirections = ShuffleDirections(); | ||
| foreach(var (dx, dy) in shuffledDirections) | ||
| { | ||
| if(IsInBounds(maze, x + dx, y + dy) && !maze[x + dx, y + dy]) | ||
| { | ||
| maze[x + dx, y + dy] = true; | ||
| break; | ||
| } | ||
| } | ||
| removed = true; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } while (removed); | ||
| } | ||
| } |
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 @@ | ||
| public class MazeStep | ||
| { | ||
| public MazePoint Position {get; set;} | ||
| public EntityAction Direction {get; set;} | ||
| public MazeStep(MazePoint position, EntityAction direction) | ||
| { | ||
| Position = position; | ||
| Direction = direction; | ||
| } | ||
| } |
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,18 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| <ApplicationIcon>monstermaze.ico</ApplicationIcon> | ||
| <Company></Company> | ||
| <Authors>Geoff Thompson (geoff.t.nz2 @ gmail.com)</Authors> | ||
| <Description>A console window game, where the player has to escape the maze while being chased by monsters.</Description> | ||
| </PropertyGroup> | ||
| <PropertyGroup Condition="'$(Configuration)'=='Release'"> | ||
| <Optimize>True</Optimize> | ||
| <PublishTrimmed>true</PublishTrimmed> | ||
| <SelfContained>true</SelfContained> | ||
| <RuntimeIdentifier>win-x64</RuntimeIdentifier> | ||
| </PropertyGroup> | ||
| </Project> |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.