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?
Card Game Code
-
- Posts: 23
- Joined: Sat Jun 12, 2010 3:31 am
Re: Card Game Code
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.)
- Stryker, AKA Jordan
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.)
- Stryker, AKA Jordan
-
- Site Admin
- Posts: 1986
- Joined: Tue Aug 09, 2005 3:21 am
- Location: UK
- Contact:
Re: Card Game Code
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.
Look at the link fcg posted, it gives two good algorithms for precisely this scenario, the second of which is incredibly simple.
Some simple code to illustratewikipedia 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.
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;
}
-
- Posts: 23
- Joined: Sat Jun 12, 2010 3:31 am
Re: Card Game Code
Might I ask why? I find it easier and more convenient. And it's Random.org, so it's safe.WinterMute wrote:]...using a web page to generate data for a DS game seems like a rather peculiar thing to do.
Just a thought, though, I'm no expert.
- Stryker, AKA Jordan
-
- Site Admin
- Posts: 1986
- Joined: Tue Aug 09, 2005 3:21 am
- Location: UK
- Contact:
Re: Card Game Code
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.
For most normal game randomness the libc functions are more than adequate, you really don't need cryptographically secure randomness.
Re: Card Game Code
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.
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.
Who is online
Users browsing this forum: No registered users and 0 guests