-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMazeGenerator.cs
More file actions
156 lines (134 loc) · 4.14 KB
/
Copy pathMazeGenerator.cs
File metadata and controls
156 lines (134 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MazeRunners;
using Spectre.Console;
namespace MazeRunners
{
public class MazeGenerator
{
public int ancho;
public int altura;
public Casilla[,] maze;
public Random rand = new Random();
public (int x,int y) winner;
public MazeGenerator(int ancho, int altura)
{
this.ancho = ancho;
this.altura = altura;
maze = new Casilla[ancho, altura];
winner = (ancho/2,altura/2);
rand = new Random();
}
public Casilla[,] GenerateMaze()
{
for (int i = 0; i < ancho; i++)
{
for (int j = 0; j < altura; j++)
{
maze[i,j] = new Muro((i,j));
}
}
NuevoCamino(1,1);
if (maze[1,altura - 3] is Muro)
{
maze[1,altura - 3] = new Camino((1,ancho - 3));
}
if (maze[ancho - 3,1] is Muro)
{
maze[ancho - 3,1] = new Camino((ancho - 3,1));
}
if (maze[ancho - 3,altura - 2] is Muro)
{
maze[ancho - 3,altura - 2] = new Camino((ancho - 3,altura - 2)) ;
}
ConectarCasilla(maze, winner);
PonerTrampas();
return maze;
}
public void NuevoCamino(int ancho, int altura)
{
maze[ancho, altura] = new Camino((ancho, altura));
var move = new(int, int)[]
{
(-2, 0),
(2, 0),
(0, 2),
(0, -2)
};
move = move.OrderBy(x => rand.Next()).ToArray();
foreach(var (x, y) in move)
{
int nx = ancho + x;
int ny = altura + y;
if (IsBlock(nx,ny) && maze[nx,ny] is Muro)
{
maze[ancho + x / 2, altura + y / 2] = new Camino((ancho + x / 2, altura + y / 2));
NuevoCamino(nx, ny);
}
}
}
public void ConectarCasilla(Casilla[,] maze, (int x, int y) winner)
{
if (!(maze[winner.x,winner.y] is Camino))
{
maze[winner.x,winner.y] = new Camino(winner);
return;
}
var dir = new List<(int, int)> {(0,1) , (1,0) , (0,-1) , (-1,0)};
Shuffle(dir);
foreach (var (dx, dy) in dir)
{
int nx = winner.x + dx;
int ny = winner.y + dy;
if (IsBlock(nx,ny) && maze[nx,ny] is Muro)
{
maze[winner.x + dx, winner.y + dy] = new Camino((winner.x + dx/2, winner.y + dy/2));
break;
}
}
maze[winner.x,winner.y] = new Winner(winner);
}
public void PonerTrampas()
{
int count = 10;
for (int i = 0; i < count; i++)
{
int x, y;
do
{
x = rand.Next(2, ancho - 3);
y = rand.Next(2, altura - 3);
}
while ((maze[x,y] is Camino));
int TipoTrampa = rand.Next(3);
switch (TipoTrampa)
{
case 0:
maze[x,y] = new SlowTrap((x,y));
break;
case 1:
maze[x,y] = new TeleportTrap((x,y));
break;
case 2:
maze[x,y] = new StunTrap((x,y));
break;
}
}
}
public bool IsBlock(int x, int y) => x > 0 && x < ancho && y > 0 && y < altura;
public void Shuffle<T>(IList<T> list)
{
int n = list.Count;
while (n > 1)
{
n--;
int k = rand.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
}
}