Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
algo
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bertrand NÉRON
algo
Commits
2bc18452
Verified
Commit
2bc18452
authored
5 years ago
by
Bertrand NÉRON
Browse files
Options
Downloads
Patches
Plain Diff
add file to create labyrinth (from yoann code)
parent
75c4e45f
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Graph/graph/maze.py
+58
-0
58 additions, 0 deletions
Graph/graph/maze.py
with
58 additions
and
0 deletions
Graph/graph/maze.py
0 → 100644
+
58
−
0
View file @
2bc18452
import
random
class
Maze
:
def
__init__
(
self
,
nb_rows
,
nb_cols
):
self
.
nb_rows
=
nb_rows
self
.
nb_cols
=
nb_cols
self
.
maze
=
[[
"
.
"
]
*
nb_cols
for
_
in
range
(
nb_rows
)]
"""
Fill randomly the maze regarding the wall_proba
@param wall_proba Independent probability for each tile to be a wall
@param seed Random seed for reproducibility
"""
def
fill_random
(
self
,
wall_proba
=
0.5
,
seed
=
None
):
if
seed
:
random
.
seed
(
seed
)
for
row
in
self
.
maze
:
for
idx
in
range
(
self
.
nb_cols
):
row
[
idx
]
=
"
#
"
if
random
.
random
()
<
wall_proba
else
"
.
"
"""
Save a maze to a file
"""
def
save
(
self
,
filename
):
with
open
(
filename
,
"
w
"
)
as
out
:
out
.
write
(
f
"
{
self
.
nb_rows
}
{
self
.
nb_cols
}
"
)
for
row
in
self
.
maze
:
out
.
write
(
""
.
join
(
row
)
+
"
\n
"
)
"""
Load a maze from a file
"""
def
load
(
self
,
filename
):
with
open
(
filename
)
as
input
:
# Parse header
header
=
input
.
readline
()
header
=
[
int
(
x
)
for
x
in
header
.
strip
().
split
(
"
"
)]
# Construct maze properties
self
.
nb_rows
,
self
.
nb_cols
=
header
self
.
maze
=
[[
"
.
"
]
*
self
.
nb_cols
for
_
in
range
(
self
.
nb_rows
)]
# Fill the maze
for
row_idx
in
range
(
self
.
nb_rows
):
line
=
input
.
readline
()
for
col_idx
in
range
(
self
.
nb_cols
):
self
.
maze
[
row_idx
][
col_idx
]
=
line
[
col_idx
]
def
__repr__
(
self
):
rows_str
=
[]
for
row
in
self
.
maze
:
rows_str
.
append
(
""
.
join
(
row
))
return
"
\n
"
.
join
(
rows_str
)
if
__name__
==
"
__main__
"
:
m
=
Maze
(
10
,
10
)
print
(
m
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment