|
| 1 | +import random |
| 2 | +import art ## importing logo from that |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +print (art.logo) |
| 10 | + |
| 11 | +is_start=False |
| 12 | +print("Welcome to Blackjack Game") |
| 13 | +if input("Do you want to start the game?'y' for yes 'n'for no: " ) =='y': |
| 14 | + is_start=True |
| 15 | + |
| 16 | +else: |
| 17 | + |
| 18 | + is_start=False |
| 19 | + |
| 20 | +while is_start: |
| 21 | + |
| 22 | + def deal_card(): ## this function will choose random card |
| 23 | + cards=[11,2,3,4,5,6,7,8,9,10,10,10,10] ## list of cards |
| 24 | + return random.choice(cards) |
| 25 | + |
| 26 | + def calculate_score(cards): ## it will calculate score |
| 27 | + if sum(cards)==21 and len(cards)==2: |
| 28 | + return 0 |
| 29 | + if 11 in cards and sum(cards)>21: |
| 30 | + cards.remove(11) |
| 31 | + cards.append(1) |
| 32 | + return sum(cards) |
| 33 | + |
| 34 | + def compare (user_score,computer_score): ## this is to comapre user and computer score |
| 35 | + if user_score==computer_score: |
| 36 | + return "Draw!" |
| 37 | + elif computer_score==0: |
| 38 | + return "Lose!opponent has Blackjack" |
| 39 | + elif user_score>21: |
| 40 | + return "You went over. You loose!" |
| 41 | + elif user_score>computer_score: |
| 42 | + return "You won!" |
| 43 | + else: |
| 44 | + return "You won!" |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | + user_cards=[deal_card(),deal_card()] ## first initialization of user cards |
| 49 | + computer_cards=[deal_card(),deal_card()] # and computer cards which will contain two cards at first |
| 50 | + is_game_over=False # boolean it will decide when game will stop |
| 51 | + |
| 52 | + while not is_game_over: # until when is_game_over will be false the game will continue |
| 53 | + |
| 54 | + user_score=calculate_score(user_cards) #keeping user score |
| 55 | + computer_score=calculate_score(computer_cards) # and computer score in a variable |
| 56 | + print(f" Your cards: {user_cards}, current score: {user_score}") |
| 57 | + print(f"First card of computer: {computer_cards[0]}") |
| 58 | + if user_score==0 or user_score>21 or computer_score==0: # at this condition we will like to exit our game |
| 59 | + is_game_over=True |
| 60 | + else: |
| 61 | + print("You want to draw another card?") |
| 62 | + user_choice=input("Type 'y' to draw and 'n'to pass: ") ## asking user to continue or not |
| 63 | + if user_choice=='y': |
| 64 | + user_cards.append(deal_card()) ## adding new card to list |
| 65 | + else: |
| 66 | + is_game_over=True |
| 67 | + |
| 68 | + while computer_score!=0 and computer_score<17: ## computer will continue according to condition |
| 69 | + computer_cards.append(deal_card()) |
| 70 | + computer_score=calculate_score(computer_cards) ##calculating computer score |
| 71 | + |
| 72 | + print(f" Your final hand: {user_cards}, final score: {user_score}") |
| 73 | + print(f" Your final hand: {computer_cards}, final score: {computer_score}") |
| 74 | + |
| 75 | + print(compare(user_score,computer_score)) |
| 76 | + if input("Do you want to play another game?'y' for yes 'n'for no: " ) =='y': |
| 77 | + is_start=True |
| 78 | + else: |
| 79 | + is_start=False |
| 80 | + |
0 commit comments