Skip to content

Commit

Permalink
Add method to get the next action of a dealer
Browse files Browse the repository at this point in the history
Add the get_action method to the Dealer class to be able to get the next action of the dealer based on the cards that they have. Also add testing for the new method.
  • Loading branch information
ExcaliburZero committed Dec 21, 2015
1 parent 35e3bd5 commit a2d471c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
23 changes: 23 additions & 0 deletions blackjack/dealers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""This module is used to store the classes related to the dealer."""
from .people import Person
from .actions import Action


class Dealer(Person):
Expand All @@ -16,3 +17,25 @@ def __init__(self):
self.name = "Dealer"
# The cards that the dealer has
self.cards = []

def get_action(self):
"""
The method that is used to get the next action of the dealer.
:returns Action: The next action of the dealer.
"""

# See if the dealer's cards are certainly over or equal to 17
any_below = False
for result in self.card_values():
if result < 17:
any_below = True

# Decide on the action based on whether or not all possibilities are over or equal to 17
if any_below:
next_action = Action("Hit")
else:
next_action = Action("Stand")

# Return the action
return next_action
30 changes: 30 additions & 0 deletions tests/test_dealer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import unittest

from blackjack import Dealer
from blackjack import Card


class TestDealer(unittest.TestCase):
Expand All @@ -20,3 +21,32 @@ def test_initial_cards(self):
# Make sure the dealer does not start with any cards
dealer = Dealer()
self.assertEqual(dealer.cards, [])

def test_get_action(self):
"""A test which makes sure that the get_action method of the Dealer class functions properly."""

# Try several test cases
test_cases = [
[Card("Clubs", "2"), Card("Clubs", "2"), "Hit", ],
[Card("Clubs", "10"), Card("Clubs", "10"), "Stand", ],
[Card("Clubs", "10"), Card("Clubs", "7"), "Stand", ],
[Card("Clubs", "10"), Card("Clubs", "6"), "Hit", ],
[Card("Clubs", "Ace"), Card("Clubs", "6"), "Hit", ],
[Card("Clubs", "Ace"), Card("Clubs", "6"), Card("Clubs", "10"), "Stand", ],
[Card("Clubs", "Jack"), Card("Clubs", "7"), "Stand", ],
[Card("Clubs", "Queen"), Card("Clubs", "6"), "Hit", ],
[Card("Clubs", "King"), Card("Clubs", "6"), "Hit", ],
[Card("Clubs", "Ace"), Card("Clubs", "Ace"), Card("Clubs", "6"), "Hit", ],
[Card("Clubs", "Ace"), Card("Clubs", "Ace"), Card("Clubs", "6"), "Hit", ],
[Card("Clubs", "7"), Card("Clubs", "8"), "Hit", ],
[Card("Clubs", "7"), Card("Clubs", "8"), Card("Clubs", "Ace"), "Hit", ],
[Card("Clubs", "7"), Card("Clubs", "8"), Card("Clubs", "2"), "Stand", ],
[Card("Clubs", "Jack"), Card("Clubs", "Queen"), "Stand", ],
]
for case in test_cases:
dealer = Dealer()
cards = len(case) - 1
for index in range(cards):
dealer.add_card(case[index])
action = dealer.get_action()
self.assertEqual(action.move, case[-1], msg="The test of the get_action method with the index of " + str(test_cases.index(case)) + " failed.")

0 comments on commit a2d471c

Please sign in to comment.