blackjack.py

Created by cent20

Created on March 16, 2025

3.11 KB

Un script python sur console.


# https://tiplanet.org/forum/article.php?f=49&t=26896
import random 

def creer_paquet():
    """Crée un paquet de cartes standard (valeurs de 1 à 11, 4 fois chacune)."""
    return [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 11] * 4

def tirer_carte(paquet):
    """Tire une carte du paquet et la retire."""
    return paquet.pop(random.randint(0, len(paquet) - 1))

def calculer_score(cartes):
    """Calcule le score total d'une main, en tenant compte des As."""
    score = sum(cartes)
    as_count = cartes.count(11)
    while score > 21 and as_count:
        score -= 10  # Transformer un As de 11 à 1
        as_count -= 1
    return score

def afficher_main(nom, cartes):
    """Affiche les cartes sous forme lisible."""
    print(nom,str(cartes)[1:len(str(cartes))-1])

def jouer_blackjack():
    argent = 42
    mise = 1
    paquet = creer_paquet()
    
    while 0 < argent < 84:
        if len(paquet) < 15:
            print("Nouveau paquet de cartes mélangé.")
            paquet = creer_paquet()
        
        print("Argent disponible :", argent)
        print("0:Rester 1:Tirer 2:Doubler\n")
        
        cartes_joueur = [tirer_carte(paquet), tirer_carte(paquet)]
        cartes_croupier = [tirer_carte(paquet)]
        
        afficher_main("Votre main", cartes_joueur)
        print("Carte du croupier :", cartes_croupier[0])
        
        while True:
            action = int(input("Choix = "))
            if action == 2 and argent >= mise * 2:
                mise *= 2
                print("Mise doublée !")
                cartes_joueur.append(tirer_carte(paquet))
                afficher_main("Votre main", cartes_joueur)
                break
            elif action == 1:
                cartes_joueur.append(tirer_carte(paquet))
                afficher_main("Votre main", cartes_joueur)
                if calculer_score(cartes_joueur) > 21:
                    break
            else:
                break
        
        score_joueur = calculer_score(cartes_joueur)
        if score_joueur > 21:
            print("Vous avez dépassé 21. \nVous perdez cette manche.")
            argent -= mise
            input()
        else:
            # Le croupier joue
            while calculer_score(cartes_croupier) < 17:
                cartes_croupier.append(tirer_carte(paquet))
            
            afficher_main("Main du croupier", cartes_croupier)
            
            score_croupier = calculer_score(cartes_croupier)
            if score_croupier > 21 or score_joueur > score_croupier:
                print("Vous gagnez cette manche !")
                argent += mise
            elif score_joueur < score_croupier:
                print("Le croupier gagne. \nVous perdez cette manche.")
                argent -= mise
            else:
                print("Égalité !")
            input()
        
        mise = 1  # Réinitialisation de la mise
        print("\n"*7)
    
    if argent >= 84:
        print("Bravo, vous avez atteint 2x42 !\nVous gagnez !")
    else:
        print("Vous n'avez plus d'argent. \nVous perdez !\n")
    print("La partie est terminée ...")

# Lancer le jeu
jouer_blackjack()

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.