Ajout TP 7 et avancement sur Echecs avant POO

This commit is contained in:
Geoffrey Frogeye 2014-11-14 08:51:44 +01:00
parent 43b02a5537
commit 42e98e2b9d
8 changed files with 575 additions and 56 deletions

View file

Before

Width:  |  Height:  |  Size: 4.3 KiB

After

Width:  |  Height:  |  Size: 4.3 KiB

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 3.2 KiB

After

Width:  |  Height:  |  Size: 3.2 KiB

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 6.9 KiB

After

Width:  |  Height:  |  Size: 6.9 KiB

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 4.8 KiB

After

Width:  |  Height:  |  Size: 4.8 KiB

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 3 KiB

After

Width:  |  Height:  |  Size: 3 KiB

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 2.3 KiB

After

Width:  |  Height:  |  Size: 2.3 KiB

Before After
Before After

View file

@ -1,3 +1,5 @@
# pylint: disable=W0603
# INFOS
# Pièces :
@ -32,7 +34,7 @@ from random import randint
CASES_COTE = 8
j_grille = None
auxBlancs = None
j_joueur = True
def j_eNoir(xD, yD): # TODO Peut être considérablement amélioré
i = 1
@ -48,21 +50,23 @@ def j_cGrille():
for x in range(CASES_COTE):
colonne = []
for y in range(CASES_COTE):
if j_eNoir(x, y):
colonne.append(0)
else:
colonne.append(-1)
colonne.append(0)
j_grille.append(colonne)
def j_remplirGrille():
global j_grille
j_grille[2][2] = 5
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]
def j_nvPartie():
j_cGrille()
j_remplirGrille()
global auxBlancs
auxBlancs = True
global j_joueur
j_joueur = True
def j_cPion(x, y, piece):
"""
@ -70,30 +74,62 @@ def j_cPion(x, y, piece):
j_grille[x][y] = piece
return True
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
return True
else:
return -2
else:
return -1
def j_dPion(x1, y1, x2, y2):
# TODO Vérification du mouvement possible
assert(j_grille[x1][y1] > 0), "ERR1"
assert(j_grille[x2][y2] != 0), "ERR2"
j_grille[x2][y2] = j_grille[x1][y1]
return True
# def poserPion(x, y):
# global auxBlancs
# if j_grille[x][y] == 0:
# j_grille[x][y] = 1
# pion(x, y, auxBlancs)
# auxBlancs = not auxBlancs
# elif j_grille[x][y] == -1:
# statut('On joue sur les cases noires !')
# else:
# statut('Il y a déjà quelque chose ici.')
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
# GUI
DECX = 0
DECY = 0
COTE_CASE = 50
MARGE_PIONS = 5
TEMPS_ANIM = 200
INTER_ANIM = 10
g_imagesOriginales = []
g_imagesRedim = []
g_grilleDamier = None
g_grillePions = None
@ -102,6 +138,14 @@ fen = None
can = None
chaine = None
# Animation
g_x1 = None
g_y1 = None
g_x2 = None
g_y2 = None
g_pion = None
g_anim = None
def g_fen():
global fen, can, chaine
fen = Tk()
@ -119,14 +163,49 @@ def g_fen():
def g_statut(texte, delai=0):
chaine.config(text=texte)
print(texte)
# TODO Timeout effacer si parametre
# 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('')
def g_cCase(x, y):
if j_eNoir(x, y):
couleur = 'black'
couleur = '#D18B47'
else:
couleur = 'white'
couleur = '#FFCE9E'
return can.create_rectangle(x*COTE_CASE, y*COTE_CASE, \
(x+1)*COTE_CASE, (y+1)*COTE_CASE, fill=couleur)
@ -143,32 +222,39 @@ def g_cPion(x, y, piece):
global g_grillePions
global g_photos
if piece > 0:
nom = 'sprites/'
if piece%10 == 5:
nom += 'reine'
else:
nom += 'pion'
if piece < 10:
nom += 'B'
else:
nom += 'N'
nom += '.gif'
g_photos.append(PhotoImage(file=nom))
sample = int(504/(COTE_CASE-MARGE_PIONS))
g_photos[-1] = g_photos[-1].subsample(sample)
g_grillePions[x][y] = can.create_image((x+.5)*COTE_CASE, (y+.5)*COTE_CASE, image=g_photos[-1])
# g_grillePions[x][y] = can.create_oval(x*COTE_CASE+MARGE_PIONS, y*COTE_CASE+MARGE_PIONS, \
# (x+1)*COTE_CASE-MARGE_PIONS, (y+1)*COTE_CASE-MARGE_PIONS, \
# outline='gray', width=2, fill='white' if piece < 10 else 'black')
g_grillePions[x][y] = can.create_image((x+.5)*COTE_CASE, \
(y+.5)*COTE_CASE, image=g_imagesRedim[piece])
else:
g_grillePions[x][y] = False
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()
def g_dPion(x1, y1, x2, y2):
global g_grillePions
pion = g_grillePions[x1][y1]
can.coords(pion, x2*COTE_CASE+MARGE_PIONS, y2*COTE_CASE+MARGE_PIONS, (x2+1)*COTE_CASE-MARGE_PIONS, (y2+1)*COTE_CASE-MARGE_PIONS)
# 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]
g_grillePions[x1][y1] = False
g_grillePions[x2][y2] = pion
g_dPionAnim()
# g_dPionFinal()
def g_cGrille():
global g_grillePions
@ -185,9 +271,33 @@ def g_remplirGrille(j_grilleF):
for y in range(0, CASES_COTE):
g_cPion(x, y, j_grilleF[x][y])
def g_init():
g_fen()
g_importerImages()
g_redimImages()
# Frontend
def f_cPion(x, y, piece):
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):
if j_cPion(x, y, piece):
g_cPion(x, y, piece)
return True
@ -198,10 +308,12 @@ def f_cPionAlea():
g_cPion(randint(1, CASES_COTE), randint(1, CASES_COTE), 1)
def f_dPion(x1, y1, x2, y2):
if j_dPion(x1, y1, x2, y2):
test = j_dPion(x1, y1, x2, y2)
if test == True:
g_dPion(x1, y1, x2, y2)
return True
else:
g_statut('Impossible ! ('+str(test)+')') # TODO Messages corrects
return False
def f_nvPartie():
@ -210,13 +322,24 @@ def f_nvPartie():
g_cGrille()
g_remplirGrille(j_grille)
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
def f_clic(event):
x = event.x//COTE_CASE
y = event.y//COTE_CASE
f_cPion(x, y, 1)
f_dClic(x, y)
# MAIN
g_fen()
f_nvPartie()
f_init()