{"id":1358,"date":"2025-01-01T12:56:40","date_gmt":"2025-01-01T11:56:40","guid":{"rendered":"https:\/\/vroamam.com\/wordpress\/?p=1358"},"modified":"2025-01-01T12:56:40","modified_gmt":"2025-01-01T11:56:40","slug":"day-11-capstone-project","status":"publish","type":"post","link":"https:\/\/vroamam.com\/wordpress\/blog\/day-11-capstone-project\/","title":{"rendered":"Day 11 &#8211; Capstone Project"},"content":{"rendered":"\n<p>Write a text based Blackjack game.<\/p>\n\n\n\n<p>My Solution:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import random # needed to draw a random item from the list\nfrom art import logo\nimport os\n\n# sets up the card we can play with\ncards = &#91;11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]\n\ndef deal_card():\n    \"\"\"Returns a random card from the deck\"\"\"\n    card = random.choice(cards)\n    return card\n\ndef check_ace(cards):\n    \"\"\"Take a list of cards and check of the ace should be 1 or 11\"\"\"\n    if 11 in cards and sum(cards) > 21:\n        cards.remove(11)\n        cards.append(1)\n    return sum(cards)\n\ndef is_blackjack(hand):\n    \"\"\"Take a list of cards and return True if it's a blackjack, False otherwise\"\"\"\n    # NOTE I did not include checks for only two cards - I need to revisit this, but\n    # it is used in result which takes scores not cards so needs adjusting as well\n\n    if sum(hand) == 21 and len(hand) == 2:\n        return True\n    else:\n        return False\n\ndef is_bust(score):\n    \"\"\"Take a score and return True if the score is over 21, False otherwise\"\"\"\n    if score > 21:\n        return True\n    else:\n        return False\n\ndef result(u_score, c_score):\n    \"\"\"Take two scores and compare them to determine the winner\"\"\"\n    # define complex messages\n    player_win = f\"\\n{'*' * 20} \\n You win \\n{'*' * 20}\"\n    dealer_win = f\"\\n{'*' * 20} \\n Dealer wins \\n{'*' * 20}\"\n    draw = f\"\\n{'*' * 20} \\n !Draw! \\n{'*' * 20}\"\n    player_bust = f\"\\n{'*' * 20} \\n Your score {u_score}. You Bust. You lose \\n{'*' * 20}\"\n    dealer_bust = f\"\\n{'*' * 20} \\n Dealer score {c_score}. Dealer Bust. You win \\n{'*' * 20}\"\n    if u_score > c_score and not is_bust(u_score):\n        return player_win\n    elif c_score > u_score and not is_bust(c_score):\n        return dealer_win\n    elif u_score == c_score:\n        return draw\n    elif is_bust(u_score):\n        return player_bust\n    elif is_bust(c_score):\n        return dealer_bust\n\ndef play_game():\n    \"\"\"Play a single game of blackjack\"\"\"\n    # Setup empty lists to hold each players cards\n    user_cards = &#91;]\n    dealer_cards = &#91;]\n\n    # set the game_over token to false\n    is_game_over = False\n\n    # deal the first two cards\n    # the _ can be used when you don't need the value in range inside the loop\n    # for example: for i in range(2): would work but we never need to know the value of i\n    # so we can use an underscore instead\n\n    for _ in range(2):\n        user_cards.append(deal_card())\n        dealer_cards.append(deal_card())\n\n    # play the game until it ends\n    while not is_game_over:\n        # calculate the scores from the list items\n        user_score = check_ace(user_cards)\n        dealer_score = check_ace(dealer_cards)\n        print(f\"User cards {user_cards} Total {user_score}\")\n        print(f\"Dealers first card {dealer_cards&#91;0]}\")\n\n        # Check if either player has blackjack. if they do the game ends\n        if is_blackjack(dealer_cards):\n            is_game_over = True\n            return result(user_score, dealer_score)\n        elif is_blackjack(user_cards):\n            is_game_over = True\n            return result(user_score, dealer_score)\n\n\n        if is_game_over:    # This would be true if either player had blackjack or had already bust.\n            return result(user_score, dealer_score)\n        else: # If neither player has blackjack or has gone bust, the user needs to decide if they want more cards\n            # Ask the user if they want another card\n            user_should_deal = input(\"++Type 'y' to get another card, type 'n' to pass: \")\n            while user_should_deal == \"y\" and not is_game_over: # as long as the user say yes\n                user_cards.append(deal_card())                  # deal another card to the dealer\n                user_score = check_ace(user_cards)              # calculate the score and check for aces\n                if is_bust(user_score):                         # check user score for > 21\n                    print(f\"User's cards {user_cards} Total {user_score}.\")\n                    is_game_over = True\n                    return result(user_score, dealer_score)\n                else:                                           # if not == 21 nor > 21\n                    print(f\"Player's cards {user_cards} Total {user_score}.\")\n                    user_should_deal = input(\"--Type 'y' to get another card, type 'n' to pass: \") # ask if they want another card\n\n            while dealer_score &lt; 17 and not is_game_over:       # One the player has stopped taking cards. If the game has not ended\n                dealer_cards.append(deal_card())                # deal another card to the dealer\n                dealer_score = check_ace(dealer_cards)          # add up the dealers score\n                if is_bust(dealer_score):                       # check for bust\n                    print(\"dealer bust\")\n                    is_game_over = True\n                    return result(user_score, dealer_score)\n\n                                                                            # once the dealer score > 17 stop\n            print(f\"User's cards {user_cards} Total {user_score}.\")         # print the hands\n            print(f\"Dealer's cards {dealer_cards} Total {dealer_score}.\")   # print the scores'\n            return result(user_score, dealer_score)                         # calculate and print the result\n\nplay = 'y'\nprint(logo)\nwhile play == 'y':\n    message = play_game()\n    print(message)\n    play = input(\"\\nDo you want to play again? (y\/n): \")\n    if play == 'n':\n        print(\"Thanks for playing!\")\n    else:\n        os.system('cls')\n        print(logo)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Write a text based Blackjack game. My Solution:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"","ocean_second_sidebar":"","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"","ocean_custom_header_template":"","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"","ocean_menu_typo_font_family":"","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"on","ocean_gallery_id":[],"footnotes":""},"categories":[144,148,3],"tags":[146,145,11],"class_list":["post-1358","post","type-post","status-publish","format-standard","hentry","category-100daysofcode","category-python","category-training","tag-100daysofcode","tag-python","tag-training","entry"],"_links":{"self":[{"href":"https:\/\/vroamam.com\/wordpress\/wp-json\/wp\/v2\/posts\/1358","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vroamam.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vroamam.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vroamam.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vroamam.com\/wordpress\/wp-json\/wp\/v2\/comments?post=1358"}],"version-history":[{"count":1,"href":"https:\/\/vroamam.com\/wordpress\/wp-json\/wp\/v2\/posts\/1358\/revisions"}],"predecessor-version":[{"id":1359,"href":"https:\/\/vroamam.com\/wordpress\/wp-json\/wp\/v2\/posts\/1358\/revisions\/1359"}],"wp:attachment":[{"href":"https:\/\/vroamam.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1358"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vroamam.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1358"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vroamam.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}