I've been trying to make so that I have one class per file for easier
Unlike Java, which practically mandates the class<=>file format,
Python is perfectly happy with multiple related classes in one file --
and often /easier/ to read that way as one is not forced to jump around
finding dependent files to understand related classes.
This has been a recurring problem for me in languages such as C++ and
Java. I'm used to programming in C.
Please don't be offended, but looking at the specified file... it
shows...
Ignoring the language in use, I'd suggest a few studies in software
design, functional decomposition, data structures, and algorithms... to
start...
I lost count of how many times this
if rank==0:
rankstr = "ace"
elif rank==10:
rankstr = "jack"
elif rank==11:
rankstr = "queen"
elif rank==12:
rankstr = "king"
else:
rankstr=str(rank+1)
appears in the file, but a concept normally emphasized early in a
programming career is that when you have many identical (or near enough
that a minor tweak can serve for all) occurrences of the same code, it
should probably be turned into a function.
However, this is even better served by usage of data structures...
_RANKS = [ "ace", "two", "three", "four", "five",
"six", "seven", "eight", "nine",
"ten", "jack", "queen", "king" ]
as a module-wide "constant", and then replace all those if/elif...
blocks with a simple:
rankstr = _RANKS[rank]
Worse though, your code seems to have used the class keyword just
for the "see, I used classes" factor, with no logical concept of object
orientation. Instead you are creating singleton instances that are being
referenced as global data across the classes...
A simplistic approach for object orientation is to start with a text
description of the problem (in this case, a description of how the game
is played, perhaps). Find the key nouns in that description -- nouns
often (not always) turn into the classes. Find the verbs in the
description -- these are possible methods of the most closely related
noun. Find adjectives -- these may become attributes of the classes.
Generic Card Game:
The GAME uses one DECK of 52 playing CARDs (standard arrangement:
_ranks_ of Ace, 2, ..., 10, Jack, Queen, King, in the _suits_ of Clubs,
Diamonds, Hearts, Spades). _n_ PLAYERs take turns... The DECK is
/shuffled/, then CARDs are /dealt/ to each PLAYER, each, in turn,
/drawing/ one card from the DECK until they have a HAND of m-CARDs.
As a start (if you want multiple files)...
-=-=-=-=-=- carddeck.py
import random
class Card(object):
_RANKS = [ "ace", "two", "three", "four", "five",
"six", "seven", "eight", "nine",
"ten", "jack", "queen", "king" ]
_SUITS = [ "spades", "clubs", "hearts", "diamonds" ]
def __init__(self, rank, suit):
self.rank = rank
self.suit = suit
self.name = "%s of %s" % (Card._RANKS[self.rank],
Card._SUITS[self.suit])
class Deck(object):
def __init__(self):
# cryptic initialization of 52 cards <G>
# relies on Python 2.4 or earlier behavior
# read the documentation for 2.5 or later to determine changes
self._deck = [Card(i % 13, i / 13) for i in range(52)]
self._discards = []
def shuffle(self):
self._deck.extend(self._discards) #combine
self._discards = []
random.shuffle(self._deck)
def draw(self):
return self._deck.pop() #returns one card from deck
def discard(self, crd):
self._discards.append(crd) #puts card into discard pile
-=-=-=-=-=- player.py
import carddeck #at this point, this may or may not be needed
class Hand(object):
def __init__(self):
self._cards = []
def pull(self, pos=0):
return self._cards.pop(pos)
def size(self):
return len(self._cards)
def put(self, crd):
self._cards.append(crd)
def cards(self):
return [ crd.name for crd in self._cards]
class Player(object):
def __init__(self):
self._hand = Hand()
def pull(self, pos=0):
#I'm pulling a "law of demeter" here; player does not
#directly manipulate cards in the hand
return self._hand.pull(pos)
def size(self):
return self._hand.size()
def put(self, crd):
self._hand.put(crd)
def showHand(self):
for crd in self._hand.cards():
print crd
-=-=-=-=- game.py
import carddeck
import player
class Game(object):
def __init__(self, numPlayers=2):
self._players = [player.Player() for i in range(numPlayers)
self._deck = carddeck.Deck()
self._deck.shuffle()
for i in range(7): #assume each player starts with 7 cards
for p in self._players:
p.put(self._deck.draw())
# rest left as an exercise for the student...
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/