[Echecs] Ajout vérification Echec et Mat

This commit is contained in:
Geoffrey Frogeye 2014-12-07 14:21:13 +01:00
parent b11dce6472
commit 6cdd795602

View file

@ -20,6 +20,9 @@ class LogiqueEchecs:
self.cGrille()
self.remplirGrille()
self.joueur = True
self.partieFinie = False
self.victorieux = None
self.nvPartie()
@staticmethod
@ -34,12 +37,14 @@ class LogiqueEchecs:
self.grille.append(colonne)
def remplirGrille(self):
speciales = [2, 3, 4, 6, 5, 4, 3, 2]
for i in range(0, 8):
self.grille[i][0] = speciales[i] + 10
self.grille[i][1] = 11
self.grille[i][6] = 1
self.grille[i][7] = speciales[i]
# speciales = [2, 3, 4, 6, 5, 4, 3, 2]
# for i in range(0, 8):
# self.grille[i][0] = speciales[i] + 10
# self.grille[i][1] = 11
# self.grille[i][6] = 1
# self.grille[i][7] = speciales[i]
self.grille = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 4, 0, 0, 0, 0], [0, 2, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 6], [0, 0, 0, 3, 0, 0, 0, 0], [16, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
self.joueur = False
def nvPartie(self):
self.cGrille()
@ -222,6 +227,20 @@ class LogiqueEchecs:
self.grille[x1][y1], self.grille[x2][y2] = 0, self.grille[x1][y1]
self.joueur = not self.joueur
def vEchecMat(self):
"""
Vérifie si le joueur actuel est en échec et mat et prend les mesures nécessiares.
(CÀD Le joueur actuel ne peut effectuer aucun mouvement)
"""
for x in range(0, CASES_COTE):
for y in range(0, CASES_COTE):
if len(self.mvtsPossibles(x, y)) > 0:
print(x, y, self.mvtsPossibles(x, y))
return False
self.partieFinie = True
self.victorieux = not self.joueur
return True
def dPion(self, x1, y1, x2, y2):
test = self.mvtPossible(x1, y1, x2, y2)
retour = {
@ -229,6 +248,7 @@ class LogiqueEchecs:
'message': test,
'deplacer': [], # Pions à déplacer
'supprimer': [], # Pions à supprimer
'echecMat': False
}
if test == MVT_OK:
retour['valide'] = True
@ -237,6 +257,7 @@ class LogiqueEchecs:
retour['deplacer'].append([x1, y1, x2, y2])
self.grille[x1][y1], self.grille[x2][y2] = 0, self.grille[x1][y1]
self.joueur = not self.joueur
self.vEchecMat()
return retour
# GUI
@ -387,12 +408,18 @@ class PlateauTk:
self.cPion(x, y, j_grilleF[x][y])
# Interaction
def statutPrendre(self):
if self.logique.joueur:
joueur = 'blancs'
@staticmethod
def nomJoueur(joueur, pluriel=True):
if joueur:
nom = 'blanc'
else:
joueur = 'noirs'
self.statut('Prendre (' + joueur + ')')
nom = 'noir'
if pluriel:
nom += 's'
return nom
def statutPrendre(self):
self.statut('Prendre (' + self.nomJoueur(self.logique.joueur) + ')')
@staticmethod
def animationDCoords(i):
@ -477,6 +504,9 @@ class PlateauTk:
}
self.animer(animation)
def victoire(self):
self.statut('Victoire des ' + self.nomJoueur(self.logique.victorieux) + ' !')
self.coulDamier()
def dPion(self, x1, y1, x2, y2):
test = self.logique.dPion(x1, y1, x2, y2)
@ -489,35 +519,40 @@ class PlateauTk:
self.animerD((d[0] + .5) * COTE_CASE, (d[1] + .5) * COTE_CASE, \
(d[2] + .5) * COTE_CASE, (d[3] + .5) * COTE_CASE, \
self.grillePions[d[2]][d[3]])
if test['echecMat']:
print('Win!')
else:
self.statut('Déplacment impossible ! (' + test['message'] + ')')
return test['valide']
def dClic(self, x, y):
# if not len(self.animations):
if self.dEtape: # Prendre
self.dx1, self.dy1 = x, y
self.coulDamier() # Effacement des surbrillances
if self.logique.aSonTour(self.logique.grille[self.dx1][self.dy1]): # Si possible jouer
self.coulCase(self.dx1, self.dy1, 1)
self.mvtPossibleSansEchecs = self.logique.mvtsPossibles(self.dx1, self.dy1) # Surbrillance bleue
for i in self.mvtPossibleSansEchecs: # Surbrillances vertes
self.coulCase(i[0], i[1], 2)
self.statut('Poser')
self.dEtape = not self.dEtape
else: # Si pas pssible jouer
self.coulCase(self.dx1, self.dy1, 3)
self.animerC(self.dx1, self.dy1)
else: # Poser
self.dx2, self.dy2 = x, y
if self.dPion(self.dx1, self.dy1, self.dx2, self.dy2) or (self.dx1 == self.dx2 and self.dy1 == self.dy2): # Si déplacement fait / Annule dépalcement
self.coulDamier() # Effacer Surbrillance
self.dEtape = not self.dEtape
self.statutPrendre()
else: # Si mauvais déplacement
self.coulCase(self.dx2, self.dy2, 3)
self.animerC(self.dx2, self.dy2)
if not self.logique.partieFinie:
if self.dEtape: # Prendre
self.dx1, self.dy1 = x, y
self.coulDamier() # Effacement des surbrillances
if self.logique.aSonTour(self.logique.grille[self.dx1][self.dy1]): # Si possible jouer
self.coulCase(self.dx1, self.dy1, 1)
self.mvtPossibleSansEchecs = self.logique.mvtsPossibles(self.dx1, self.dy1) # Surbrillance bleue
for i in self.mvtPossibleSansEchecs: # Surbrillances vertes
self.coulCase(i[0], i[1], 2)
self.statut('Poser')
self.dEtape = not self.dEtape
else: # Si pas pssible jouer
self.coulCase(self.dx1, self.dy1, 3)
self.animerC(self.dx1, self.dy1)
else: # Poser
self.dx2, self.dy2 = x, y
if self.dPion(self.dx1, self.dy1, self.dx2, self.dy2) or (self.dx1 == self.dx2 and self.dy1 == self.dy2): # Si déplacement fait / Annule dépalcement
self.coulDamier() # Effacer Surbrillance
self.dEtape = not self.dEtape
if self.logique.partieFinie:
self.victoire()
else:
self.statutPrendre()
else: # Si mauvais déplacement
self.coulCase(self.dx2, self.dy2, 3)
self.animerC(self.dx2, self.dy2)
def clic(self, event):
x = event.x // COTE_CASE