This repository has been archived on 2019-08-09. You can view files and clone it, but cannot push or open issues or pull requests.
s1-tp/S1/Echecs/echecs.py

240 lines
7.2 KiB
Python
Raw Normal View History

2014-11-20 18:59:50 +01:00
CASES_COTE = 8
class LogiqueEchecs:
# grille = None
# joueur = True
def __init__(self, self):
self.grille = []
self.cGrille()
self.remplirGrille()
self.joueur = True
self.nvPartie()
@staticmethod
def eNoir(xD, yD): # TODO Peut être considérablement amélioré
i = 1
for x in range(0, CASES_COTE):
i += 1
for y in range(0, CASES_COTE):
i += 1
if x == xD and y == yD:
return i%2
def cGrille(self, self):
for x in range(CASES_COTE):
colonne = []
for y in range(CASES_COTE):
colonne.append(0)
self.grille.append(colonne)
def remplirGrille(self, 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]
def nvPartie(self, self):
self.cGrille()
self.remplirGrille()
self.joueur = True
def cPion(self, self, x, y, piece):
"""
"""
self.grille[x][y] = piece
return True
def mvtPossible(self, self, x1, y1, x2, y2):
pion = self.grille[x1][y1]
if (pion >= 10 and self.joueur == False) or \
(pion < 10 and self.joueur == True):
if pion > 0 and self.grille[x2][y2] <= 0:
tPion = pion%10
if tPion == 1:
if x1 == x2:
if self.joueur:
if y2 == y1-1:
return True
else:
return -4
else:
if y2 == y1+1:
return True
else:
return -4
else:
return -4
elif tPion == 6:
if x2 <= x1+1 and x2 >= x1-1 and y2 <= y1+1 and y2 >= y1-1 \
and (x1 != x2 or y1 != y2):
return True
else:
return -4
else:
return -3
return True
else:
return -2
else:
return -1
def dPion(self, self, x1, y1, x2, y2):
test = self.mvtPossible(x1, y1, x2, y2)
if test == True:
self.grille[x2][y2] = self.grille[x1][y1]
self.grille[x1][y1] = 0
# self.joueur = not self.joueur # DEBUG
return True
else:
return test
# GUI
from tkinter import *
DECX = 0
DECY = 0
COTE_CASE = 50
MARGE_PIONS = 5
TEMPS_ANIM = 200
INTER_ANIM = 10
class PlateauTkAnim:
def __init__(self, can, x1, x2, y1, y2, pion):
self.can = can
self.x1 = x1
self.y1 = y1
self.x2 = x2
self.y2 = y2
self.pion = pion
self.anim = anim
final()
# def dPionAnim(self):
# x = self.x1 + (self.x2-self.x1) * (self.anim/TEMPS_ANIM)
# y = self.y1 + (self.y2-self.y1) * (self.anim/TEMPS_ANIM)
# can.coords(self.pion, x, y)
# self.anim += INTER_ANIM
# if self.anim < TEMPS_ANIM:
# fen.after(INTER_ANIM, self.dPionAnim)
# else:
# self.dPionFinal()
def final(self):
self.can.coords(self.pion, self.x2, self.y2)
class PlateauTk:
def __init__(self):
self.imagesOriginales = []
self.imagesRedim = []
self.grilleDamier = None
self.grillePions = None
self.photos = []
self.fen = None
self.can = None
self.chaine = None
self.creerFen()
self.importerImages()
self.redimImages()
def creerFen(self):
self.fen = Tk()
self.fen.title("Jeu d'Échecs")
self.can = Canvas(self.fen, width=COTE_CASE*CASES_COTE, \
height=COTE_CASE*CASES_COTE, bg="ivory")
self.can.grid(row=0, column=1, columnspan=3)
# self.can.bind('<Button-1>', f_clic)
self.chaine = Label(self.fen, text="Aux blancs")
self.chaine.grid(row=2, column=2, padx=3, pady=3)
# Button(self.fen, text="Nv. Partie", command=f_nvPartie).grid(row=2, \
# column=1, padx=3, pady=3)
Button(self.fen, text="Quitter", command=self.fen.destroy).grid(row=2, \
column=3, padx=3, pady=3)
def statut(self, texte, delai=0):
self.chaine.config(text=texte)
# TODO Timeout effacer si parametre / Liste
def importerImages(self):
for piece in range(0, 21):
nom = 'sprites/'
if piece%10 == 1:
nom += 'pion'
elif piece%10 == 2:
nom += 'tour'
elif piece%10 == 3:
nom += 'cavalier'
elif piece%10 == 4:
nom += 'fou'
elif piece%10 == 5:
nom += 'dame'
elif piece%10 == 6:
nom += 'roi'
else:
self.imagesOriginales.append('')
continue
if piece < 10:
nom += 'B'
else:
nom += 'N'
nom += '.gif'
self.imagesOriginales.append(PhotoImage(file=nom))
def redimImages(self):
sample = int(504/(COTE_CASE-MARGE_PIONS))
for piece in range(0, 21):
if self.imagesOriginales[piece] != '':
self.imagesRedim.append(self.imagesOriginales[piece].subsample(sample))
else:
self.imagesRedim.append('')
def cCase(self, x, y):
if LogiqueEchecs.eNoir(x, y):
couleur = '#D18B47'
else:
couleur = '#FFCE9E'
return self.can.create_rectangle(x*COTE_CASE, y*COTE_CASE, \
(x+1)*COTE_CASE, (y+1)*COTE_CASE, fill=couleur)
def cDamier(self):
self.grilleDamier = []
for x in range(0, CASES_COTE):
colonne = []
for y in range(0, CASES_COTE):
colonne.append(self.cCase(x + DECX, y + DECY))
self.grilleDamier.append(colonne)
def cPion(self, x, y, piece):
if piece > 0:
self.grillePions[x][y] = can.create_image((x+.5)*COTE_CASE, \
(y+.5)*COTE_CASE, image=self.imagesRedim[piece])
else:
self.grillePions[x][y] = False
def dPion(self, x1, y1, x2, y2):
# TODO Bloquer l'entrée pendant l'anim
self.grillePions[x2][y2] = self.grillePions[x1][y1]
self.grillePions[x1][y1] = False
PlateauTkAnim(self.can, (x1+.5)*COTE_CASE, (y1+.5)*COTE_CASE, \
(x2+.5)*COTE_CASE, (y2+.5)*COTE_CASE, self.grillePions[x1][y1])
def cGrille(self):
self.grillePions = []
for x in range(0, CASES_COTE): # Crée self.grillePions
colonne = []
for y in range(0, CASES_COTE):
colonne.append(False)
self.grillePions.append(colonne)
def remplirGrille(self, j_grilleF):
for x in range(0, CASES_COTE): # Remplis self.grillePions
for y in range(0, CASES_COTE):
self.cPion(x, y, j_grilleF[x][y])