[Echecs] Lisibilité système déplacement et ajout fou et dame

This commit is contained in:
Geoffrey Frogeye 2014-12-06 23:19:27 +01:00
parent 665b963193
commit f71bf87a1f

View file

@ -71,6 +71,77 @@ class LogiqueEchecs:
return (self.ePionNoir(pion) and self.joueur == False) or \ return (self.ePionNoir(pion) and self.joueur == False) or \
(self.ePionBlanc(pion) and self.joueur == True) (self.ePionBlanc(pion) and self.joueur == True)
def mvtPossiblePion(self, x1, y1, x2, y2):
if x1 == x2 and self.grille[x2][y2] <= 0: # Avance
if self.joueur:
if y2 == y1 - 1:
return 1
elif y1 == 6 and y2 == 4 and self.grille[x1][5] == 0:
return MVT_OK
else:
return MVT_N_AUTORISE
else:
if y2 == y1 + 1:
return 1
elif y1 == 1 and y2 == 3 and self.grille[x1][2] == 0:
return MVT_OK
else:
return MVT_N_AUTORISE
elif abs(x1-x2) == 1: # Saut
if self.joueur:
if y2 == y1 - 1 and \
self.ePionNoir(self.grille[x2][y2]):
return MVT_OK
else:
return MVT_N_AUTORISE
else:
if y2 == y1 + 1 and \
self.ePionBlanc(self.grille[x2][y2]):
return MVT_OK
else:
return MVT_N_AUTORISE
else:
return MVT_N_AUTORISE
def mvtPossibleTour(self, x1, y1, x2, y2):
if y1 == y2:
sens = (x2-x1)//abs(x2-x1)
for x in range(x1+sens, x2, sens):
if self.grille[x][y1] > 0:
return MVT_OBSTRUCTION
elif x1 == x2:
sens = (y2-y1)//abs(y2-y1)
for y in range(y1+sens, y2, sens):
if self.grille[x1][y] > 0:
return MVT_OBSTRUCTION
else:
return MVT_N_AUTORISE
return MVT_OK
def mvtPossibleFou(self, x1, y1, x2, y2):
if abs(x2-x1) == abs(y2-y1):
sensX = (x2-x1)//abs(x2-x1)
sensY = (y2-y1)//abs(y2-y1)
x = x1
y = y1
dist = 0
distTot = abs(x2-x1)
while dist < distTot:
dist += 1
x += sensX
y += sensY
if self.grille[x][y] > 0:
if dist == distTot:
return MVT_OK # Saut
else:
return MVT_OBSTRUCTION
return MVT_OK # Vide
else:
return MVT_N_AUTORISE
def mvtPossibleCavalier(self, x1, y1, x2, y2):
return MVT_PION_INC
def mvtPossible(self, x1, y1, x2, y2): def mvtPossible(self, x1, y1, x2, y2):
pion = self.grille[x1][y1] pion = self.grille[x1][y1]
if self.aSonTour(pion): if self.aSonTour(pion):
@ -78,53 +149,24 @@ class LogiqueEchecs:
if not self.aSonTour(self.grille[x2][y2]): if not self.aSonTour(self.grille[x2][y2]):
tPion = pion % 10 tPion = pion % 10
if tPion == 1: # Pion if tPion == 1: # Pion
if x1 == x2 and self.grille[x2][y2] <= 0: # Avance return self.mvtPossiblePion(x1, y1, x2, y2)
if self.joueur: elif tPion == 2: # Tour
if y2 == y1 - 1: return self.mvtPossibleTour(x1, y1, x2, y2)
return 1 elif tPion == 3: # Cavalier
elif y1 == 6 and y2 == 4: return self.mvtPossibleCavalier(x1, y1, x2, y2)
return MVT_OK elif tPion == 4: # Fou
else: return self.mvtPossibleFou(x1, y1, x2, y2)
return MVT_N_AUTORISE elif tPion == 5: # Dame
else: tour = self.mvtPossibleTour(x1, y1, x2, y2)
if y2 == y1 + 1: fou = self.mvtPossibleFou(x1, y1, x2, y2)
return 1 if tour == MVT_OK or fou == MVT_OK:
elif y1 == 1 and y2 == 3: return MVT_OK
return MVT_OK elif tour == MVT_OBSTRUCTION or fou == MVT_OBSTRUCTION:
else: return MVT_OBSTRUCTION
return MVT_N_AUTORISE
elif abs(x1-x2) == 1: # Saut
if self.joueur:
if y2 == y1 - 1 and \
self.ePionNoir(self.grille[x2][y2]):
return MVT_OK
else:
return MVT_N_AUTORISE
else:
if y2 == y1 + 1 and \
self.ePionBlanc(self.grille[x2][y2]):
return MVT_OK
else:
return MVT_N_AUTORISE
else: else:
return MVT_N_AUTORISE return MVT_N_AUTORISE
elif tPion == 2:
if y1 == y2:
sens = (x2-x1)//abs(x2-x1)
for x in range(x1+sens, x2, sens):
if self.grille[x][y1] > 0:
return MVT_OBSTRUCTION
elif x1 == x2:
sens = (y2-y1)//abs(y2-y1)
for y in range(y1+sens, y2, sens):
if self.grille[x1][y] > 0:
return MVT_OBSTRUCTION
else:
return MVT_N_AUTORISE
return MVT_OK
elif tPion == 6: # Roi elif tPion == 6: # Roi
if x2 <= x1 + 1 and x2 >= x1 - 1 and y2 <= y1 + 1 and \ if abs(x2-x1) <= 1 and abs(y2-y1) <= 1:
y2 >= y1 - 1:
return MVT_OK return MVT_OK
else: else:
return MVT_N_AUTORISE return MVT_N_AUTORISE
@ -150,7 +192,7 @@ class LogiqueEchecs:
test = self.mvtPossible(x1, y1, x2, y2) test = self.mvtPossible(x1, y1, x2, y2)
if test == MVT_OK: if test == MVT_OK:
self.grille[x1][y1], self.grille[x2][y2] = 0, self.grille[x1][y1] self.grille[x1][y1], self.grille[x2][y2] = 0, self.grille[x1][y1]
self.joueur = not self.joueur # self.joueur = not self.joueur
return test return test
# GUI # GUI