Tuesday, February 9, 2016

w5d2 - git, finally. And some refactoring.

20160206_173651 We had a long weekend in the center of Costa Rica, near Arenal. My plant-obsessed photos are here.  It was a magnificent experience, and a welcome respite from the unrelenting heat and brown foliage of Guanacaste. It was also a welcome chance to rest my brain after the feast of the first 4 weeks.

On returning, I managed to finish my refactoring of the Blackjack game from Week 3.  Because what else would you do after 5 hours of driving across Costa Rica?  A playable demo is here.  The github is here.

On Jeremy's (one instructor) advice, I reworked the play-handling such that each turn was handled by a method in the Game class, rather than passing the dealer's hand to a method in each player instance.  As Jeremy pointed out, you really don't want classes on the same organizational level talking to each other directly.  I also cleaned up a lot of detritus and cleaned up some variables (i.e. temporary variables used only within a particular method don't need to be 'self'd.), swapped out a few for iterators for list comprehensions, etc.

The other big changes were to add betting game play (with blackjacks handled appropriately), the ability to add or remove players after each round (and even to sit out a round, then come back in).  I also built a new function for my 'entry validation' module that refuses to accept an answer that is in a list.  (If you're adding a player, you don't want to have duplicate names.)  My existing validation functions only checked whether an entry was in a list of acceptable answers.  Also, it now uses a 6-deck shoe that gets restored when it reaches 75% used.  Take that, card counters!

The game still doesn't handle doubling bets, insurance, or splitting a hand.  Splitting a hand would be the hardest to add since the Player class is currently configured to hold only one hand per instance.  To split hands, I would have to make each player have an array of Hand instances (analogous to how the Game instance has an array of player instances).  All of the player Hand methods would have to be updated to "for hand in player.hands: <do thing>".  Maybe there's an easier way, but I can't see it right now. I need to focus on the Django projects we're working on now, so I'll leave Blackjack as-is.

To be fair, it's in pretty good shape, and I'm reasonably happy with it.

No comments:

Post a Comment