Need help with Python programming problem. 
Author Message
 Need help with Python programming problem.

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
5) a) Read 3 cards
   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:
  ##Read 3 cards

  ##calculate points

  ## calculate bonus points

  ## Add points and bonus points to total

  ## etc

##Print winner

Thanks.



Wed, 26 Oct 2005 20:14:17 GMT  
 Need help with Python programming problem.

Quote:

> 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?


Wed, 26 Oct 2005 20:43:07 GMT  
 Need help with Python programming problem.
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


Quote:
> 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
> 5) a) Read 3 cards
>    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:
>   ##Read 3 cards

>   ##calculate points

>   ## calculate bonus points

>   ## Add points and bonus points to total

>   ## etc

> ##Print winner

> Thanks.



Wed, 26 Oct 2005 23:31:25 GMT  
 Need help with Python programming problem.


Quote:

>> 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.


Thu, 27 Oct 2005 00:35:12 GMT  
 Need help with Python programming problem.

Quote:
> Make a program that simulates a card game.

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


Wed, 26 Oct 2005 23:44:00 GMT  
 Need help with Python programming problem.

Quote:

> 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



Wed, 26 Oct 2005 22:04:59 GMT  
 Need help with Python programming problem.


Quote:

>> 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.



Thu, 27 Oct 2005 02:41:04 GMT  
 Need help with Python programming problem.

Quote:
> 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.

Quote:
> 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?

Quote:

> -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.

Quote:

> 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
> 5) a) Read 3 cards
>    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.



Wed, 26 Oct 2005 21:49:23 GMT  
 Need help with Python programming problem.

Quote:
> 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.


Thu, 27 Oct 2005 03:30:05 GMT  
 Need help with Python programming problem.
Quoth gk:
  [...]

Quote:
> 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."

                                 Brian Kernighan and Lorrinda Cherry



Thu, 27 Oct 2005 03:59:43 GMT  
 Need help with Python programming problem.

Quote:

> 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!

Quote:
> hmm. that link to iblio doesn't work.

ah! once again it's rolling away.

nirinA



Thu, 27 Oct 2005 04:33:32 GMT  
 Need help with Python programming problem.

Quote:
> hmm. that link to iblio doesn't work.

Try
http://py.vaults.ca/apyllo2.py/81439935
http://www.andamooka.org/reader.pl?section=thinkpython


Thu, 27 Oct 2005 05:57:32 GMT  
 Need help with Python programming problem.
Take a look here. It might help you figure out something.
http://www.ibiblio.org/obp/thinkCS/thinkCSpy/chap15.htm

What other newsgroups have you askesked help to?

Kuta



Quote:
> 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
> 5) a) Read 3 cards
>    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:
>   ##Read 3 cards

>   ##calculate points

>   ## calculate bonus points

>   ## Add points and bonus points to total

>   ## etc

> ##Print winner

> Thanks.



Thu, 27 Oct 2005 08:28:27 GMT  
 Need help with Python programming problem.
On Sun, 11 May 2003 00:28:27 GMT

Quote:

> Take a look here. It might help you figure out something.
> 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.


Thu, 27 Oct 2005 13:15:06 GMT  
 
 [ 14 post ] 

 Relevant Pages 

1. Interesting Programming Problem, Need Some Help

2. Need help on small programming problem.

3. Interesting Programming Problem, Need Some Help

4. need help with fortan 77 programming problems

5. Need Help: Problem upgrading python on Linux box

6. I need help........programming help!

7. i need help to modify my cobol program can someone help soon please

8. Need help: cross-platform problems with balloon help and adjustable panes

9. Calling a python program within a python program

10. Help on visual programming needed!

11. C++ and Assembly Programming need help in Linux pleaseeeeeeeeeeeeeeeeeeee

12. Need help. Programming position available

 

 
Powered by phpBB® Forum Software