[Tutor] Cards Program

Bruce Sass bsass@freenet.edmonton.ab.ca
Wed, 21 Feb 2001 13:50:49 -0700 (MST)

I gotta agree with Rob, take a more object oriented approach.

Not only could you have classes for instances of dealer and player
objects, the deck(s) of cards and the rules of the game can also be
objects.  To see that last one, consider the "I am the Walrus" state
machine a few messages back[1], then have each "personality" be the
possibilities for a particular state of the game.

The top level could look something like this:

eric = AIPokerPlayer()
john = AIPokerPlayer()
tim = HumanPokerPlayer()
players = [john, eric, tim]
gametype = "stud"
game = Poker(players, gametype)
while not game.winner:
print "The winner is...", game.winner

When game is instantiated it would setup to mediate stud poker between
eric and john - get a new deck, start a dealer, tell the dealer to
deal a hand to everyone.  The game() calls will move the game along;
e.g., "how many cards do you want?", "what's your bet?", `is there
a winner', etc.

A *Player would need to respond to the various queries the game could
generate; for the human player it is a matter of prompting and
waiting, the ai player would need to know the rules.

The *Player classes and Poker class could be derived from classes that
define the stuff in common to a variety of different types of players
and games.  So maybe you would have:

class Poker(CardGame): ...
class AIPlayer(GamePlayer): ...
class AIPokerPlayer(AIPlayer): ...

Ya, ok, I haven't answered your question; but I hope I have shown you
a way of looking at the problem that lets you break it down into
independent pieces so it is easier to handle.

- Bruce

[1] ...state machine core... in case you missed it

class StateMachine:
    def __init__(self):
        # do stuff that is done only one time. e.g.,
        self.state = 0
    def __call__(self):
        # move to the next state
        self.state = ...
        self.handlestate(self.state)  # whatever

On Wed, 21 Feb 2001, Rob Andrews wrote:
> How about adding a little extra flow control during the card dealing so that
> a list of players is iterated through and dealt to w/o having to restart the
> deal with a fresh deck?
> One idea is to try coding this program using classes, having a *dealer* and
> several *player* objects.
> ----- Original Message -----
> From: "Timothy M. Brauch" <tbrauch@mindless.com>
> > I am slowly working on a program that simulates dealing cards.  While
> > right now I have no real purpose for this program, it is just something
> > I thought I'd like to try.  However, I have come to a stumbling block.
> > As it is right now, I can get the program to deal as many cards as I
> > want, but I cannot think of a way to get the cards into seperate hands,
> > other than just having the program deal out the cards and then manually
> > split these cards up into hands myself.  That is not what I am looking
> > for.  Here is my code so far:
<...code snipped...>
> > This code will work for one hand, but if I try to just run it again for
> > anotehr hand, there will be repeated cards.  Ideally I'd like to be able
> > to have the program ask me how many hands I want dealt and how many
> > cards in each hand.  But I just cannot seem to make anything I try
> > work.  Any help would be appreciated.