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)]:
return HORIZONTAL
else:
return 0
return -1
def mise_à_jour(self):
@ -142,10 +142,14 @@ class Plateau:
@params self
@return void
"""
L = []
for bille in self.billes:
if self.est_ce_un_trou(bille[0], bille[1]) == 0:
self.billes.pop(bille)
if self.est_ce_un_trou(bille[0], bille[1]) == 2:
x, y = bille[0], bille[1]
bille[2].compte -= 1
self.billes.remove(bille)
L.append((x,y))
return L
class Tirette:
def __init__(self, numéro, plateau, orientation=random.randint(0,1), trous=[]):
@ -198,7 +202,7 @@ class Tirette:
@return boolean
"""
# Ancienne position de la tirette
ancienne_position = tirette.position
ancienne_position = self.position
# Pour chaque trou de la tirette
for trou in self.trous:
@ -208,12 +212,24 @@ class Tirette:
self.plateau.trous[self.orientation][self.calcul_pos(trou)] = False
# 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 +
(nouvelle_position - ancienne_position))] = True
# 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:
def __init__(self, plateau):
@ -222,7 +238,9 @@ class Joueur:
"""
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)
@params (x,y) coordonnées
@ -231,6 +249,10 @@ class Joueur:
"""
if not self.plateau.est_ce_un_trou(x,y) == 2:
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.compte += 1
return True
return False

93
piege.py Normal file → Executable file
View File

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