HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

OOP Blackjack in Python

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
pythonoopblackjack

Problem

I worked on this for a software engineer interview as the take home challenge. The company came back with this constructive feedback:



  • You made a slight mistake in ace handling that led to the inability


to handle multiple aces

  • You hard-coded global variables for player1 and player2 state (why is this bad?)



  • There was also some duplicated print code, some minor style issues,


and a small functionality issue.


Although the interview process is over, I want to improve this code for my own knowledge. Any advice you have on the above three points or additional feedback would be appreciated!

```
import random

#initializing constants
CARDS = ('A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K')
RANKS = {'A':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, '10':10, 'J':10, 'Q':10, 'K':10}

'''Card consists of one element in RANKS.
This class is capable of returning the
value of the rank and also returning a
printable string representation of a card.'''
class Card:

'''rank refers to an element in RANKS.
If the value exists in RANKS, initialize it.
Else, print "Invalid card: 'rank'"'''
def __init__(self, rank):
if(rank in RANKS):
self.rank = rank
else:
self.rank = None
print("Invalid card: ", rank)

#Returns the value of this card's rank
def get_rank(self):
return self.rank

#Returns printable string representation of the card
def __str__(self):
return str(self.rank)

'''Hand consists of a list of cards.
This class is capable of adding
a card, returning the total value of the
hand, printing face up cards, showing the
player their full hand if they consent,
checking if the player has a bust, and
returning a printable representation
of a hand.'''
class Hand:
#hand refers to a list of cards
def __init__(self):
self.hand = []

#Adds a card to the list
def add_card(self, card):
self.hand.appen

Solution

I'll try to address some of those minor style issues which he/she might have meant:

-
As I've pointed out in the comment, please pep8 (lint) your code. It would help you iron out a lot of style issues in the code. Your docstrings for the functions should also be inside your function definition. The follow the following format:

What it does
:param_name: explain about the parameter
:return: Explain what the method returns


-
Your naming convention is a bit off. In the line self.rank = rank, use an underscore for the instance variables. [A nice reference to why]

-
By You hard-coded global variables for player1 and player2 state, I guess maybe they want to hint that they want the players as classes, and then instantiate them with required params and payer-specific methods. (I am not completely sure about this. Maybe, let a better dev confirm this)

Code Snippets

What it does
:param_name: explain about the parameter
:return: Explain what the method returns

Context

StackExchange Code Review Q#142192, answer score: 6

Revisions (0)

No revisions yet.