Version finale

This commit is contained in:
neox 2024-01-05 16:06:24 +01:00
parent ed49815d96
commit f9607b6443
No known key found for this signature in database
GPG Key ID: 2974E1D5F25DFCC8
2 changed files with 100 additions and 31 deletions

38
game.py
View File

@ -132,7 +132,7 @@ class Plateau:
elif self.trous[HORIZONTAL][calcul_coord(x,y)]: elif self.trous[HORIZONTAL][calcul_coord(x,y)]:
return HORIZONTAL return HORIZONTAL
else: else:
return 0 return -1
def mise_à_jour(self): def mise_à_jour(self):
@ -142,10 +142,14 @@ class Plateau:
@params self @params self
@return void @return void
""" """
L = []
for bille in self.billes: for bille in self.billes:
if self.est_ce_un_trou(bille[0], bille[1]) == 0: if self.est_ce_un_trou(bille[0], bille[1]) == 2:
self.billes.pop(bille) x, y = bille[0], bille[1]
bille[2].compte -= 1
self.billes.remove(bille)
L.append((x,y))
return L
class Tirette: class Tirette:
def __init__(self, numéro, plateau, orientation=random.randint(0,1), trous=[]): def __init__(self, numéro, plateau, orientation=random.randint(0,1), trous=[]):
@ -198,7 +202,7 @@ class Tirette:
@return boolean @return boolean
""" """
# Ancienne position de la tirette # Ancienne position de la tirette
ancienne_position = tirette.position ancienne_position = self.position
# Pour chaque trou de la tirette # Pour chaque trou de la tirette
for trou in self.trous: for trou in self.trous:
@ -208,12 +212,24 @@ class Tirette:
self.plateau.trous[self.orientation][self.calcul_pos(trou)] = False self.plateau.trous[self.orientation][self.calcul_pos(trou)] = False
# Met à jour la position du trou dans l'espace si dans le plateau # Met à jour la position du trou dans l'espace si dans le plateau
if trou - nouvelle_position > 0: if trou + (nouvelle_position - ancienne_position) < DIM:
self.plateau.trous[self.orientation][self.calcul_pos(trou + self.plateau.trous[self.orientation][self.calcul_pos(trou +
(nouvelle_position - ancienne_position))] = True (nouvelle_position - ancienne_position))] = True
# Mise à jour de la position de la tirette # Mise à jour de la position de la tirette
tirette.position = nouvelle_position self.position = nouvelle_position
def pousser(self):
if self.position > 0:
self.déplacer(self.position - 1)
return True
return False
def tirer(self):
if self.position < 3:
self.déplacer(self.position + 1)
return True
return False
class Joueur: class Joueur:
def __init__(self, plateau): def __init__(self, plateau):
@ -222,7 +238,9 @@ class Joueur:
""" """
self.plateau = plateau self.plateau = plateau
def placer_bille(self, x, y): self.compte = 0
def placer_bille(self, nextplayers, x, y):
""" """
la methode verifie si il n'y a pas de trou et de billes aux coordonnées du tuple (x,y) la methode verifie si il n'y a pas de trou et de billes aux coordonnées du tuple (x,y)
@params (x,y) coordonnées @params (x,y) coordonnées
@ -231,6 +249,10 @@ class Joueur:
""" """
if not self.plateau.est_ce_un_trou(x,y) == 2: if not self.plateau.est_ce_un_trou(x,y) == 2:
if not (x,y,self) in self.plateau.billes: if not (x,y,self) in self.plateau.billes:
for nextplayer in nextplayers:
if (x,y,nextplayer) in self.plateau.billes:
return False
self.plateau.billes.append((x,y,self)) self.plateau.billes.append((x,y,self))
self.compte += 1
return True return True
return False return False

93
piege.py Normal file → Executable file
View File

