From f71bf87a1ffe393b2b395bbf51fda0e6dcc8e6d6 Mon Sep 17 00:00:00 2001 From: Geoffrey Frogeye Date: Sat, 6 Dec 2014 23:19:27 +0100 Subject: [PATCH] =?UTF-8?q?[Echecs]=20Lisibilit=C3=A9=20syst=C3=A8me=20d?= =?UTF-8?q?=C3=A9placement=20et=20ajout=20fou=20et=20dame?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- S1/Echecs/echecs.py | 134 +++++++++++++++++++++++++++++--------------- 1 file changed, 88 insertions(+), 46 deletions(-) diff --git a/S1/Echecs/echecs.py b/S1/Echecs/echecs.py index b497551..1d7f4d6 100755 --- a/S1/Echecs/echecs.py +++ b/S1/Echecs/echecs.py @@ -71,6 +71,77 @@ 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): + 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): pion = self.grille[x1][y1] if self.aSonTour(pion): @@ -78,53 +149,24 @@ class LogiqueEchecs: if not self.aSonTour(self.grille[x2][y2]): tPion = pion % 10 if tPion == 1: # Pion - 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: - return MVT_OK - else: - return MVT_N_AUTORISE - else: - if y2 == y1 + 1: - return 1 - elif y1 == 1 and y2 == 3: - 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 + return self.mvtPossiblePion(x1, y1, x2, y2) + elif tPion == 2: # Tour + return self.mvtPossibleTour(x1, y1, x2, y2) + elif tPion == 3: # Cavalier + return self.mvtPossibleCavalier(x1, y1, x2, y2) + elif tPion == 4: # Fou + return self.mvtPossibleFou(x1, y1, x2, y2) + elif tPion == 5: # Dame + tour = self.mvtPossibleTour(x1, y1, x2, y2) + fou = self.mvtPossibleFou(x1, y1, x2, y2) + if tour == MVT_OK or fou == MVT_OK: + return MVT_OK + elif tour == MVT_OBSTRUCTION or fou == MVT_OBSTRUCTION: + return MVT_OBSTRUCTION else: - 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 + return MVT_N_AUTORISE elif tPion == 6: # Roi - if x2 <= x1 + 1 and x2 >= x1 - 1 and y2 <= y1 + 1 and \ - y2 >= y1 - 1: + if abs(x2-x1) <= 1 and abs(y2-y1) <= 1: return MVT_OK else: return MVT_N_AUTORISE @@ -150,7 +192,7 @@ class LogiqueEchecs: test = self.mvtPossible(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 + # self.joueur = not self.joueur return test # GUI