{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# Milestone Project 2 - Walkthrough Steps Workbook\n",
    "Below is a set of steps for you to follow to try to create the Blackjack Milestone Project game!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Game Play\n",
    "To play a hand of Blackjack the following steps must be followed:\n",
    "1. Create a deck of 52 cards\n",
    "2. Shuffle the deck\n",
    "3. Ask the Player for their bet\n",
    "4. Make sure that the Player's bet does not exceed their available chips\n",
    "5. Deal two cards to the Dealer and two cards to the Player\n",
    "6. Show only one of the Dealer's cards, the other remains hidden\n",
    "7. Show both of the Player's cards\n",
    "8. Ask the Player if they wish to Hit, and take another card\n",
    "9. If the Player's hand doesn't Bust (go over 21), ask if they'd like to Hit again.\n",
    "10. If a Player Stands, play the Dealer's hand. The dealer will always Hit until the Dealer's value meets or exceeds 17\n",
    "11. Determine the winner and adjust the Player's chips accordingly\n",
    "12. Ask the Player if they'd like to play again"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Playing Cards\n",
    "A standard deck of playing cards has four suits (Hearts, Diamonds, Spades and Clubs) and thirteen ranks (2 through 10, then the face cards Jack, Queen, King and Ace) for a total of 52 cards per deck. Jacks, Queens and Kings all have a rank of 10. Aces have a rank of either 11 or 1 as needed to reach 21 without busting. As a starting point in your program, you may want to assign variables to store a list of suits, ranks, and then use a dictionary to map ranks to values."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## The Game\n",
    "### Imports and Global Variables\n",
    "** Step 1: Import the random module. This will be used to shuffle the deck prior to dealing. Then, declare variables to store suits, ranks and values. You can develop your own system, or copy ours below. Finally, declare a Boolean value to be used to control <code>while</code> loops. This is a common practice used to control the flow of the game.**\n",
    "\n",
    "    suits = ('Hearts', 'Diamonds', 'Spades', 'Clubs')\n",
    "    ranks = ('Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace')\n",
    "    values = {'Two':2, 'Three':3, 'Four':4, 'Five':5, 'Six':6, 'Seven':7, 'Eight':8, 'Nine':9, 'Ten':10, 'Jack':10,\n",
    "             'Queen':10, 'King':10, 'Ace':11}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "suits = ('Hearts', 'Diamonds', 'Spades', 'Clubs')\n",
    "ranks = ('Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace')\n",
    "values = {'Two':2, 'Three':3, 'Four':4, 'Five':5, 'Six':6, 'Seven':7, 'Eight':8, 'Nine':9, 'Ten':10, 'Jack':10,\n",
    "         'Queen':10, 'King':10, 'Ace':11}\n",
    "\n",
    "playing = True"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Class Definitions\n",
    "Consider making a Card class where each Card object has a suit and a rank, then a Deck class to hold all 52 Card objects, and can be shuffled, and finally a Hand class that holds those Cards that have been dealt to each player from the Deck."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Step 2: Create a Card Class**<br>\n",
    "A Card object really only needs two attributes: suit and rank. You might add an attribute for \"value\" - we chose to handle value later when developing our Hand class.<br>In addition to the Card's \\_\\_init\\_\\_ method, consider adding a \\_\\_str\\_\\_ method that, when asked to print a Card, returns a string in the form \"Two of Hearts\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Card():\n",
    "    \n",
    "    def __init__(self, suit, rank):\n",
    "        self.suit = suit\n",
    "        self.rank = rank\n",
    "    \n",
    "    def __str__(self):\n",
    "        return self.rank + \" of \" + self.suit"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Step 3: Create a Deck Class**<br>\n",
    "Here we might store 52 card objects in a list that can later be shuffled. First, though, we need to *instantiate* all 52 unique card objects and add them to our list. So long as the Card class definition appears in our code, we can build Card objects inside our Deck \\_\\_init\\_\\_ method. Consider iterating over sequences of suits and ranks to build out each card. This might appear inside a Deck class \\_\\_init\\_\\_ method:\n",
    "\n",
    "    for suit in suits:\n",
    "        for rank in ranks:\n",
    "\n",
    "In addition to an \\_\\_init\\_\\_ method we'll want to add methods to shuffle our deck, and to deal out cards during gameplay.<br><br>\n",
    "OPTIONAL: We may never need to print the contents of the deck during gameplay, but having the ability to see the cards inside it may help troubleshoot any problems that occur during development. With this in mind, consider adding a \\_\\_str\\_\\_ method to the class definition."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Deck:\n",
    "    \n",
    "    def __init__(self):\n",
    "        self.deck = []  # start with an empty list\n",
    "        for suit in suits:\n",
    "            for rank in ranks:\n",
    "                self.deck.append(Card(suit,rank))\n",
    "    \n",
    "    def __str__(self):\n",
    "        deck_comp = ''\n",
    "        for card in self.deck:\n",
    "            deck_comp += '\\n' + card.__str__()\n",
    "        return \" The deck has : \"+ deck_comp\n",
    "\n",
    "    def shuffle(self):\n",
    "        random.shuffle(self.deck)\n",
    "        \n",
    "    def deal(self):\n",
    "        single_card = self.deck.pop()\n",
    "        return single_card"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "TESTING: Just to see that everything works so far, let's see what our Deck looks like!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " The deck has : \n",
      "King of Spades\n",
      "Nine of Clubs\n",
      "Three of Clubs\n",
      "Queen of Diamonds\n",
      "Ace of Diamonds\n",
      "Jack of Spades\n",
      "Jack of Hearts\n",
      "Five of Diamonds\n",
      "Seven of Clubs\n",
      "Ace of Hearts\n",
      "Three of Diamonds\n",
      "Eight of Spades\n",
      "King of Diamonds\n",
      "Five of Hearts\n",
      "Three of Spades\n",
      "King of Clubs\n",
      "Jack of Clubs\n",
      "Two of Diamonds\n",
      "Nine of Hearts\n",
      "Six of Clubs\n",
      "Queen of Hearts\n",
      "Four of Diamonds\n",
      "Five of Spades\n",
      "Ten of Spades\n",
      "Six of Diamonds\n",
      "Ace of Clubs\n",
      "Ten of Diamonds\n",
      "Three of Hearts\n",
      "King of Hearts\n",
      "Ten of Clubs\n",
      "Eight of Clubs\n",
      "Two of Hearts\n",
      "Two of Clubs\n",
      "Five of Clubs\n",
      "Eight of Diamonds\n",
      "Ten of Hearts\n",
      "Seven of Diamonds\n",
      "Seven of Spades\n",
      "Six of Hearts\n",
      "Queen of Spades\n",
      "Two of Spades\n",
      "Nine of Diamonds\n",
      "Four of Hearts\n",
      "Ace of Spades\n",
      "Queen of Clubs\n",
      "Jack of Diamonds\n",
      "Six of Spades\n",
      "Seven of Hearts\n",
      "Eight of Hearts\n",
      "Four of Clubs\n",
      "Four of Spades\n",
      "Nine of Spades\n"
     ]
    }
   ],
   "source": [
    "test_deck = Deck()\n",
    "test_deck.shuffle()\n",
    "print(test_deck)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<__main__.Card at 0x257ba13b780>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_deck.deal()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Great! Now let's move on to our Hand class."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Step 4: Create a Hand Class**<br>\n",
    "In addition to holding Card objects dealt from the Deck, the Hand class may be used to calculate the value of those cards using the values dictionary defined above. It may also need to adjust for the value of Aces when appropriate."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Hand:\n",
    "    def __init__(self):\n",
    "        self.cards = []  # start with an empty list as we did in the Deck class\n",
    "        self.value = 0   # start with zero value\n",
    "        self.aces = 0    # add an attribute to keep track of aces\n",
    "    \n",
    "    def add_card(self,card):\n",
    "        # card passed in\n",
    "        # from Deck.deal() --> single Card(suit,rank)\n",
    "        self.cards.append(card)\n",
    "        # has a rank, use that to get the numerical value from the dictionary\n",
    "        self.value += values[card.rank]\n",
    "        \n",
    "        # Check for aces in the pulled card\n",
    "        if card.rank == 'Ace':\n",
    "            self.aces += 1\n",
    "            \n",
    "    \n",
    "    def adjust_for_ace(self):\n",
    "        # Check if the value is greater than 21 and there is an ace in it\n",
    "        if self.value > 21 and self.aces:\n",
    "            self.value -= 10\n",
    "            self.aces -= 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_deck = Deck()\n",
    "test_deck.shuffle()\n",
    "# Pull one card out\n",
    "pulled_card = test_deck.deal()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ace of Spades\n"
     ]
    }
   ],
   "source": [
    "print(pulled_card)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Ace'"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pulled_card.rank"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_player = Hand()\n",
    "test_player.add_card(pulled_card)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "11"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_player.value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [],
   "source": [
    "new_card = test_deck.deal()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Six of Spades\n"
     ]
    }
   ],
   "source": [
    "print(new_card)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_player.add_card(new_card)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "27"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_player.value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_player.adjust_for_ace()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "17"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_player.value"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Step 5: Create a Chips Class**<br>\n",
    "In addition to decks of cards and hands, we need to keep track of a Player's starting chips, bets, and ongoing winnings. This could be done using global variables, but in the spirit of object oriented programming, let's make a Chips class instead!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Chips:\n",
    "    \n",
    "    def __init__(self, total = 100):\n",
    "        self.total = total  # This can be set to a default value or supplied by a user input\n",
    "        self.bet = 0\n",
    "        \n",
    "    def win_bet(self):\n",
    "        self.total += self.bet\n",
    "    \n",
    "    def lose_bet(self):\n",
    "        self.total -= self.bet"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Function Defintions\n",
    "A lot of steps are going to be repetitive. That's where functions come in! The following steps are guidelines - add or remove functions as needed in your own program."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Step 6: Write a function for taking bets**<br>\n",
    "Since we're asking the user for an integer value, this would be a good place to use <code>try</code>/<code>except</code>. Remember to check that a Player's bet can be covered by their available chips."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [],
   "source": [
    "def take_bet(chips):\n",
    "    \n",
    "    # waiting\n",
    "    waiting = True\n",
    "    while waiting:\n",
    "        try:\n",
    "            chips.bet = int(input('Please enter the amount you want to bet: '))\n",
    "        except:\n",
    "            print('Please enter an integer')\n",
    "        else:\n",
    "            if self.bet > self.total:\n",
    "                print('Your bet exceeds your total earnings. Your total : {}'.format(self.total))\n",
    "            else:\n",
    "                waiting = False\n",
    "            "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Step 7: Write a function for taking hits**<br>\n",
    "Either player can take hits until they bust. This function will be called during gameplay anytime a Player requests a hit, or a Dealer's hand is less than 17. It should take in Deck and Hand objects as arguments, and deal one card off the deck and add it to the Hand. You may want it to check for aces in the event that a player's hand exceeds 21."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [],
   "source": [
    "def hit(deck,hand):\n",
    "    \n",
    "    single_card = deck.deal()\n",
    "    hand.add_card(single_card)\n",
    "    hand.adjust_for_ace()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Step 8: Write a function prompting the Player to Hit or Stand**<br>\n",
    "This function should accept the deck and the player's hand as arguments, and assign playing as a global variable.<br>\n",
    "If the Player Hits, employ the hit() function above. If the Player Stands, set the playing variable to False - this will control the behavior of a <code>while</code> loop later on in our code."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [],
   "source": [
    "def hit_or_stand(deck,hand):\n",
    "    global playing  # to control an upcoming while loop\n",
    "    \n",
    "    while True:\n",
    "        x = input('Do you want to hit or stand? Please enter h or s ')\n",
    "\n",
    "        if x[0].lower() == 'h':\n",
    "            hit(deck,hand)\n",
    "\n",
    "        elif x[0].lower() == 's':\n",
    "            print('Player is standing, Dealers Turn')\n",
    "            playing = False\n",
    "\n",
    "        else:\n",
    "            print('Sorry I did not get that. Please enter h or s')\n",
    "            continue\n",
    "        \n",
    "        break       \n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Step 9: Write functions to display cards**<br>\n",
    "When the game starts, and after each time Player takes a card, the dealer's first card is hidden and all of Player's cards are visible. At the end of the hand all cards are shown, and you may want to show each hand's total value. Write a function for each of these scenarios."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [],
   "source": [
    "def show_some(player,dealer):\n",
    "    \n",
    "    print('DEALERS HAND:')\n",
    "    print('*one card is hidden*')\n",
    "    print(dealer.card[1])\n",
    "    print('PLAYERS HAND:')\n",
    "    print('\\n')\n",
    "    for card in player.cards:\n",
    "        print(card)\n",
    "    \n",
    "def show_all(player,dealer):\n",
    "    \n",
    "    print('DEALERS HAND:')\n",
    "    print('\\n')\n",
    "    for card in dealer.cards:\n",
    "        print(card)\n",
    "    print('PLAYERS HAND:')\n",
    "    print('\\n')\n",
    "    for card in player.cards:\n",
    "        print(card)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Step 10: Write functions to handle end of game scenarios**<br>\n",
    "Remember to pass player's hand, dealer's hand and chips as needed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [],
   "source": [
    "def player_busts(player,dealer,chips):\n",
    "    print('PLAYER BUST!')\n",
    "    chips.lose_bet()\n",
    "\n",
    "def player_wins(player,dealer,chips):\n",
    "    print('PLAYER WIN!')\n",
    "    chips.win_bet()\n",
    "\n",
    "def dealer_busts(player,dealer,chips):\n",
    "    print('PLAYER WIN! Dealer busts')\n",
    "    chips.win_bet()\n",
    "    \n",
    "def dealer_wins(player,dealer,chips):\n",
    "    print('DEALER WINS!')\n",
    "    chips.lose_bet()\n",
    "    \n",
    "def push(player,dealer):\n",
    "    print('PLAYER AND DEALER TIE! Its a PUSH.')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### And now on to the game!!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "while True:\n",
    "    # Print an opening statement\n",
    "\n",
    "    \n",
    "    # Create & shuffle the deck, deal two cards to each player\n",
    "\n",
    "    \n",
    "        \n",
    "    # Set up the Player's chips\n",
    "    \n",
    "    \n",
    "    # Prompt the Player for their bet\n",
    "\n",
    "    \n",
    "    # Show cards (but keep one dealer card hidden)\n",
    "\n",
    "    \n",
    "    while playing:  # recall this variable from our hit_or_stand function\n",
    "        \n",
    "        # Prompt for Player to Hit or Stand\n",
    "        \n",
    "        \n",
    "        # Show cards (but keep one dealer card hidden)\n",
    " \n",
    "        \n",
    "        # If player's hand exceeds 21, run player_busts() and break out of loop\n",
    "        \n",
    "\n",
    "            break\n",
    "\n",
    "    # If Player hasn't busted, play Dealer's hand until Dealer reaches 17\n",
    "    \n",
    "    \n",
    "        # Show all cards\n",
    "    \n",
    "        # Run different winning scenarios\n",
    "        \n",
    "    \n",
    "    # Inform Player of their chips total \n",
    "    \n",
    "    # Ask to play again\n",
    "\n",
    "        break"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And that's it! Remember, these steps may differ significantly from your own solution. That's OK! Keep working on different sections of your program until you get the desired results. It takes a lot of time and patience! As always, feel free to post questions and comments to the QA Forums.\n",
    "# Good job!"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
