This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2007-08-05 22:46:52

mdusoe
Scratcher
Registered: 2007-06-14
Posts: 32

Cribbage Solitaire

Please come check out my Solitaire version of cribbage.

http://scratch.mit.edu/projects/mdusoe/27162

It features some pretty cool array handling techniques using boolean flags to be able to get and set a large number of different values quickly without a REALLY big If...ElseIf...ElseIf...ElseIf...ElseIf...

Thank you Mayhem for the images, and I found some sounds from various card games I have on my computer. 

Now I need to add an actual cribbage board...  hmmm...

Mike.

Offline

 

#2 2007-08-06 09:54:08

mdusoe
Scratcher
Registered: 2007-06-14
Posts: 32

Re: Cribbage Solitaire

In response to one of Mayhem's comments on the page, I will attempt to describe the bsics of my Card Handling techniques.

I use a small area (about 6 pixels wide) on the left of the screen to represent my array of cards. I will call it the CardStrip.  Each row in the strip can be looked at as 6 "bits" in a binary number.   To ease things later, I broke them into two section with the first four bits being the number (0-12) and the last 2 being the suit (0-3).  ValueHandler and SuitHandler manage the writing to/reading from these rows by responding to GetValue and SetValue. 

When Shuffle is called, I select 2 random numbers from 0-51.  Then I swap them (see deckOfCards where it handles ShuffleDeck and SwapCards).  I repeat this 50 times, which yields a "pretty well" shuffled result.

As Mayhem suggested, I could have used 52 sprites with a faster shuffling routine.  I was concerned about later when I was using the values that performance would be slower (for reasons which I won't go into here), but I may still go that route another day....

So, now that the deck is shuffled, I rely on deckOfCards to maintain it's current pointer to the deck, and call GetCardFromDeck 5 times.  Each of these cards is moved to another section of the CardStrip by setting, before calling GetCardFromDeck, the spot in the array where the next card should go.  Then, I the Card sprite manages the visual, ie. move to the right spot, play a sound, witch costume, and stamp.  Same goes for when you select one - set the FromCard/ToCard appropriately, call moveCard which updates the CardStrip, then use the Card sprite to do the visuals.

Now, once you have your 4 hands defined, plus the crib, look at the cardstrip.  you will see that each hand has its own section of the strip.  This will allow for easier manipulation later when we go to score.  Now, we take the next card as the cut card, and add it to all 5 hands (4 plus the crib).  So each hand is in its own section with 5 cards to score with.  Hand 1 is in position 70-74, Hand 2 is in 80-84, etc...

Scoring becomes a matter of just looking at the array, ignoring the images themselves.  The most challenging, believe it or not, was trying to score the runs.  I ended up just sorting th2e cards (simple bubble sort) and then brute forcing all combinations, since there are only about 16 possibilities. 

LEt me know if there is anything more I can help with!

Mike.

Offline

 

#3 2009-10-06 17:46:51

coolperson
Scratcher
Registered: 2007-08-21
Posts: 1000+

Re: Cribbage Solitaire

Hey, wasn't this the game that BoltBait remixed and got on the front page? Cool!


Coolperson- 2012 National Vex Robotics Finalist!
http://www.scratch.mit.edu/ext/youtube/?v=sn7p84DQFyk

Offline

 

Board footer