# Cards deck problem

A

#### Arun Nair

Can any one help me with this im not getting it even after reading
books because there is not much of discussion anywhere

a>
Implement a calss that represents a playing card. The class should
implement the following methods:
_ _ init _ _ (self, rank, suit) Creates a card.
rank is an integer in range of 1 to 13 (Ace:1, King 13), suit is a
character in set {'h','d','c','s'}
getRank(self) Returns the rank of the card
getSuit(self) Returns the suit of the card
BJValue(self) Returns the 'Blackjack Value' of the card (Ace:1, Face
card:10)
_ _ str _ _ (self) Returns a string naming the card. For example: 'Ace

Test your class by writing a 'test harness' that generates 'n' randomly
generated cards, where 'n' is supplied by the user. Print out the
string associated with each card and it's 'Blackjack value'

b>
Extend the card class to display in a graphics window. the new class
should support the following method: draw(self, win, center) Draws a
card in a window.
Use the extended class to display a hand of five random cards.

c> Write a program that creates a list of card objects as above and
print out the cards grouped by suit and in rank order in each suit.
the program should read the values for the list of cards from a file,
where each line in the file specifies a single card with the rank and
then the suit separated by a space. Hint: sort the list first by rank,
and then by suit.

d> Create a new class deck that represents a pack of 52 cards. Which
supports the following methods:

_ _init_ _ (self) - Creates a deck of cards in standard order.
shuffle(self) - Randomizes the order of the cards
dealCard(self) - Returns a single card from the top of the deck, and
removes the card from the deck.
cardsLeft(self) - Returns the number of cards left in the deck.
Test your class by having it deal out a sequence of 'n' cards where 'n'
is a number input by the user. The program should either print out the
cards, or display them in a window.

Your urgent and quick reply help will be appreciated the most.

Arun Nair

P

#### Paul Rubin

Arun Nair said:
Your urgent and quick reply help will be appreciated the most.

Do you have a specific question? What are you having trouble with?

B

#### Ben Finney

Arun Nair said:
Can any one help me with this im not getting it even after reading
books because there is not much of discussion anywhere

Perhaps the discussion should be between yourself and your teacher, or
the other students in your class. We're not here to do your homework
assignments.

A

#### Arun Nair

Hey paul i dont know how to implement this stuff if you have any ebook
or any notes which can halp me then i would like to try it and
implement my and refer back to you for any errors comming out of it

Regards,

Arun

A

#### Arun Nair

Im so sorry about that but the lecturer doesnt teaches anything so its
become very difficult for me to do it if you guys have any notes on
classes it will be very helpfull. and i can try on my own.

regards,

Arun

F

D

#### Dennis Lee Bieber

Can any one help me with this im not getting it even after reading
books because there is not much of discussion anywhere
Have you tried the standard Python tutorial?
a>
Implement a calss that represents a playing card. The class should
implement the following methods:
_ _ init _ _ (self, rank, suit) Creates a card.

It's spelled __init__, not _ _ init _ _
rank is an integer in range of 1 to 13 (Ace:1, King 13), suit is a
character in set {'h','d','c','s'}
getRank(self) Returns the rank of the card
getSuit(self) Returns the suit of the card

These two are unneeded unless you plan to compute the values at run
time rather than return the values used to initialize (and if you do
need to compute values later, using "properties" masks the use of
"getters").
BJValue(self) Returns the 'Blackjack Value' of the card (Ace:1, Face
card:10)

Last time I played, the Ace had a dual value; it could be used as
"1" or "11".
_ _ str _ _ (self) Returns a string naming the card. For example: 'Ace

Spelled __str__
Test your class by writing a 'test harness' that generates 'n' randomly
generated cards, where 'n' is supplied by the user. Print out the
string associated with each card and it's 'Blackjack value'
Must be a homework assignment -- surely you aren't expecting us to
supply all this in response to your request for help.
b>
Extend the card class to display in a graphics window. the new class
should support the following method: draw(self, win, center) Draws a
card in a window.
Use the extended class to display a hand of five random cards.
Ugh! It took me a few weeks to figure out how to do a simple form in
Tkinter... I'm sure not going to try to create graphics for a deck of
cards...
d> Create a new class deck that represents a pack of 52 cards. Which
supports the following methods:

