Commit 890e6e34 authored by Maël RENAULT's avatar Maël RENAULT
Browse files

Update IHM_Sudoku.py

parent 2ff76cea
......@@ -5,6 +5,11 @@ import copy
from PyQt5.QtGui import *
from Classes import *
import copy
from PyQt5.QtGui import QFont
from datetime import datetime
from time import gmtime, strftime
class Button(QToolButton):
def __init__(self, text, parent=None):
......@@ -15,39 +20,42 @@ class Button(QToolButton):
def sizeHint(self):
size = super(Button, self).sizeHint()
size.setHeight(size.height() + 20)
size.setHeight(size.height() + 30)
size.setWidth(max(size.width(), size.height()))
return size
class Grille_sudoku(QWidget):
Numcases = 82
ordi = Ordinateur()
k = True
def __init__(self, parent=None):
super(Grille_sudoku, self).__init__(parent)
painter = QPainter()
painter.begin(self)
painter.setRenderHint(QPainter.Antialiasing)
painter.setPen(Qt.red)
painter.setBrush(Qt.white)
painter.drawLine(10, 11, 11, -1000)
self.digitButtons = []
self.grille_utilise = None
for i in range(Grille_sudoku.Numcases):
self.digitButtons.append(self.createButton(str(i),
self.setItem))
self.BSolution = QPushButton('Solution')
self.BSolution.clicked.connect(self.donner_solution)
for i in range(0,9):
Grille_sudoku.k = (i//3 == 0 or i//3 == 2)
for j in range(0,3):
if Grille_sudoku.k:
for u in range(0,3):
self.digitButtons.append(self.createButtonGrey("",
self.setItem))
Grille_sudoku.k = False
else:
for u in range(0,3):
self.digitButtons.append(self.createButtonWhite("",
self.setItem))
Grille_sudoku.k = True
mainLayout = QGridLayout()
mainLayout.setSizeConstraint(QLayout.SetFixedSize)
for i in range(0,9):
for j in range(0,9):
mainLayout.addWidget(self.digitButtons[i+(j-1)*9],i,j)
mainLayout.addWidget(self.digitButtons[i+j*9],i,j)
#mainLayout.addWidget(self.BSolution,10,0)
mainLayout.setHorizontalSpacing(15)
mainLayout.setVerticalSpacing(15)
self.setLayout(mainLayout)
#création de la menu bar
......@@ -70,18 +78,25 @@ class Grille_sudoku(QWidget):
self.setWindowTitle("Grille de Sudoku")
def createButton(self, text, member):
def createButtonWhite(self, text, member):
button = Button('')
button.clicked.connect(member)
button.setStyleSheet("background-color : white")
return button
def createButtonGrey(self, text, member):
button = Button('')
button.clicked.connect(member)
button.setStyleSheet("background-color : lightgray")
return button
def charger_une_grille(self,grille):
for i in range(0,len(grille)):
for j in range (0,len(grille)):
if grille[i,j]==0:
self.digitButtons[i + (j - 1) * 9].setText('')
for j in range(0,len(grille)):
if grille[i,j] == 0:
self.digitButtons[i + j * 9].setText('')
else:
self.digitButtons[i+(j-1)*9].setText(str(grille[i,j]))
self.digitButtons[i + j * 9].setText(str(grille[i,j]))
self.grille_utilise = copy.deepcopy(grille)
def setItem(self):
......@@ -92,28 +107,56 @@ class Grille_sudoku(QWidget):
if ok and item:
clickedButton.setText(item)
def donner_solution(self):
Grille_sudoku.ordi.solve(self.grille_utilise)
self.charger_une_grille()
def nouvelle_grille(self):
grl = Grille()
grille = grl.génération(5000)
items = ('Facile','Moyen','Difficile')
item, ok = QInputDialog.getItem(self, "QInputDialog.getItem()",
"Chiffres", items, 0, False)
def newFile(self):
print(1)
if ok and item == 'Facile':
grille = grl.supprimer_cases(15)
elif ok and item == 'Moyen':
grille = grl.supprimer_cases(25)
elif ok and item == 'Difficile':
grille = grl.supprimer_cases(35)
self.charger_une_grille(grille)
def open(self):
print(2)
def save(self):
print(3)
user = "louis"
grille = copy.deepcopy(self.grille_utilise)
nom_fichier = "sauvegarde_" + str(user) + "_" + strftime("%d_%m_%Y_%Hh%Mm%Ss") + ".txt"
fichier = open(nom_fichier, "a")
fichier.write(str(user) + " " + strftime("%d_%m_%Y_%H:%M:%S") + "\n")
for i in range(grille.shape[0]):
for j in range(grille.shape[1]):
if j == (grille.shape[1] - 1):
fichier.write(str(grille[i, j]) + "\n")
else:
fichier.write(str(grille[i, j]))
fichier.close()
def solution(self):
print(4)
ordi = Ordinateur()
ordi.solve(self.grille_utilise)
grille = ordi.solution[0]
print(grille)
self.ajout = Correction()
self.ajout.charger_une_grille2(grille)
self.ajout.show()
def createActions(self):
self.newAct = QAction("&Nouvelle Grille", self, shortcut=QKeySequence.New,
statusTip="Create a new file", triggered=self.newFile)
statusTip="Crée une nouvelle grille", triggered=self.nouvelle_grille)
self.solutionAct = QAction("&Afficher Solution", self, shortcut=QKeySequence.Print,
statusTip="Print the document", triggered=self.solution)
statusTip="Affiche la solution de la grille utilisée", triggered=self.solution)
self.openAct = QAction("&Charger...", self, shortcut=QKeySequence.Open,
statusTip="Open an existing file", triggered=self.open)
......@@ -130,7 +173,77 @@ class Grille_sudoku(QWidget):
self.fileMenu.addAction(self.openAct)
self.fileMenu.addAction(self.saveAct)
class Correction(Grille_sudoku):
Numcases = 82
ordi = Ordinateur()
k = True
compteur = 0
def __init__(self, parent=None):
super(Grille_sudoku, self).__init__(parent)
self.digitButtons = []
for i in range(0, 9):
Grille_sudoku.k = (i // 3 == 0 or i // 3 == 2)
for j in range(0, 3):
if Grille_sudoku.k:
for u in range(0, 3):
self.digitButtons.append(self.createButtonGrey("",
self.setItem))
Grille_sudoku.k = False
else:
for u in range(0, 3):
self.digitButtons.append(self.createButtonWhite("",
self.setItem))
Grille_sudoku.k = True
mainLayout = QGridLayout()
mainLayout.setSizeConstraint(QLayout.SetFixedSize)
for i in range(0, 9):
for j in range(0, 9):
mainLayout.addWidget(self.digitButtons[i + j * 9], i, j)
mainLayout.setHorizontalSpacing(15)
mainLayout.setVerticalSpacing(15)
self.setLayout(mainLayout)
self.setWindowTitle("Solution")
def createButtonWhite(self, text, member):
button = Button('')
button.clicked.connect(member)
button.setStyleSheet("background-color : white")
return button
def createButtonGrey(self, text, member):
button = Button('')
button.clicked.connect(member)
button.setStyleSheet("background-color : lightgray")
return button
def charger_une_grille2(self,grille):
for i in range(0,len(grille)):
for j in range(0,len(grille)):
if grille[i,j] == 0:
self.digitButtons[i + j * 9].setText('')
else:
self.digitButtons[i + j * 9].setText(str(grille[i,j]))
self.grille_utilise = copy.deepcopy(grille)
def setItem(self):
clickedButton = self.sender()
items = ('1','2','3','4','5','6','7','8','9')
item, ok = QInputDialog.getItem(self, "QInputDialog.getItem()",
"Chiffres", items, 0, False)
if ok and item:
clickedButton.setText(item)
if __name__ == '__main__':
......@@ -142,12 +255,11 @@ if __name__ == '__main__':
[4, 5, 6, 7, 8, 9, 1, 2, 3],
[7, 8, 9, 1, 2, 3, 4, 5, 6],
[2, 3, 4, 5, 6, 7, 8, 9, 1],
[5, 6, 0, 8, 9, 1, 2, 3, 4], # génération de la grille élémentaire
[5, 6, 7, 8, 9, 1, 2, 3, 4], # génération de la grille élémentaire
[8, 9, 1, 2, 3, 4, 5, 6, 7],
[3, 4, 5, 6, 7, 8, 9, 1, 2],
[6, 7, 8, 9, 1, 2, 3, 4, 5],
[9, 1, 2, 3, 4, 5, 6, 7, 8]])
calc.charger_une_grille(GE)
calc.show()
sys.exit(app.exec_())
\ No newline at end of file
sys.exit(app.exec_())
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment