The Bridge Playing example

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

The Bridge Playing example

Andy Burnett
<< Todd said:
A deck, hand, pile, all can be implemented as just an
OrderedCollection.  You might start with a Deal or BridgeRound or
something.  It might have some methods like:

initialize
       | hands deck |
       deck := Card bridgeDeck shuffled asOrderedCollection. "just like
english"
       north := OrderedCollection new.
       south := OrderedCollection new.
       east := OrderedCollection new.
       west := OrderedCollection new.
       trick := OrderedCollection new.


-- snip -- >>


Todd, thanks for writing that up. I found it really interesting to see how you tackled the problem.  I think it would be great if - as time permits - the experts of the list could take a problem - nice simple ones would be good, and show how you might solve it.  It is also very interesting to see how you refine/modify each others' solutions.

cheers
AB

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

Re: The Bridge Playing example

tblanchard
I'm sitting here watching Bootcamp install Windows XP - guess I'll  
kill some time. :-)

On Aug 24, 2008, at 4:58 PM, Andy Burnett wrote:

> Todd, thanks for writing that up. I found it really interesting to  
> see how you tackled the problem.  I think it would be great if - as  
> time permits - the experts of the list could take a problem - nice  
> simple ones would be good, and show how you might solve it.  It is  
> also very interesting to see how you refine/modify each others'  
> solutions.

Well my knowledge of bridge is pretty superficial and it is possible I  
have just hit the bottom.  You guys pose problems and I'm sure someone  
will point you towards a solution.  I totally empathize with the whole  
"Just can't get started" problem. Anyhow, some obvious initial  
refinements:

1) Randal is absolutely right that the variables north,east,south,west  
should probably be put into some kind of indexed storage because you  
tend to access them in a kind of sequence (going around the circle  
either playing or dealing) and you do the same thing to each.  So his  
refinement

hands := IdentityDictionary newFrom:
  (#(north east south west) collect: [:each | each -> Set new]).

is good.  It creates a dictionary where the keys are symbols and the  
values are Sets.  So now, if you want north's cards, you say

(hands at: #north)

and to "go around the circle" you could say

#(north east south west) do: [:position | (hands at: position)  
doSomethingOrOther ]

 From a style perspective, I'm not thrilled about pulling things out  
of dictionaries constantly and I'd be likely to implement accessors  
for convenience

north
    ^ hands at: #north

because I think it is prettier to say

self north doSomethingOrOther

than

(hands at: #north) doSomethingOrOther

but you could argue that, given the cyclic nature of a card game,  
you'd probably never use them.

2) How to model a hand

I started with OrderedCollection because cards are dealt in an order  
and I half-thought the order might matter.  However, it turns out that  
it doesn't really matter in Bridge which is probably why Randal chose  
Set.  Set has no concept of ordering and has the added nice property  
of disallowing a card from being in a hand twice, which is somewhat  
more realistic in Bridge but not so much in a game like Pinochle where  
you can have two of the same card.  You could solve that by using  
IdentitySet I suppose.

Incidentally, that is why Card responds to bridgeDeck rather than just  
deck.  You might add pinochleDeck or canastaDeck.  These are decks  
made out of the same kinds of cards, but composed differently (I dated  
a cards fiend in college - I've forgotten most of the rules but  
remember a few basics).

Actually, in Bridge, players invariably sort their hands by suit, then  
rank as soon as they are dealt so maybe a SortedCollection with a  
custom sort block makes more sense.  At least if you are displaying  
the cards to human users.  Something like:

bySuitsThenRank := : [:x :y | ((x suit) = (y suit)) ifTrue: [x rank <  
y rank] ifFalse: [x suit < y suit]]. " block value is true if x and y  
are in right order, false if not"

now you get

hands := IdentityDictionary newFrom:
  (#(north east south west) collect: [:each | each ->  
(SortedCollection sortBlock:  bySuitsThenRank) ]).

This gives me a typical hand looking like:
a SortedCollection(Ace of Clubs Jack of Clubs Ace of Hearts 3 of  
Hearts 4 of Hearts 5 of Hearts Ace of Diamonds 6 of Diamonds 10 of  
Diamonds Jack of Diamonds 2 of Spades 3 of Spades Queen of Spades)

