import time
import os
import random
def clear_screen():
os.system("cls" if os.name == "nt" else "clear")
def sum_cards(cards):
if 11 in cards and sum(cards) > 21:
cards [ cards.index(11) ] = 1
return sum(cards)
def take_card():
card = random.choice(cards)
return card
def comparison(user_cards, computer_cards):
total_user = sum_cards(user_cards)
total_computer = sum_cards(computer_cards)
if total_user == total_computer:
print("DRAW 🙂")
elif total_user > total_computer:
print("YOU ARE HIGHER. YOU WIN 😎")
else:
print("YOU ARE LOWER. YOU LOSE 😢")
def final_hand(user_cards, computer_cards):
total_user = sum_cards(user_cards)
total_computer = sum_cards(computer_cards)
time.sleep(1)
print(f"\nYour final hand: {user_cards} with score {total_user}")
print(f"Computer's final hand: {computer_cards} with score {total_computer}")
print()
def twenty_one():
clear_screen()
print("Preparing the game....")
time.sleep(2)
user_cards = []
computer_cards = []
for _ in range(2):
user_cards.append(take_card())
computer_cards.append(take_card())
while True:
total_user = sum_cards(user_cards)
print(f"\nYour cards are {user_cards}, current score {total_user}")
print(f"Computer's first card is {computer_cards[0]}")
if total_user < 21:
another = input("Get another card? y/n: ").lower()
if another == "y":
user_cards.append(take_card())
else:
break
else:
break
# USER BLACK JACK
if user_cards in black_jack_cases:
final_hand(user_cards, computer_cards)
# COMPUTER ALSO BLACK JACK
if computer_cards in black_jack_cases:
print("DRAW BLACK JACK 🙂 🙂 🙂")
# USER ONLY BLACK JACK
else:
print("YOU GOT *** BLACK JACK *** YOU WIN 😎 😎 😎")
# USER NOT BLACK JACK
else:
# USER > 21
if sum_cards(user_cards) > 21:
final_hand(user_cards, computer_cards)
print("You went over 21. YOU LOSE 😓")
# USER <= 21
else:
# COMPUTER BLACK JACK
if computer_cards in black_jack_cases:
final_hand(user_cards, computer_cards)
print("COMPUTER GOT BLACK JACK # YOU LOSE # 😓 😓 😓")
# COMPUTER NOT BLACK JACK
else:
while sum_cards(computer_cards) < 17:
computer_cards.append(take_card())
final_hand(user_cards, computer_cards)
# COMPUTER > 21
if sum_cards(computer_cards) > 21:
print("Computer went over 21. YOU WIN 😎")
# COMPARISON
else:
comparison(user_cards, computer_cards)
black_jack_cases = [[11,10],[10,11]]
cards = [11,2,3,4,5,6,7,8,9,10,10,10,10]
while True:
game_choice = input("\nChoose a game to start (type 'exit' to quit)\n\n1-Froggy\n2-Twenty one\n3-Snake\n----------\n").lower()
if game_choice not in ["froggy", "twenty one", "snake", "exit"]:
print("\nInvalid choice")
else:
if game_choice == "twenty one":
twenty_one()
elif game_choice == "exit":
print("\nExiting the program....")
time.sleep(2)
break
else:
print(f"\nStill working on {game_choice}")
إعجاب واحد (1)
جميل ، مبروك على المستوى الثانى
إعجاب واحد (1)
أحسنت ما شاء الله
وجميل أنك أضفت Exit
إعجابَين (2)