مشروع لعبة بلاك جاك

السلام عليكم ورحمة الله وبركاته

اليوم أنهيت مشروع بلاك جاك للمرة لا أدري كم :sweat_smile:

وانا (أخيرا) راضية جدا عن الكود من حيث العمل والتنظيم :innocent:

الكود الذي كتبته فيه بعض الإختلافات عن الذي كتبه الأستاذ إبراهيم

أحببت مشاركتكم الكود لأشارككم سعادتي وأيضا لتجربوه وتعطوني رأيكم

ملحوظة: يمكنكم بدأ اللعبة بكتابة twenty one أو بكتابة 2

هذا هو الكود :point_down:

import os
import time
import random

def clear_screen():
    os.system('cls' if os.name == 'nt' else 'clear')

def games_in_app():
    print("\nChoose a game to start...\n")
    print("1- Froggy")
    print("2- Twenty one")
    print("3- Snake")

def give_card():
    all_cards = [11,2,3,4,5,6,7,8,9,10,10,10,10]
    card = random.choice(all_cards)
    return card

def calculate_score(cards):
    score = sum(cards)
    return score

def compare(user_cards,computer_cards):
    '''Comparing cards and telling the winner'''
    user_score = calculate_score(user_cards)
    computer_score = calculate_score(computer_cards)
    result = {
        "user_21": "You Win The Black Jack 😎🔥💫",
        "computer_21": "The computer Wins The Black Jack 😰",
        "user_over": "You Went Over 21 🫢 \nYou Lose 😞",
        "computer_over": "The Computer Went Over 21 \nYou Win 🥳",
        "draw": "It's Draw 😇",
        "user_win": "You Win 🥳",
        "computer_win": "You Lose 😞",
    }
    if user_score == 21:
        print(result["user_21"])
    elif computer_score == 21:
        print(result["computer_21"])
    elif user_score > 21:
        print(result["user_over"])
    elif computer_score > 21:
        print(result["computer_over"])
    elif user_score == computer_score:
        print(result["draw"])
    elif user_score > computer_score:
        print(result["user_win"])
    else:
        print(result["computer_win"])

def play_twenty_one():
    clear_screen()
    print("Starting game...")
    time.sleep(2)
    clear_screen()
    print('''
 ____  _            _       _            _    
| __ )| | __ _  ___| | __  | | __ _  ___| | __
|  _ \| |/ _` |/ __| |/ /  | |/ _` |/ __| |/ /
| |_) | | (_| | (__|   < |_| | (_| | (__|   < 
|____/|_|\__,_|\___|_|\_\___/ \__,_|\___|_|\_\.
          
''')
    user_cards = [give_card() for _ in range(2)]
    computer_cards = [give_card() for _ in range(2)]
    while True:
        if calculate_score(user_cards) < 21 :
            print(f"Your cards are {user_cards}, Current score is {calculate_score(user_cards)}")
            print(f"Computer first card is {computer_cards[0]}")
            if input("\nGet another card? (Y/N): ").upper() == 'Y':
                    user_cards.append(give_card())
                    clear_screen()
            else:
                break
        elif calculate_score(user_cards) > 21 and 11 in user_cards:
            user_cards.remove(11)
            user_cards.append(1)
        else:
            break
    while True: 
        if calculate_score(computer_cards) < 17 and calculate_score(user_cards) <= 21:
            computer_cards.append(give_card())
        elif calculate_score(computer_cards) > 21 and 11 in computer_cards:
            computer_cards.remove(11)
            computer_cards.append(1)
        else:
            break
    clear_screen()
    print(f"Your final hand {user_cards}, Current score is {calculate_score(user_cards)}")
    print(f"Computer final hand {computer_cards}, Cumputer score is {calculate_score(computer_cards)}\n")
    compare(user_cards,computer_cards)

while True:
    games_in_app()
    chosen_game = input("---------\n")
    if chosen_game.lower() == 'twenty one' or chosen_game == '2': 
        play_twenty_one()
    else:
        print("\nThis game is not available currently, Please choose another game.")
        input("\nPress any key to continue ")
        clear_screen()

4 إعجابات

جميل ، ما شاء الله :heart:
مبروك دخولك المستوي الثانى :heart:

إعجابَين (2)

شكرا لك والله يبارك فيك :dizzy: :shamrock:

إعجاب واحد (1)