Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Projets Sudoku ARP
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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Cours L3 Info
Projets Sudoku ARP
Commits
375db74e
Commit
375db74e
authored
7 months ago
by
(Fanny DE WEERDT)
Browse files
Options
Downloads
Patches
Plain Diff
correction des fautes
parent
f9cf08e5
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
sudoku.py
+20
-20
20 additions, 20 deletions
sudoku.py
with
20 additions
and
20 deletions
sudoku.py
+
20
−
20
View file @
375db74e
...
...
@@ -2,7 +2,7 @@
---------------------------------------------------------------------------------------------
ARP - L3 - 2024
Projets : Sudoku
Ce programme devrait résoudre le problème de la r
e
solution d
'
un sudoku.
Ce programme devrait résoudre le problème de la r
é
solution d
'
un sudoku.
---------------------------------------------------------------------------------------------
"""
...
...
@@ -17,7 +17,7 @@ class State:
"""
Un état décrit une grille MxM remplie d’entiers (0 à M).
O indique que la case est vide.
Un autre chiffre indique que la case est rempli
s
par ce nombre
s
Un autre chiffre indique que la case est rempli
e
par ce nombre
"""
def
__init__
(
self
,
grid
:
List
[
List
[
int
]],
move
:
Tuple
[
int
,
int
])
->
None
:
...
...
@@ -27,7 +27,7 @@ class State:
self
.
N_COLS
=
len
(
self
.
grid
[
0
])
self
.
last_move
=
move
if
self
.
N_COLS
!=
self
.
N_COLS
:
assert
False
,
"
ERROR:
La grille est incorrecte
(N
o
mb
re de colonne
different
du
n
o
mb
re de ligne
).
"
assert
False
,
"
ERROR:
invalid grid format
(N
u
mb
er of columns
different
from
n
u
mb
er of rows
).
"
self
.
score
=
self
.
get_nb_case_vide
()
def
__repr__
(
self
)
->
str
:
...
...
@@ -56,7 +56,7 @@ class State:
return
description
def
get_nb_case_vide
(
self
)
->
int
:
'''
Renvoie le nombre de case déjà visités.
'''
'''
Renvoie le nombre de case
s
déjà visité
e
s.
'''
i
=
0
for
row
in
self
.
grid
:
for
num
in
row
:
...
...
@@ -80,7 +80,7 @@ class Sudoku_Problem:
def
initial_state
(
self
)
->
State
:
"""
Renvoie l’état initial
"""
#file_path = self.choisir_fichier_au_hasard("grid")
file_path
=
"
grid/sudoku_74.txt
"
# grille de test
e
file_path
=
"
grid/sudoku_74.txt
"
# grille de test
try
:
with
open
(
file_path
,
'
r
'
)
as
file
:
grid
=
[]
...
...
@@ -95,7 +95,7 @@ class Sudoku_Problem:
def
est_vide
(
self
,
state
:
State
,
action
:
Tuple
[
Tuple
[
int
,
int
],
int
])
->
bool
:
return
state
.
grid
[
action
[
0
][
0
]][
action
[
0
][
1
]]
==
0
def
accept_carre
e
(
self
,
state
:
State
,
action
:
Tuple
[
Tuple
[
int
,
int
],
int
])
->
bool
:
def
accept_carre
(
self
,
state
:
State
,
action
:
Tuple
[
Tuple
[
int
,
int
],
int
])
->
bool
:
rc
=
int
(
math
.
sqrt
(
state
.
N_ROWS
))
for
i
in
range
((
action
[
0
][
0
]
//
rc
)
*
rc
,
(
action
[
0
][
0
]
//
rc
+
1
)
*
rc
):
for
j
in
range
((
action
[
0
][
1
]
//
rc
)
*
rc
,
(
action
[
0
][
1
]
//
rc
+
1
)
*
rc
):
...
...
@@ -123,7 +123,7 @@ class Sudoku_Problem:
def
last_in_carre
e
(
self
,
state
:
State
,
case
:
Tuple
[
int
,
int
])
->
(
bool
,
Tuple
[
Tuple
[
int
,
int
],
int
]):
def
last_in_carre
(
self
,
state
:
State
,
case
:
Tuple
[
int
,
int
])
->
(
bool
,
Tuple
[
Tuple
[
int
,
int
],
int
]):
nombre
=
[
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
]
rc
=
int
(
math
.
sqrt
(
state
.
N_ROWS
))
cellule
=
(
-
1
,
-
1
)
...
...
@@ -197,51 +197,51 @@ class Sudoku_Problem:
"""
Renvoie les actions qui peuvent être exécutées dans l’état donné.
"""
actions_possible
=
[]
# Si
m
a grille a un chiffre ou il ne manque qu'une seule case,
je la
détermin
e
et
je renvoie
l'action associée.
# Si
l
a grille a un chiffre ou
s'
il ne manque qu'une seule case,
il est
détermin
é
et l'action associée
est retournée
.
for
nombre
in
range
(
state
.
N_ROWS
):
boolean
,
action
=
self
.
last_number
(
state
,
nombre
+
1
)
if
boolean
:
if
self
.
est_vide
(
state
,
action
)
and
self
.
accept_carre
e
(
state
,
action
)
and
self
.
accept_column
(
state
,
action
)
and
self
.
accept_row
(
state
,
action
):
if
self
.
est_vide
(
state
,
action
)
and
self
.
accept_carre
(
state
,
action
)
and
self
.
accept_column
(
state
,
action
)
and
self
.
accept_row
(
state
,
action
):
actions_possible
.
append
(
action
)
return
actions_possible
else
:
return
[]
# Si
m
a grille a une ligne ou il ne manque un seul chiffre,
je le
détermin
e
et
je renvoie
l'action associée.
# Si
l
a grille a une ligne ou
s'
il ne manque
qu'
un seul chiffre,
il est
détermin
é
et l'action associée
est retournée
.
for
ligne
in
range
(
state
.
N_ROWS
):
boolean
,
action
=
self
.
last_in_row
(
state
,
(
0
,
ligne
))
if
boolean
:
if
self
.
est_vide
(
state
,
action
)
and
self
.
accept_carre
e
(
state
,
action
)
and
self
.
accept_column
(
state
,
action
)
and
self
.
accept_row
(
state
,
action
):
if
self
.
est_vide
(
state
,
action
)
and
self
.
accept_carre
(
state
,
action
)
and
self
.
accept_column
(
state
,
action
)
and
self
.
accept_row
(
state
,
action
):
actions_possible
.
append
(
action
)
return
actions_possible
else
:
return
[]
# Si
m
a grille a une colonne ou il ne manque un seul chiffre,
je le
détermin
e
et
je renvoie
l'action associée.
# Si
l
a grille a une colonne ou
s'
il ne manque
qu'
un seul chiffre,
il est
détermin
é
et l'action associée
est retournée
.
for
colonne
in
range
(
state
.
N_COLS
):
boolean
,
action
=
self
.
last_in_column
(
state
,
(
colonne
,
0
))
if
boolean
:
if
self
.
est_vide
(
state
,
action
)
and
self
.
accept_carre
e
(
state
,
action
)
and
self
.
accept_column
(
state
,
action
)
and
self
.
accept_row
(
state
,
action
):
if
self
.
est_vide
(
state
,
action
)
and
self
.
accept_carre
(
state
,
action
)
and
self
.
accept_column
(
state
,
action
)
and
self
.
accept_row
(
state
,
action
):
actions_possible
.
append
(
action
)
return
actions_possible
else
:
return
[]
# Si
m
a grille a un carre
e
ou il ne manque un seul chiffre,
je le
détermin
e
et
je renvoie
l'action associée.
# Si
l
a grille a un carre ou
s'
il ne manque
qu'
un seul chiffre,
il est
détermin
é
et l'action associée
est retournée
.
for
row
in
range
(
3
):
for
col
in
range
(
3
):
boolean
,
action
=
self
.
last_in_column
(
state
,
(
row
*
3
,
col
*
3
))
if
boolean
:
if
self
.
est_vide
(
state
,
action
)
and
self
.
accept_carre
e
(
state
,
action
)
and
self
.
accept_column
(
state
,
action
)
and
self
.
accept_row
(
state
,
action
):
if
self
.
est_vide
(
state
,
action
)
and
self
.
accept_carre
(
state
,
action
)
and
self
.
accept_column
(
state
,
action
)
and
self
.
accept_row
(
state
,
action
):
actions_possible
.
append
(
action
)
return
actions_possible
else
:
return
[]
# Sinon,
je genere
toute les actions possible et imaginable et
retourne
toute celle qui sont possibles dans
m
a grille
# Sinon, toute
s
les actions possible
s sont générées
et imaginable
s
et toute
s
celle
s
qui sont possibles dans
l
a grille
sont retournées
list_actions
:
List
[
Tuple
[
Tuple
[
int
,
int
],
int
]]
=
[((
i
,
j
),
n
)
for
i
in
range
(
state
.
N_COLS
)
for
j
in
range
(
state
.
N_ROWS
)
for
n
in
range
(
state
.
N_COLS
)]
for
action
in
list_actions
:
if
self
.
est_vide
(
state
,
action
)
and
self
.
accept_carre
e
(
state
,
action
)
and
self
.
accept_column
(
state
,
action
)
and
self
.
accept_row
(
state
,
action
):
if
self
.
est_vide
(
state
,
action
)
and
self
.
accept_carre
(
state
,
action
)
and
self
.
accept_column
(
state
,
action
)
and
self
.
accept_row
(
state
,
action
):
actions_possible
.
append
(
action
)
return
actions_possible
...
...
@@ -270,12 +270,12 @@ class Node:
"""
Modélise un nœud dans une arborescence de recherche. Un nœud contient :
- une description de l’état réel de ce nœud
- un pointeur vers son parent (le nœud dont il est le successeur)
- l’action qui nous a amené
s
à cet état
- l’action qui nous a amené à cet état
- Le coût total du chemin pour atteindre le nœud à partir du nœud racine.
"""
def
__init__
(
self
,
state
:
State
,
parent
=
None
,
action
=
None
,
path_cost
=
0
):
"""
Cré
ez
un nœud d’arborescence de recherche, dérivé d’une parente par une action.
"""
"""
Cré
é
un nœud d’arborescence de recherche, dérivé d’une parente par une action.
"""
self
.
state
=
state
self
.
parent
=
parent
self
.
action
=
action
...
...
@@ -309,7 +309,7 @@ class Node:
return
[
self
.
child_node
(
problem
,
action
)
for
action
in
problem
.
actions
(
self
.
state
)]
def
solution
(
self
)
->
List
[
Tuple
[
int
,
int
]]:
"""
Retourne
z
la séquence d’actions pour aller de la racine à ce nœud.
"""
"""
Retourne la séquence d’actions pour aller de la racine à ce nœud.
"""
return
[
node
.
action
for
node
in
self
.
path
()[
1
:]]
def
path
(
self
)
->
List
[
"
Node
"
]:
...
...
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