I suspect this would have been my second class when creating the
original response. After all, the "pure random" "test harness" in (a)
could produce a sequence of identical cards.
_ _init_ _ (self) - Creates a deck of cards in standard order.
shuffle(self) - Randomizes the order of the cards

Does your instructor realize that this is a one-liner in Python
dealCard(self) - Returns a single card from the top of the deck, and
removes the card from the deck.

Mentally, "dealing" is distributing cards to all players... I tend
to think of a single card as a "draw"...
cardsLeft(self) - Returns the number of cards left in the deck.

Some more one-liners...

Since your assignment order starts with the card class, I'll just
sketch out the deck class (especially as it is a bunch of one-liners
except for the init... AND THIS IS MORE THAN I SHOULD... NO ERROR
CHECKING IS DONE.

import random

class Deck(object):
def __init__(self):
self._deck = []
for s in "hdcs":
for r in range(13): #note, this gives 0 - 12
self._deck.append(Card(r, s))

def shuffleDeck(self):
random.shuffle(self._deck)

def drawCard(self):
return self._deck.pop(0) #can error if the deck is empty

def cardsLeft(self):
return len(self._deck)

That should be enough to show the syntax used in defining a class...
--
Wulfraed Dennis Lee Bieber KD6MOG
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/

D

#### Dennis Lee Bieber

Im so sorry about that but the lecturer doesnt teaches anything so its
become very difficult for me to do it if you guys have any notes on
classes it will be very helpfull. and i can try on my own.
For "notes" that are Python related, there is the tutorial that is
part of the documentation of all full Python installs. If the tutorial
is insufficient, the text of "Dive into Python" is available online (and
is in the help system of the ActiveState Python release).

"Notes" on classes, in general, means texts books on object-oriented
analysis and design. That is a much larger subject, and properly done
will be language independent.
--
Wulfraed Dennis Lee Bieber KD6MOG
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/

M

#### Michael Naunton

It is clear your professor does not understand computer science. Switch
to a new university now.

As noted in the problem, a deck has 52 cards. cardsLeft(self) therefore
always returns 52.

This may seem pendantic, but CS is mostly about thinking about (and thus
naming) things clearly. Find someone who does.

-- Michael

D

#### Dennis Lee Bieber

This may seem pendantic, but CS is mostly about thinking about (and thus

<snicker> Someday I should arrange to do a lost-wax casting of a
/pendant/ with the definition of /pedant/ on it <G>
--
Wulfraed Dennis Lee Bieber KD6MOG
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/

M

#### mensanator

Dennis said:
<snicker> Someday I should arrange to do a lost-wax casting of a
/pendant/ with the definition of /pedant/ on it <G>

Why not a /pedant/ with a description of /pendant/ on it?

C

#### Cameron Walsh

Why not a /pedant/ with a description of /pendant/ on it?
As fun as it would be, I haven't found too many /pedants/ who want to
have the definition of /pendant/ written on them.

You've just given me an idea for an online T-Shirt printing business.
Thank you, and your royalties will be in the post soon, just provide me
with your credit card details.

G

#### Gabriel Genellina

It is clear your professor does not understand computer science. Switch
to a new university now.

As noted in the problem, a deck has 52 cards. cardsLeft(self) therefore
always returns 52.

This may seem pendantic, but CS is mostly about thinking about (and thus
naming) things clearly. Find someone who does.

Uhm, maybe it's a matter of language, but how do you name the pile of
cards remaining to be dealt once the game begins?
At least in Argentina, it's called the same ("mazo") as the full,
original, set of cards.
The "remaining cards" interpretation for "deck" would be consistent
with the required interfase: shuffle, dealCard, cardsLeft...

--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar

B

#### Ben Finney

Gabriel Genellina said:
Uhm, maybe it's a matter of language, but how do you name the pile of
cards remaining to be dealt once the game begins?

In English? I'd call that a deck. However, that deck doesn't contain
52 cards any more, so a thing that behaves that way doesn't fit the
definition of "deck" in the problem.

Perhaps a better definition would have used language like "... begins
with 52 cards" or the like, and described the properties of a deck to
be modelled.

Is this merely pedantic? If we were describing the rules of a game to
be played by humans, yes. But in this case we're describing parameters
of a problem to be modelled in a computer, hopefully independently by
each student in a class. Getting the problem defined precisely is
essential, otherwise judging the result fairly is impossible.

(good sigmonster, have a cookie)