Simple Blackjack game

Text-based Blackjack game in Python
February 19, 2016 – 01:15 am
Simple Blackjack game in

UML class diagram showing relationshipsYou should follow the style guide. I was thinking that NewBlackjackGame was an odd name for a class, but it turns out it's a function; it should therefore be new_blackjack_game. (That being said, I think all of the logic in that function should really be in Game, see below.)

There is no point having a main that just calls one other function. Either rename that function to main, or call the function directly if __name__ == "__main__":.

You only need to hold the values for face cards in the values_lookup, which should be a class attribute (it's the same for all cards/decks). The card's value can just be an integer for non-face cards, then you can dict.get either the face card value or use it directly. For example:

class Card: FACES = {'Ace': 1, 'Jack': 10, 'Queen': 10, 'King': 10} def __init__(self, face, suit): self.face = face self.suit = suit def __str__(self): return "{0.face} of {0.suit}".format(self) @property def value(self): return self.FACES.get(self.face, self.face)

Note the use of @property, which is a Pythonic way to implement the getters and setters used in other languages. In use:

> c = Card("Ace", "spades") >> str(c) 'Ace of spades' >> c.value 1 >> c = Card(9, "diamonds") >> str(c) '9 of diamonds' >> c.value 9

As already pointed out, don't hard-code the name of the Players. If you keep the Dealer separate, you can have as many others as you like:

class Game: def __init__(self, *players, start_credit=100): self.dealer = Dealer self.deck = Deck self.players = [Player(player, start_credit) for player in players] ...

Then play like:

game = Game("Anna", "Bob", "Chris") game.play # replaces `NewBlackjackGame`

I think the main issue is having logic in odd places. I would suggest a structure like the following:

  • Card - holds the value and suit of an individual card;
  • Deck - holds the cards and the logic for shuffling and dealing;
  • Hand - another collection of cards, with the logic for adding up scores. The hand doesn't need to know its owner, the logic for which Cards in the Hand to show should be in the Player. It shouldn't access a global deck - implement e.g. def draw_from(self, deck):;
  • Player - a player has a Hand of Cards, the input logic (hit_or_stand, which should incorporate validation), the rules for showing the cards, their credit and stats, etc.;
  • Dealer - a subclass of Player holding the dealer's playing logic and the different rules for showing its hand;
  • Game - holds the deck and the players and the logic for progressing through the game, including e.g. history, which should be Game instance attributes rather than global variables.
  • Even within the existing classes, your logic is all over the place. Consider this simplified implementation of Hand:

    class Hand: def __init__(self): # initialise the empty list of cards def draw_from(self, deck): # draw a card from the deck and add to list def return_to(self, deck): # empty list of cards and return to deck @property def value(self): # all the value logic goes here

    Source: codereview.stackexchange.com
    Interesting facts

    Questron is a fantasy role-playing video game series produced by Strategic Simulations, Inc.
    In Questron the player takes on the role of a young serf who tries to make a name for himself by traveling the realm in order to gain the power and experience necessary to defeat the wicked "Mantor", ruler of the "Land of Evil".
    The gameplay and...

    You might also like
    Simple Blackjack game in C# ~ Part 3
    Simple Blackjack game in C# ~ Part 3
    Simple Blackjack game in C# ~ Part 5
    Simple Blackjack game in C# ~ Part 5
    Popular Q&A
    avatar
    What does the Templar awareness do in AC Revelations?

    It will result in them attacking one of your dens.

    Related Posts