-
Notifications
You must be signed in to change notification settings - Fork 1
/
labyGen.py
212 lines (158 loc) · 6.65 KB
/
labyGen.py
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import sys,os,random
#UTILISATION
#
# python ./labyGen
# Cree un fichier lab*I*
#
# Vous pouvez modifier l'algo de generation de labyrinthe a vos risques et perils.
#
#UTILISATION
#ICI ON REDEFINI LES VARIABLES DU LABYRINTHE (POSSIBLE DE LES CHANGER A LA MAIN DANS LE FICHIER POUR FAIRE D'AUTRE TESTS)
xPlayer = 1
yPlayer = 1
xExit = 9
yExit = 9
wLaby = 10
hLaby = 10
class Entity(object):
def __init__(self,pos_x,pos_y):
self.x = pos_x
self.y = pos_y
class Cell(Entity):
def __init__(self,pos_x,pos_y):
Entity.__init__(self,pos_x,pos_y)
self.type = 0 # EMPTY
class Player(Entity):
def __init__(self,pos_x,pos_y):
Entity.__init__(self,pos_x,pos_y)
class Labyrinthe(object):
def __init__(self,h,w):
self.grid = []
for i in range(h+1):
row = []
for j in range(w+1):
row.append(Cell(i,j))
self.grid.append(row)
self.h = h
self.w = w
self.S = 1
self.E = 2
self.HORIZONTAL = 1
self.VERTICAL = 2
# print self.grid[20][20].type
for i in range(1,w-1):
for j in range(1,h-1):
self.grid[i][j].type = 1
divide(self, self.grid, 1, 1, w-2, h-2)
def choose_orientation(width, height):
if width < height:
return 1
elif height < width:
return 2
else:
return (1 if random.random() > 0.5 else 2)
def getRandom(x,n):
return random.randint(x,n)
def divide(self, grid, x, y, width, height):
if width == 2 and height == 2:
if random.random() > 0.5:
dx = 1
dy = 0
else:
dx = 0
dy = 1
wall_x = x if dx else getRandom(x,x+width)
wall_y = y if dy else getRandom(y,y+height)
lenght = height if dy else width
passage_x = wall_x if dy else getRandom(x,x+width)
passage_y = wall_y if dx else getRandom(y,y+height)
passage3_x = passage_x
passage3_y = passage_y
if dy and grid[wall_x][wall_y-1].type == 1:
passage3_x = wall_x
# print
passage3_y = wall_y
if dx and grid[wall_x-1][wall_y].type == 1:
passage3_x = wall_x
passage3_y = wall_y
passage2_x = passage_x
passage2_y = passage_y
if dx and grid[x+width][wall_y].type == 1 :
passage2_x = x+width-1
# print "useful 3"
passage2_y = wall_y
if dy and grid[wall_x][y+height].type == 1 :
passage2_x = wall_x
# print "useful 4"
passage2_y = y+height-1
i = wall_x
j = wall_y
# print "Juste avant la boucle on a x=",x,"y=",y,"wall_x=",wall_x,"wall_y=",wall_y,"passage_x=",passage_x,"passage_y=",passage_y,"lenght",lenght
for k in range(1,lenght+1):
if (i != passage_x or j != passage_y) and (i != passage2_x or j!=passage2_y) and (i!=passage3_x or j!=passage3_y):
grid[i][j].type = 0
i += dx
j += dy
elif width <= 2 or height <= 2:
return None
else :
if width < height:
dx = 1
dy = 0
elif height < width:
dx = 0
dy = 1
elif random.random() > 0.5:
dx = 1
dy = 0
else:
dx = 0
dy = 1
wall_x = x if dx else getRandom(x+1,x+width-2)
wall_y = y if dy else getRandom(y+1,y+height-2)
lenght = height if dy else width
passage_x = wall_x if dy else getRandom(x+1,x+width-2)
passage_y = wall_y if dx else getRandom(y+1,y+height-2)
passage3_x = wall_x if dy else getRandom(x+1,x+width-2)
passage3_y = wall_y if dx else getRandom(y+1,y+height-2)
if dy and grid[wall_x][wall_y-1].type == 1:
passage3_x = wall_x
# print
passage3_y = wall_y
if dx and grid[wall_x-1][wall_y].type == 1:
passage3_x = wall_x
passage3_y = wall_y
passage2_x = passage_x
passage2_y = passage_y
if dx and grid[x+width][wall_y].type == 1 :
passage2_x = x+width-1
# print "useful 3"
passage2_y = wall_y
if dy and grid[wall_x][y+height].type == 1 :
passage2_x = wall_x
# print "useful 4"
passage2_y = y+height-1
i = wall_x
j = wall_y
# print "Juste avant la boucle on a x=",x,"y=",y,"wall_x=",wall_x,"wall_y=",wall_y,"passage_x=",passage_x,"passage_y=",passage_y,"lenght",lenght
for k in range(1,lenght+1):
if (i != passage_x or j != passage_y) and (i != passage2_x or j!=passage2_y) and (i!=passage3_x or j!=passage3_y):
grid[i][j].type = 0
i += dx
j += dy
if dy : #horizontale
divide(self,grid,x,y,wall_x-x,height)
divide(self,grid,wall_x+1,y,x+width-wall_x-1,height)
else:
divide(self,grid,x,y,width,wall_y-y)
divide(self,grid,x,wall_y+1,width,y+height-wall_y-1)
laby = Labyrinthe(wLaby,hLaby)
laby.grid[xExit-1][yExit-1].type = 2
player = Player(xPlayer,yPlayer)
name = "lab"+str(random.randint(0,20))
fileLab = open(name,"w")
fileLab.write(str(xPlayer+1)+"\n"+str(yPlayer+1)+"\n"+str(wLaby)+"\n"+str(hLaby)+"\n")
for i in range(0,laby.w):
for j in range(0,laby.h):
fileLab.write(str(-1*(laby.grid[i][j].type-1)))
fileLab.write("\n")