patternpythonModerate
Blackjack game in Python
Viewed 0 times
gamepythonblackjack
Problem
```
import random
from random import choice
import os
import time
import texttable
class Stack():
def __init__(self, replay, winnings):
self.start_amount = 100
self.stored_bet = 0
self.stored_end = 0
self.replay = replay
if self.replay == 0:
self.stored_end = 0 #needs to be initialized
else:
self.stored_end = self.stored_end + winnings
def load_account(self):
self.stored_end = self.start_amount
def account(self, begin, change):
end = float(begin) + float(change)
self.stored_end = end # store class variable??
return(begin, change, end)
def bet_test(self, miss_type):
# collect's bet and check input
# miss_type should start as 0
possible_bets = ['5', '10', '15', '20', '25']
while True:
print "\nWhat is your bet? (5, 10, 15, 20, 25)"
bet = raw_input(' >')
if bet in possible_bets:
bet = int(bet)
if self.replay == 1:
begin = self.stored_end
self.stored_bet = int(bet)
break
else:
if bet > self.stored_end:
print "You don't have enough for that bet."
time.sleep(2)
else:
begin = self.stored_end
self.stored_bet = bet
break
class DECK():
def __init__(self):
suite = ('Spades', 'Hearts', 'Diamonds', 'Clubs')
rank = ('2', '3', '4', '5', '6', '7', '8', '9', '10', "Jack", "Queen", "King", "Ace")
self.full_deck = {}
n = 0
i = 0
for n in range(6):
for s in suite:
for r in rank:
self.full_deck[i + n] = "%s of %s" % (r, s)
i += 1
n += 1
self.values = {'Ace':11, '2': 2, '3': 3, '4': 4, '5'
import random
from random import choice
import os
import time
import texttable
class Stack():
def __init__(self, replay, winnings):
self.start_amount = 100
self.stored_bet = 0
self.stored_end = 0
self.replay = replay
if self.replay == 0:
self.stored_end = 0 #needs to be initialized
else:
self.stored_end = self.stored_end + winnings
def load_account(self):
self.stored_end = self.start_amount
def account(self, begin, change):
end = float(begin) + float(change)
self.stored_end = end # store class variable??
return(begin, change, end)
def bet_test(self, miss_type):
# collect's bet and check input
# miss_type should start as 0
possible_bets = ['5', '10', '15', '20', '25']
while True:
print "\nWhat is your bet? (5, 10, 15, 20, 25)"
bet = raw_input(' >')
if bet in possible_bets:
bet = int(bet)
if self.replay == 1:
begin = self.stored_end
self.stored_bet = int(bet)
break
else:
if bet > self.stored_end:
print "You don't have enough for that bet."
time.sleep(2)
else:
begin = self.stored_end
self.stored_bet = bet
break
class DECK():
def __init__(self):
suite = ('Spades', 'Hearts', 'Diamonds', 'Clubs')
rank = ('2', '3', '4', '5', '6', '7', '8', '9', '10', "Jack", "Queen", "King", "Ace")
self.full_deck = {}
n = 0
i = 0
for n in range(6):
for s in suite:
for r in rank:
self.full_deck[i + n] = "%s of %s" % (r, s)
i += 1
n += 1
self.values = {'Ace':11, '2': 2, '3': 3, '4': 4, '5'
Solution
- Python convention is to name classes with CamelCase, not ALL_CAPS
- Don't use 1 and 0 for boolean values, use True and False
- Don't mix UI and logic. You should have some classes/functions that implement the rules for the game, and completely different classes/functions for the user interface.
- Avoid the use of mutable class variable like
Hand.instances
- Your classes don't seem to have a defined identity. Your
Stackclass seems like it should represent a stack of chips. But it also seems to be keeping track of past history a little bit? I'm really not quite sure what the replay logic is doing.
- The rules of the game seem to be strewn all over the code. Look at the number of places that have 21.
Let's take a detailed look at one function:
def bet_test(self, miss_type):
# collect's bet and check input
# miss_type should start as 0
possible_bets = ['5', '10', '15', '20', '25']Constant lists like this should be global constants.
while True:
print "\nWhat is your bet? (5, 10, 15, 20, 25)"
bet = raw_input(' >')
if bet in possible_bets:
bet = int(bet)I don't like reusing variables for different things. i.e. the text and number version of the bet. I'd store them in different names.
if self.replay == 1:Use
if self.replay: for boolean valuesbegin = self.stored_endYou set begin, but you don't seem to do anything with it...
self.stored_bet = int(bet)bet is already an int
break
else:
if bet > self.stored_end:Why not use an
elif here to force, print "You don't have enough for that bet."
time.sleep(2)
else:
begin = self.stored_end
self.stored_bet = bet
breakYou've done this twice. You should rearrange the code so that it happens only once.
Here's my reworking of that function:
def bet_test(self, miss_type):
# collect's bet and check input
# miss_type should start as 0
possible_bets = range(5, min(25, self.stored_end+1), 5)
while True:
print "\nWhat is your bet? (%s)" % (", ".join(possible_bets))
try:
bet = int(raw_input(' >'))
except ValueError:
pass # make them try again
else:
if bet in possible_bets:
self.stored_bet = bet
break
self.stored_bet = betCode Snippets
def bet_test(self, miss_type):
# collect's bet and check input
# miss_type should start as 0
possible_bets = ['5', '10', '15', '20', '25']while True:
print "\nWhat is your bet? (5, 10, 15, 20, 25)"
bet = raw_input(' >')
if bet in possible_bets:
bet = int(bet)if self.replay == 1:begin = self.stored_endself.stored_bet = int(bet)Context
StackExchange Code Review Q#15219, answer score: 11
Revisions (0)
No revisions yet.