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/testsQt.py

223 lines
5.2 KiB
Python

# 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
auxBlancs = None
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):
if j_eNoir(x, y):
colonne.append(0)
else:
colonne.append(-1)
j_grille.append(colonne)
def j_remplirGrille():
global j_grille
j_grille[2][2] = 5
def j_nvPartie():
j_cGrille()
j_remplirGrille()
global auxBlancs
auxBlancs = True
def j_cPion(x, y, piece):
"""
"""
j_grille[x][y] = piece
return True
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.')
# GUI
DECX = 0
DECY = 0
COTE_CASE = 50
MARGE_PIONS = 5
g_grilleDamier = None
g_grillePions = None
g_photos = []
fen = None
can = None
chaine = None
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)
print(texte)
# TODO Timeout effacer si parametre
def g_cCase(x, y):
if j_eNoir(x, y):
couleur = 'black'
else:
couleur = 'white'
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:
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')
else:
g_grillePions[x][y] = False
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)
g_grillePions[x1][y1] = False
g_grillePions[x2][y2] = pion
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])
# Frontend
def f_cPion(x, y, piece):
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):
if j_dPion(x1, y1, x2, y2):
g_dPion(x1, y1, x2, y2)
return True
else:
return False
def f_nvPartie():
j_nvPartie()
g_cDamier()
g_cGrille()
g_remplirGrille(j_grille)
def f_clic(event):
x = event.x//COTE_CASE
y = event.y//COTE_CASE
f_cPion(x, y, 1)
# MAIN
g_fen()
f_nvPartie()