Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
ap-Tanoh mbah ange pascal
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
Package registry
Model registry
Operate
Environments
Terraform modules
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
M'Bah Ange Pascal Tanoh
ap-Tanoh mbah ange pascal
Commits
a721ff1c
Commit
a721ff1c
authored
1 year ago
by
Tanoh Mbah-ange-pascal
Browse files
Options
Downloads
Patches
Plain Diff
dernier ajout
parent
f014d0f5
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
TP5/minesweeper/minesweeper.py
+45
-24
45 additions, 24 deletions
TP5/minesweeper/minesweeper.py
with
45 additions
and
24 deletions
TP5/minesweeper/minesweeper.py
+
45
−
24
View file @
a721ff1c
...
...
@@ -46,7 +46,7 @@ def neighborhood(x: int, y: int, width: int, height: int) -> list[tuple[int, int
return the list of coordinates of the neighbors of position (x,y) in a
grid of size width*height
précondition: 0 <= x < width and 0 <= y < height
préconditi
$$$ game = Minesweeper(20, 10, 4)
on: 0 <= x < width and 0 <= y < height
examples:
...
...
@@ -62,18 +62,13 @@ def neighborhood(x: int, y: int, width: int, height: int) -> list[tuple[int, int
[(2, 8), (2, 9), (3, 8), (4, 8), (4, 9)]
"""
neighbors
=
[]
for
distance_x
in
range
(
-
1
,
2
):
for
distance_y
in
range
(
-
1
,
2
):
x_1
,
y_1
=
x
+
distance_x
,
y
+
distance_y
if
0
<=
x_1
<
width
and
0
<=
y_1
<
height
and
(
x_1
,
y_1
)
!=
(
x
,
y
):
neighbors
.
append
((
x_1
,
y_1
))
for
i
in
range
(
x
-
1
,
x
+
2
):
for
j
in
range
(
y
-
1
,
y
+
2
):
if
(
i
,
j
)
!=
(
x
,
y
)
and
0
<=
i
<
width
and
0
<=
j
<
height
:
neighbors
.
append
((
i
,
j
))
return
neighbors
class
Minesweeper
():
"""
$$$ game = Minesweeper(20, 10, 4)
...
...
@@ -90,6 +85,14 @@ class Minesweeper():
False
$$$
"""
def
__init__
(
self
,
width
:
int
=
30
,
height
:
int
=
20
,
nbombs
:
int
=
99
):
"""
...
...
@@ -114,19 +117,18 @@ class Minesweeper():
$$$ game.state == GameState.unfinished
True
"""
self
.
width
=
width
self
.
height
=
height
self
.
nbombs
=
nbombs
self
.
state
=
GameState
.
unfinished
self
.
grid
=
[[
cell
()
for
i
in
range
(
width
)]
for
j
in
range
(
height
)]
self
.
width
=
width
self
.
height
=
height
self
.
nbombs
=
nbombs
self
.
state
=
GameState
.
unfinished
n
=
0
while
n
<
nbombs
:
y
=
random
.
randint
(
0
,
width
-
1
)
x
=
random
.
randint
(
0
,
height
-
1
)
if
not
sel
f
.
grid
[
x
][
y
].
is_bomb
:
self
.
_
put_a_bomb_at
(
x
,
y
)
y
=
randint
(
0
,
width
-
1
)
x
=
randint
(
0
,
height
-
1
)
if
not
sel
.
grid
[
x
][
y
].
is_bomb
:
self
.
put_a_bomb_at
(
x
,
y
)
n
=
n
+
1
def
get_cell
(
self
,
x
:
int
,
y
:
int
)
->
Cell
:
"""
...
...
@@ -138,7 +140,7 @@ class Minesweeper():
$$$ sum(1 for x in range(20) for y in range(10) if game.get_cell(x, y).is_bomb)
4
"""
...
return
self
.
_grid
[
y
][
x
]
def
_put_a_bomb_at
(
self
,
x
:
int
,
y
:
int
):
"""
...
...
@@ -163,7 +165,19 @@ class Minesweeper():
$$$ all(game.get_cell(x, y).nbombs_in_neighborhood == 1 for x, y in voisins)
True
"""
...
if
0
<=
x
<
self
.
width
and
0
<=
y
<
self
.
height
:
case
=
self
.
grid
[
y
][
x
]
if
not
case
.
nbombs_in_neighborhood
:
case
.
nbombs_in_neighborhood
=
True
for
i
in
range
(
x
-
1
,
x
+
2
):
for
j
in
range
(
y
-
1
,
y
+
2
):
if
0
<=
i
<
self
.
width
and
0
<=
j
<
self
.
height
:
case_voisine
=
self
.
grid
[
j
][
i
]
if
not
case_voisine
.
nbombs_in_neighborhood
:
case_voisine
.
nbombs_in_neighborhood
+=
1
def
all_cells_are_revealed_or_bomb
(
self
)
->
bool
:
"""
...
...
@@ -210,7 +224,14 @@ class Minesweeper():
$$$ game.state
GameState.losing
"""
...
res
=
True
for
i
in
range
(
self
.
width
):
for
j
in
range
(
self
.
height
):
cell
=
self
.
grid
[
j
][
i
]
if
not
(
cell
.
is_revealed
and
cell
.
is_bomb
):
res
=
False
return
res
if
(
__name__
==
'
__main__
'
):
...
...
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