2014-11-14 08:51:44 +01:00
|
|
|
# pylint: disable=W0603
|
|
|
|
|
2014-11-07 01:30:48 +01:00
|
|
|
# INFOS
|
|
|
|
|
|
|
|
# Pièces :
|
|
|
|
|
|
|
|
# [1-6] : Blancs
|
|
|
|
# [11-6] : Noirs
|
|
|
|
|
|
|
|
# X1 : Pion
|
|
|
|
# X2 : Tour
|
|
|
|
# X3 : Cavalier
|
|
|
|
# x4 : Fou
|
|
|
|
# X5 : Dame
|
|
|
|
# X6 : Roi
|
|
|
|
|
|
|
|
|
|
|
|
# j_ jeu : le logique du jeu lui même
|
|
|
|
# g_ GUI : l'interface graphique
|
|
|
|
# f_ Frontend : ce qui associe les deux
|
|
|
|
# _e : est
|
|
|
|
# _c : crée
|
|
|
|
# _d : déplace
|
|
|
|
|
|
|
|
|
|
|
|
# IMPORTS
|
|
|
|
from tkinter import *
|
|
|
|
from random import randint
|
|
|
|
|
|
|
|
# FONCTIONS
|
|
|
|
|
|
|
|
|
|
|
|
# Jeu
|
|
|
|
CASES_COTE = 8
|
|
|
|
|
|
|
|
j_grille = None
|
2014-11-14 08:51:44 +01:00
|
|
|
j_joueur = True
|
2014-11-07 01:30:48 +01:00
|
|
|
|
|
|
|
def j_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 j_cGrille():
|
|
|
|
global j_grille
|
|
|
|
j_grille = []
|
|
|
|
for x in range(CASES_COTE):
|
|
|
|
colonne = []
|
|
|
|
for y in range(CASES_COTE):
|
2014-11-14 08:51:44 +01:00
|
|
|
colonne.append(0)
|
2014-11-07 01:30:48 +01:00
|
|
|
j_grille.append(colonne)
|
|
|
|
|
|
|
|
def j_remplirGrille():
|
|
|
|
global j_grille
|
2014-11-14 08:51:44 +01:00
|
|
|
speciales = [2, 3, 4, 6, 5, 4, 3, 2]
|
|
|
|
for i in range(0, 8):
|
|
|
|
j_grille[i][0] = speciales[i] + 10
|
|
|
|
j_grille[i][1] = 11
|
|
|
|
j_grille[i][6] = 1
|
|
|
|
j_grille[i][7] = speciales[i]
|
2014-11-07 01:30:48 +01:00
|
|
|
|
|
|
|
def j_nvPartie():
|
|
|
|
j_cGrille()
|
|
|
|
j_remplirGrille()
|
2014-11-14 08:51:44 +01:00
|
|
|
global j_joueur
|
|
|
|
j_joueur = True
|
2014-11-07 01:30:48 +01:00
|
|
|
|
|
|
|
def j_cPion(x, y, piece):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
j_grille[x][y] = piece
|
|
|
|
return True
|
|
|
|
|
2014-11-14 08:51:44 +01:00
|
|
|
def j_mvtPossible(x1, y1, x2, y2):
|
|
|
|
pion = j_grille[x1][y1]
|
|
|
|
if (pion >= 10 and j_joueur == False) or (pion < 10 and j_joueur == True):
|
|
|
|
if pion > 0 and j_grille[x2][y2] <= 0:
|
|
|
|
tPion = pion%10
|
|
|
|
if tPion == 1:
|
|
|
|
if x1 == x2:
|
|
|
|
if j_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
|
2014-11-07 01:30:48 +01:00
|
|
|
|
2014-11-14 08:51:44 +01:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return -2
|
|
|
|
else:
|
|
|
|
return -1
|
2014-11-07 01:30:48 +01:00
|
|
|
|
2014-11-14 08:51:44 +01:00
|
|
|
def j_dPion(x1, y1, x2, y2):
|
|
|
|
test = j_mvtPossible(x1, y1, x2, y2)
|
|
|
|
if test == True:
|
|
|
|
global j_joueur, j_grille
|
|
|
|
j_grille[x2][y2] = j_grille[x1][y1]
|
|
|
|
j_grille[x1][y1] = 0
|
|
|
|
# j_joueur = not j_joueur # DEBUG
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return test
|
2014-11-07 01:30:48 +01:00
|
|
|
|
|
|
|
# GUI
|
|
|
|
DECX = 0
|
|
|
|
DECY = 0
|
|
|
|
COTE_CASE = 50
|
|
|
|
MARGE_PIONS = 5
|
2014-11-14 08:51:44 +01:00
|
|
|
TEMPS_ANIM = 200
|
|
|
|
INTER_ANIM = 10
|
|
|
|
|
|
|
|
g_imagesOriginales = []
|
|
|
|
g_imagesRedim = []
|
|
|
|
|
2014-11-07 01:30:48 +01:00
|
|
|
|
|
|
|
g_grilleDamier = None
|
|
|
|
g_grillePions = None
|
|
|
|
g_photos = []
|
|
|
|
fen = None
|
|
|
|
can = None
|
|
|
|
chaine = None
|
|
|
|
|
2014-11-14 08:51:44 +01:00
|
|
|
# Animation
|
|
|
|
g_x1 = None
|
|
|
|
g_y1 = None
|
|
|
|
g_x2 = None
|
|
|
|
g_y2 = None
|
|
|
|
g_pion = None
|
|
|
|
g_anim = None
|
|
|
|
|
2014-11-07 01:30:48 +01:00
|
|
|
def g_fen():
|
|
|
|
global fen, can, chaine
|
|
|
|
fen = Tk()
|
|
|
|
fen.title("Jeu d'Échecs")
|
|
|
|
can = Canvas(fen, width=COTE_CASE*CASES_COTE, height=COTE_CASE*CASES_COTE, \
|
|
|
|
bg="ivory")
|
|
|
|
can.grid(row=0, column=1, columnspan=3)
|
|
|
|
can.bind('<Button-1>', f_clic)
|
|
|
|
chaine = Label(fen, text="Aux blancs")
|
|
|
|
chaine.grid(row=2, column=2, padx=3, pady=3)
|
|
|
|
Button(fen, text="Nv. Partie", command=f_nvPartie).grid(row=2, column=1, \
|
|
|
|
padx=3, pady=3)
|
|
|
|
Button(fen, text="Quitter", command=fen.destroy).grid(row=2, column=3, \
|
|
|
|
padx=3, pady=3)
|
|
|
|
|
|
|
|
def g_statut(texte, delai=0):
|
|
|
|
chaine.config(text=texte)
|
2014-11-14 08:51:44 +01:00
|
|
|
# TODO Timeout effacer si parametre / Liste
|
|
|
|
|
|
|
|
def g_importerImages():
|
|
|
|
global g_imagesOriginales
|
|
|
|
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:
|
|
|
|
g_imagesOriginales.append('')
|
|
|
|
continue
|
|
|
|
if piece < 10:
|
|
|
|
nom += 'B'
|
|
|
|
else:
|
|
|
|
nom += 'N'
|
|
|
|
nom += '.gif'
|
|
|
|
g_imagesOriginales.append(PhotoImage(file=nom))
|
|
|
|
|
|
|
|
def g_redimImages():
|
|
|
|
global g_imagesRedim
|
|
|
|
sample = int(504/(COTE_CASE-MARGE_PIONS))
|
|
|
|
for piece in range(0, 21):
|
|
|
|
if g_imagesOriginales[piece] != '':
|
|
|
|
g_imagesRedim.append(g_imagesOriginales[piece].subsample(sample))
|
|
|
|
else:
|
|
|
|
g_imagesRedim.append('')
|
|
|
|
|
2014-11-07 01:30:48 +01:00
|
|
|
|
|
|
|
def g_cCase(x, y):
|
|
|
|
if j_eNoir(x, y):
|
2014-11-14 08:51:44 +01:00
|
|
|
couleur = '#D18B47'
|
2014-11-07 01:30:48 +01:00
|
|
|
else:
|
2014-11-14 08:51:44 +01:00
|
|
|
couleur = '#FFCE9E'
|
2014-11-07 01:30:48 +01:00
|
|
|
return can.create_rectangle(x*COTE_CASE, y*COTE_CASE, \
|
|
|
|
(x+1)*COTE_CASE, (y+1)*COTE_CASE, fill=couleur)
|
|
|
|
|
|
|
|
def g_cDamier():
|
|
|
|
global g_grilleDamier
|
|
|
|
g_grilleDamier = []
|
|
|
|
for x in range(0, CASES_COTE):
|
|
|
|
colonne = []
|
|
|
|
for y in range(0, CASES_COTE):
|
|
|
|
colonne.append(g_cCase(x + DECX, y + DECY))
|
|
|
|
g_grilleDamier.append(colonne)
|
|
|
|
|
|
|
|
def g_cPion(x, y, piece):
|
|
|
|
global g_grillePions
|
|
|
|
global g_photos
|
|
|
|
if piece > 0:
|
2014-11-14 08:51:44 +01:00
|
|
|
g_grillePions[x][y] = can.create_image((x+.5)*COTE_CASE, \
|
|
|
|
(y+.5)*COTE_CASE, image=g_imagesRedim[piece])
|
2014-11-07 01:30:48 +01:00
|
|
|
else:
|
|
|
|
g_grillePions[x][y] = False
|
|
|
|
|
2014-11-14 08:51:44 +01:00
|
|
|
def g_dPionFinal():
|
|
|
|
can.coords(g_pion, g_x2, g_y2)
|
|
|
|
|
|
|
|
def g_dPionAnim():
|
|
|
|
global g_pion, g_anim
|
|
|
|
x = g_x1 + (g_x2-g_x1) * (g_anim/TEMPS_ANIM)
|
|
|
|
y = g_y1 + (g_y2-g_y1) * (g_anim/TEMPS_ANIM)
|
|
|
|
can.coords(g_pion, x, y)
|
|
|
|
g_anim += INTER_ANIM
|
|
|
|
if g_anim < TEMPS_ANIM:
|
|
|
|
fen.after(INTER_ANIM, g_dPionAnim)
|
|
|
|
else:
|
|
|
|
g_dPionFinal()
|
|
|
|
|
2014-11-07 01:30:48 +01:00
|
|
|
def g_dPion(x1, y1, x2, y2):
|
2014-11-14 08:51:44 +01:00
|
|
|
# TODO Bloquer l'entrée pendant l'anim
|
|
|
|
global g_grillePions, g_x1, g_y1, g_x2, g_y2, g_pion, g_anim
|
|
|
|
g_x1 = (x1+.5)*COTE_CASE
|
|
|
|
g_y1 = (y1+.5)*COTE_CASE
|
|
|
|
g_x2 = (x2+.5)*COTE_CASE
|
|
|
|
g_y2 = (y2+.5)*COTE_CASE
|
|
|
|
g_pion = g_grillePions[x1][y1]
|
|
|
|
g_anim = 0
|
|
|
|
# can.coords(g_grillePions[x1][y1], (x2+.5)*COTE_CASE, (y2+.5)*COTE_CASE)
|
|
|
|
g_grillePions[x2][y2] = g_grillePions[x1][y1]
|
2014-11-07 01:30:48 +01:00
|
|
|
g_grillePions[x1][y1] = False
|
2014-11-14 08:51:44 +01:00
|
|
|
g_dPionAnim()
|
|
|
|
# g_dPionFinal()
|
2014-11-07 01:30:48 +01:00
|
|
|
|
|
|
|
def g_cGrille():
|
|
|
|
global g_grillePions
|
|
|
|
g_grillePions = []
|
|
|
|
for x in range(0, CASES_COTE): # Crée g_grillePions
|
|
|
|
colonne = []
|
|
|
|
for y in range(0, CASES_COTE):
|
|
|
|
colonne.append(False)
|
|
|
|
g_grillePions.append(colonne)
|
|
|
|
|
|
|
|
def g_remplirGrille(j_grilleF):
|
|
|
|
global g_grillePions
|
|
|
|
for x in range(0, CASES_COTE): # Remplis g_grillePions
|
|
|
|
for y in range(0, CASES_COTE):
|
|
|
|
g_cPion(x, y, j_grilleF[x][y])
|
|
|
|
|
2014-11-14 08:51:44 +01:00
|
|
|
def g_init():
|
|
|
|
g_fen()
|
|
|
|
g_importerImages()
|
|
|
|
g_redimImages()
|
2014-11-07 01:30:48 +01:00
|
|
|
|
|
|
|
# Frontend
|
2014-11-14 08:51:44 +01:00
|
|
|
|
|
|
|
f_origine = True
|
|
|
|
f_x1 = -1
|
|
|
|
f_y1 = -1
|
|
|
|
f_x2 = -1
|
|
|
|
f_y2 = -1
|
|
|
|
|
|
|
|
def f_statutPrendre():
|
|
|
|
if j_joueur:
|
|
|
|
joueur = 'blancs'
|
|
|
|
else:
|
|
|
|
joueur = 'noirs'
|
|
|
|
g_statut('Prendre (' + joueur + ')')
|
|
|
|
|
|
|
|
def f_init():
|
|
|
|
g_init()
|
|
|
|
f_nvPartie()
|
|
|
|
f_statutPrendre()
|
|
|
|
|
|
|
|
|
|
|
|
def f_cPion(x, y, piece):
|
2014-11-07 01:30:48 +01:00
|
|
|
if j_cPion(x, y, piece):
|
|
|
|
g_cPion(x, y, piece)
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def f_cPionAlea():
|
|
|
|
g_cPion(randint(1, CASES_COTE), randint(1, CASES_COTE), 1)
|
|
|
|
|
|
|
|
def f_dPion(x1, y1, x2, y2):
|
2014-11-14 08:51:44 +01:00
|
|
|
test = j_dPion(x1, y1, x2, y2)
|
|
|
|
if test == True:
|
2014-11-07 01:30:48 +01:00
|
|
|
g_dPion(x1, y1, x2, y2)
|
|
|
|
return True
|
|
|
|
else:
|
2014-11-14 08:51:44 +01:00
|
|
|
g_statut('Impossible ! ('+str(test)+')') # TODO Messages corrects
|
2014-11-07 01:30:48 +01:00
|
|
|
return False
|
|
|
|
|
|
|
|
def f_nvPartie():
|
|
|
|
j_nvPartie()
|
|
|
|
g_cDamier()
|
|
|
|
g_cGrille()
|
|
|
|
g_remplirGrille(j_grille)
|
|
|
|
|
2014-11-14 08:51:44 +01:00
|
|
|
def f_dClic(x, y):
|
|
|
|
global f_origine, f_x1, f_y1, f_x2, f_y2
|
|
|
|
if f_origine:
|
|
|
|
f_x1, f_y1 = x, y
|
|
|
|
# TODO Colorer case
|
|
|
|
g_statut('Poser')
|
|
|
|
else:
|
|
|
|
f_x2, f_y2 = x, y
|
|
|
|
f_dPion(f_x1, f_y1, f_x2, f_y2)
|
|
|
|
f_statutPrendre()
|
|
|
|
f_origine = not f_origine
|
|
|
|
|
|
|
|
|
2014-11-07 01:30:48 +01:00
|
|
|
def f_clic(event):
|
|
|
|
x = event.x//COTE_CASE
|
|
|
|
y = event.y//COTE_CASE
|
2014-11-14 08:51:44 +01:00
|
|
|
f_dClic(x, y)
|
2014-11-07 01:30:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-11-14 08:51:44 +01:00
|
|
|
f_init()
|