Cet article est écrit par un débutant qui programme depuis un mois et demi, alors lisez-le attentivement.
environnement ・ Windows 10 (peut être fait sur Mac du tout)
Langue utilisée ・ Python3
Editeur à utiliser ・ VScode
blackjack.py
import random
deck = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] * 4
def deal():
hand = []
for i in range(2):
random.shuffle(deck)
card = deck.pop()
if card == 11:
card = "J"
if card == 12:
card = "Q"
if card == 13:
card = "K"
if card == 1:
card = "A"
hand.append(card)
return hand
def hit(hand):
random.shuffle(deck)
card = deck.pop()
if card == 11:
card = "J"
if card == 12:
card = "Q"
if card == 13:
card = "K"
if card == 1:
card = "A"
hand.append(card)
return hand
def total(hand):
score = 0
for card in hand:
if card == "J" or card == "Q" or card == "K":
score = score + 10
elif card == "A":
if score >= 11:
score = score + 1
else:
score += 11
else:
score += card
return score
def play_again():
again = input("Voulez-vous rejouer?(Y/N): ")
if again == "y" or again == "Y":
# game()
return
else:
print("Je vous remercie pour votre travail acharné!")
exit()
def result(dealer_hand, player_hand):
if total(player_hand) > total(dealer_hand):
print(
f"\n Total concessionnaire{total(dealer_hand)}Votre total{total(player_hand)}est.\033[32mYOU WIN!\033[0m")
elif total(dealer_hand) > total(player_hand):
print(
f"\n Total concessionnaire{total(dealer_hand)}Votre total{total(player_hand)}est.\033[91mYOU LOSE...\033[0m")
def game():
dealer_hand = deal()
player_hand = deal()
print(f"La carte du croupier est{dealer_hand[0]}est.")
print(f"La carte du joueur est{player_hand}Le total est{total(player_hand)}est.")
choice = 0
while choice != quit:
choice = input("Voulez-vous frapper? Voulez-vous vous lever?(HIT/STAND): ").lower()
if choice == "hit":
hit(player_hand)
print(
f"\n à toi{player_hand[-1]}Est distribuée et la carte est{player_hand}Le total est{total(player_hand)}est.")
if total(player_hand) > 21:
print("Vous avez dépassé 21.\033[91mYOU LOSE...\033[0m")
choice = quit
elif choice == "stand":
print(
f"\n La deuxième carte du croupier est{dealer_hand[1]}Le total est{total(dealer_hand)}est.")
while total(dealer_hand) < 17:
hit(dealer_hand)
print(
f"Au concessionnaire{dealer_hand[-1]}Est distribuée et la carte est{dealer_hand}Le total est{total(dealer_hand)}est.")
if total(dealer_hand) > 21:
print("Le nombre de concessionnaires a dépassé 21.\033[32mYOU WIN!\033[0m")
choice = quit
if total(dealer_hand) <= 21:
result(dealer_hand, player_hand)
choice = quit
game()
Appuyez simplement dessus dans le terminal pour démarrer le jeu.
Recommended Posts