Card Game Code

Posts that don't fit in the other forums
Post Reply
Greensprings
Posts: 1
Joined: Thu Dec 23, 2010 11:43 pm

Card Game Code

Post by Greensprings » Fri Dec 24, 2010 12:02 am

Hi,
I am new to NDS programming

I am going to try to create a card game engine for the NDS

My question is how do you simulate shuffling a deck of cards?

I was thinking that I would create a 4x13 matrix, then populate it with random numbers from 1 to 52

then the position of the number in the 4x13 matrix would identify the card, and then the 1x52 matrix would represent the deck?

as my linear algebra is rusty, is there a way to do this using mathematics, or would I have to write a program to
1 generate a random number from 1-52
2 check the matrix to see if the number has already been used
3 load the number into the matrix and move to the next location?

What do you think, anyone know of anyone who has been down this road before?

fcg
Posts: 16
Joined: Wed Nov 24, 2010 10:51 pm

Re: Card Game Code

Post by fcg » Fri Dec 24, 2010 9:26 pm


Jordan_Gray
Posts: 23
Joined: Sat Jun 12, 2010 3:31 am

Re: Card Game Code

Post by Jordan_Gray » Wed Dec 29, 2010 3:40 am

That sounds like a fun project.

But wouldn't it be a 13x4 matrix?

But, to get back on track, you could really just create some random decks and then have the DS pull them up. For example, you could go to random.org, then use the playing card shuffler to generate a random deck. (Reload the page a few times, or open in new tabs/windows to get new decks.)

I've used it before and it's ridiculously helpful. Hope it helps you too! (This way you don't have to use as much algebra.:lol:)

- Stryker, AKA Jordan

WinterMute
Site Admin
Posts: 1845
Joined: Tue Aug 09, 2005 3:21 am
Location: UK
Contact:

Re: Card Game Code

Post by WinterMute » Wed Dec 29, 2010 12:46 pm

No, it would be either a 52 element array or a 52 element list, there's zero need to have a dimension for suits regardless of row vs column ordering. There isn't even any algebra involved and using a web page to generate data for a DS game seems like a rather peculiar thing to do.

Look at the link fcg posted, it gives two good algorithms for precisely this scenario, the second of which is incredibly simple.
wikipedia wrote: The second, generally known as the Knuth shuffle or Fisher–Yates shuffle, is a linear-time algorithm which involves moving through the pack from top to bottom, swapping each card in turn with another card from a random position in the part of the pack that has not yet been passed through (including itself). Providing that the random numbers are unbiased, this will always generate a random permutation.
Some simple code to illustrate

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, char **argv) {
	int i, swapcard, tmp, suit, card;
	int deck[52];
	
	srand(time(NULL));

	for (i = 0; i < 52; i++) deck[i] = i;

	for (i = 0; i < 51; i++) {
		swapcard = (rand() % (52 - i - 1)) + i + 1;
		tmp = deck[i];
		deck[i] = deck[swapcard];
		deck[swapcard] = tmp;
	}
	
	const char *suitname[] = { "Hearts", "Clubs", "Diamonds", "Spades"};
	const char *cardname[] = { "Ace", "Two", "Three", "Four",
	 						"Five", "Six", "Seven", "Eight",
	 						"Nine", "Ten", "Jack", "Queen", "King"};
	
	for (i = 0; i < 52; i++ ) {
		suit = deck[i] / 13;
		card = deck[i] % 13;
		
		printf("%s of %s\n", cardname[card], suitname[suit]);
	}
	return 0;
}
Help keep devkitPro toolchains free, Donate today

Personal Blog

Jordan_Gray
Posts: 23
Joined: Sat Jun 12, 2010 3:31 am

Re: Card Game Code

Post by Jordan_Gray » Wed Dec 29, 2010 11:33 pm

WinterMute wrote:]...using a web page to generate data for a DS game seems like a rather peculiar thing to do.
Might I ask why? I find it easier and more convenient. And it's Random.org, so it's safe.

Just a thought, though, I'm no expert.

- Stryker, AKA Jordan

WinterMute
Site Admin
Posts: 1845
Joined: Tue Aug 09, 2005 3:21 am
Location: UK
Contact:

Re: Card Game Code

Post by WinterMute » Thu Dec 30, 2010 11:16 pm

You can't assume that the end user will have internet access on his DS so reading data from a website during gameplay is out. Then, even if there is net access available, what happens if the site is inaccessible?

For most normal game randomness the libc functions are more than adequate, you really don't need cryptographically secure randomness.
Help keep devkitPro toolchains free, Donate today

Personal Blog

User avatar
Izhido
Posts: 107
Joined: Fri Oct 19, 2007 10:57 pm
Location: Costa Rica
Contact:

Re: Card Game Code

Post by Izhido » Mon Jan 03, 2011 6:07 pm

Also, if for some reason you simply can't stand libc randomness, there are a few alternatives. One of my apps uses a freely available implementation of the Mersenne Twister algorithm. The sources are here: http://dsotaku.drunkencoders.com/Bitswe ... er.src.zip .

What's that you say? This is only a shoddy excuse to promote my DS game? Sir, I have no idea what you're talking about. :D

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests