FAQ
Been trying to get the following problem to work. So far its been
hectic since i am very new to programming:

Make a program that simulates a card game.

You have a Deck of cards that contains the following:

-4 suits (spades, hearts, diamonds, clubs)
-Each suit has 14 ranks and points assigned to them:

2= 2 points
3= 3 points
4= 4 points
5= 5 points
6= 6 points
7= 7 points
8= 8 points
9= 9 points
10= 10 points
Jack= 11 points
Queen= 12 points
King= 13 points
Ace= 14 points

First the computer picks 3 cards and then the user gets 3 cards. The
following rules are used to calculate the total points accumulated
for the computer and the user:

-Each Card carries points equivalent to its rank except the Ace card,
which is worth 14 points.

-If all 3 cards are of the same suit, then add 30 bonus points to the
total points of the 3 cards.

- If all 3 cards are of the same rank, add 50 points bonus to the
total points of the 3 cards.

- if all 3 cards are face cards (meaning they have faces like a
king,queen,jack) and are of the same rank, then add 100 points bonus
to the total points of the 3 cards.

- if all 3 cards of the same suit are a sequence, add 30 points bonus
to the total points of the 3 cards.

At the end of the game the winner is announced between the computer
and the user.

The following is what i think should be done (got help on it from
another forum), but i don't know much syntax and therefore am stuck:

1) Create somewhere to store the deck of 52 cards (e.g. a list named
Deck)
(Hint - search for list.append() and list.pop() help)
2) Create somewhere to store user points and computer points
3) Fill "Deck" with 52 cards, one of each suite
4) Shuffle the deck
b) Calculate points
c) Calculate bonus points
d) Add to user or computer store
e) Repeat until the deck is empty
6) Print scores

import random

##Store the deck as a list
Deck = []
UserPoints = 0
CompPoints = 0

##Populate the deck with cards
##Hint - for x in range(14)
##Hint2 - for s in ['hearts', 'spades']

##Shuffle the deck
random.shuffle(deck)

##Main loop
while Deck:

##calculate points

## calculate bonus points

## Add points and bonus points to total

## etc

##Print winner

Thanks.

## Search Discussions

•  at May 10, 2003 at 1:49 pm ⇧

On Saturday 10 May 2003 05:14, gk wrote:
Been trying to get the following problem to work. So far its been
hectic since i am very new to programming:
this is a fairly large problem to start out with for a new programmer. I
would highly recommend you break the problem down into smaller, more
digestible pieces.

it also sounds really similar to homework from a programming class, so direct
help may not be as useful to you (or others). Part of learning to program is
learning to assemble the pieces of a program.
Make a program that simulates a card game.
You have a Deck of cards that contains the following:
-4 suits (spades, hearts, diamonds, clubs)
-Each suit has 14 ranks and points assigned to them:
how would you represent a card in the program? how would you decide what a
card's value was? its suit?
-If all 3 cards are of the same suit, then add 30 bonus points to the
total points of the 3 cards.

- If all 3 cards are of the same rank, add 50 points bonus to the
total points of the 3 cards.

- if all 3 cards are face cards (meaning they have faces like a
king,queen,jack) and are of the same rank, then add 100 points bonus
to the total points of the 3 cards.

- if all 3 cards of the same suit are a sequence, add 30 points bonus
to the total points of the 3 cards.
these are simply rules to be applied to 3 cards. Attack each rule on its own
and then try to combine them.
The following is what i think should be done (got help on it from
another forum), but i don't know much syntax and therefore am stuck:

1) Create somewhere to store the deck of 52 cards (e.g. a list named
Deck)
(Hint - search for list.append() and list.pop() help)
2) Create somewhere to store user points and computer points
3) Fill "Deck" with 52 cards, one of each suite
4) Shuffle the deck
b) Calculate points
c) Calculate bonus points
d) Add to user or computer store
e) Repeat until the deck is empty
6) Print scores
this sounds reasonable, however, remember to give each player cards (-:

Read the tutorials on the python site, perhaps grab a copy of a python book or
read one of the online texts. Then start slowly. Trying to tackle the whole
program will just lead to frustration.

I'd start with deciding how i wanted to represent a card and then work on the
point calculation. Once a score can be computed for a hand making a game out
of it just requires wrapping the pieces together.

Since you are new to programming and Python, I would like to recommend to join
and read the Python tutor mailing list. It is aimed directly at people in
your position and has a much lower volume than this list.
•  at May 10, 2003 at 2:04 pm ⇧

gk wrote:
Been trying to get the following problem to work. So far its been
hectic since i am very new to programming:
[...] but i don't know much syntax and therefore am stuck:
It won't help you learn if we give you the complete solution. Do you
have any concrete question?

-- Gerhard
•  at May 10, 2003 at 3:31 pm ⇧
Hi.
This may be overkill, and it will likely be a little less straight-forward
but here's a suggested framework:

Make a card class. Each card knows its rank and suit.

Make an enumeration of suits, i.e., and face values
SUITS = (HEARTS, SPADES, DIAMONDS, CLUBS) = range(4)
FACES = (JACKS, QUEENS, KINGS) = range(11,14)
this'll let you do things like check for face cards, and/or for flushes.

Make a player class. Each player has a 'hand' and a 'score' and perhaps a
score_hand() method. One benefit of this approach is that you can subclass
this class for each type of card game, and you'll usually only have to
override the score_hand() method. For example,
you can make a

class PokerPlayer(Player):
def score_hand(self):
# score hand using poker rules

You should also make the card instances comparable, so that you can do
things like
# say we're inside a Player instance method
self.hand.sort() # hand is a list of cards
This'll make it easier to spot runs.

Make a deck class as a subclass of list. i.e., class Deck(list): ... .
Populate the deck with the appropriate 52 card instances. The deck should
have shuffle()
and deal() methods. deal() should return a list of three card instances.
Then you can do things like:
# shuffle cards
deck.shuffle()
# deal cards to players 'user' and 'computer'
user.hand = deck.deal(3)
computer.hand = deck.deal(3)

One problem with your game as it stands is that if you only have two players
and each player only gets 3 cards for each hand, you will run short of cards
before exhausting the deck. In other words, 6 does not divide evenly into
52, so you can't 'e) Repeat until the deck is empty'
Just so you know...

Anyway, this is the outline of one approach. There are several others.
Good luck with what you're doing.
Sean

"gk" <topsoil at mail.com> wrote in message
news:Xns937753D7E64B7topsoilmailcom at 199.184.165.241...
Been trying to get the following problem to work. So far its been
hectic since i am very new to programming:

Make a program that simulates a card game.

You have a Deck of cards that contains the following:

-4 suits (spades, hearts, diamonds, clubs)
-Each suit has 14 ranks and points assigned to them:

2= 2 points
3= 3 points
4= 4 points
5= 5 points
6= 6 points
7= 7 points
8= 8 points
9= 9 points
10= 10 points
Jack= 11 points
Queen= 12 points
King= 13 points
Ace= 14 points

First the computer picks 3 cards and then the user gets 3 cards. The
following rules are used to calculate the total points accumulated
for the computer and the user:

-Each Card carries points equivalent to its rank except the Ace card,
which is worth 14 points.

-If all 3 cards are of the same suit, then add 30 bonus points to the
total points of the 3 cards.

- If all 3 cards are of the same rank, add 50 points bonus to the
total points of the 3 cards.

- if all 3 cards are face cards (meaning they have faces like a
king,queen,jack) and are of the same rank, then add 100 points bonus
to the total points of the 3 cards.

- if all 3 cards of the same suit are a sequence, add 30 points bonus
to the total points of the 3 cards.

At the end of the game the winner is announced between the computer
and the user.

The following is what i think should be done (got help on it from
another forum), but i don't know much syntax and therefore am stuck:

1) Create somewhere to store the deck of 52 cards (e.g. a list named
Deck)
(Hint - search for list.append() and list.pop() help)
2) Create somewhere to store user points and computer points
3) Fill "Deck" with 52 cards, one of each suite
4) Shuffle the deck
b) Calculate points
c) Calculate bonus points
d) Add to user or computer store
e) Repeat until the deck is empty
6) Print scores

import random

##Store the deck as a list
Deck = []
UserPoints = 0
CompPoints = 0

##Populate the deck with cards
##Hint - for x in range(14)
##Hint2 - for s in ['hearts', 'spades']

##Shuffle the deck
random.shuffle(deck)

##Main loop
while Deck:

##calculate points

## calculate bonus points

## Add points and bonus points to total

## etc

##Print winner

Thanks.
•  at May 10, 2003 at 3:44 pm ⇧
From: "gk" <topsoil at mail.com>
Make a program that simulates a card game.
http://www.ibiblio.org/obp/thinkCSpy/chap15.htm
•  at May 10, 2003 at 4:35 pm ⇧
Paul Rubin <http://phr.cx at NOSPAM.invalid> wrote in
news:7xy91frmzo.fsf at ruckus.brouhaha.com:
gk <topsoil at mail.com> writes:
Been trying to get the following problem to work. So far its been
hectic since i am very new to programming:
This sounds like a homework problem. What kind of help are you
looking for?

Any kind of help. Whatever you can tell me. Thanks.
•  at May 10, 2003 at 6:41 pm ⇧
Gerhard H?ring <gh at ghaering.de> wrote in
news:mailman.1052588416.20608.python-list at python.org:
gk wrote:
Been trying to get the following problem to work. So far its been
hectic since i am very new to programming:
[...] but i don't know much syntax and therefore am stuck:
It won't help you learn if we give you the complete solution. Do you
have any concrete question?

-- Gerhard
You're right. But the thing is that i was thrown into python all of a
sudden without any programming background. Now, i understand the
problem and the logic. Its just that the syntax is killing me.

hmm. that link to iblio doesn't work.
•  at May 10, 2003 at 7:59 pm ⇧
Quoth gk:
[...]
You're right. But the thing is that i was thrown into python all of a
sudden without any programming background. Now, i understand the
problem and the logic. Its just that the syntax is killing me.
If it really is just the syntax, then the Python Tutorial should
get you on your way quite quickly:

<http://www.python.org/doc/current/tut/tut.html>

But it's hard to believe that, with no programming background, the
only thing you're having trouble with is the syntax. Here's a
(slightly edited) example from your OP:

##Store the deck as a list
Deck = []

##Populate the deck with cards

This last is a fine high-level outline of what you want to do next
-- "populate the deck with cards". But there's lots of nuts and
bolts missing. Perhaps most saliently: what's a card? A tuple of
its suit and rank? An integer in [0..51], its suit and rank
inferred? An instance of some class, with .isfacecard() and like
methods? This is not a question of syntax; it is a question of
implementation.

You do seem to "understand the logic" at a high level; but writing
a program requires that you understand it all the way down to the
nuts and bolts level too.

If you don't know how to answer the question "what's a card?" in
the context of your program, then I'd again suggest the Python
Tutorial. Also, as others have suggested, the tutor mailing list
might be of more assistance to you than we can be.

--
Steven Taschuk "The world will end if you get this wrong."
staschuk at telusplanet.net -- "Typesetting Mathematics -- User's Guide",
Brian Kernighan and Lorrinda Cherry
•  at May 10, 2003 at 8:33 pm ⇧

gk wrote:
You're right. But the thing is that i was thrown into python all of a
sudden without any programming background. Now, i understand the
problem and the logic. Its just that the syntax is killing me.
so, start with the tutorial. it is shipped with Python or browse it here:
http://www.python.org/doc/current/tut/tut.html
you can find a lot of helpful documentation at
http://www.python.org/
Python is really easy to learn, but the problem you have to solve is far
from trivial, if you begin with programming.
good luck!
hmm. that link to iblio doesn't work.
ah! once again it's rolling away.

nirinA
•  at May 10, 2003 at 9:57 pm ⇧
•  at May 10, 2003 at 7:30 pm ⇧

to join and read the Python tutor mailing list. It is aimed directly
at people in your position and has a much lower volume than this list.
Thanks. I will look into it.
•  at May 11, 2003 at 12:28 am ⇧
http://www.ibiblio.org/obp/thinkCS/thinkCSpy/chap15.htm

What other newsgroups have you askesked help to?

Kuta

"gk" <topsoil at mail.com> ha scritto nel messaggio
news:Xns937753D7E64B7topsoilmailcom at 199.184.165.241...
Been trying to get the following problem to work. So far its been
hectic since i am very new to programming:

Make a program that simulates a card game.

You have a Deck of cards that contains the following:

-4 suits (spades, hearts, diamonds, clubs)
-Each suit has 14 ranks and points assigned to them:

2= 2 points
3= 3 points
4= 4 points
5= 5 points
6= 6 points
7= 7 points
8= 8 points
9= 9 points
10= 10 points
Jack= 11 points
Queen= 12 points
King= 13 points
Ace= 14 points

First the computer picks 3 cards and then the user gets 3 cards. The
following rules are used to calculate the total points accumulated
for the computer and the user:

-Each Card carries points equivalent to its rank except the Ace card,
which is worth 14 points.

-If all 3 cards are of the same suit, then add 30 bonus points to the
total points of the 3 cards.

- If all 3 cards are of the same rank, add 50 points bonus to the
total points of the 3 cards.

- if all 3 cards are face cards (meaning they have faces like a
king,queen,jack) and are of the same rank, then add 100 points bonus
to the total points of the 3 cards.

- if all 3 cards of the same suit are a sequence, add 30 points bonus
to the total points of the 3 cards.

At the end of the game the winner is announced between the computer
and the user.

The following is what i think should be done (got help on it from
another forum), but i don't know much syntax and therefore am stuck:

1) Create somewhere to store the deck of 52 cards (e.g. a list named
Deck)
(Hint - search for list.append() and list.pop() help)
2) Create somewhere to store user points and computer points
3) Fill "Deck" with 52 cards, one of each suite
4) Shuffle the deck
b) Calculate points
c) Calculate bonus points
d) Add to user or computer store
e) Repeat until the deck is empty
6) Print scores

import random

##Store the deck as a list
Deck = []
UserPoints = 0
CompPoints = 0

##Populate the deck with cards
##Hint - for x in range(14)
##Hint2 - for s in ['hearts', 'spades']

##Shuffle the deck
random.shuffle(deck)

##Main loop
while Deck:

##calculate points

## calculate bonus points

## Add points and bonus points to total

## etc

##Print winner

Thanks.
•  at May 11, 2003 at 5:15 am ⇧

On Sun, 11 May 2003 00:28:27 GMT "King Kuta" wrote:

http://www.ibiblio.org/obp/thinkCS/thinkCSpy/chap15.htm

What other newsgroups have you askesked help to?

Kuta

Thanks for all the help guys. I really appreciate it. Will probably end up churning something weird.

## Related Discussions

Discussion Overview
 group python-list categories python posted May 10, '03 at 12:14p active May 11, '03 at 5:15a posts 13 users 7 website python.org

### 7 users in discussion

Content

People

Support

Translate

site design / logo © 2021 Grokbase