i have a balckjack code, which does not seem to run, in python, it comes up with syntax error, i have try sortng it out. does not seem to work. below is my code, if anyone can work out wht wrong with it. that will be great. thereis an attched file, to see the code more cleaer. - from random import choice as randomcards
-
-
def total(hand):
-
# how many aces in the hand
-
aces = hand.count(11)
-
# to complicate things a little the ace can be 11 or 1
-
# this little while loop figures it out for you
-
t = sum(hand)
-
# you have gone over 21 but there is an ace
-
if t > 21 and aces > 0:
-
while aces > 0 and t > 21:
-
# this will switch the ace from 11 to 1
-
t -= 10
-
aces -= 1
-
return t
-
# a suit of cards in blackjack assume the following values
-
cards = [2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11]
-
-
# there are 4 suits per deck and usually several decks
-
# this way you can assume the cards list to be an unlimited pool
-
-
cwin = 0 # computer win counter
-
pwin = 0 # player win counter
-
while True:
-
player = []
-
# draw 2 cards for the player to start
-
player.append(rc(cards))
-
player.append(rc(cards))
-
pbust = False # player busted flag
-
cbust = False # computer busted flag
-
while True:
-
# loop for the player's play ...
-
tp = total(player)
-
print "The player has these cards %s with a total value of %d" % (player, tp)
-
-
if tp > 21:
-
print "--> The player is busted!"
-
pbust = True
-
break
-
-
else tp == 21:
-
print "\a BLACKJACK!!!"
-
break
-
else:
-
hs = raw_input("Hit or Stand/Done (h or s): ").lower()
-
if 'h' in hs:
-
player.append(rc(cards))
-
else:
-
break
-
while True:
-
# loop for the computer's play ...
-
comp = []
-
comp.append(rc(cards))
-
comp.append(rc(cards))
-
# dealer generally stands around 17 or 18
-
while True:
-
tc = total(comp)
-
if tc < 18:
-
comp.append(rc(cards))
-
else:
-
break
-
print "the computer has %s for a total of %d" % (comp, tc)
-
# now figure out who won ...
-
if tc > 21:
-
print "--> The computer is busted!"
-
cbust = True
-
if pbust == False:
-
print "The player wins!"
-
pwin += 1
-
elif tc > tp:
-
print "The computer wins!"
-
cwin += 1
-
elif tc == tp:
-
print "It's a draw!"
-
elif tp > tc:
-
if pbust == False:
-
print "The player wins!"
-
pwin += 1
-
elif cbust == False:
-
print "The computer wins!"
-
cwin += 1
-
break
-
print
-
print "Wins, player = %d computer = %d" % (pwin, cwin)
-
exit = raw_input("Press Enter (q to quit): ").lower()
-
if 'q' in exit:
-
break
-
print
-
-
print "Thanks for playing blackjack with the computer!"
30 9039 bvdet 2,851
Expert Mod 2GB
Your indentation is all messed up. You have a break outside of a loop. This statement is invalid Python:
Function rc() is undefined.
ok understand, how would i sort out, the "else tp ==21" cause i dont seem to understand wht the problem is. and how i solve it.
Use elif:
else cannot be used with a condition, but elif can.
Hope this helps.
yeh, but if you see my code, i have used "elif"
ok thnaks boxfish, i have changed to elif, but it still has syntax error.
bvdet 2,851
Expert Mod 2GB
imran akhtar,
If you experience an error with your code, please post the error message and include all tracebacks. That will give us (and you) an idea of what the problem is. The statement "it still has syntax error" does not tell me anything that I don't already know. A syntax error could be something as simple as a missing parenthesis.
-BV
there is still an error at this code "elif tp ==21:" and there is pop up which just displays an error with syntax.
here is another black jack code,, it has erro message, which comes up, whic says, - from random import cards, games mportError: cannot import name cards
below is the code: - # Blackjack
-
# From 1 to 7 players compete against a dealer
-
-
from random import cards, games
-
-
class BJ_Card(cards.Card):
-
""" A Blackjack Card. """
-
ACE_VALUE = 1
-
-
def get_value(self):
-
if self.is_face_up:
-
value = BJ_Card.RANKS.index(self.rank) + 1
-
if value > 10:
-
value = 10
-
else:
-
value = None
-
return value
-
-
value = property(get_value)
-
-
-
class BJ_Deck(cards.Deck):
-
""" A Blackjack Deck. """
-
def populate(self):
-
for suit in BJ_Card.SUITS:
-
for rank in BJ_Card.RANKS:
-
self.cards.append(BJ_Card(rank, suit))
-
-
-
class BJ_Hand(cards.Hand):
-
""" A Blackjack Hand. """
-
def __init__(self, name):
-
super(BJ_Hand, self).__init__()
-
self.name = name
-
-
def __str__(self):
-
rep = self.name + ":\t" + super(BJ_Hand, self).__str__()
-
if self.total:
-
rep += "(" + str(self.total) + ")"
-
return rep
-
-
def get_total(self):
-
# if a card in the hand has value of None, then total is None
-
for card in self.cards:
-
if not card.value:
-
return None
-
-
# add up card values, treat each Ace as 1
-
total = 0
-
for card in self.cards:
-
total += card.value
-
-
# determine if hand contains an Ace
-
contains_ace = False
-
for card in self.cards:
-
if card.value == BJ_Card.ACE_VALUE:
-
contains_ace = True
-
-
# if hand contains Ace and total is low enough, treat Ace as 11
-
if contains_ace and total <= 11:
-
# add only 10 since we've already added 1 for the Ace
-
total += 10
-
-
return total
-
-
total = property(get_total)
-
-
def is_busted(self):
-
return self.total > 21
-
-
-
class BJ_Player(BJ_Hand):
-
""" A Blackjack Player. """
-
def is_hitting(self):
-
response = games.ask_yes_no("\n" + self.name + ", do you want a hit? (Y/N): ")
-
return response == "y"
-
-
def bust(self):
-
print self.name, "busts."
-
self.lose()
-
-
def lose(self):
-
print self.name, "loses."
-
-
def win(self):
-
print self.name, "wins."
-
-
def push(self):
-
print self.name, "pushes."
-
-
-
class BJ_Dealer(BJ_Hand):
-
""" A Blackjack Dealer. """
-
def is_hitting(self):
-
return self.total < 17
-
-
def bust(self):
-
print self.name, "busts."
-
-
def flip_first_card(self):
-
first_card = self.cards[0]
-
first_card.flip()
-
-
-
class BJ_Game(object):
-
""" A Blackjack Game. """
-
def __init__(self, names):
-
self.players = []
-
for name in names:
-
player = BJ_Player(name)
-
self.players.append(player)
-
-
self.dealer = BJ_Dealer("Dealer")
-
-
self.deck = BJ_Deck()
-
self.deck.populate()
-
self.deck.shuffle()
-
-
def get_still_playing(self):
-
remaining = []
-
for player in self.players:
-
if not player.is_busted():
-
remaining.append(player)
-
return remaining
-
-
# list of players still playing (not busted) this round
-
still_playing = property(get_still_playing)
-
-
def __additional_cards(self, player):
-
while not player.is_busted() and player.is_hitting():
-
self.deck.deal([player])
-
print player
-
if player.is_busted():
-
player.bust()
-
-
def play(self):
-
# deal initial 2 cards to everyone
-
self.deck.deal(self.players + [self.dealer], per_hand = 2)
-
self.dealer.flip_first_card() # hide dealer's first card
-
for player in self.players:
-
print player
-
print self.dealer
-
-
# deal additional cards to players
-
for player in self.players:
-
self.__additional_cards(player)
-
-
self.dealer.flip_first_card() # reveal dealer's first
-
-
if not self.still_playing:
-
# since all players have busted, just show the dealer's hand
-
print self.dealer
-
else:
-
# deal additional cards to dealer
-
print self.dealer
-
self.__additional_cards(self.dealer)
-
-
if self.dealer.is_busted():
-
# everyone still playing wins
-
for player in self.still_playing:
-
player.win()
-
else:
-
# compare each player still playing to dealer
-
for player in self.still_playing:
-
if player.total > self.dealer.total:
-
player.win()
-
elif player.total < self.dealer.total:
-
player.lose()
-
else:
-
player.push()
-
-
# remove everyone's cards
-
for player in self.players:
-
player.clear()
-
self.dealer.clear()
-
-
-
def main():
-
print "\t\tWelcome to Blackjack!\n"
-
-
names = []
-
number = games.ask_number("How many players? (1 - 7): ", low = 1, high = 8)
-
for i in range(number):
-
name = raw_input("Enter player name: ")
-
names.append(name)
-
print
-
-
game = BJ_Game(names)
-
-
again = None
-
while again != "n":
-
game.play()
-
again = games.ask_yes_no("\nDo you want to play again?: ")
-
-
-
main()
-
raw_input("\n\nPress the enter key to exit.")
i have sorted the problem, with the "elif tp ==21:" but know program has another error message,
when you now run the program, the error message stops here,were i have highlighted in bold, thats were the error message stops (the error message which is displayed syntax error)
while True:
# loop for the computer's play ...
comp = [] comp.append(rc(cards))
comp.append(rc(cards)) # dealer generally stands around 17 or 18
while True
bvdet 2,851
Expert Mod 2GB
The random module does not define names cards or games. Apparently, the author of this code added these names to his random module or created his own random module.
bvdet 2,851
Expert Mod 2GB
This is likely an indentation error. Did you ever define function rc()?
yeh i thought this defined the rc() "from random import choice as rc" and i dont thinnk its indetation error, as normally python, will says if it was indentation error.
bascially i have black jack, which works, fine, but how do get the Aces count as 1 or 11. belwo is the code, which is allready made, how and were would i add in the new aces code
below is the code.. - import random as r
-
deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11]*4
-
computer = []
-
player = []
-
c = 'y'
-
-
#Clear works only if you run it from command line
-
def clear():
-
import os
-
if os.name == 'nt':
-
os.system('CLS') #Pass CLS to cmd
-
if os.name == 'posix':
-
os.system('clear') #Pass clear to terminal
-
-
def showHand():
-
hand = 0
-
for i in player: hand += i #Tally up the total
-
print "The computer is showing a %d" % computer[0]
-
print "Your hand totals: %d (%s)" % (hand, player)
-
-
#Gives player and computer their cards
-
def setup():
-
for i in range(2):
-
dealcomputer = deck[r.randint(1, len(deck)-1)]
-
dealPlayer = deck[r.randint(1, len(deck)-1)]
-
computer.append(dealcomputer)
-
player.append(dealPlayer)
-
deck.pop(dealcomputer)
-
deck.pop(dealPlayer)
-
setup()
-
while c != 'q':
-
showHand()
-
c = raw_input("[D]ealt [S]tick [Q]uit: ").lower()
-
clear()
-
if c == 'd':
-
dealPlayer = deck[r.randint(1, len(deck)-1)]
-
player.append(dealPlayer)
-
deck.pop(dealPlayer)
-
hand = 0
-
for i in computer: hand += i
-
if not hand > 17: #computer strategy.
-
dealplayer = deck[r.randint(1, len(deck)-1)]
-
computer.append(dealplayer)
-
deck.pop(dealplayer)
-
hand = 0
-
for i in player: hand += i
-
if hand > 21:
-
print "BUST!"
-
player = [] #Clear player hand
-
computer = [] #Clear computer's hand
-
setup() #Run the setup again
-
hand = 0
-
for i in computer: hand +=i
-
if hand > 21:
-
print "computer Busts!"
-
player = []
-
computer = []
-
setup()
-
elif c == 's':
-
cHand = 0 #computer's hand total
-
pHand = 0 #Player's hand total
-
for i in computer: cHand += i
-
for i in player: pHand += i
-
if pHand > cHand:
-
print "FTW!" #If playerHand (pHand) is greater than computeHand (cHand) you win...
-
computer = []
-
player = []
-
setup()
-
else:
-
print "FTL!" #...otherwise you loose.
-
computer= []
-
player = []
-
setup()
-
else:
-
if c == 'q':
-
-
-
exit = raw_input("Press Enter (q to quit): ").lower()
-
if 'q' in exit:
-
break
-
print
-
-
#print "Thanks for playing blackjack with the computer!"
-
-
## gb = raw_input("Toodles. [Hit Enter]")
-
## else:
-
## print "Invalid choice."
-
-
-
#print
-
#print "Wins, player = %d computer = %d" % (pwin, cwin)
-
## exit = raw_input("Press Enter (q to quit): ").lower()
-
## if 'q' in exit:
-
## break
-
## print
-
##
-
## print "Thanks for playing blackjack with the computer!"
bvdet 2,851
Expert Mod 2GB
imran akhtar,
I have merged the three threads you started on blackjack. It is against posting guidelines to create multiple threads on the same question. Apparently, you have obtained code from various places (none of which you wrote), and they all have problems which you are asking us to help with (with little or no effort on your part). In the future, please refrain from this type of posting.
-BV
Moderator
ok i understand, and i would like to say sorry.
if you can just help me with this code, i be very much obliged, with this black jack code, it which works, fine, but how do i get the Aces count as 1 or 11. belwo is the code, which is allready made, how and were would i add in the new aces code. - import random as r
-
deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11]*4
-
computer = []
-
player = []
-
c = 'y'
-
-
#Clear works only if you run it from command line
-
def clear():
-
import os
-
if os.name == 'nt':
-
os.system('CLS') #Pass CLS to cmd
-
if os.name == 'posix':
-
os.system('clear') #Pass clear to terminal
-
-
def showHand():
-
hand = 0
-
for i in player: hand += i #Tally up the total
-
print "The computer is showing a %d" % computer[0]
-
print "Your hand totals: %d (%s)" % (hand, player)
-
-
#Gives player and computer their cards
-
def setup():
-
for i in range(2):
-
dealcomputer = deck[r.randint(1, len(deck)-1)]
-
dealPlayer = deck[r.randint(1, len(deck)-1)]
-
computer.append(dealcomputer)
-
player.append(dealPlayer)
-
deck.pop(dealcomputer)
-
deck.pop(dealPlayer)
-
setup()
-
while c != 'q':
-
showHand()
-
c = raw_input("[D]ealt [S]tick [Q]uit: ").lower()
-
clear()
-
if c == 'd':
-
dealPlayer = deck[r.randint(1, len(deck)-1)]
-
player.append(dealPlayer)
-
deck.pop(dealPlayer)
-
hand = 0
-
for i in computer: hand += i
-
if not hand > 17: #computer strategy.
-
dealplayer = deck[r.randint(1, len(deck)-1)]
-
computer.append(dealplayer)
-
deck.pop(dealplayer)
-
hand = 0
-
for i in player: hand += i
-
if hand > 21:
-
print "BUST!"
-
player = [] #Clear player hand
-
computer = [] #Clear computer's hand
-
setup() #Run the setup again
-
hand = 0
-
for i in computer: hand +=i
-
if hand > 21:
-
print "computer Busts!"
-
player = []
-
computer = []
-
setup()
-
elif c == 's':
-
cHand = 0 #computer's hand total
-
pHand = 0 #Player's hand total
-
for i in computer: cHand += i
-
for i in player: pHand += i
-
if pHand > cHand:
-
print "you Win!" #If playerHand (pHand) is greater than computeHand (cHand) you win...
-
computer = []
-
player = []
-
setup()
-
else:
-
print "computer win!" #...otherwise you loose.
-
computer= []
-
player = []
-
setup()
-
else:
-
if c == 'q':
-
-
-
gb= raw_input("Press Enter (q to quit): ").lower()
-
if 'q' in exit:
-
break
-
-
-
# print "Wins, player = %d computer = %d" % (player, computer)
-
-
print "Thanks for playing blackjack with the computer!"
I don't know the rules to blackjack, but from what I read on Wikipedia, it looks like the player can choose whether to value an ace as 1 or 11 in order to increase his chance of winning; am I right? So if that's the case, do you want to prompt the user asking whether she wants to value an ace as 1 or 11, or have the computer find the best value for the card? For storing aces in your array of cards, I think you can pick any value you want, for example 1, -1, or "a", and have the program interpert that as an ace.
bvdet 2,851
Expert Mod 2GB
Where's the "Hit Me" option? I think it would be better to actually deal cards that can be valued, and let the computer decide if an ace should be 1 or 11. I have code for Cards and Deck classes that could be used in a blackjack game. I have added to and modified them. I don't know where the original code came from. - import random
-
-
class Card(object):
-
-
suitList = ["Hearts", "Diamonds", "Clubs", "Spades"]
-
rankList = ["Invalid", "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"]
-
scoreList = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
-
-
def __init__ (self, suit=0, rank=1):
-
if suit >= 0 and suit <= 3:
-
self.suit = suit
-
else:
-
self.suit = 0
-
if rank >= 1 and rank <= 13:
-
self.rank = rank
-
else:
-
self.rank = 1
-
self.score = self.scoreList[self.rank]
-
self.cname = self.rankList[self.rank]
-
self.sname = self.suitList[self.suit]
-
-
def __str__(self):
-
return self.rankList[self.rank] + " of " + self.suitList[self.suit]
-
-
def __repr__(self):
-
return self.rankList[self.rank] + " of " + self.suitList[self.suit]
-
-
def __cmp__(self, other):
-
i = cmp(self.rank, other.rank)
-
if i == 0:
-
return cmp(self.suit, other.suit)
-
return i
-
-
class Deck(object):
-
def __init__(self):
-
self.cards = []
-
for suit in range(4):
-
for rank in range(1,14):
-
self.cards.append(Card(suit, rank))
-
-
def __getitem__(self, i):
-
return self.cards[i]
-
-
def __iter__(self):
-
for card in self.cards:
-
yield card
-
-
def __len__(self):
-
return len(self.cards)
-
-
def __str__(self):
-
return '\n'.join([str(c) for c in self])
-
-
def __repr__(self):
-
return str(self.cards)
-
-
def deal_hand(n, deck):
-
'''
-
Randomly deal and remove n cards from a Deck object.
-
'''
-
hand = []
-
if len(deck) >= n:
-
for i in range(n):
-
hand.append(deck.cards.pop(random.choice(range(len(deck)))))
-
else:
-
print "There are not enough cards in the deck to deal %d cards." % n
-
return hand
-
-
def hit_hand(hand, deck):
-
if len(deck):
-
hand.append(deck.cards.pop(random.choice(range(len(deck)))))
-
-
def score_hand_BJ(hand):
-
score = sum([card.score for card in hand])
-
if 'Ace' in [card.cname for card in hand]:
-
if score <= 11:
-
score += 10
-
return score
-
-
deck1 = Deck()
-
hand1 = deal_hand(2, deck1)
-
hand2 = deal_hand(2, deck1)
-
print hand1, hand2
-
print score_hand_BJ(hand1)
-
print score_hand_BJ(hand2)
-
-
hit_hand(hand1, deck1)
-
print hand1
-
print score_hand_BJ(hand1)
Sample output: - >>> [Jack of Diamonds, 6 of Hearts] [Ace of Hearts, Jack of Clubs]
-
16
-
21
-
[Jack of Diamonds, 6 of Hearts, King of Spades]
-
26
-
>>> hit_hand(hand2, deck1)
-
>>> hand2
-
[Ace of Hearts, Jack of Clubs, 8 of Spades]
-
>>> score_hand(hand2)
-
19
-
>>>
In the output and interaction above, hand2 had an Ace, and was originally scored as 11. I hit hand2, and the Ace was scored as 1.
thanks for ur help, but the code, above, is bit counfusing, as it runs, how do i actually play it,
bvdet 2,851
Expert Mod 2GB
The code I posted is not a complete game, just some code that may be useful in a blackjack game. When I find some time, I'll add some to it. I've got to get some work done now.
-BV
ok thanks, when you have time, please do so, i am very greatful, for your help.
bvdet 2,851
Expert Mod 2GB
Following is the complete (so far) code listing of a blackjack game, using classes. At this time, there is no check for "blackjack" or "natural" (a total of 21 in your first two cards). - import random
-
-
class Card(object):
-
-
suitList = ["Hearts", "Diamonds", "Clubs", "Spades"]
-
rankList = ["Invalid", "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"]
-
scoreList = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
-
-
def __init__ (self, suit=0, rank=1):
-
if suit >= 0 and suit <= 3:
-
self.suit = suit
-
else:
-
self.suit = 0
-
if rank >= 1 and rank <= 13:
-
self.rank = rank
-
else:
-
self.rank = 1
-
self.score = self.scoreList[self.rank]
-
self.cname = self.rankList[self.rank]
-
self.sname = self.suitList[self.suit]
-
-
def __str__(self):
-
return self.rankList[self.rank] + " of " + self.suitList[self.suit]
-
-
def __repr__(self):
-
return self.rankList[self.rank] + " of " + self.suitList[self.suit]
-
-
def __cmp__(self, other):
-
i = cmp(self.rank, other.rank)
-
if i == 0:
-
return cmp(self.suit, other.suit)
-
return i
-
-
class Deck(object):
-
def __init__(self):
-
self.cards = []
-
for suit in range(4):
-
for rank in range(1,14):
-
self.cards.append(Card(suit, rank))
-
-
def __getitem__(self, i):
-
return self.cards[i]
-
-
def __iter__(self):
-
for card in self.cards:
-
yield card
-
-
def __len__(self):
-
return len(self.cards)
-
-
def __str__(self):
-
return '\n'.join([str(c) for c in self])
-
-
def __repr__(self):
-
return str(self.cards)
-
-
class BJ(object):
-
-
dealer_hit_threshold = 15
-
-
def __init__(self):
-
'''
-
Designed for 2 players. Player 2 is the dealer. Dealer wins a tie.
-
Results are appended to self.results:
-
[[winner, playerhand, dealerhand], [winner, playerhand, dealerhand]]
-
Results are formatted for output.
-
'''
-
self.results = []
-
self.deck = Deck()
-
-
def deal_hand(self, n, deck):
-
'''
-
Randomly deal and remove n cards from a Deck object.
-
'''
-
hand = []
-
if len(deck) >= n:
-
for i in range(n):
-
hand.append(deck.cards.pop(random.choice(range(len(deck)))))
-
else:
-
print "There are not enough cards in the deck to deal %d cards." % n
-
return hand
-
-
def hit_hand(self, hand, deck):
-
if len(deck):
-
hand.append(deck.cards.pop(random.choice(range(len(deck)))))
-
return True
-
else: return False
-
-
def score_hand_BJ(self, hand):
-
score = sum([card.score for card in hand])
-
if 'Ace' in [card.cname for card in hand]:
-
if score <= 11:
-
score += 10
-
return score
-
-
def play(self):
-
playing = False
-
print
-
while True:
-
option = raw_input("(D)eal, (H)it Me, (S)tand, (Q)uit")
-
if option.upper() == "D":
-
if playing:
-
print "You are already playing a hand!"
-
else:
-
playing = True
-
hand1 = self.deal_hand(2, self.deck)
-
hand2 = self.deal_hand(2, self.deck)
-
print "Player 1 Hand: %s\nPlayer 2 Hand: %s" % (hand1, hand2)
-
print "Player 1 Score: %s\nPlayer 2 Score: %s" % (self.score_hand_BJ(hand1), self.score_hand_BJ(hand2))
-
while self.score_hand_BJ(hand2) <= BJ.dealer_hit_threshold:
-
if not self.deck:
-
print "The deck is empty. Start over."
-
self.play_over()
-
return
-
else:
-
self.hit_hand(hand2, self.deck)
-
print "Dealer took a HIT. Current HAND: %s Score: %s" % (hand2, self.score_hand_BJ(hand2))
-
if self.score_hand_BJ(hand2) > 21:
-
print "Dealer BUSTS. Player wins."
-
self.results.append(["Player", hand1, hand2])
-
playing = False
-
print
-
-
elif option.upper() == "H":
-
if playing:
-
if not self.deck:
-
print "The deck is empty. Start over."
-
self.play_over()
-
return
-
else:
-
self.hit_hand(hand1, self.deck)
-
print "Player took a HIT. Current HAND: %s Score: %s" % (hand1, self.score_hand_BJ(hand1))
-
if self.score_hand_BJ(hand1) > 21:
-
print "Player BUSTS. Dealer wins."
-
self.results.append(["Dealer", hand1, hand2])
-
playing = False
-
print
-
else:
-
print "You must DEAL before taking a HIT"
-
-
elif option.upper() == "S":
-
if playing:
-
if self.score_hand_BJ(hand1) > self.score_hand_BJ(hand2):
-
print "PLAYER wins this hand. Score: %s to %s" % (self.score_hand_BJ(hand1), self.score_hand_BJ(hand2))
-
self.results.append(["Player", hand1, hand2])
-
else:
-
print "DEALER wins this hand. Score: %s to %s" % (self.score_hand_BJ(hand1), self.score_hand_BJ(hand2))
-
self.results.append(["Dealer", hand1, hand2])
-
else:
-
print "You must DEAL before selecting option to STAND."
-
playing = False
-
print
-
-
elif option.upper() == "Q":
-
self.play_over()
-
return
-
-
def play_over(self):
-
dd = dict.fromkeys(["Player", "Dealer"], 0)
-
for hand in self.results:
-
dd[hand[0]] += 1
-
playerwins = dd["Player"]
-
dealerwins = dd["Dealer"]
-
if playerwins > dealerwins:
-
resultList = ["", "Player won %d hand%s to %d." % (playerwins, ["", "s"][playerwins>1 or 0], dealerwins)]
-
elif playerwins == dealerwins:
-
resultList = ["There was a tie %d hand%s to %d." % (playerwins, ["", "s"][playerwins>1 or 0], dealerwins)]
-
else:
-
resultList = ["Dealer won %d hand%s to %d." % (dealerwins, ["", "s"][dealerwins>1 or 0], playerwins)]
-
resultList.append("Hands Played:")
-
if self.results:
-
for hand in self.results:
-
resultList.append(" Player: %s Dealer: %s" % (", ".join([str(c) for c in hand[1]]), ", ".join([str(c) for c in hand[2]])))
-
else:
-
resultList.append("No hands were played")
-
print '\n'.join(resultList)
-
-
if __name__ == "__main__":
-
g = BJ()
-
g.play()
Sample run: - >>>
-
Player 1 Hand: [8 of Clubs, 10 of Hearts]
-
Player 2 Hand: [Ace of Diamonds, 7 of Clubs]
-
Player 1 Score: 18
-
Player 2 Score: 18
-
Player took a HIT. Current HAND: [8 of Clubs, 10 of Hearts, 10 of Diamonds] Score: 28
-
Player BUSTS. Dealer wins.
-
-
Player 1 Hand: [2 of Hearts, Queen of Hearts]
-
Player 2 Hand: [2 of Clubs, Jack of Spades]
-
Player 1 Score: 12
-
Player 2 Score: 12
-
Dealer took a HIT. Current HAND: [2 of Clubs, Jack of Spades, Ace of Clubs] Score: 13
-
Dealer took a HIT. Current HAND: [2 of Clubs, Jack of Spades, Ace of Clubs, 3 of Clubs] Score: 16
-
Player took a HIT. Current HAND: [2 of Hearts, Queen of Hearts, 7 of Hearts] Score: 19
-
PLAYER wins this hand. Score: 19 to 16
-
-
Player 1 Hand: [Ace of Hearts, 4 of Diamonds]
-
Player 2 Hand: [7 of Diamonds, 10 of Spades]
-
Player 1 Score: 15
-
Player 2 Score: 17
-
Player took a HIT. Current HAND: [Ace of Hearts, 4 of Diamonds, 9 of Clubs] Score: 14
-
Player took a HIT. Current HAND: [Ace of Hearts, 4 of Diamonds, 9 of Clubs, Queen of Spades] Score: 24
-
Player BUSTS. Dealer wins.
-
-
Player 1 Hand: [6 of Hearts, King of Diamonds]
-
Player 2 Hand: [6 of Diamonds, 4 of Clubs]
-
Player 1 Score: 16
-
Player 2 Score: 10
-
Dealer took a HIT. Current HAND: [6 of Diamonds, 4 of Clubs, 9 of Spades] Score: 19
-
Player took a HIT. Current HAND: [6 of Hearts, King of Diamonds, Jack of Hearts] Score: 26
-
Player BUSTS. Dealer wins.
-
-
Player 1 Hand: [Jack of Clubs, 5 of Hearts]
-
Player 2 Hand: [Queen of Diamonds, 5 of Clubs]
-
Player 1 Score: 15
-
Player 2 Score: 15
-
Dealer took a HIT. Current HAND: [Queen of Diamonds, 5 of Clubs, 3 of Hearts] Score: 18
-
Player took a HIT. Current HAND: [Jack of Clubs, 5 of Hearts, King of Spades] Score: 25
-
Player BUSTS. Dealer wins.
-
-
Player 1 Hand: [4 of Spades, 6 of Spades]
-
Player 2 Hand: [King of Hearts, 10 of Clubs]
-
Player 1 Score: 10
-
Player 2 Score: 20
-
Player took a HIT. Current HAND: [4 of Spades, 6 of Spades, 5 of Spades] Score: 15
-
Player took a HIT. Current HAND: [4 of Spades, 6 of Spades, 5 of Spades, 5 of Diamonds] Score: 20
-
DEALER wins this hand. Score: 20 to 20
-
-
Player 1 Hand: [6 of Clubs, 8 of Spades]
-
Player 2 Hand: [3 of Spades, 8 of Diamonds]
-
Player 1 Score: 14
-
Player 2 Score: 11
-
Dealer took a HIT. Current HAND: [3 of Spades, 8 of Diamonds, Queen of Clubs] Score: 21
-
DEALER wins this hand. Score: 14 to 21
-
-
Player 1 Hand: [8 of Hearts, King of Clubs]
-
Player 2 Hand: [Jack of Diamonds, 9 of Hearts]
-
Player 1 Score: 18
-
Player 2 Score: 19
-
Player took a HIT. Current HAND: [8 of Hearts, King of Clubs, 3 of Diamonds] Score: 21
-
PLAYER wins this hand. Score: 21 to 19
-
-
Dealer won 6 hands to 2.
-
Hands Played:
-
Player: 8 of Clubs, 10 of Hearts, 10 of Diamonds Dealer: Ace of Diamonds, 7 of Clubs
-
Player: 2 of Hearts, Queen of Hearts, 7 of Hearts Dealer: 2 of Clubs, Jack of Spades, Ace of Clubs, 3 of Clubs
-
Player: Ace of Hearts, 4 of Diamonds, 9 of Clubs, Queen of Spades Dealer: 7 of Diamonds, 10 of Spades
-
Player: 6 of Hearts, King of Diamonds, Jack of Hearts Dealer: 6 of Diamonds, 4 of Clubs, 9 of Spades
-
Player: Jack of Clubs, 5 of Hearts, King of Spades Dealer: Queen of Diamonds, 5 of Clubs, 3 of Hearts
-
Player: 4 of Spades, 6 of Spades, 5 of Spades, 5 of Diamonds Dealer: King of Hearts, 10 of Clubs
-
Player: 6 of Clubs, 8 of Spades Dealer: 3 of Spades, 8 of Diamonds, Queen of Clubs
-
Player: 8 of Hearts, King of Clubs, 3 of Diamonds Dealer: Jack of Diamonds, 9 of Hearts
-
>>>
Comments are encouraged.
-BV
this qucik question wht does it mean , by the pontoon’ rule.
would do u mean by "At this time, there is no check for "blackjack" or "natural" (a total of 21 in your first two cards)."
i played the game, it is pretty good, is this the complete code.
bvdet 2,851
Expert Mod 2GB
The code is complete, as far as what I have written. Can't you play a complete game?
If a player gets 21 with the first two cards. it is called a "natural" or "blackjack". The code does not check for this. If I add anything to the code, it will be a check for a "blackjack".
-BV
this code is a poontoon rule, or is a stick at 16 rule.
bvdet 2,851
Expert Mod 2GB
This is not pontoon blackjack. The dealer sticks when the value of his hand is greater than the value of variable dealer_hit_threshold.
-BV
hi, does anyone, know the forums, email address or contanct details , i found this one "privacy@bytes.com" but does not seem to work, as i sent emails, and does seem to be a valid email address keeps failing to be sent .
bvdet 2,851
Expert Mod 2GB @imran akhtar
To whom do you want to send an email? Asking questions via private email is discouraged.
-BV
Moderator
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Charlie Cosse |
last post by:
Asymptopia BlackJack is written in Python and uses PyGame for
multimedia.
Version 1.2 contains both Windows and Linux install scripts.
Asymptopia BlackJack is a full-featured casino-style...
|
by: slyphiad |
last post by:
Here's the problem that i got...
I'm trying to create a blackjack game. Here, I'm trying to create 2
blackjack games. A game with bet and without bet. So basically what i
did, was create 2...
|
by: CapMaster |
last post by:
I've found some programs of how to create a standard game of blackjack on C++. But how would you do it using structs? Here is my assignment:
Problem Statement: The purpose of this project is to...
|
by: blinkrebel |
last post by:
Hello
I need some help implementing the game of blackjack using the xturtle package.
the instructions can be found at
http://katie.luther.edu/moodle/file.php/2387/BlackJack.pdf
and .gif's...
|
by: EXotiX |
last post by:
hey im new to vb6 and I am making a blackjack game but can not randomize the array correctly. Could you help please
Option Explicit
Const NumItems As Integer = 53
Public Sub RandomizeCards()...
|
by: devilinthebox |
last post by:
I am not really familar with Java and I need help with creating this simple Blackjack program.
Here is a layout of how the program should output:
If the computer has more than 16 it wins,...
|
by: devilinthebox |
last post by:
I'm fairly new to java and need help with adding letters (J, Q, K, A) into the program and adding values for each. Thanks.
// February 8, 2008
// The "BlackJack" class.
import java.awt.*;...
|
by: Z E R O |
last post by:
I'm in a really fast pace training program and we are covering a lot of material in a relatively short period of time. I've really not had much trouble up until this problem. I had to do one other...
|
by: imran akhtar |
last post by:
hi, i am making black jack code, but i am stuck, i have made start, but for reason gettin errors, which i dont seem be able to fix, below is my code wht i started.
import random
deck = *4...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |