ok i understand, and i would like to say sorry.
blackjack game
Collapse
X
-
-
blackjack
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.
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!"
Comment
-
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.Comment
-
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.
[code=Python]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.appe nd(Card(suit, rank))
def __getitem__(sel f, 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(dec k.cards.pop(ran dom.choice(rang e(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(dec k.cards.pop(ran dom.choice(rang e(len(deck)))))
def score_hand_BJ(h and):
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(h and1)
print score_hand_BJ(h and2)
hit_hand(hand1, deck1)
print hand1
print score_hand_BJ(h and1)[/code]
Sample output:
[code=Python]>>> [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(hand 2)
19
>>> [/code]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.Comment
-
thanks for ur help, but the code, above, is bit counfusing, as it runs, how do i actually play it,Comment
-
-
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).
[code=Python]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.appe nd(Card(suit, rank))
def __getitem__(sel f, 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_thre shold = 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(dec k.cards.pop(ran dom.choice(rang e(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(dec k.cards.pop(ran dom.choice(rang e(len(deck)))))
return True
else: return False
def score_hand_BJ(s elf, 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)e al, (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_han d_BJ(hand1), self.score_hand _BJ(hand2))
while self.score_hand _BJ(hand2) <= BJ.dealer_hit_t hreshold:
if not self.deck:
print "The deck is empty. Start over."
self.play_over( )
return
else:
self.hit_hand(h and2, 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.ap pend(["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(h and1, 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.ap pend(["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_han d_BJ(hand1), self.score_hand _BJ(hand2))
self.results.ap pend(["Player", hand1, hand2])
else:
print "DEALER wins this hand. Score: %s to %s" % (self.score_han d_BJ(hand1), self.score_hand _BJ(hand2))
self.results.ap pend(["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.appe nd("Hands Played:")
if self.results:
for hand in self.results:
resultList.appe nd(" Player: %s Dealer: %s" % (", ".join([str(c) for c in hand[1]]), ", ".join([str(c) for c in hand[2]])))
else:
resultList.appe nd("No hands were played")
print '\n'.join(resul tList)
if __name__ == "__main__":
g = BJ()
g.play()[/code]
Sample run:[code=Python]>>>
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
>>> [/code]
Comments are encouraged.
-BVComment
-
-
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)."Comment
-
-
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" .
-BVComment
-
-
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 .Comment
Comment