Pseudocode for card shuffling game

Started by
8 comments, last by danatkinson 20 years, 3 months ago
ARRGH! Hi there. I''ll come straight out with it. I was hoping that somebody could help me with understanding pseudocode. First, I''ll be straight... It''s for an assignment. I don''t want to beat around the bush saying: "It''s for a friend.", although it would be for them as well. I need to write some pseudocode for a card shuffling game and I''m really struggling to find any decent pseudocode for card shuffles like the riffle or the perfect shuffle. I''ve been to loads of game sites that deal with this but the pseduocode is basically just structured English and I can''t use it. If anyone has any good links to sites that have something like this, I would be very grateful. Obviously I''m not asking anyone to do my work for me because I couldn''t learn from it but some sort of guide would be helpful. Thanks for any help. Dan Atkinson Mind Compression
Dan AtkinsonMind Compression
Advertisement
Psuedocode is, by nature, just structured English! Its a way of "developing" an application in a manner that can be written easily in any language.

For applying psuedocode you just need to stop thinking in a programming fashion, in a way. For example, the array of cards (or whatever collection structure you are using) can simply be refered to as the deck.

The shuffle will involve selecting a number of cards (better if random) and moving them from the bottom of the deck to the top of the deck.

The psuedocode could read something like:

Generate number of shufflesWhile the deck is being shuffledTake bottom x cardsMove cards to the top of the deckIs deck shuffled?yes: Stop shufflingno: reshuffle 


SketchSoft OFFLINE | SketchNews OFFLINE | NewKlear Studios
www.aidanwalsh(.net)(.info)
It is structured English for those who start out on it but at my level I have to have something more than simple statements.

I want to have a array of deck which uses array of card. Therefore a two dimensional array must be created.

What I''m going to try and do is create split the deck into two (or more). Then using a temporary file randomly place a card at a time from each split into the temporary file. At the end of doing this process, I want to end up with a completely shuffled deck. The problem is, I can''t pseudo this. I''ve flowcharted it but I can''t bring my mind to think in pseudo terms.

I need to have

variables, if''s, then''s, until''s and the like. Something that can be easily used in Pascal.
Dan AtkinsonMind Compression
ok,
card array[52];vector stack1;vector stack2;while(amount of shuffles)  for i = 0 to 52    if(rand() & 1)      stack1.push_back(array)<br>    else<br>      stack2.push_back(array)<br>  next i<br><br>  i=0<br>  <br>  while(stack1.empty() && stack2.empty() == false)<br>     if(stack1.empty() == true)<br>          array = stack2.at(0)<br>          stack2.erase(0)<br>          i++<br>          continue<br>     else if(stack2.empty() == true)<br>          array = stack1.at(0)<br>          stack1.erase(0)<br>          i++<br>          continue<br>     endif<br><br>    if(rand() && 1)<br>          array = stack2.at(0)<br>          stack2.erase(0)<br>          i++<br>          continue<br>     else<br>          array = stack1.at(0)<br>          stack1.erase(0)<br>          i++<br>     endif<br>   loop<br>loop   </pre>   <br>There is somewhat what you're trying to do.  You won't be able to hand in this code, because it uses a lot of things you have no clue how to do.  <img src="smile.gif" width=15 height=15 align=middle><br><br>Look off of it, however, and I think you'll be able to create your own verson. <br><br>~~~~~<br>"One Reality is worth &#111;ne thousand dreams"<br><a href="http://www-personal.umd.umich.edu/~ekallio/slimeking.php"> Download and play Slime King I</a>.<br>     <br><br><SPAN CLASS=editedby>[edited by - dede &#111;n January 15, 2004 12:32:55 PM]</SPAN>   <br><br><SPAN CLASS=editedby>[edited by - dede on January 15, 2004 12:33:22 PM]</SPAN>
~~~~~Screaming Statue Software. | OpenGL FontLibWhy does Data talk to the computer? Surely he's Wi-Fi enabled... - phaseburn
Thankyou for your help but are there any websites about this type of stuff? I''ve looked all over and can''t really find of use. Only actual games with their source code but it''s not my intention to blatently copy someone elses work and call it my own. I wrote out the pseudocode for the assignment which involves scoring based on the number of picture cards in a hand and the number of cards in a suit. All that I don''t have is a decent enough pseudocode to get through.

I wanted to collect about 3 or 4 examples of pseudocode from the net with an explanation of each and then choose one and edit that one to something I can understand and use.

Yours is exactly what I''m looking for but I haven''t used ++''s and &&''s (nor have I heard of them) .

Thankyou for your input though!

Dan Atkinson
Mind Compression
Dan AtkinsonMind Compression
Oh and Slime King I???

Very cute!!!
Dan AtkinsonMind Compression
The typical shuffling algorithm goes as follow:
Let D be a deck of cardsLet S be the deck sizefor each card D[C] in D:   Let R be a random number in [C,S)   Swap D[C] and D[R] 


In C:
#include <stdlib.h>struct card { /* ... */ };void shuffle(deck, decksize){   for(int current=0; current<decksize; ++current)   {       // Yes, I know, it''s not a perfect random distrbution       int other = rand() % (decksize-current) + current;       struct card tmp = deck[current];       deck[current] = deck[other];       deck[other] = tmp;   }}int main(){  int decksize = 52;  struct card deck[decksize];  /* initialize */  /* ... */  /* shuffle */  shuffle(deck, decksize);  return 0;}


in C++:
#include <vector>#include <algorithm>struct card { /* ... */ };int main(){  std::vector<card> deck(52);  /* initialize */  /* ... */  /* shuffle */  std::random_shuffle( deck.begin(), deck.end() );}


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan (C programming language co-inventor)
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
quote:Original post by danatkinson
Yours is exactly what I''m looking for but I haven''t used ++''s and &&''s (nor have I heard of them) .

I''m guessing you may not have used much C/C++. Anyway, I hope that this helps.

The ++ is the C/C++ operator for incrementation. for example, given i equals 1, i++ equals 2.

The && operator is the C/C++ boolean AND comparison operator. For example, given a equals 1, and b equals 1 the following code returns true:
if(a && b)return TRUE; 

Given a equals 1, and b equals 0, the following code does not return true:
if(a && b)return TRUE; 



----------------------------------
"There are 10 kinds of people in this world, those who know binary, and those who don''t"
----------------------------------"War does not determine who is right, only who is left." -Bertrand Russell
While I''d actually just use std::random_shuffle for a real game, if you want to mimic real shuffling, thats not quite the way to do it. Most people shuffle by dividing the deck in half and interleaving the two together, or by pulling out a chunk of cards from the center and putting them on top. This is pretty easy to do in pseudo code, but I''m not into doing your homework for you. Its hardly a complicated algorithm. Just write the steps in plain English and you don''t need any ++, &&, etc.
The c2.com wiki is your friend.

This topic is closed to new replies.

Advertisement