Add files via upload

main
RubenSchoonbaert 8 months ago committed by GitHub
parent 6e3b3f8a33
commit 93017e17f0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,43 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.33424.131
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Wpf_2D", "Doolhof\Wpf_2D.csproj", "{D47F7DEA-1854-4C5B-A652-DDDF08285F6A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Global", "Logica\Global.csproj", "{3890B9BB-6647-4796-AAEE-76CF0A50FD3D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Logic", "Logic\Logic.csproj", "{59C74047-8A4A-4968-B064-CD6DBF338F3D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Wpf_3D", "Wpf_3D\Wpf_3D.csproj", "{D35B6C66-9D23-4F3A-853C-15550B4C0D1E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D47F7DEA-1854-4C5B-A652-DDDF08285F6A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D47F7DEA-1854-4C5B-A652-DDDF08285F6A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D47F7DEA-1854-4C5B-A652-DDDF08285F6A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D47F7DEA-1854-4C5B-A652-DDDF08285F6A}.Release|Any CPU.Build.0 = Release|Any CPU
{3890B9BB-6647-4796-AAEE-76CF0A50FD3D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3890B9BB-6647-4796-AAEE-76CF0A50FD3D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3890B9BB-6647-4796-AAEE-76CF0A50FD3D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3890B9BB-6647-4796-AAEE-76CF0A50FD3D}.Release|Any CPU.Build.0 = Release|Any CPU
{59C74047-8A4A-4968-B064-CD6DBF338F3D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{59C74047-8A4A-4968-B064-CD6DBF338F3D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{59C74047-8A4A-4968-B064-CD6DBF338F3D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{59C74047-8A4A-4968-B064-CD6DBF338F3D}.Release|Any CPU.Build.0 = Release|Any CPU
{D35B6C66-9D23-4F3A-853C-15550B4C0D1E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D35B6C66-9D23-4F3A-853C-15550B4C0D1E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D35B6C66-9D23-4F3A-853C-15550B4C0D1E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D35B6C66-9D23-4F3A-853C-15550B4C0D1E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {AE57EF3C-8877-450E-B4C8-E0C5DA2ADBF2}
EndGlobalSection
EndGlobal

@ -0,0 +1,245 @@
using Global;
using Logica;
using System.Drawing;
namespace Logic {
public class BinaryTreeAlgoritm {
public Maze maze;
private bool isDone = false;
private static readonly Random random = new Random();
private List<Cell> oldVisitedCells = new List<Cell>();
private List<Cell> newVisitedCells = new List<Cell>();
private Cell stop;
private Cell start;
public BinaryTreeAlgoritm(Maze maze) {
this.maze = maze;
}
public void Generate() {
while (!isDone) {
SetAllWalls();
GenerateCells(100);
GenerateBorder();
start = CalculateStartStop(1);
stop = CalculateStartStop(maze.length - 2);
SolveMaze(start);
}
if (isDone) {
SetWalls();
}
}
/// <summary>
/// Generate Maze cel by cel
/// </summary>
/// <param name="placepercentage"></param>
private void GenerateCells(int placepercentage) {
for (int y = 0; y < maze.height; y++) {
for (int x = 0; x < maze.length; x++) {
if (y % 2 == 0 && x % 2 == 0) {
FillCell(x, y);
}
else {
if (GetRandomNumber(placepercentage) >= 49) {
if (x < maze.length - 1) {
FillCell(x + 1, y);
}
}
else {
if (y < maze.height - 1) {
FillCell(x, y + 1);
}
}
}
}
}
}
private void SetAllWalls() {
foreach (Cell cell in maze.cels) {
maze.cels[cell.x, cell.y].isWall = true;
}
}
/// <summary>
/// Return a random number
/// </summary>
/// <param name="placepercentage"></param>
/// <returns></returns>
private int GetRandomNumber(int placepercentage) {
int randomInt = random.Next(placepercentage);
return randomInt;
}
/// <summary>
/// Mark cell as visited and color it
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
private void FillCell(int x, int y) {
maze.cels[x, y].color = Color.Black;
maze.cels[x, y].isWall = false;
}
/// <summary>
/// Generate borders
/// </summary>
private void GenerateBorder() {
for (int i = 0; i < maze.length; i++) {
maze.cels[i, 0].color = Color.Red;
maze.cels[i, 0].isWall = true;
maze.cels[i, maze.length - 1].color = Color.Red;
maze.cels[i, maze.length - 1].isWall = true;
}
for (int j = 0; j < maze.height - 1; j++) {
maze.cels[0, j].color = Color.Red;
maze.cels[0, j].isWall = true;
maze.cels[maze.height - 1, j].color = Color.Red;
maze.cels[maze.height - 1, j].isWall = true;
}
}
/// <summary>
/// Find a usable cell in a row for the start or the stop of the maze.
/// </summary>
/// <param name="x"></param>
private Cell CalculateStartStop(int x) {
Cell cel = new Cell();
if (x >= 0 && x < maze.length) {
for (int i = 0; i < maze.length; i++) {
Random random = new Random();
int randomY = random.Next(0, maze.length);
if (maze.cels[x, randomY].color == Color.Black) {
if (x == 1) {
maze.ball = new Ball();
maze.ball.SetLocation(x, randomY);
}
else {
maze.cels[x, randomY].color = Color.Blue;
}
maze.cels[x, randomY].visited = true;
maze.cels[x, randomY].isWall = false;
cel.setLocation(x, randomY);
cel.id = maze.cels[x, randomY].id;
break;
}
}
}
return cel;
}
/// <summary>
/// Solve the maze with an algoritm to see if its solvable
/// </summary>
/// <param name="start"></param>
private void SolveMaze(Cell start) {
bool solved = false;
oldVisitedCells.Add(start);
while (!solved && !isDone) {
solved = true;
foreach (Cell cell in oldVisitedCells.ToList()) {
if (FillInAvailableNeighbours(cell.x, cell.y)) {
if (!(cell.color == Color.Red)) {
solved = false;
}
}
}
oldVisitedCells = newVisitedCells.ToList();
newVisitedCells.Clear();
}
}
/// <summary>
/// See if the neighbouring cells are available
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
private bool FillInAvailableNeighbours(int x, int y) {
bool isOk = false;
if (!IsVisited(x + 1, y)) {
SetVisited(x + 1, y);
isOk = true;
}
if (!IsVisited(x - 1, y)) {
SetVisited(x - 1, y);
isOk = true;
}
if (!IsVisited(x, y + 1)) {
SetVisited(x, y + 1);
isOk = true;
}
if (!IsVisited(x, y - 1)) {
SetVisited(x, y - 1);
isOk = true;
}
return (isOk);
}
/// <summary>
/// Return if the cell has been visited
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
private bool IsVisited(int x, int y) {
if (maze.cels[x, y].id == stop.id) {
isDone = true;
}
if (x >= 0 && x < maze.length && y >= 0 && y < maze.height) {
if (maze.cels[x, y].isWall == false) {
return maze.cels[x, y].visited;
}
else return true;
}
else if (x == -1 || y == -1 || x == maze.length || y == maze.height) {
return true;
}
else {
return true;
}
}
/// <summary>
/// Set the cell as visited to prevent index array errors
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
private void SetVisited(int x, int y) {
Cell cell = maze.cels[x, y];
cell.setLocation(x, y);
if (cell.isWall == false && cell.visited == false) {
cell.visited = true;
newVisitedCells.Add(cell);
}
}
/// <summary>
/// Not too proud of this fix
/// </summary>
private void SetWalls() {
for (int x = 0; x < maze.length; x++) {
for (int y = 0; y < maze.height; y++) {
if (maze.cels[x, y].color == Color.Red) {
maze.cels[x, y].isWall = true;
}
}
}
}
}
}

@ -0,0 +1,321 @@
using Global;
using Logica;
using System.Drawing;
namespace Logic {
public class DepthFirstAlgoritm {
public Maze maze;
public bool isDone = false;
private static readonly Random random = new Random();
private int x;
private int y;
private int attempts = 0;
private bool goodStep;
private int startcellId;
public DepthFirstAlgoritm(Maze maze) {
this.maze = maze;
x = 0;
y = 0;
}
/// <summary>
/// Generate the maze.
/// </summary>
public void Generate() {
maze.length--;
maze.height--;
CalculateIdStartCell();
while (isDone == false) {
SetStep();
}
CalculateStartStop(1);
CalculateStartStop(maze.length - 1);
maze.height++;
maze.length++;
GenerateBorder();
SetWalls();
}
/// <summary>
/// Set a step in a random direction. If there are too many unsuccesfull attempts it will force a step
/// </summary>
private void SetStep() {
goodStep = false;
attempts = 0;
while (goodStep == false && isDone == false) {
goodStep = false;
int tempX = x;
int tempY = y;
if (attempts > 5) {
ForceStep();
break;
}
else {
Direction direction = GenerateRandomMovement();
switch (direction) {
case Direction.up: tempY = y - 1; break;
case Direction.down: tempY = y + 1; break;
case Direction.right: tempX = x + 1; break;
case Direction.left: tempX = x - 1; break;
}
if (tempY >= 1 && tempX >= 1 && tempX < maze.length && tempY < maze.height) {
if (CheckStepInvalid(tempX, tempY) || CheckNeighbours(x, y, tempX, tempY)) {
attempts = attempts + 1;
}
else {
x = tempX;
y = tempY;
maze.cels[x, y].SetVisited(true);
maze.cels[x, y].color = Color.Red;
goodStep = true;
}
}
else {
attempts = attempts + 1;
}
}
}
}
/// <summary>
/// will force check every direction for a good step.
/// </summary>
private void ForceStep() {
if (CheckStepInvalid((x + 1), y) == false && CheckNeighbours(x, y, x + 1, y) == false) {
goodStep = true;
x++;
}
else if (CheckStepInvalid((x - 1), y) == false && CheckNeighbours(x, y, x - 1, y) == false) {
goodStep = true;
x--;
}
else if (CheckStepInvalid(x, (y + 1)) == false && CheckNeighbours(x, y, x, y + 1) == false) {
goodStep = true;
y++;
}
else if (CheckStepInvalid(x, (y - 1)) == false && CheckNeighbours(x, y, x, y - 1) == false) {
goodStep = true;
y--;
}
if (goodStep) {
maze.cels[x, y].SetVisited(true);
maze.cels[x, y].color = Color.Red;
attempts = 0;
}
if (goodStep == false) {
BacktrackStep();
}
}
/// <summary>
/// Backstep in a random direction untill a good step has been found
/// if the step is back to the startcell then the maze is done.
/// </summary>
private void BacktrackStep() {
int tempY = y;
int tempX = x;
maze.cels[x, y].isBacktracked = true;
Direction[] directions = Enum.GetValues(typeof(Direction))
.Cast<Direction>()
.OrderBy(_ => Guid.NewGuid())
.ToArray();
//Random order
foreach (Direction direction in directions) {
switch (direction) {
case Direction.up: tempY = y - 1; break;
case Direction.down: tempY = y + 1; break;
case Direction.right: tempX = x + 1; break;
case Direction.left: tempX = x - 1; break;
}
if (IsVisited(tempX, tempY) == true) {
y = tempY;
x = tempX;
maze.cels[x, y].color = Color.Black;
break;
}
}
if (maze.cels[x, y].id == startcellId) {
isDone = true;
}
}
/// <summary>
/// Check if a cell has a neighbouring cell that has been visited
/// </summary>
/// <param name="x1"></param>
/// <param name="y1"></param>
/// <param name="x2"></param>
/// <param name="y2"></param>
/// <returns></returns>
private bool CheckNeighbours(int x1, int y1, int x2, int y2) {
bool notOk = false;
// Check adjacent neighbors
if (IsVisited(x2 + 1, y2) && x2 + 1 != x1) {
notOk = true;
}
else if (IsVisited(x2 - 1, y2) && x2 - 1 != x1) {
notOk = true;
}
else if (IsVisited(x2, y2 + 1) && y2 + 1 != y1) {
notOk = true;
}
else if (IsVisited(x2, y2 - 1) && y2 - 1 != y1) {
notOk = true;
}
// Check diagonal neighbors
if (IsVisited(x2 + 1, y2 + 1) && x2 + 1 != x1 && y2 + 1 != y1) {
notOk = true;
}
else if (IsVisited(x2 - 1, y2 - 1) && x2 - 1 != x1 && y2 - 1 != y1) {
notOk = true;
}
else if (IsVisited(x2 + 1, y2 - 1) && x2 + 1 != x1 && y2 - 1 != y1) {
notOk = true;
}
else if (IsVisited(x2 - 1, y2 + 1) && x2 - 1 != x1 && y2 + 1 != y1) {
notOk = true;
}
return notOk;
}
/// <summary>
/// Check if a cell has been visited to prevent array out of bounds errors
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
private bool IsVisited(int x, int y) {
if (x >= 1 && x < maze.length && y >= 1 && y < maze.height) {
return maze.cels[x, y].visited;
}
else if (x == 0 || y == 0 || x == maze.length || y == maze.height) {
return false;
//wall, so can ignore
}
else {
return true;
}
}
/// <summary>
/// Generate a random direction
/// </summary>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
private Direction GenerateRandomMovement() {
int randomNumber = random.Next(1, 5);
switch (randomNumber) {
case 1:
return Direction.up;
case 2:
return Direction.down;
case 3:
return Direction.left;
case 4:
return Direction.right;
default:
throw new InvalidOperationException("Invalid random number generated.");
}
}
/// <summary>
/// Pick random cell at the left side to generate the maze from
/// </summary>
private void CalculateIdStartCell() {
y = random.Next(1, maze.height - 1);
x = random.Next(1, maze.length - 1);
startcellId = maze.cels[x, y].id;
maze.cels[x, y].SetVisited(true);
}
/// <summary>
/// Find a usable cell in a row for the start or the stop of the maze.
/// </summary>
/// <param name="x"></param>
private void CalculateStartStop(int x) {
if (x >= 1 && x < maze.length) {
for (int i = 1; i < maze.length; i++) {
Random random = new Random();
int randomY = random.Next(0, maze.length);
if (maze.cels[x, randomY].color == Color.Black) {
if (x == 1) {
maze.ball = new Ball();
maze.ball.SetLocation(x, randomY);
}
else {
maze.cels[x, randomY].color = Color.Blue;
Cell endCell = new Cell();
endCell.setLocation(x, randomY);
maze.endCell = endCell;
}
break;
}
}
}
}
public void GenerateBorder() {
int width = maze.length;
int height = maze.height;
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
if (x == 0 || x == width - 1 || y == 0 || y == height - 1) {
maze.cels[x, y].isWall = true;
}
}
}
}
/// <summary>
/// Check if a step is invalid
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
private bool CheckStepInvalid(int x, int y) {
bool isInvalid = true;
if (x >= 1 && x < maze.length && y >= 1 && y < maze.height) {
if (x < maze.length && x >= 1 && isInvalid != false) {
isInvalid = false;
}
if (y < maze.height && y >= 1 && isInvalid != false) {
isInvalid = false;
}
if (IsVisited(x, y) == true) {
isInvalid = true;
}
}
return isInvalid;
}
/// <summary>
/// Not too proud of this fix
/// </summary>
private void SetWalls() {
for (int x = 0; x < maze.length; x++) {
for (int y = 0; y < maze.height; y++) {
if (maze.cels[x, y].color == Color.Red) {
maze.cels[x, y].isWall = true;
}
}
}
}
}
}

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Logica\Global.csproj" />
</ItemGroup>
</Project>

@ -0,0 +1,155 @@
using Global;
using Logica;
namespace Logic {
public class PhysicsEngine {
public Ball ball { get; set; }
private Maze maze;
private double timeInterval = 0.1; //(100ms)
public bool isFinished = false;
double Dv = 0;
double Dh = 0;
private double elasticity = 0.5;
public PhysicsEngine(Ball ball, Maze maze) {
this.ball = ball;
this.maze = maze;
}
/// <summary>
/// Checks for collisions on both axis
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
public void CheckForCollision(double x, double y) {
CheckXAxis(x);
CheckYAxis(y);
}
/// <summary>
/// Check the x axis for collisions
/// </summary>
/// <param name="x"></param>
internal void CheckXAxis(double x) {
double arrayX = x - 0.5;
bool directionCheck = ball.x > x; // true for left, false for right
bool axisCheck = false;
for (double i = -ball.size; i <= ball.size; i += 0.1) {
double checkX = directionCheck ? arrayX - ball.size : arrayX + ball.size;
double checkY = ball.y - 0.5 + i;
if (maze.cels[RoundToNearest(checkX), RoundToNearest(checkY)].isWall) {
axisCheck = true;
break;
}
}
if (!axisCheck) {
ball.x = x;
}
else {
Dv = -Dv * elasticity;
}
}
/// <summary>
/// Check if the y axis collides with the edge
/// </summary>
/// <param name="y"></param>
internal void CheckYAxis(double y) {
double arrayY = y - 0.5;
bool directionCheck = ball.y > y; // true for up, false for down
bool axisCheck = false;
for (double i = -ball.size; i <= ball.size; i += 0.1) {
double checkX = ball.x - 0.5 + i;
double checkY = directionCheck ? arrayY - ball.size : arrayY + ball.size;
if (maze.cels[RoundToNearest(checkX), RoundToNearest(checkY)].isWall) {
axisCheck = true;
break;
}
}
if (!axisCheck) {
ball.y = y;
}
else {
Dh = -Dh * elasticity;
}
}
/// <summary>
/// Check if the ball is in the end cell.
/// </summary>
/// <returns></returns>
internal bool CheckFinished() {
double tolerance = 0.5; // Adjust the tolerance as needed
if (Math.Abs(ball.x - maze.endCell.x) < tolerance && Math.Abs(ball.y - maze.endCell.y) < tolerance) {
return true;
}
else {
return false;
}
}
//round double to the nearest integer
public int RoundToNearest(double value) {
return (int)Math.Ceiling(value);
}
/// <summary>
/// Calculate angle to x and y forces
/// </summary>
/// <param name="vertR"></param>
/// <param name="horzR"></param>
/// <returns></returns>
public Ball CalculateAngleMovement(double vertR, double horzR) {
double radianAngleX = vertR * (Math.PI / 180);
double radianAngleY = horzR * (Math.PI / 180);
double aX = 9.81 * Math.Sin(radianAngleX);
double aY = 9.81 * Math.Sin(radianAngleY);
Dh += (1.0 / 2.0) * aX * Math.Pow(timeInterval, 2);
Dv += (1.0 / 2.0) * aY * Math.Pow(timeInterval, 2);
AddFriction();
double nextX = ball.x + Dv;
double nextY = ball.y - Dh;
CheckForCollision(nextX, nextY);
if (CheckFinished()) {
isFinished = true;
}
else {
isFinished = false;
}
return ball;
}
/// <summary>
/// Add friction the the ball
/// </summary>
private void AddFriction() {
double friction = 0.15;
Dv *= (1.0 - friction * timeInterval);
Dh *= (1.0 - friction * timeInterval);
}
}
}

@ -0,0 +1,92 @@
using Global;
using Logica;
using System.Drawing;
namespace Logic {
public class StaticMazeGenerator {
public Maze maze;
public StaticMazeGenerator() {
maze = new Maze(20, 20);
maze.GenerateGrid(Color.Black);
GenerateStaticMaze();
}
/// <summary>
/// First set the border of the maze.
/// Then call the GenerateWallls() methode to fill in the rest of the maze
/// &set the start and end cell.
/// </summary>
public void GenerateStaticMaze() {
for (int x = 0; x < maze.length; x++) {
maze.cels[x, 0].isWall = true;
maze.cels[x, maze.height - 1].isWall = true;
maze.cels[x, 0].color = Color.Red;
maze.cels[x, maze.height - 1].color = Color.Red;
}
for (int y = 0; y < maze.height; y++) {
maze.cels[0, y].isWall = true;
maze.cels[maze.length - 1, y].isWall = true;
maze.cels[0, y].color = Color.Red;
maze.cels[maze.length - 1, y].color = Color.Red;
}
// Set the start & stop cell
maze.ball = new Ball();
maze.ball.SetLocation(1, 2);
maze.cels[18, 16].color = Color.Blue;
maze.endCell = maze.cels[18, 16];
Cell endCell = new Cell();
endCell.setLocation(maze.length - 2, maze.height - 2);
maze.endCell = endCell;
GenerateWalls();
}
/// <summary>
/// Set the maze using a static list of id's that are walls.
/// </summary>
private void GenerateWalls() {
//list of ids
List<int> wallIds = new List<int> {
0, 20, 40, 60, 80, 100, 120, 140, 160, 180,
200, 220, 240, 260, 280, 300, 320, 340, 360, 380,
1, 21, 121, 141, 261, 361, 381, 2, 62, 102, 122,
182, 222, 302, 322, 382, 3, 43, 63, 163, 183, 203,
223, 263, 283, 303, 323, 343, 383, 4, 64, 84, 104,
124, 144, 164, 264, 304, 344, 384, 5, 45, 65, 205,
225, 245, 265, 385, 6, 26, 46, 106, 126, 146, 166,
186, 206, 226, 266, 286, 326, 346, 366, 386, 7, 87,
107, 127, 167, 227, 267, 287, 327, 347, 387, 8, 48,
68, 88, 208, 228, 388, 9, 89, 109, 149, 169, 269,
309, 349, 369, 389, 10, 50, 90, 170, 190, 210, 230,
250, 270, 290, 310, 370, 390, 11, 31, 51, 91, 131,
231, 291, 311, 331, 391, 12, 92, 112, 132, 152, 172,
192, 212, 232, 272, 292, 372, 392, 13, 33, 73, 93,
113, 193, 213, 273, 333, 353, 373, 393, 14, 34, 94,
114, 154, 234, 254, 274, 294, 334, 394, 15, 75, 95,
155, 175, 195, 215, 235, 255, 335, 355, 395, 16, 56,
76, 136, 156, 196, 236, 256, 296, 316, 336, 396, 17,
57, 117, 137, 317, 377, 397, 18, 98, 118, 178, 218,
258, 278, 358, 378, 398, 19, 39, 59, 79, 99, 119,
139, 159, 179, 199, 219, 239, 259, 279, 299, 319, 339,
359, 379, 399
};
var wallCells = wallIds.Select(id => new { X = id % maze.length, Y = id / maze.length });
foreach (var cell in wallCells) {
int x = cell.X;
int y = cell.Y;
if (x > 0 && x < maze.length && y > 0 && y < maze.height) {
maze.cels[x, y].isWall = true;
maze.cels[x, y].color = Color.Red;
}
}
}
}
}

@ -0,0 +1,373 @@
using Global;
using Logica;
using System.Drawing;
namespace Logic {
public class WallsCreationAlgoritm {
public Maze maze;
public bool isDone = false;
public int wallpercentage = 25;
public int destructionpercentage = 15;
private static readonly Random random = new Random();
private List<int> Rows = new List<int>();
private List<int> Columns = new List<int>();
private List<Cell> oldVisitedCells = new List<Cell>();
private List<Cell> newVisitedCells = new List<Cell>();
private Cell stopid;
public WallsCreationAlgoritm(Maze maze) {
this.maze = maze;
}
public void Generate() {
CreateWalls(false); // Create walls for columns
CreateWalls(true); // Create walls for rows
PlaceWalls();
GenerateBorder();
Cell startid = CalculateStartStop(1);
stopid = CalculateStartStop(maze.length - 2);
SolveMaze(startid);
SetWalls();
}
/// <summary>
/// Source: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator
/// </summary>
/// <param name="forRows"></param>
private void CreateWalls(bool forRows) {
int rowCount = maze.height; //rows
int colCount = maze.length; //columns
for (int i = 2; i < (forRows ? rowCount - 2 : colCount - 2); i++) {
if (!HasAdjacentFilledRow(i, forRows) && random.Next(100) < wallpercentage) {
if (forRows) {
Rows.Add(i);
}
else {
Columns.Add(i);
}
}
}
}
/// <summary>
/// Check if an adjacent row is not filled so theres always a gap
/// </summary>
/// <param name="index"></param>
/// <param name="RowColumn"></param>
/// <returns></returns>
private bool HasAdjacentFilledRow(int index, bool RowColumn) {
if (RowColumn) {
if (Rows.Contains(index - 1) || Rows.Contains(index + 1)) {
return true;
}
else {
return false;
}
}
else {
if (Columns.Contains(index - 1) || Columns.Contains(index + 1)) {
return true;
}
else {
return false;
}
}
}
/// <summary>
/// Place the vertical and horizontal walls
/// </summary>
private void PlaceWalls() {
foreach (int x in Columns) {
int[] randomIndex = Randomize(maze.height);
bool oneGapPlaced = false;
while (oneGapPlaced == false) {
for (int i = 0; i < maze.height; i++) {
int y = randomIndex[i];
if (!maze.cels[x, y].isWall) {
if (!oneGapPlaced) {
if (random.Next(100) < 15 && CheckCentrumOfCross(x, y)) {
oneGapPlaced = true;
continue; // gap
}
}
maze.cels[x, y].isWall = true;
maze.cels[x, y].color = Color.Red;
}
}
}
MakeGap(x, false);
}
foreach (int y in Rows) {
int[] randomIndex = Randomize(maze.length);
bool gapPlaced = false;
while (gapPlaced == false) {
for (int i = 0; i < maze.length; i++) {
int x = randomIndex[i];
if (!maze.cels[x, y].isWall) {
if (!gapPlaced) {
if (random.Next(100) < 15 && CheckCentrumOfCross(x, y)) {
gapPlaced = true;
continue; //gap
}
}
maze.cels[x, y].isWall = true;
maze.cels[x, y].color = Color.Red;
}
}
}
MakeGap(y, true);
}
}
/// <summary>
/// Make a gap
/// </summary>
/// <param name="xy"></param>
/// <param name="forRows"></param>
private void MakeGap(int xy, bool forRows) {
for (int i = 1; i < (forRows ? maze.height - 1 : maze.length - 1); i++) {
if (random.Next(100) < destructionpercentage && (forRows ? CheckCentrumOfCross(i, xy) : CheckCentrumOfCross(xy, i))) {
if (forRows) {
maze.cels[i, xy].isWall = false;
//maze.cels[i, xy].visited = false;
maze.cels[i, xy].color = Color.Black;
}
else {
maze.cels[xy, i].isWall = false;
// maze.cels[xy, i].visited = false;
maze.cels[xy, i].color = Color.Black;
}
}
}
}
/// <summary>
/// Check if an empty cell is not surrounded by walls.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
private bool CheckCentrumOfCross(int x, int y) {
int i = 0;
if (!(x - 1 < 0 || x + 1 >= maze.length || y - 1 < 0 || y + 1 >= maze.height)) {
if (maze.cels[x + 1, y].color == Color.Red) {
i++;
}
if (maze.cels[x - 1, y].color == Color.Red) {
i++;
}
if (maze.cels[x, y + 1].color == Color.Red) {
i++;
}
if (maze.cels[x, y - 1].color == Color.Red) {
i++;
}
}
if (i >= 3) {
return false;
}
else {
return true;
}
}
/// <summary>
/// Taken from https://stackoverflow.com/questions/56378647/fisher-yates-shuffle-in-c-sharp
/// </summary>
/// <param name="size"></param>
/// <returns></returns>
private int[] Randomize(int size) {
int[] index = new int[size];
for (int i = 0; i < size; i++) {
index[i] = i;
}
for (int i = size - 1; i > 0; i--) {
int j = random.Next(0, i + 1);
int temp = index[i];
index[i] = index[j];
index[j] = temp;
}
return index;
}
/// <summary>
/// Find a usable cell in a row for the start or the stop of the maze.
/// </summary>
/// <param name="x"></param>
private Cell CalculateStartStop(int x) {
Cell cel = new Cell();
if (x >= 0 && x < maze.length) {
for (int i = 0; i < maze.length; i++) {
Random random = new Random();
int randomY = random.Next(0, maze.length);
if (maze.cels[x, randomY].color == Color.Black) {
if (x == 1) {
maze.ball = new Ball();
maze.ball.SetLocation(x, randomY);
}
else {
maze.cels[x, randomY].color = Color.Blue;
}
maze.cels[x, randomY].isWall = false;
cel.setLocation(x, randomY);
cel.id = maze.cels[x, randomY].id;
break;
}
}
}
return cel;
}
/// <summary>
/// Create a border
/// </summary>
private void GenerateBorder() {
for (int i = 0; i < maze.length; i++) {
maze.cels[i, 0].color = Color.Red;
maze.cels[i, 0].isWall = true;
maze.cels[i, maze.length - 1].color = Color.Red;
maze.cels[i, maze.length - 1].isWall = true;
}
for (int j = 0; j < maze.height - 1; j++) {
maze.cels[0, j].color = Color.Red;
maze.cels[0, j].isWall = true;
maze.cels[maze.height - 1, j].color = Color.Red;
maze.cels[maze.height - 1, j].isWall = true;
}
}
/// <summary>
/// Solve the maze with an algoritm to see if its solvable
/// </summary>
/// <param name="start"></param>
private void SolveMaze(Cell start) {
int i = 0;
while (!isDone && i < 1) {
bool solved = false;
oldVisitedCells.Add(start);
while (!solved && !isDone) {
solved = true;
foreach (Cell cell in oldVisitedCells.ToList()) {
if (FillInAvailableNeighbours(cell.x, cell.y)) {
solved = false;
}
if (isDone) {
break;
}
}
oldVisitedCells = newVisitedCells.ToList();
newVisitedCells.Clear();
}
if (!isDone && i < 1) {
MakeAdditionalGaps();
i++;
}
}
}
/// <summary>
/// Make additional gaps
/// </summary>
private void MakeAdditionalGaps() {
foreach (int x in Columns) {
MakeGap(x, false);
}
foreach (int y in Rows) {
MakeGap(y, true);
}
}
/// <summary>
/// See if the neighbouring cells are available
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
private bool FillInAvailableNeighbours(int x, int y) {
bool isOk = false;
if (!IsVisited(x + 1, y)) {
SetVisited(x + 1, y);
isOk = true;
}
if (!IsVisited(x - 1, y)) {
SetVisited(x - 1, y);
isOk = true;
}
if (!IsVisited(x, y + 1)) {
SetVisited(x, y + 1);
isOk = true;
}
if (!IsVisited(x, y - 1)) {
SetVisited(x, y - 1);
isOk = true;
}
return (isOk);
}
/// <summary>
/// Set the cell as visited to prevent index array errors
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
private void SetVisited(int x, int y) {
Cell cell = maze.cels[x, y];
cell.setLocation(x, y);
if (cell.isWall == false && cell.visited == false) {
//cell.color = Color.White;
cell.visited = true;
newVisitedCells.Add(cell);
if (cell.id == stopid.id) {
isDone = true;
}
}
}
/// <summary>
/// Return if the cell has been visited
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
private bool IsVisited(int x, int y) {
if (x >= 0 && x < maze.length && y >= 0 && y < maze.height) {
return maze.cels[x, y].visited;
}
else if (x == -1 || y == -1 || x == maze.length || y == maze.height) {
return true;
}
else {
return true;
}
}
/// <summary>
/// Not too proud of this fix
/// </summary>
private void SetWalls() {
for (int x = 0; x < maze.length; x++) {
for (int y = 0; y < maze.height; y++) {
if (maze.cels[x, y].color == Color.Red) {
maze.cels[x, y].isWall = true;
}
}
}
}
}
}

@ -0,0 +1,36 @@
{
"runtimeTarget": {
"name": ".NETCoreApp,Version=v7.0",
"signature": ""
},
"compilationOptions": {},
"targets": {
".NETCoreApp,Version=v7.0": {
"Logic/1.0.0": {
"dependencies": {
"Global": "1.0.0"
},
"runtime": {
"Logic.dll": {}
}
},
"Global/1.0.0": {
"runtime": {
"Global.dll": {}
}
}
}
},
"libraries": {
"Logic/1.0.0": {
"type": "project",
"serviceable": false,
"sha512": ""
},
"Global/1.0.0": {
"type": "project",
"serviceable": false,
"sha512": ""
}
}
}

@ -0,0 +1,23 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("Logic")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
[assembly: System.Reflection.AssemblyProductAttribute("Logic")]
[assembly: System.Reflection.AssemblyTitleAttribute("Logic")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
// Generated by the MSBuild WriteCodeFragment class.

@ -0,0 +1 @@
55bfe306e4c1a29ea5a87d332ae418f3fee8f1af

@ -0,0 +1,11 @@
is_global = true
build_property.TargetFramework = net7.0
build_property.TargetPlatformMinVersion =
build_property.UsingMicrosoftNETSdkWeb =
build_property.ProjectTypeGuids =
build_property.InvariantGlobalization =
build_property.PlatformNeutralAssembly =
build_property.EnforceExtendedAnalyzerRules =
build_property._SupportedPlatformList = Linux,macOS,Windows
build_property.RootNamespace = Logic
build_property.ProjectDir = C:\Users\ruben\source\repos\2324-ap-rubenschoonbaert\Doolhof\Logic\

@ -0,0 +1,8 @@
// <auto-generated/>
global using global::System;
global using global::System.Collections.Generic;
global using global::System.IO;
global using global::System.Linq;
global using global::System.Net.Http;
global using global::System.Threading;
global using global::System.Threading.Tasks;

@ -0,0 +1,15 @@
C:\Users\ruben\source\repos\2324-ap-rubenschoonbaert\Doolhof\Logic\bin\Debug\net7.0\Logic.deps.json
C:\Users\ruben\source\repos\2324-ap-rubenschoonbaert\Doolhof\Logic\bin\Debug\net7.0\Logic.dll
C:\Users\ruben\source\repos\2324-ap-rubenschoonbaert\Doolhof\Logic\bin\Debug\net7.0\Logic.pdb
C:\Users\ruben\source\repos\2324-ap-rubenschoonbaert\Doolhof\Logic\bin\Debug\net7.0\Global.dll
C:\Users\ruben\source\repos\2324-ap-rubenschoonbaert\Doolhof\Logic\bin\Debug\net7.0\Global.pdb
C:\Users\ruben\source\repos\2324-ap-rubenschoonbaert\Doolhof\Logic\obj\Debug\net7.0\Logic.csproj.AssemblyReference.cache
C:\Users\ruben\source\repos\2324-ap-rubenschoonbaert\Doolhof\Logic\obj\Debug\net7.0\Logic.GeneratedMSBuildEditorConfig.editorconfig
C:\Users\ruben\source\repos\2324-ap-rubenschoonbaert\Doolhof\Logic\obj\Debug\net7.0\Logic.AssemblyInfoInputs.cache
C:\Users\ruben\source\repos\2324-ap-rubenschoonbaert\Doolhof\Logic\obj\Debug\net7.0\Logic.AssemblyInfo.cs
C:\Users\ruben\source\repos\2324-ap-rubenschoonbaert\Doolhof\Logic\obj\Debug\net7.0\Logic.csproj.CoreCompileInputs.cache
C:\Users\ruben\source\repos\2324-ap-rubenschoonbaert\Doolhof\Logic\obj\Debug\net7.0\Logic.csproj.CopyComplete
C:\Users\ruben\source\repos\2324-ap-rubenschoonbaert\Doolhof\Logic\obj\Debug\net7.0\Logic.dll
C:\Users\ruben\source\repos\2324-ap-rubenschoonbaert\Doolhof\Logic\obj\Debug\net7.0\refint\Logic.dll
C:\Users\ruben\source\repos\2324-ap-rubenschoonbaert\Doolhof\Logic\obj\Debug\net7.0\Logic.pdb
C:\Users\ruben\source\repos\2324-ap-rubenschoonbaert\Doolhof\Logic\obj\Debug\net7.0\ref\Logic.dll

@ -0,0 +1,122 @@
{
"format": 1,
"restore": {
"C:\\Users\\ruben\\source\\repos\\2324-ap-rubenschoonbaert\\Doolhof\\Logic\\Logic.csproj": {}
},
"projects": {
"C:\\Users\\ruben\\source\\repos\\2324-ap-rubenschoonbaert\\Doolhof\\Logica\\Global.csproj": {
"version": "1.0.0",
"restore": {
"projectUniqueName": "C:\\Users\\ruben\\source\\repos\\2324-ap-rubenschoonbaert\\Doolhof\\Logica\\Global.csproj",
"projectName": "Global",
"projectPath": "C:\\Users\\ruben\\source\\repos\\2324-ap-rubenschoonbaert\\Doolhof\\Logica\\Global.csproj",
"packagesPath": "C:\\Users\\ruben\\.nuget\\packages\\",
"outputPath": "C:\\Users\\ruben\\source\\repos\\2324-ap-rubenschoonbaert\\Doolhof\\Logica\\obj\\",
"projectStyle": "PackageReference",
"configFilePaths": [
"C:\\Users\\ruben\\AppData\\Roaming\\NuGet\\NuGet.Config",
"C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.Offline.config"
],
"originalTargetFrameworks": [
"net7.0"
],
"sources": {
"C:\\Program Files (x86)\\Microsoft SDKs\\NuGetPackages\\": {},
"https://api.nuget.org/v3/index.json": {}
},
"frameworks": {
"net7.0": {
"targetAlias": "net7.0",
"projectReferences": {}
}
},
"warningProperties": {
"warnAsError": [
"NU1605"
]
}
},
"frameworks": {
"net7.0": {
"targetAlias": "net7.0",
"imports": [
"net461",
"net462",
"net47",
"net471",
"net472",
"net48",
"net481"
],
"assetTargetFallback": true,
"warn": true,
"frameworkReferences": {
"Microsoft.NETCore.App": {
"privateAssets": "all"
}
},
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\7.0.201\\RuntimeIdentifierGraph.json"
}
}
},
"C:\\Users\\ruben\\source\\repos\\2324-ap-rubenschoonbaert\\Doolhof\\Logic\\Logic.csproj": {
"version": "1.0.0",
"restore": {
"projectUniqueName": "C:\\Users\\ruben\\source\\repos\\2324-ap-rubenschoonbaert\\Doolhof\\Logic\\Logic.csproj",
"projectName": "Logic",
"projectPath": "C:\\Users\\ruben\\source\\repos\\2324-ap-rubenschoonbaert\\Doolhof\\Logic\\Logic.csproj",
"packagesPath": "C:\\Users\\ruben\\.nuget\\packages\\",
"outputPath": "C:\\Users\\ruben\\source\\repos\\2324-ap-rubenschoonbaert\\Doolhof\\Logic\\obj\\",
"projectStyle": "PackageReference",
"configFilePaths": [
"C:\\Users\\ruben\\AppData\\Roaming\\NuGet\\NuGet.Config",
"C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.Offline.config"
],
"originalTargetFrameworks": [
"net7.0"
],
"sources": {
"C:\\Program Files (x86)\\Microsoft SDKs\\NuGetPackages\\": {},
"https://api.nuget.org/v3/index.json": {}
},
"frameworks": {
"net7.0": {
"targetAlias": "net7.0",
"projectReferences": {
"C:\\Users\\ruben\\source\\repos\\2324-ap-rubenschoonbaert\\Doolhof\\Logica\\Global.csproj": {
"projectPath": "C:\\Users\\ruben\\source\\repos\\2324-ap-rubenschoonbaert\\Doolhof\\Logica\\Global.csproj"
}
}
}
},
"warningProperties": {
"warnAsError": [
"NU1605"
]
}
},
"frameworks": {
"net7.0": {
"targetAlias": "net7.0",
"imports": [
"net461",
"net462",
"net47",
"net471",
"net472",
"net48",
"net481"
],
"assetTargetFallback": true,
"warn": true,
"frameworkReferences": {
"Microsoft.NETCore.App": {
"privateAssets": "all"
}
},
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\7.0.201\\RuntimeIdentifierGraph.json"
}
}
}
}
}

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<RestoreSuccess Condition=" '$(RestoreSuccess)' == '' ">True</RestoreSuccess>
<RestoreTool Condition=" '$(RestoreTool)' == '' ">NuGet</RestoreTool>
<ProjectAssetsFile Condition=" '$(ProjectAssetsFile)' == '' ">$(MSBuildThisFileDirectory)project.assets.json</ProjectAssetsFile>
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">$(UserProfile)\.nuget\packages\</NuGetPackageRoot>
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\ruben\.nuget\packages\</NuGetPackageFolders>
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.5.0</NuGetToolVersion>
</PropertyGroup>
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<SourceRoot Include="C:\Users\ruben\.nuget\packages\" />
</ItemGroup>
</Project>

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" />

@ -0,0 +1,91 @@
{
"version": 3,
"targets": {
"net7.0": {
"Global/1.0.0": {
"type": "project",
"framework": ".NETCoreApp,Version=v7.0",
"compile": {
"bin/placeholder/Global.dll": {}
},
"runtime": {
"bin/placeholder/Global.dll": {}
}
}
}
},
"libraries": {
"Global/1.0.0": {
"type": "project",
"path": "../Logica/Global.csproj",
"msbuildProject": "../Logica/Global.csproj"
}
},
"projectFileDependencyGroups": {
"net7.0": [
"Global >= 1.0.0"
]
},
"packageFolders": {
"C:\\Users\\ruben\\.nuget\\packages\\": {}
},
"project": {
"version": "1.0.0",
"restore": {
"projectUniqueName": "C:\\Users\\ruben\\source\\repos\\2324-ap-rubenschoonbaert\\Doolhof\\Logic\\Logic.csproj",
"projectName": "Logic",
"projectPath": "C:\\Users\\ruben\\source\\repos\\2324-ap-rubenschoonbaert\\Doolhof\\Logic\\Logic.csproj",
"packagesPath": "C:\\Users\\ruben\\.nuget\\packages\\",
"outputPath": "C:\\Users\\ruben\\source\\repos\\2324-ap-rubenschoonbaert\\Doolhof\\Logic\\obj\\",
"projectStyle": "PackageReference",
"configFilePaths": [
"C:\\Users\\ruben\\AppData\\Roaming\\NuGet\\NuGet.Config",
"C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.Offline.config"
],
"originalTargetFrameworks": [
"net7.0"
],
"sources": {
"C:\\Program Files (x86)\\Microsoft SDKs\\NuGetPackages\\": {},
"https://api.nuget.org/v3/index.json": {}
},
"frameworks": {
"net7.0": {
"targetAlias": "net7.0",
"projectReferences": {
"C:\\Users\\ruben\\source\\repos\\2324-ap-rubenschoonbaert\\Doolhof\\Logica\\Global.csproj": {
"projectPath": "C:\\Users\\ruben\\source\\repos\\2324-ap-rubenschoonbaert\\Doolhof\\Logica\\Global.csproj"
}
}
}
},
"warningProperties": {
"warnAsError": [
"NU1605"
]
}
},
"frameworks": {
"net7.0": {
"targetAlias": "net7.0",
"imports": [
"net461",
"net462",
"net47",
"net471",
"net472",
"net48",
"net481"
],
"assetTargetFallback": true,
"warn": true,
"frameworkReferences": {
"Microsoft.NETCore.App": {
"privateAssets": "all"
}
},
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\7.0.201\\RuntimeIdentifierGraph.json"
}
}
}
}

@ -0,0 +1,8 @@
{
"version": 2,
"dgSpecHash": "lnsbpXwor5nRCr6Tm3wWDBsSBG3WZotKkmmojwVOQjrBQQyCkrRNjzOAE7kYTdFtA0vMNVACSunO5a8ZDrgffA==",
"success": true,
"projectFilePath": "C:\\Users\\ruben\\source\\repos\\2324-ap-rubenschoonbaert\\Doolhof\\Logic\\Logic.csproj",
"expectedPackageFiles": [],
"logs": []
}
Loading…
Cancel
Save