[Echecs] Empêche de mettre son roi en Echec

This commit is contained in:
Geoffrey Frogeye 2014-12-07 13:45:03 +01:00
parent 39ba53fcb8
commit b11dce6472

View file

@ -1,3 +1,5 @@
import copy
CASES_COTE = 8
MVT_INCONNU = 'Cause inconnue'
@ -9,6 +11,7 @@ MVT_SAUT_AMI = 'Saut ami'
MVT_PION_INC = 'Pion inconnu'
MVT_N_AUTORISE = 'Non-autorisé'
MVT_OBSTRUCTION = 'Pion en chemin'
MVT_ECHEC = 'Échec au roi'
class LogiqueEchecs:
@ -62,7 +65,7 @@ class LogiqueEchecs:
return (self.ePionNoir(pion) and self.joueur == False) or \
(self.ePionBlanc(pion) and self.joueur == True)
def mvtPossiblePion(self, x1, y1, x2, y2):
def mvtPossibleSansEchecPion(self, x1, y1, x2, y2):
if x1 == x2 and self.grille[x2][y2] <= 0: # Avance
if self.joueur:
if y2 == y1 - 1:
@ -94,7 +97,7 @@ class LogiqueEchecs:
else:
return MVT_N_AUTORISE
def mvtPossibleTour(self, x1, y1, x2, y2):
def mvtPossibleSansEchecTour(self, x1, y1, x2, y2):
if y1 == y2:
sens = (x2-x1)//abs(x2-x1)
for x in range(x1+sens, x2, sens):
@ -109,7 +112,7 @@ class LogiqueEchecs:
return MVT_N_AUTORISE
return MVT_OK
def mvtPossibleFou(self, x1, y1, x2, y2):
def mvtPossibleSansEchecFou(self, x1, y1, x2, y2):
if abs(x2-x1) == abs(y2-y1):
sensX = (x2-x1)//abs(x2-x1)
sensY = (y2-y1)//abs(y2-y1)
@ -130,29 +133,29 @@ class LogiqueEchecs:
else:
return MVT_N_AUTORISE
def mvtPossibleCavalier(self, x1, y1, x2, y2):
def mvtPossibleSansEchecCavalier(self, x1, y1, x2, y2):
if (abs(x2-x1) == 2 and abs(y2-y1) == 1) or (abs(y2-y1) == 2 and abs(x2-x1) == 1):
return MVT_OK
else:
return MVT_N_AUTORISE
def mvtPossible(self, x1, y1, x2, y2):
def mvtPossibleSansEchec(self, x1, y1, x2, y2):
pion = self.grille[x1][y1]
if self.aSonTour(pion):
if (x1 != x2 or y1 != y2):
if not self.aSonTour(self.grille[x2][y2]):
tPion = pion % 10
if tPion == 1: # Pion
return self.mvtPossiblePion(x1, y1, x2, y2)
return self.mvtPossibleSansEchecPion(x1, y1, x2, y2)
elif tPion == 2: # Tour
return self.mvtPossibleTour(x1, y1, x2, y2)
return self.mvtPossibleSansEchecTour(x1, y1, x2, y2)
elif tPion == 3: # Cavalier
return self.mvtPossibleCavalier(x1, y1, x2, y2)
return self.mvtPossibleSansEchecCavalier(x1, y1, x2, y2)
elif tPion == 4: # Fou
return self.mvtPossibleFou(x1, y1, x2, y2)
return self.mvtPossibleSansEchecFou(x1, y1, x2, y2)
elif tPion == 5: # Dame
tour = self.mvtPossibleTour(x1, y1, x2, y2)
fou = self.mvtPossibleFou(x1, y1, x2, y2)
tour = self.mvtPossibleSansEchecTour(x1, y1, x2, y2)
fou = self.mvtPossibleSansEchecFou(x1, y1, x2, y2)
if tour == MVT_OK or fou == MVT_OK:
return MVT_OK
elif tour == MVT_OBSTRUCTION or fou == MVT_OBSTRUCTION:
@ -173,6 +176,37 @@ class LogiqueEchecs:
else:
return MVT_SELECTION
return MVT_INCONNU
def mvtPossible(self, x1, y1, x2, y2):
test = self.mvtPossibleSansEchec(x1, y1, x2, y2)
if test == MVT_OK:
# On copie la partie actuelle pour tester le mouvement et vérifier l'échec
copie = copy.deepcopy(self);
copie.dPionSansEchec(x1, y1, x2, y2)
mvtsPossiblesTousAdverses = []
pionRoi = 6
if not self.joueur:
pionRoi += 10
roi = [-1, -1]
for x in range(0, CASES_COTE):
for y in range(0, CASES_COTE):
mvtsPossiblesTousAdverses += copie.mvtsPossiblesSansEchec(x, y)
if copie.grille[x][y] == pionRoi:
roi = [x, y]
if roi in mvtsPossiblesTousAdverses:
return MVT_ECHEC
else:
return test
else:
return test
def mvtsPossiblesSansEchec(self, x1, y1):
tableau = []
for x2 in range(0, CASES_COTE):
for y2 in range(0, CASES_COTE):
if self.mvtPossibleSansEchec(x1, y1, x2, y2) == MVT_OK:
tableau.append([x2, y2])
return tableau
def mvtsPossibles(self, x1, y1):
tableau = []
@ -182,6 +216,12 @@ class LogiqueEchecs:
tableau.append([x2, y2])
return tableau
def dPionSansEchec(self, x1, y1, x2, y2):
test = self.mvtPossibleSansEchec(x1, y1, x2, y2)
if test == MVT_OK:
self.grille[x1][y1], self.grille[x2][y2] = 0, self.grille[x1][y1]
self.joueur = not self.joueur
def dPion(self, x1, y1, x2, y2):
test = self.mvtPossible(x1, y1, x2, y2)
retour = {
@ -460,8 +500,8 @@ class PlateauTk:
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.mvtPossibles = self.logique.mvtsPossibles(self.dx1, self.dy1) # Surbrillance bleue
for i in self.mvtPossibles: # Surbrillances vertes
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