which is how I typically sort my bridge hands.

If you want to do your own card game, I recommend trying to write  
blackjack.  That's a program I used to assign my C students.  Write a  
program that lets a person play the computer (who is dealer). The  
rules are dead easy (dealer hits on 16, stays on 17 I think) but there  
are nuances in evaluating the current hand when aces are involved.  
Also, in BJ, I'd go back to OrderedCollection for representing a hand  
because order matters - the first card is the 'down' card and is not  
revealed to the other players until the end of the round.

Just tossing ideas.

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

Re: The Bridge Playing example

Marcin Tustin
In reply to this post by Andy Burnett
For anyone who's interested, here's an implementation with the cards represented as integers (or wrapping integers). Perhaps this could be the basis of a super-compact representation. In any case, sorting hands becomes simpler, but either dealing (creating a card object) or checking that a play is legal becomes more involved (because at some point you would have to reconstruct the symbolic information regarding suit and rank).

Alternatively, if you want to make the deck bridge-specific you could change the undealt variable into an Array or OrderedCollection, and after shuffling treat each of the quarters as belonging to a specific hand. Again, more compact, less simplicity. Taking something out of the hand could be represented by replacing that card's integer with 0 in the deck/hand array.

Another alternative would be to use an "ordinary" representation for shuffling etc. then write that into a bit-packed representation with each two bits representing a player, as the original questioner wanted. Some utility classes could go over that to make handling such a thing look like an array of players, and use a formatting method similar to the one used in the attached code.



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

PlayingCards.st (3K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: The Bridge Playing example

tblanchard
I don't have time to look at it today, but people interested in the  
best of both worlds should check out how Character and String works.  
There's only one instance of Character representing the letter 'a' in  
the whole system, but there are many letter 'a's.  String stores bytes  
for each character for compactness, but gives you back a Character  
object when you call at:.  It is worth looking at how.  The pattern is  
referenced in Object Design Patterns as 'Flyweight'.

-Todd Blanchard

On Aug 25, 2008, at 4:21 AM, Marcin Tustin wrote:

> For anyone who's interested, here's an implementation with the cards  
> represented as integers (or wrapping integers). Perhaps this could  
> be the basis of a super-compact representation. In any case, sorting  
> hands becomes simpler, but either dealing (creating a card object)  
> or checking that a play is legal becomes more involved (because at  
> some point you would have to reconstruct the symbolic information  
> regarding suit and rank).
>
> Alternatively, if you want to make the deck bridge-specific you  
> could change the undealt variable into an Array or  
> OrderedCollection, and after shuffling treat each of the quarters as  
> belonging to a specific hand. Again, more compact, less simplicity.  
> Taking something out of the hand could be represented by replacing  
> that card's integer with 0 in the deck/hand array.
>
> Another alternative would be to use an "ordinary" representation for  
> shuffling etc. then write that into a bit-packed representation with  
> each two bits representing a player, as the original questioner  
> wanted. Some utility classes could go over that to make handling  
> such a thing look like an array of players, and use a formatting  
> method similar to the one used in the attached code.
>
>
> <PlayingCards.st>_______________________________________________
> 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: The Bridge Playing example

Charles Gray-3
In reply to this post by Marcin Tustin
Marcin Tustin <mm3 <at> zepler.net> writes:


>
> Attachment (PlayingCards.st): application/octet-stream, 2323 bytes
>
>
When I clicked on the attachment, it wasn't found.



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

Re: Re: The Bridge Playing example

David T. Lewis
On Mon, Aug 25, 2008 at 08:06:35PM +0000, Charles Gray wrote:
> Marcin Tustin <mm3 <at> zepler.net> writes:
> >
> > Attachment (PlayingCards.st): application/octet-stream, 2323 bytes
> >
> When I clicked on the attachment, it wasn't found.

Perhaps your mail system removed the attachment.

Attached is a copy of Marcin Tustin's PlayingCards.st in a zip file.

Dave


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

PlayingCards.st.zip (1K) Download Attachment