@ -9,7 +9,7 @@
# - Intention : cible le dragon le plus fort accessible (orgueil) # - Intention : cible le dragon le plus fort accessible (orgueil)
# #
# Doc FLTK : https://antoinemeyer.frama.io/fltk/ # Doc FLTK : https://antoinemeyer.frama.io/fltk/
import random, fltk, time, os, importlib import random, fltk, time, os, importlib, time
import game import game
## VARIABLES DE CONFIGURATION ## VARIABLES DE CONFIGURATION
@ -42,6 +42,7 @@ def run_game(joueur1, joueur2, plateau):
# Current player (XXX, joueur1 commence toujours) # Current player (XXX, joueur1 commence toujours)
currentplayer = joueur1 currentplayer = joueur1
nextplayer = joueur2 nextplayer = joueur2
gagnant = None
fltk.cree_fenetre(LARGEUR_FENETRE, HAUTEUR_FENETRE) fltk.cree_fenetre(LARGEUR_FENETRE, HAUTEUR_FENETRE)
fltk.texte(LARGEUR_FENETRE/2, HAUTEUR_FENETRE/20, fltk.texte(LARGEUR_FENETRE/2, HAUTEUR_FENETRE/20,
@ -66,10 +67,8 @@ def run_game(joueur1, joueur2, plateau):
} }
# Boucle principale # Boucle principale
while gamephase != FIN:
fltk.mise_a_jour()
plateau.mise_à_jour()
while gamephase != FIN:
if not retry: if not retry:
# inversion des rôles # inversion des rôles
currentplayer, nextplayer = nextplayer, currentplayer currentplayer, nextplayer = nextplayer, currentplayer
@ -82,33 +81,31 @@ def run_game(joueur1, joueur2, plateau):
if gamephase == PLACEMENT: if gamephase == PLACEMENT:
strphase = "placement des billes" strphase = "placement des billes"
elif gamephase == TIRETTES: elif gamephase == TIRETTES:
strphase = "manipulation des tirettes" strphase = "manipulation des tirettes, \nclic droit pour tirer, clic gauche pour pousser"
fltk.efface("joueur") fltk.efface("joueur")
fltk.texte(LARGEUR_FENETRE/2, HAUTEUR_FENETRE/10, fltk.texte(LARGEUR_FENETRE/2, HAUTEUR_FENETRE/10,
"Phase {} : au tour du {}".format(strphase, strjoueur), "Phase {} : au tour du {}".format(strphase, strjoueur),
couleur="green", couleur="green",
taille=10, taille=20,
ancrage='center', ancrage='center',
tag="joueur") tag="joueur")
else:
retry = False retry = True
# Affichage plateau # Affichage plateau
# Tirettes verticales # Tirettes verticales
for i in range(DIM): for i in range(DIM):
#XXX récupérer la position de la tirette -> n
# boucler x fois pour afficher des barres
n = plateau.tirettes[0][i].position n = plateau.tirettes[0][i].position
fltk.efface("tv{}".format(i))
fltk.image( OFFSET_X + (i+1)*62, fltk.image( OFFSET_X + (i+1)*62,
OFFSET_Y + (DIM+1)*62 + n*30, OFFSET_Y + (DIM+1)*62 + n*30,
modeles["tirette_vert"], modeles["tirette_vert"],
ancrage = "center", ancrage = "center",
largeur=50, largeur=50,
hauteur=50, hauteur=50,
tag="{}".format(i)) tag="tv{}".format(i))
for x in range(n): for x in range(n):
fltk.image( OFFSET_X + (i+1)*62, fltk.image( OFFSET_X + (i+1)*62,
@ -117,7 +114,7 @@ def run_game(joueur1, joueur2, plateau):
ancrage = "center", ancrage = "center",
largeur=50, largeur=50,
hauteur=50, hauteur=50,
tag="{}".format(i)) tag="tv{}".format(i))
# Tirettes horizontales # Tirettes horizontales
for j in range(DIM): for j in range(DIM):
@ -125,13 +122,14 @@ def run_game(joueur1, joueur2, plateau):
# boucler x fois pour afficher des barres # boucler x fois pour afficher des barres
n = plateau.tirettes[1][j].position n = plateau.tirettes[1][j].position
fltk.efface("th{}".format(j))
fltk.image( OFFSET_X + 20 + (0)*62 - n*30, fltk.image( OFFSET_X + 20 + (0)*62 - n*30,
OFFSET_Y + 20 + (j+1)*62, OFFSET_Y + 20 + (j+1)*62,
modeles["tirette_horiz"], modeles["tirette_horiz"],
ancrage = "center", ancrage = "center",
largeur=55, largeur=55,
hauteur=55, hauteur=55,
tag="{}".format(i)) tag="th{}".format(j))
for x in range(n): for x in range(n):
fltk.image( OFFSET_X + 20 + (0)*62 - x*30, fltk.image( OFFSET_X + 20 + (0)*62 - x*30,
@ -140,7 +138,7 @@ def run_game(joueur1, joueur2, plateau):
ancrage = "center", ancrage = "center",
largeur=50, largeur=50,
hauteur=50, hauteur=50,
tag="{}".format(i)) tag="th{}".format(j))
# Plateau # Plateau
for i in range(0,DIM): for i in range(0,DIM):
@ -161,7 +159,8 @@ def run_game(joueur1, joueur2, plateau):
tag="{},{}".format(i,j)) tag="{},{}".format(i,j))
potentiel_trou = plateau.est_ce_un_trou(i,j) potentiel_trou = plateau.est_ce_un_trou(i,j)
if potentiel_trou != 0: #print(potentiel_trou)
if potentiel_trou > -1:
if potentiel_trou == 2: if potentiel_trou == 2:
modele_trou = modeles["trou"] modele_trou = modeles["trou"]
elif potentiel_trou == HORIZONTAL: elif potentiel_trou == HORIZONTAL:
@ -178,6 +177,10 @@ def run_game(joueur1, joueur2, plateau):
tag="{},{}".format(i,j)) tag="{},{}".format(i,j))
# Billes # Billes
for bille in plateau.mise_à_jour():
x,y = bille[0], bille[1]
fltk.efface("b{},{}".format(x,y))
for bille in plateau.billes: for bille in plateau.billes:
if bille[2] is joueur1: if bille[2] is joueur1:
fltk.image( OFFSET_X + 2 + (bille[0]+1)*62, fltk.image( OFFSET_X + 2 + (bille[0]+1)*62,
@ -186,7 +189,7 @@ def run_game(joueur1, joueur2, plateau):
ancrage = "center", ancrage = "center",
largeur=30, largeur=30,
hauteur=30, hauteur=30,
tag="{},{}".format(i,j)) tag="b{},{}".format(i,j))
if bille[2] is joueur2: if bille[2] is joueur2:
fltk.image( OFFSET_X + 2 + (bille[0]+1)*62, fltk.image( OFFSET_X + 2 + (bille[0]+1)*62,
@ -195,10 +198,26 @@ def run_game(joueur1, joueur2, plateau):
ancrage = "center", ancrage = "center",
largeur=30, largeur=30,
hauteur=30, hauteur=30,
tag="{},{}".format(i,j)) tag="b{},{}".format(i,j))
#XXX Affichage instructions liées au mode de jeu if gamephase == TIRETTES and currentplayer.compte == 0:
gagnant = nextplayer
gamephase = FIN
if currentplayer is joueur1:
strjoueur = "joueur2"
elif currentplayer is joueur2:
strjoueur = "joueur1"
fltk.efface("joueur")
fltk.texte(LARGEUR_FENETRE/2, HAUTEUR_FENETRE/10,
"Le joueur {} a gagné !".format(strjoueur),
couleur="red",
taille=20,
ancrage='center',
tag="joueur")
fltk.mise_a_jour() fltk.mise_a_jour()
event = fltk.attend_ev() event = fltk.attend_ev()
@ -222,15 +241,14 @@ def run_game(joueur1, joueur2, plateau):
x = fltk.abscisse(event) x = fltk.abscisse(event)
y = fltk.ordonnee(event) y = fltk.ordonnee(event)
print("Clic sur coords ({},{})".format(x,y)) #print("Clic sur coords ({},{})".format(x,y))
if gamephase == PLACEMENT: if gamephase == PLACEMENT:
i = int( (x - OFFSET_X - 2 + 62/2)/62 - 1) i = int( (x - OFFSET_X - 2 + 62/2)/62 - 1)
j = int( (y - OFFSET_Y - 20 + 62/2)/62 - 1) j = int( (y - OFFSET_Y - 20 + 62/2)/62 - 1)
if i<DIM and j<DIM and i>=0 and j>=0: if i<DIM and j<DIM and i>=0 and j>=0:
print(" case {},{}".format(i,j)) retry = not currentplayer.placer_bille([nextplayer], i,j)
retry = not currentplayer.placer_bille(i,j)
if len(plateau.billes) == 10: if len(plateau.billes) == 10:
gamephase = TIRETTES gamephase = TIRETTES
@ -240,7 +258,35 @@ def run_game(joueur1, joueur2, plateau):
i = int( (x - OFFSET_X - 2 + 62/2)/62 - 1) i = int( (x - OFFSET_X - 2 + 62/2)/62 - 1)
j = int( (y - OFFSET_Y - 20 + 62/2)/62 - 1) j = int( (y - OFFSET_Y - 20 + 62/2)/62 - 1)
print(" case {},{}".format(i,j))
if i<DIM and j>=DIM-1 and i>=0 and j>=0:
print(" tirette {}".format(i))
retry = not plateau.tirettes[VERTICAL][i].tirer()
if i<DIM and j<DIM and i<=0 and j>=0:
print(" tirette {}".format(j))
retry = not plateau.tirettes[HORIZONTAL][j].tirer()
if "ClicDroit" in fltk.type_ev(event):
# XXX à améliorer
x = fltk.abscisse(event)
y = fltk.ordonnee(event)
#print("Clic sur coords ({},{})".format(x,y))
if gamephase == TIRETTES:
# XXX
i = int( (x - OFFSET_X - 2 + 62/2)/62 - 1)
j = int( (y - OFFSET_Y - 20 + 62/2)/62 - 1)
if i<DIM and j>=DIM-1 and i>=0 and j>=0:
print(" tirette {}".format(i))
retry = not plateau.tirettes[VERTICAL][i].pousser()
if i<DIM and j<DIM and i<=0 and j>=0:
print(" tirette {}".format(j))
retry = not plateau.tirettes[HORIZONTAL][j].pousser()
## Fonction principale ## Fonction principale
@ -253,6 +299,7 @@ def main():
joueur2 = game.Joueur(plateau) joueur2 = game.Joueur(plateau)
run_game(joueur1, joueur2, plateau) run_game(joueur1, joueur2, plateau)
time.sleep(5)
return 0 return 0
main() main()