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.

110 lines
3.9 KiB

2 years ago
from File import File_chaine
import tkinter as tk
2 years ago
from PIL import Image, ImageTk
2 years ago
import sqlite3
2 years ago
import random
2 years ago
class Carte:
2 years ago
def __init__(self, couleur, valeur):
2 years ago
self.valeur = valeur
self.couleur = couleur
def compare(self, other):
2 years ago
if self.valeur > other.valeur:
2 years ago
return 1
2 years ago
elif self.valeur < other.valeur:
2 years ago
return -1
2 years ago
return 0
2 years ago
def __str__(self) -> str:
2 years ago
if self.valeur > 10:
return str(['Valet', 'Dame', 'Roi', 'As'][self.valeur-10]) + ' de ' + self.couleur
2 years ago
return str(self.valeur) + ' de ' + self.couleur
2 years ago
class Jeux:
2 years ago
def __init__(self, nb) -> None:
self.paquet = []
2 years ago
self.nombre_carte = nb
if self.nombre_carte != 32 and self.nombre_carte != 52:
raise ValueError("Saissisez 32 ou 52 cartes")
2 years ago
2 years ago
# Création du paquet de carte en fonction du nombre de carte
conn = sqlite3.connect('bdd.db')
c = conn.cursor()
if self.nombre_carte == 32:
c.execute('SELECT * FROM Cartes WHERE belote=True')
else:
c.execute('SELECT * FROM Carte')
data = c.fetchall()
for i, carte in enumerate(data):
2 years ago
self.paquet.append(Carte(data[i][0], data[i][1]))
self.jeu()
2 years ago
2 years ago
def distribute(self) -> File_chaine:
joueur = [File_chaine(), File_chaine()]
while len(self.paquet) > 0:
for i in range(2):
random_number = random.randint(0, len(self.paquet)-1)
joueur[i].enfiler(self.paquet.pop(random_number))
return joueur
2 years ago
def jeu(self):
2 years ago
self.j1, self.j2 = self.distribute()
while not (self.j1.est_vide() or self.j2.est_vide()):
self.bataille()
2 years ago
2 years ago
if self.j1.est_vide():
2 years ago
# self.j1 a perdu donc self.j2 gagne toutes les cartes
2 years ago
print("Désolée l'ordinateur à gagné")
2 years ago
else:
2 years ago
print("Bravo vous avez gagné")
2 years ago
def bataille(self, buffer_draw=[File_chaine(), File_chaine()]):
2 years ago
print('Vous: {} Il vous reste {} cartes'.format(self.j1.tete(), self.j1.taille()), "L'ordinateur: {} Il reste {} cartes à l'ordinateur".format(self.j2.tete(), self.j2.taille()), sep='\n')
input()
2 years ago
j1_active_carte, j2_active_carte = self.j1.tete(), self.j2.tete()
2 years ago
if j1_active_carte.compare(j2_active_carte) == 0 :
2 years ago
try:
2 years ago
print()
print("Egalitée !!")
print()
2 years ago
# envers
buffer_draw[0].enfiler(self.j1.defiler()) # carte du dessus
buffer_draw[0].enfiler(self.j1.defiler()) # carte a l'envers
buffer_draw[1].enfiler(self.j2.defiler()) # carte du dessus
buffer_draw[1].enfiler(self.j2.defiler()) # carte a l'envers
# print('next_buff' ,buffer_draw[0], buffer_draw[1])
self.bataille(buffer_draw)
except IndexError:
pass
2 years ago
elif j1_active_carte.compare(j2_active_carte) == 1 :
2 years ago
print()
print("Vous gagnez la carte")
print()
2 years ago
while not buffer_draw[0].est_vide():
self.j1.enfiler(buffer_draw[0].defiler())
self.j1.enfiler(buffer_draw[1].defiler())
self.j1.enfiler(self.j2.defiler())
2 years ago
self.j1.enfiler(self.j1.defiler())
2 years ago
elif j1_active_carte.compare(j2_active_carte) == -1 :#self.j1.tete().compare(self.j2.tete()) == -1 :
2 years ago
print()
print("Lordinateur gagne la carte")
print()
2 years ago
while not buffer_draw[0].est_vide():
self.j2.enfiler(buffer_draw[0].defiler())
self.j2.enfiler(buffer_draw[1].defiler())
self.j2.enfiler(self.j1.defiler())
2 years ago
self.j2.enfiler(self.j2.defiler())
2 years ago
jeu = Jeux(int(input("Choississez 32 ou 52 cartes: ")))