You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
4.9 KiB

from File import File_chaine as File
import random
from time import sleep
class Carte:
"carte"
def __init__(self, valeur, couleur):
self.valeur = valeur
self.couleur = couleur
def compare(self, other):
if self.valeur > other.valeur :
return 1
elif self.valeur < other.valeur :
return -1
elif self.valeur == other.valeur :
return 0
def __repr__(self):
return str(self.valeur)
def __str__(self):
return str(self.valeur) + " de " + self.couleur
class Jeux:
def __init__(self, nbmax):
i = 0
valeurs = [1, 7, 8, 9, 10, 11, 12, 13, 2, 3, 4, 5, 6]
couleurs = ["coeur", "pique", "carreau", "trefle"]
jeu = []
for val in valeurs:
if i >= nbmax:
break
for coul in couleurs:
jeu.append(Carte(val, coul))
i += 1
random.shuffle(jeu)
self.paquet = jeu
def distribue(self):
jeu1, jeu2 = File(), File()
i = 0
for carte in self.paquet:
if i%2 == 0:
jeu1.enfiler(carte)
else:
jeu2.enfiler(carte)
i += 1
return jeu1, jeu2
"""
def partie(jeu1, jeu2):
while not jeu1.est_vide() and not jeu2.est_vide():
c1 = jeu1.defiler()
c2 = jeu2.defiler()
gain = File()
gain.enfiler(c1)
gain.enfiler(c2)
resultat = c1.compare(c2)
elif resultat == 0 :
while jeu1.taille()>=2 and jeu2.taille()>=2 and resulat == 0:
gain.enfiler(jeu1.defiler())
gain.enfiler(jeu2.defiler())
c1 = jeu1.defiler()
c2 = jeu2.defiler()
gain.enfiler(c1)
gain.enfiler(c2)
resultat = c1.compare(c2)
if jeu1
if resultat == 1:
while not gain.est_vide():
jeu1.enfiler(gain.defiler())
elif resultat == -1 :
while not gain.est_vide():
jeu2.enfiler(gain.defiler())
"""
class partie_TeSt:
def __init__(self, nb_carte = 52):
jeux = Jeux(nb_carte)
self.j1, self.j2 = jeux.distribue()
self.Round = 1
self.Gain = File()
def play_round():
self.Round += 1
c1, c2 = self.j1.defiler(), self.j2.defiler()
self.Gain.enfiler(c1)
self.Gain.enfiler(c2)
resultat = c1.compare(c2)
while resultat == 0 and self.j1.taille() >= 2 and self.j2.taille() >= 2:
if random.randint(1, 2) == 1:
self.Gain.enfiler(self.j1.defiler())
self.Gain.enfiler(self.j2.defiler())
else:
self.Gain.enfiler(self.j2.defiler())
self.Gain.enfiler(self.j1.defiler())
c1, c2 = self.j1.defiler(), self.j2.defiler()
self.Gain.enfiler(c1)
self.Gain.enfiler(c2)
resultat = c1.compare(c2)
if resultat == 1:
while not self.Gain.est_vide():
self.j1.enfiler(self.Gain.defiler())
elif resultat == -1:
while not self.Gain.est_vide():
self.j2.enfiler(self.Gain.defiler())
while not (j1.est_vide() or j2.est_vide() or winner == "égalité"):
print(Round)
Round += 1
c1 = j1.defiler()
c2 = j2.defiler()
gain = File()
gain.enfiler(c1)
gain.enfiler(c2)
resultat = c1.compare(c2)
while resultat == 0 and j1.taille() >= 2 and j2.taille() >= 2:
if random.randint(1, 2) == 1:
gain.enfiler(j1.defiler())
gain.enfiler(j2.defiler())
else:
gain.enfiler(j2.defiler())
gain.enfiler(j1.defiler())
c1 = j1.defiler()
c2 = j2.defiler()
if random.randint(1, 2) == 1:
gain.enfiler(c2)
gain.enfiler(c1)
else:
gain.enfiler(c1)
gain.enfiler(c2)
resultat = c1.compare(c2)
if resultat == 1:
winner = "c'est j1 qui a gagné"
while not gain.est_vide():
j1.enfiler(gain.defiler())
elif resultat == -1:
winner = "c'est j2 qui a gagné"
while not gain.est_vide():
j2.enfiler(gain.defiler())
else:
if j1.taille() < 2 and not j2.taille() < 2:
winner = "c'est j2 qui a gagné"
elif not j1.taille() < 2 and j2.taille() < 2:
winner = "c'est j2 qui a gagné"
else:
winner = "égalité"
break
print(j1, j2)
return winner
jeu = Jeux(8)
j1, j2 = jeu.distribue()
print(partie_TeSt(j1, j2))