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

Multiplayer blackjack game

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

Problem

I don't really want to convert to using classes, but other feedback is appreciated. Game loop is at the bottom.

colors.py

r = '\033[0;31m'#red
o = '\033[1;31m'#orange
y = '\033[0;33m'#yellow
g = '\033[0;32m'#green
b = '\033[0;34m'#blue
v = '\033[1;35m'#violet
B03 = '\033[1;30m'
B02 = '\033[0;30m'
B01 = '\033[1;32m'
B00 = '\033[1;33m'
B0 = '\033[1;34m'
B1 = '\033[1;36m'
B2 = '\033[0;37m'
B3 = '\033[1;37m'
m = '\033[0;35m'#magenta
cy = '\033[0;36m'#cyan
x = '\033[0m'#reset
cl = '\033[H\033[2J'#clear


Blackjack functions

```
#!/usr/bin/env python3
import colors as c
import random
import time

def createcards():
suits = ['diamonds','clubs','hearts','spades']
values = [['ace',11],['jack',10],['queen',10],['king',10],['2',2],['3',3],['4',4],['5',5],['6',6],['7',7],['8',8],['9',9],['10',10]]
standardDeck=[]
for cardList in values:
for suit in suits:
word = cardList[0] + " of " + suit
value = cardList[1]
minilist = [word, value]
standardDeck.append(minilist)
return standardDeck
def printrules():
print('''
objective:Get as close to 21 points without going over.
How to win:Be the closest to 21 points. If you go over, you lose.
Point values:All cards are worth the value on the card, and face cards are worth 10.
Aces:Aces can be used as 1 or 11 points.
''')
input('press enter to continue\n')
def knowsHowToPlay():
answered = False
while answered == False:
answer = input(c.cl + "Does everyone know how to play blackjack? (Y/n) > ").lower().strip()
if 'y' in answer:
answered = True
elif 'n' in answer:
answered = True
printrules()
else:
print('Invalid. Please say yes or no.')
def isAce(card):
if 'ace' in card:
return True
return False
def hasAce(aces):
hasAce = False
for ace in aces:
if ace == 11:
aces.remove(11)
aces.append(1)
hasAce

Solution

I like the thought of not wanting to convert everything to classes. Reading your code, I saw several things I should have done differently, so here are some remarks...

colors.py

-
You could use more descriptive variable names in colors.py, then you could also skip the comments, and they also could be ordered a bit more logical.

red     = '\033[0;31m'
orange  = '\033[1;31m'
yellow  = '\033[0;33m'
green   = '\033[0;32m'
blue    = '\033[0;34m'
violet  = '\033[1;35m'
magenta = '\033[0;35m'
cyan    = '\033[0;36m'

B03     = '\033[1;30m'
B02     = '\033[0;30m'
B01     = '\033[1;32m'
B00     = '\033[1;33m'
B0      = '\033[1;34m'
B1      = '\033[1;36m'
B2      = '\033[0;37m'
B3      = '\033[1;37m'

reset   = '\033[0m'
clear   = '\033[H\033[2J'


-
In the above I also added some newlines and spaces to improve readability, although the last is debatable.

Blackjack functions

  • For readability use new lines between functions (must be somewhere in PEP8)



  • Especially findBest was difficult to read, because the return statement had a blank line before it and was glued to the next function.



-
I don't care much about camalCase, PascalCase or under_score_case, but you should be at least consequent, you start with someFunction and later you use otherfunction.

-
In printWinners the order of the if elif else can be made a bit more logical with a natural order:

if len(winners) == 0:
    ...
elif len(winners) == 1:
    ...
else:
    ...


-
In printData you do:

hand = ""
for card in hands[i]:
    hand += (card + ",")
hand = hand[:-1]


This can be shortened using str.join to:

hand = ",".join(hands[i])


Game loop

  • The main loop is pretty nested, perhaps it is possible to split off some parts in functions.

Code Snippets

red     = '\033[0;31m'
orange  = '\033[1;31m'
yellow  = '\033[0;33m'
green   = '\033[0;32m'
blue    = '\033[0;34m'
violet  = '\033[1;35m'
magenta = '\033[0;35m'
cyan    = '\033[0;36m'

B03     = '\033[1;30m'
B02     = '\033[0;30m'
B01     = '\033[1;32m'
B00     = '\033[1;33m'
B0      = '\033[1;34m'
B1      = '\033[1;36m'
B2      = '\033[0;37m'
B3      = '\033[1;37m'

reset   = '\033[0m'
clear   = '\033[H\033[2J'
if len(winners) == 0:
    ...
elif len(winners) == 1:
    ...
else:
    ...
hand = ""
for card in hands[i]:
    hand += (card + ",")
hand = hand[:-1]
hand = ",".join(hands[i])

Context

StackExchange Code Review Q#142957, answer score: 4

Revisions (0)

No revisions yet.