Bonjour je suis en première général et en option NSI nous devont faire un morpion en python sur processing .
J'ai un probleme sa prend en compte quand j'appuie sur le bouton jouer du menu ducoup sa met une croix(symbole du joueur) avant que la partie ainsi qu'un rond.
Et aussi après que le rond et la croix soit apparu je ne plus jouer.
Merci de votre aide.
from random import randint
TAILLE_GRILLE = 3
TAILLE_CASE = 220
afficher = "accueil"
VIDE = 0
JOUEUR = 7
ORDINATEUR = 5
PERSONNE = 10
vainqueur = PERSONNE
tour = JOUEUR
la_partie_continue = True
def setup():
frameRate(80)
size (TAILLE_GRILLE*TAILLE_CASE,TAILLE_GRILLE*TAILLE_CASE)
def draw():
global afficher
background(0)
if afficher == "accueil":
ecran_accueil()
fermer()
elif afficher == "rejouer":
bouton_rejouer()
elif afficher == "jouer":
morpion()
elif afficher == "egalité":
bouton_egalite()
####################FONCTION DE JEU#########################################
def morpion():
global VIDE,JOUEUR,ORDINATEUR,PION_JOUEUR,PION_ORDINATEUR,PERSONNE,vainqueur,g,la_partie_continue,tour,JOUEUR
afficher_grille(g)
while le_plateau_est_plein(g) == False :
quadrillage()
colonne,ligne = le_joueur_fait_une_proposition(tour)
if la_case_est_vide(ligne,colonne,g) == True :
g[ligne][colonne] = tour
afficher_grille(g)
if alignement_reussi(JOUEUR,g) == True :
vainqueur = tour
afficher = "rejouer"
else :
if tour == JOUEUR :
tour = ORDINATEUR
else :
tour = JOUEUR
else:
afficher = "egalité"
def grille_vide():
return [[0,0,0],[0,0,0],[0,0,0]]
g = grille_vide()
def alignement_horizontal(joueur,t):
for i in range (len(t)):
s=0
for j in range(len(t[1])):
s=s+t[i][j]
if s==3*joueur:
return True
return False
def alignement_vertical(joueur,t):
for i in range (len(t)):
s=0
for j in range(len(t[1])):
s=s+t[j][i]
if s==3*joueur:
return True
return False
def alignement_premiere_diagonale(joueur,t):
s=0
for j in range(len(t)):
s=s+t[j][j]
return s==3*joueur
def alignement_seconde_diagonale(joueur,t):
s=0
for j in range(len(t)):
s=s+t[j][2-j]
if s==3*joueur:
return True
else:
return False
def alignement_reussi(joueur,t):
resultat=False
resultat=resultat or alignement_seconde_diagonale(joueur,t) or alignement_premiere_diagonale(joueur,t)or alignement_vertical(joueur,t) or alignement_horizontal(joueur,t)
return resultat
def le_plateau_est_plein(t):
for i in range(len(t)):
for j in range(len(t[0])):
if t[i][j] == 0 :
return False
return True
def le_joueur_fait_une_proposition(tour) :
if tour==JOUEUR :
prop = l_homme_fait_une_proposition()
else :
prop = l_ordinateur_fait_une_proposition()
return prop
def l_homme_fait_une_proposition():
if 440 < mouseX < 660 and 0 < mouseY< 220:
if mousePressed :
return (2,0)
if 440 < mouseX < 660 and 220 < mouseY< 440:
if mousePressed :
return (2,1)
if 440 < mouseX < 660 and 440 < mouseY< 660:
if mousePressed :
return (2,2)
if 220 < mouseX < 440 and 0 < mouseY< 220:
if mousePressed :
return (1,0)
if 220 < mouseX < 440 and 220 < mouseY< 440:
if mousePressed :
return (1,1)
if 220 < mouseX < 440 and 440 < mouseY< 660:
if mousePressed :
return (1,2)
if 0 < mouseX < 220 and 0 < mouseY< 220:
if mousePressed :
return (0,0)
if 0 < mouseX < 220 and 220 < mouseY< 440:
if mousePressed :
return (0,1)
if 0 < mouseX < 220 and 440 < mouseY< 660:
if mousePressed :
return (0,2)
def l_ordinateur_fait_une_proposition() :
return (randint(0,2),randint(0,2))
def la_case_est_vide(i,j,t):
if t[i][j] == 0 :
return True
else:
return False
##################INTERFACE GRAPHIQUE#######################################
def quadrillage():
stroke(255,255,255)
strokeWeight(7)
for i in range(1,TAILLE_GRILLE):
line(i*TAILLE_CASE,0,i*TAILLE_CASE,height)
line(0,i*TAILLE_CASE,width,i*TAILLE_CASE)
def rond(ligne,colonne):
global TAILLE_CASE
stroke(255,255,255)
strokeWeight(6)
fill(0,0,0)
xc = (colonne + 0.5) * TAILLE_CASE
yc = (ligne + 0.5) * TAILLE_CASE
ellipse(xc,yc,TAILLE_CASE-20,TAILLE_CASE-20)
def croix(ligne,colonne):
global TAILLE_CASE
stroke(255,255,255)
strokeWeight(7)
fill(0,0,0)
xc = (colonne + 0.5) * TAILLE_CASE
yc = (ligne + 0.5) * TAILLE_CASE
line(xc+TAILLE_CASE/2-15,yc-TAILLE_CASE/2+15,xc-TAILLE_CASE/2+15,yc+TAILLE_CASE/2-15)
line(xc+TAILLE_CASE/2-15,yc+TAILLE_CASE/2-15,xc-TAILLE_CASE/2+15,yc-TAILLE_CASE/2+15)
def afficher_grille(t):
for i in range(len(t)):
for j in range(len(t[0])):
if t[i][j] == 0:
pass
elif t[i][j] == 7:
croix(i,j)
else:
rond(i,j)
def ecran_accueil():
global afficher
texte_menu()
if bouton() == True:
afficher = "jouer"
def bouton():
if 88< mouseX < 88 + 200 and 345< mouseY < 345 + 100 :
textSize(60)
fill (255,255,255)
rect(88,345,200,100,8)
fill(0,0,0)
text("Jouer",116,410,0)
if mousePressed :
return True
else:
textSize(60)
fill (255,255,255)
rect(88,345,200,100,8)
fill(0,0,0)
text("Jouer",116,410,0)
return False
def fermer():
if 350< mouseX < 300 + 200 and 345< mouseY < 345 + 100 :
textSize(55)
fill (255,255,255)
rect(350,345,200,100,8)
fill(0,0,0)
text("Fermer",355,410,0)
if mousePressed :
exit()
else:
textSize(55)
fill (255,255,255)
rect(350,345,200,100,8)
fill(0,0,0)
text("Fermer",355,410,0)
def texte_menu():
fill(255,255,255)
textSize(100)
text("Morpion",120,200,0)
def texte_rejouer():
fill(255,0,0)
textSize(50)
text(vainqueur,"a gagner",150,130,0)
fill(255,255,255)
def texte_egalite():
fill(255,0,0)
textSize(50)
text("egalité",150,130,0)
fill(255,255,255)
def rejouer():
if 88< mouseX < 88 + 200 and 345< mouseY < 345 + 100 :
textSize(45)
fill (255,255,255)
rect(88,345,200,100,8)
fill(0,0,0)
text("Rejouer",105,410,0)
if mousePressed :
return True
else:
textSize(45)
fill (255,255,255)
rect(88,345,200,100,8)
fill(0,0,0)
text("Rejouer",105,410,0)
return False
def menu():
if 350< mouseX < 300 + 200 and 345< mouseY < 345 + 100 :
textSize(50)
fill (255,255,255)
rect(350,345,200,100,8)
fill(0,0,0)
text("Menu",380,410,0)
if mousePressed :
return True
else:
textSize(50)
fill (255,255,255)
rect(350,345,200,100,8)
fill(0,0,0)
text("Menu",380,410,0)
return False
def bouton_egalite():
global afficher
texte_egalite()
rejouer()
menu()
if rejouer() == True:
afficher = "jouer"
elif menu() == True:
afficher = "accueil"
def bouton_rejouer():
global afficher
texte_rejouer()
rejouer()
menu()
if rejouer() == True:
afficher = "jouer"
elif menu() == True:
afficher = "accueil"