Where do I put it?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Where do I put it?

Blake-5
I'm trying to build classes for card games.

In the abstract, I find this easy enough. I have a deck class and a card  
class. I'm toying with the idea of classes for particular games (like  
"stack", "stock", "waste" and "tableau" used in Solitaire) but haven't  
figured out whether there's any value in that yet.

Anyway, in the concrete, most card games are about the symbols. In a  
standard deck, of course, there's the rank and suit. So, for my standard  
deck, I have:

ranks
        ^ {#deuce. #three. #four. #five. #six. #seven. #eight. #nine. #ten.  
#jack. #queen. #king. #ace}

suits
        ^ {#spades. #hearts. #diamonds. #clubs}

as class methods.

Though it's the game class that's ultimately responsible for constructing  
the deck and assigning significance to the cards (e.g., whether aces are  
high, or eights are crazy, or whatever).

        Anyway, something about this approach doesn't sit quite right with me,  
like I have the responsibility/scope wrong. Any thoughts?

        ===Blake===
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

RE: Where do I put it?

Ron Teitelbaum
Hi Blake,

Just some quick thoughts.  In the real world a deck of cards is just that.
There is no meaning in the cards themselves.  Instead the meanings are in
the rule book that comes with the game that you are playing.  Also there is
no value in a card, but in the hand, or best hand out of possible hands in
the cards that you are dealt.  

Think about having seven cards Ah 3h 4h 5h 6c 7c Jh  Notice that you have
two good hands either a straight or a flush.  So from the rule book you need
to know that the best hand (a flush) is the hand with the highest value.

Given all that you need a game object that has rules, and the rules need to
be able to construct hands that have values, or run the game process to some
end.

Does that help?

Ron Teitelbaum
President / Principal Software Engineer
US Medical Record Specialists
www.USMedRec.com


> From: Blake
> Sent: Sunday, March 04, 2007 10:39 PM
>
> I'm trying to build classes for card games.
>
> In the abstract, I find this easy enough. I have a deck class and a card
> class. I'm toying with the idea of classes for particular games (like
> "stack", "stock", "waste" and "tableau" used in Solitaire) but haven't
> figured out whether there's any value in that yet.
>
> Anyway, in the concrete, most card games are about the symbols. In a
> standard deck, of course, there's the rank and suit. So, for my standard
> deck, I have:
>
> ranks
> ^ {#deuce. #three. #four. #five. #six. #seven. #eight. #nine. #ten.
> #jack. #queen. #king. #ace}
>
> suits
> ^ {#spades. #hearts. #diamonds. #clubs}
>
> as class methods.
>
> Though it's the game class that's ultimately responsible for constructing
> the deck and assigning significance to the cards (e.g., whether aces are
> high, or eights are crazy, or whatever).
>
> Anyway, something about this approach doesn't sit quite right with
> me,
> like I have the responsibility/scope wrong. Any thoughts?
>
> ===Blake===
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Where do I put it?

Blake-5
> Just some quick thoughts.  In the real world a deck of cards is just  
> that. There is no meaning in the cards themselves.  Instead the meanings  
> are in
> the rule book that comes with the game that you are playing.  Also there  
> is no value in a card, but in the hand, or best hand out of possible  
> hands in the cards that you are dealt.

Precisely. That's why nothing is in the deck or card to say that (e.g.) a  
Jack is worth "10" or that an ace is "high".

> Does that help?

Well, it's nice to know my overall thinking is right (for the value of  
"right" comprised of "Ron agrees with me"<s>). Maybe I can phrase the  
question more clearly now.

The identity of an inidivual card is its symbol (or symbol set).

In a standard deck, you have a rank and suit, like "Ace" and "Spades".
In a tarot deck, you'd have the same, and also the trumps, like "The Fool"  
and "The Hanged Man".
In a Magic deck, you'd have the name, the type, the mana cost, the  
strength, the defense, the special rules and the "color" text.

So, we have these symbols--#Ace, #TheFool, #Plains--and my design has them  
defined as part of the deck class.

The symbols have to be defined at the game level, for sure. The game needs  
to be able to evalulate hands, as you point out. I suppose they don't  
really need to be defined at the deck or card level, except in such a way  
that a particular card or group of cards can be identified. (So if, for  
example, I'm playing Go Fish, I have to be able to identify that a player  
has a Queen, and also to be able to lay down all my Queens when I have  
four.)

At the same point, below the game and above the deck, there would seem to  
be some value in having the symbols available. A standard deck, or  
variants composed of just those cards, is used for thousands of games.  
Poker, solitaire, bridge, war, go fish, crazy eights, etc., all use the  
same deck. Some remove cards, like pinochle, some use multiple decks so  
that it's harder to "cheat" (like blackjack).

Now, maybe I'm wrong on this and it should be regarded as a concidence  
that War, Poker and Bridge all use the same cards, since the cards don't  
have the same meanings. A particular user interface object could still  
treat the cards as the same.

So, maybe that's the answer: They aren't really the same cards in the  
context of a program, so they should be made at the game level.
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

RE: Where do I put it?

Ron Teitelbaum
Hi Blake,

Although I think I'm following your questions, I still may not be.  

For me it is ok to have a deck.  The deck holds onto cards.  Each instance
of card is a card that has attributes.  Symbols are good for setting the
instance variables of the cards.  You could have a method on deck #shuffle,
that sets the order of the cards.  And the cards themselves should be based
on a factory in your game.  The deck would define the available symbols and
cards, (so you could have games like pinochle), and the game will select the
appropriate deck to use.  That factory on games could be reused and
subclassed to make it easier to implement, esp. since the method shuffle
will work for any deck.  

So

Deck
--------
cards -> orderedCollection of Card
deckType -> yourDeckTypeSymbol
deckRules -> instace of CardFactory based on yourDeckTypeSymbol
--------
>>suffleDeck
        "get a new deck from the deckRules"
        self cards: self deckRules shuffle.

Game
--------
gameName -> name of game
deck -> instance of deck
--------
class>>deckType
        "return the type of deck needed for this game"

CardFactoryAbstract
---------
---------
class >> forDeckType: aDeckType
        "return subclass of factory based on aDeckType"
        ^self allSubclasses detect: [:aCardFactory |
                aCardFactory deckType = aDeckType
       ]
>> shuffle
        "return a collection of cards in random order"
        randomGenerator := SecureRandom new.
        myCards := self cards copy.
        results := OrderedCollection new: myCards size.
        [myCards isEmpty] whileFalse:
                [results add: (myCards remove: (myCards at:
((randomGenerator next)\\myCards size) + 1)]
        ^results


CardFactoryAbstract subclass: CardFactory52CardStandardDeck
---------
cards -> orderedCollection of 52 standard cards
---------
class >> deckType -> #standard52Card
class >> deckContentSymbols
        "return a group of symbols that represent the contents of the deck"

>> initializeDeck
        "use class symbols to add all cards to deck"

I just wrote all this off the top of my head and didn't test any of it, and
I didn't include all the code you need.  I guess I'm just giving you a
pattern that will work, but that leaves you plenty to learn on your own.  I
tried to make it simple, but I've been told that I don't do that very well.
So if you need a more detailed explanation I can go over each step in my
thinking, please don't be afraid to ask.  If you like we can continue this
conversation offline, either way works for me.

Happy Coding,

Ron Teitelbaum
       


> From: Blake
> Sent: Monday, March 05, 2007 3:38 AM
>
> > Just some quick thoughts.  In the real world a deck of cards is just
> > that. There is no meaning in the cards themselves.  Instead the meanings
> > are in
> > the rule book that comes with the game that you are playing.  Also there
> > is no value in a card, but in the hand, or best hand out of possible
> > hands in the cards that you are dealt.
>
> Precisely. That's why nothing is in the deck or card to say that (e.g.) a
> Jack is worth "10" or that an ace is "high".
>
> > Does that help?
>
> Well, it's nice to know my overall thinking is right (for the value of
> "right" comprised of "Ron agrees with me"<s>). Maybe I can phrase the
> question more clearly now.
>
> The identity of an inidivual card is its symbol (or symbol set).
>
> In a standard deck, you have a rank and suit, like "Ace" and "Spades".
> In a tarot deck, you'd have the same, and also the trumps, like "The Fool"
> and "The Hanged Man".
> In a Magic deck, you'd have the name, the type, the mana cost, the
> strength, the defense, the special rules and the "color" text.
>
> So, we have these symbols--#Ace, #TheFool, #Plains--and my design has them
> defined as part of the deck class.
>
> The symbols have to be defined at the game level, for sure. The game needs
> to be able to evalulate hands, as you point out. I suppose they don't
> really need to be defined at the deck or card level, except in such a way
> that a particular card or group of cards can be identified. (So if, for
> example, I'm playing Go Fish, I have to be able to identify that a player
> has a Queen, and also to be able to lay down all my Queens when I have
> four.)
>
> At the same point, below the game and above the deck, there would seem to
> be some value in having the symbols available. A standard deck, or
> variants composed of just those cards, is used for thousands of games.
> Poker, solitaire, bridge, war, go fish, crazy eights, etc., all use the
> same deck. Some remove cards, like pinochle, some use multiple decks so
> that it's harder to "cheat" (like blackjack).
>
> Now, maybe I'm wrong on this and it should be regarded as a concidence
> that War, Poker and Bridge all use the same cards, since the cards don't
> have the same meanings. A particular user interface object could still
> treat the cards as the same.
>
> So, maybe that's the answer: They aren't really the same cards in the
> context of a program, so they should be made at the game level.


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

RE: Where do I put it?

Ron Teitelbaum
Hi Blake,

I agree that you questions have general value and I'm happy to help
publicly.  I only offered to help you offline because it is clear that you
are getting something from our conversation so I wanted to remove the
distractions of the public list if it was causing any difficulty.

It does seem that there are games where the deck arrangement is so different
that it needs its own coding.  The benefit of the factory implementation
that I showed you is that you now have a deckFactory superclass to write
code that is common among all decks (like shuffle).  Ultimately the greatest
benefit of shared code comes from having more then one game that can use the
same deck.  In that case all you have to do is implement the deckType on
your game and you are good to go.

You are asking very good questions, please keep them coming!

Ron

> -----Original Message-----
> From: Blake
>
> On Mon, 05 Mar 2007 08:20:39 -0800, Ron Teitelbaum <[hidden email]>
> wrote:
>
> > I just wrote all this off the top of my head and didn't test any of it,
> > and I didn't include all the code you need.  I guess I'm just giving you
> > a
> > pattern that will work, but that leaves you plenty to learn on your
> > own.  I tried to make it simple, but I've been told that I don't do that
> > very well.  So if you need a more detailed explanation I can go over
> > each step in my
> > thinking, please don't be afraid to ask.  If you like we can continue
> > this conversation offline, either way works for me.
>
> Well, I hope the questions I post have some general educational value. I'm
> building mini-Smalltalk lessons and decided to do a series on card games
> because when =I= was learning, each game defined its own deck of cards
> with its own set of code. (If suit wasn't important to a game, for
> example, the program simply wouldn't specify it.) Lotta duplicated
> read-only code. A deck of cards seemed like a good, beginning design
> question, but I began to think (overthink?) the relationships of physical
> cards, decks, the symbols used, etc.
>
> I hadn't thought of using a factory; I'll brood on that one.
>
> Thanks.
>
> ===Blake===


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners