Thursday, January 28, 2016

w3d4 - Making a blackjack game with OOP (classes in python)

Our week 3 capstone project is to build a blackjack game using classes.  After half a day, I'm about half way done. It would be faster, but I spent a lot of time (at the instructors urging) planning my approach.  Since I'd gotten pretty good with data structures in previous weeks, I naturally thought of doing the project with lists and dictionaries with a bit of class to hold the pieces together, so to speak.  Thinking in OOP is not a huge stretch, but it's not as natural...which is the point of this project, I suspect.

My goal is build not just a blackjack game, but any game that can be played with normal deck of cards (or multiple decks...).  It should accomodate Jokers and games other than black jack with a few modifications to the variables used to construct the deck, and with a unique Game.play().

The basic outline of my game is:

class Card
   properties: name(number, or face name, or Ace), suit, value, specialflag (for cards with special properties like the Ace), symbol (in case I ever get around to figuring out how to print the suit symbols)
   method: __repr__ override to print card.name + card.suit when "print card" is used

class CardGroup
   properties: container (to hold cards)
   methods: print cards (relies on card __repr__ override

class Deck(CardGroup) (Deck is a subclass of CardGroup)
   properties: suits (used in constructing a deck - any set of suits could theoretically be used)
                     name, value tuples - connects the card name with it's value.  All face cards in blackjack are worth 10, but hearts would be different, and in poker only name and suit would matter.
                      container (Deck and Hand are subclasses of CardGroup so their self.container needs to point to different things)

   methods: make deck (based on suit, value, name info), shuffle deck (just random.shuffle), draw_card to pop a chard off the end of the list of cards

class Hand(CardGroup)
   properties: container
  methods: add_card (takes the draw_card result from the Deck), play card (not yet implemented since blackjack doesn't use that method), evaluate hand (to decide the status of the hand - just adds the point value at present which undercuts universality for other kinds of games)

class Player
  properties - name, hand, money
  methods - draw starting hand, take turn (still working on how this and Game.play() interact, but should use polymorphism

class Dealer(Player)
  properties as above
  methods - as above, but 'take turn' will be a simple AI to replicate the rules of blackjack

class Game
  properties - players, deck
  methods - add player, add deck, take turn, get new deck, monitor progress/declare winner


So, yeah.  It's a bit of overkill.  It's fun to see how the classes interact, though.  It's also a hell of a confidence boost to see this coming together after the fiasco of trying to implement linked lists using objects.  More on that later...
 


1 comment: