Re: [Tutor] blackjack game

2010-04-29 Thread Steven D'Aprano
On Fri, 30 Apr 2010 06:23:44 am Shurui Liu (Aaron Liu) wrote:

> Here is the code of this game. I want to change some part of them.
> 1. Since I don't know what part of code is "responsible" for the
> number of cards, so I don't know how to add a "card number check"
> attribute, I mean, to check the number of card is more enough for
> next time play no matter how many players there are, if cards are not
> more enough, print out a notice and stop the program;

In real life, cards are dealt from a deck, and if the deck does not have 
enough cards, something will happen -- usually the dealer will start a 
new, shuffled, deck. Does your code have a deck?

> 2. I am not sure if I can let the winner get all of the cards and
> print out what cards the winner has when the game finished.

That's a question about blackjack, not Python. Since I don't play 
blackjack, I don't know.



-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] blackjack game

2010-04-29 Thread Alan Gauld


"Shurui Liu (Aaron Liu)"  wrote

Your sig says you are studying Comp Sci and engineering.

You have posted the code for what is really a very, very, small program.
In the real world you will be expected to read and understand much
bigger programs than this - think about 1000+ files and half a million
lines of code (That was my first post uni' project - in C)

You apparently want us to read this code and interpret it for you?

Please try reading it and figuring out how it works first, then if you
have sections you want help with, come back and ask specific
questions. Don't expect us to do something you are not willing
to at least make a start on...

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/



# Blackjack
# From 1 to 7 players compete against a dealer

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 playin

Re: [Tutor] blackjack game

2010-04-29 Thread Lie Ryan
On 04/30/10 06:23, Shurui Liu (Aaron Liu) wrote:
> # Blackjack
> # From 1 to 7 players compete against a dealer

> 
> 
> Here is the code of this game. I want to change some part of them.
> 1. Since I don't know what part of code is "responsible" for the
> number of cards, so I don't know how to add a "card number check"
> attribute, I mean, to check the number of card is more enough for next
> time play no matter how many players there are, if cards are not more
> enough, print out a notice and stop the program;

The BJ_Game is responsible in querying the Deck if it has enough cards
for the round. BJ_Game will calculate the number of players and asks if
ask BJ_Deck if it can support that much player. Be careful when
calculating number of cards since in blackjack using one deck it is
possible to split one's hand into four (btw, why is BJ_Player inheriting
BJ_Deck? that would make splitting impossible; a player has a hand but
it is not a hand :-)

> 2. I am not sure if I can let the winner get all of the cards and
> print out what cards the winner has when the game finished.

that depends on the house rule, I think, some games requires all player
can see everyone else's card while other games keeps everyone's hand closed.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] blackjack game

2010-04-29 Thread Shurui Liu (Aaron Liu)
# Blackjack
# From 1 to 7 players compete against a dealer

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):