{"id":1915,"date":"2019-06-13T00:11:31","date_gmt":"2019-06-13T00:11:31","guid":{"rendered":"http:\/\/wiki.thomasandsofia.com\/?p=1915"},"modified":"2019-06-14T13:31:46","modified_gmt":"2019-06-14T13:31:46","slug":"section-11-milestone-project-2","status":"publish","type":"post","link":"https:\/\/wiki.thomasandsofia.com\/?p=1915","title":{"rendered":"Section 11: Milestone Project #2"},"content":{"rendered":"<p><a href=\"http:\/\/wiki.thomasandsofia.com\/?p=1908\">&lt; Section 10<\/a> | <a href=\"http:\/\/wiki.thomasandsofia.com\/?p=1919\">Section 12 &gt;<\/a><\/p>\n<h1>Overview &#8211; Blackjack<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9497646#questions\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9497646#questions<\/a><\/p>\n<p>Instructions<\/p>\n<ul>\n<li>Use OOP to create a BlackJack game with Python<\/li>\n<li>Computer dealer, human player<\/li>\n<li>Normal deck of cards<\/li>\n<li>Human:\n<ul>\n<li>bank amount<\/li>\n<\/ul>\n<\/li>\n<li>Dealer 1 up, 1 down<\/li>\n<li>Human 2 up<\/li>\n<li>Human goes first\n<ul>\n<li>Hit or stay\n<ul>\n<li>No splits, Insurance or Double Down<\/li>\n<\/ul>\n<\/li>\n<li>Plays until done or bust.<\/li>\n<\/ul>\n<\/li>\n<li>If not BUST, computer plays until they beat the Human (WHAT?)<\/li>\n<li>Special Rules\n<ul>\n<li>Face cards = 10<\/li>\n<li>Aces are 11 or 1 depending on requirement<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h1>My Solution<\/h1>\n<p>Not as eloquent, but far more flexible and realistic.<\/p>\n<p>blackjack.py<\/p>\n<pre>from blkjkc import *\r\n\r\ndef get_players():\r\n    while True:\r\n        try:\r\n            players = int(input(\"Enter the number of players (1 - 7): \"))\r\n        except:\r\n            print(\"Invalid number entered. Please try again.\")\r\n            continue\r\n        else:\r\n            if players &lt; 1 or players &gt; 7:\r\n                print(\"Invalid number entered. Please try again.\")\r\n                continue\r\n            else:\r\n                break\r\n    return players\r\n\r\n#get players\r\nplayer=[]\r\nnumplayers = get_players()\r\nfor i in range(0, numplayers+1):\r\n    # print(i)\r\n    player.append(Player(i))\r\n\r\n# Start game\r\ngame_on = True\r\nwhile game_on:\r\n    # get new deck\r\n    print(\"Shuffling new deck...\")\r\n    deck = Deck()\r\n    # Get bets\r\n    for player_num in range(1,numplayers+1):\r\n        player[player_num].get_bet()\r\n    #deal the first 2 cards\r\n    for i in range(0,2):\r\n        for player_num in range(0,numplayers+1):\r\n            if player[player_num].bet:\r\n                card = deck.get_card()\r\n                #print(f\"Card is {card}\")\r\n                player[player_num].hit(card)\r\n\r\n    #show everyone's hand\r\n    print(\"\\nHere are your cards...\")\r\n    for player_num in range(0,numplayers+1):\r\n        if player[player_num].bet:\r\n            hand=player[player_num].hand.show(player_num)\r\n            if player_num:\r\n                #not the dealer\r\n                tally = player[player_num].hand.tally()\r\n            else:\r\n                tally='??'\r\n            print(player[player_num].name + f\" ({tally}): \"+ hand)\r\n            if tally == 21:\r\n                print(f\"Black Jack!  You won ${player[player_num].bet}\")\r\n                player[player_num].bank += player[player_num].bet ** 2\r\n                player[player_num].bet = 0\r\n    #Start Play!\r\n    for player_num in range(1,numplayers+1):\r\n        if player[player_num].bet:\r\n            print(player[player_num].name)\r\n            while True:\r\n                print(\"You have \"+ str(player[player_num].hand.tally()))\r\n                hit=input(\"Hit or Stay (h \/ S)\")\r\n                if hit.lower() != 'h' and hit.lower() != 'hit' :\r\n                    break\r\n                #Hit\r\n                card = deck.get_card()\r\n                player[player_num].hit(card)\r\n                print(f\"Your new card is: {card}\")\r\n                tally = player[player_num].hand.tally()\r\n                if tally &gt;= 21:\r\n                    break\r\n            if player[player_num].hand.tally() &gt; 21:\r\n                print(\"BUST!\")\r\n                player[player_num].bet=0\r\n    #Dealer's turn\r\n    hand=player[0].hand.show()\r\n    tally = player[0].hand.tally()\r\n    print(f\"Dealer ({tally}): \" + hand)\r\n    while player[0].hand.tally()&lt;17:\r\n        card = deck.get_card()\r\n        player[0].hit(card) \r\n    print(f\"Dealer takes a: {card}\") \r\n    #check for winners \r\n    dealer_tally = player[0].hand.tally()\r\n    if dealer_tally &gt;21:\r\n        print(\"Dealer Busts!\")\r\n    else:\r\n        print(f\"Dealer stands on {dealer_tally}\")\r\n    for player_num in range(1,numplayers+1):\r\n        if player[player_num].bet:\r\n            if player[player_num].hand.tally()&gt;dealer_tally or dealer_tally&gt;21:\r\n                player[player_num].bank += player[player_num].bet * 2\r\n                print(f\"\\n{player[player_num].name} won ${player[player_num].bet}!\")\r\n                print(f\"You now have ${player[player_num].bank}\")\r\n            elif player[player_num].hand.tally()==dealer_tally:\r\n                player[player_num].bank += player[player_num].bet\r\n                print(f\"\\n{player[player_num].name} tied!\")\r\n            else:\r\n                print(f\"\\n{player[player_num].name} lost!\")\r\n                print(f\"You still have ${player[player_num].bank} left in your account.\")\r\n    play=input(\"\\nPlay again? (Y \/ n)\")\r\n    if play.lower() == 'n' or play.lower() == 'no':\r\n        game_on = False\r\n    else:\r\n        # reset player's hands\r\n        for player_num in range(0, numplayers+1):\r\n            player[player_num].reset()\r\nprint(\"\\nThanks for playing!\")<\/pre>\n<p>blkjkc.py<\/p>\n<pre>import random\r\nclass Card():\r\n    def __init__(self, suit, face, value):\r\n        self.suit = suit\r\n        self.face = face\r\n        self.value = value\r\n        if suit == 'H' or suit == 'D':\r\n            self.color = 'red'\r\n        else:\r\n            self.color = 'black'\r\n    def __str__(self):\r\n        return self.face + self.suit\r\n    def show(self, hole_card=False):\r\n        if not hole_card:\r\n            return self.face + self.suit\r\n        else:\r\n            return 'XX'\r\n\r\nclass Deck():\r\n    def __init__(self, decks=1):\r\n        self.deck=[]\r\n        try:\r\n            decks = int(decks)\r\n        except:\r\n            decks = 1\r\n        for deck in range(0,decks):\r\n            for suit in ['H', 'C', 'S', 'D']:\r\n                # 2 - 10\r\n                for face in range(2, 11):\r\n                    self.deck.append(Card(suit, str(face), face))\r\n                # Face cards\r\n                for face in ['K', 'Q', 'J']:\r\n                    #card = Card(suit, face, 10)\r\n                    self.deck.append(Card(suit, face, 10))\r\n                # Aces\r\n                self.deck.append(Card(suit, 'A', 11))\r\n        random.shuffle(self.deck)\r\n    def __len__(self):\r\n        return len(self.deck)\r\n    def __str__(self):\r\n        cards=[]\r\n        for card in self.deck:\r\n            cards.append(card.show())\r\n        return str(cards)\r\n    def get_card(self):\r\n        if (len(self.deck))&gt;=1:\r\n            return self.deck.pop()\r\n        return False\r\n\r\nclass Hand():\r\n    def __init__(self):\r\n        self.cards=[]\r\n    def hit(self, card):\r\n        self.cards.append(card)\r\n        return self.tally()\r\n    def show(self, show_hole=True):\r\n        firstcard=True\r\n        cards = []\r\n        for card in self.cards:\r\n            if firstcard:\r\n                firstcard = False\r\n                cards.append(card.show(not show_hole))\r\n            else:\r\n                cards.append(card.show())\r\n        return \" | \".join(cards)\r\n    def tally(self):\r\n        self.total = 0\r\n        aces=[]\r\n        for card in self.cards:\r\n            self.total += card.value\r\n        if self.total &gt; 21:\r\n            # Look for aces\r\n            for card in self.cards:\r\n                if card.face == 'A':\r\n                    self.total -= 10\r\n                    if self.total &lt;=21:\r\n                        # Stop looking for aces\r\n                        break\r\n        return self.total\r\nclass Player():\r\n    def __init__(self, player_num):\r\n        self.hand = Hand()\r\n        if not player_num:\r\n            #is dealer\r\n            self.name = 'Dealer'\r\n            self.bet = True\r\n        else:\r\n            #Get name\r\n            while True:\r\n                self.name = input(f\"\\nPlayer #{player_num}, please enter your name: \")\r\n                if len(self.name.strip()):\r\n                    break\r\n                else:\r\n                    self.name = \"Sofia\"\r\n                    break\r\n                    #print(\"Invalid name enterd. Please try again.\\n\")\r\n            while True:\r\n                try:\r\n                    self.bank=int(input(f\"Hello {self.name}. How much money did you bring? $\"))\r\n                except:\r\n                    self.bank = 10\r\n                    break\r\n                    #print(\"Invalid amount enterd. Please try again.\\n\")\r\n                    #continue\r\n                else:\r\n                    if self.bank &lt;= 0: \r\n                        self.bank = 0\r\n                        print(f\"I'm sorry, {self.name}. That is not enough money to play, but you may stay and watch.\")\r\n                        break \r\n    def get_bet(self):\r\n        if self.bank &gt; 0:\r\n            while True:\r\n                try:\r\n                    self.bet=int(input(f\"{self.name}. How much do you wish to bet? (Max {self.bank}) $\"))\r\n                except:\r\n                    self.bet = 1\r\n                    break\r\n                    #print(\"Invalid amount enterd. Please try again.\\n\")\r\n                    #continue\r\n                else:\r\n                    self.bet=abs(self.bet)\r\n                    if self.bet &gt;= self.bank:\r\n                        self.bet = self.bank\r\n                    self.bank -= self.bet\r\n                    if self.bet==0:\r\n                        print(\"OK. Please stay and watch.\")\r\n                    break\r\n        else:\r\n            self.bet = 0\r\n            print(f\"I'm sorry, {self.name}.  You do not have enough money to play, but you may stay and watch.\")\r\n        return self.bet\r\n    def hit(self, card):\r\n        self.hand.hit(card)\r\n    def reset(self):\r\n        self.hand = Hand()\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&lt; Section 10 | Section 12 &gt; Overview &#8211; Blackjack https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9497646#questions Instructions Use OOP to create a BlackJack game with Python Computer dealer, human player Normal deck of cards Human: bank amount Dealer 1 up, 1 down Human 2 up Human goes first Hit or stay No splits, Insurance or Double Down Plays until done ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/wiki.thomasandsofia.com\/?p=1915\" title=\"read more...\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[],"class_list":["post-1915","post","type-post","status-publish","format-standard","hentry","category-python-bootcamp-0-to-hero"],"_links":{"self":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/1915","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1915"}],"version-history":[{"count":5,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/1915\/revisions"}],"predecessor-version":[{"id":1922,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/1915\/revisions\/1922"}],"wp:attachment":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1915"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1915"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1915"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}