Archived

This topic is now archived and is closed to further replies.

danatkinson

Pseudocode for card shuffling game

Recommended Posts

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

Share this post


Link to post
Share on other sites
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 shuffles
While the deck is being shuffled
Take bottom x cards
Move cards to the top of the deck
Is deck shuffled?
yes: Stop shuffling
no: reshuffle



SketchSoft OFFLINE | SketchNews OFFLINE | NewKlear Studios

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
ok,

card array[52];
vector stack1;
vector stack2;
while(amount of shuffles)
for i = 0 to 52
if(rand() & 1)
stack1.push_back(array[i ])
else
stack2.push_back(array[i ])
next i

i=0

while(stack1.empty() && stack2.empty() == false)
if(stack1.empty() == true)
array[i ] = stack2.at(0)
stack2.erase(0)
i++
continue
else if(stack2.empty() == true)
array[i ] = stack1.at(0)
stack1.erase(0)
i++
continue
endif

if(rand() && 1)
array[i ] = stack2.at(0)
stack2.erase(0)
i++
continue
else
array[i ] = stack1.at(0)
stack1.erase(0)
i++
endif
loop
loop

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.

Look off of it, however, and I think you'll be able to create your own verson.

~~~~~
"One Reality is worth one thousand dreams"
Download and play Slime King I.


[edited by - dede on January 15, 2004 12:32:55 PM]

[edited by - dede on January 15, 2004 12:33:22 PM]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
The typical shuffling algorithm goes as follow:

Let D be a deck of cards
Let S be the deck size
for 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)

Share this post


Link to post
Share on other sites
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"

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites