Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / Storing random (specific, though) data in an array.

Author
Message
Sinani201
19
Years of Service
User Offline
Joined: 16th Apr 2007
Location: Aperture Science Enrichment Center
Posted: 15th May 2007 03:17
I'm making a Textas (Textas) Hold'em game entirely with text, just for a practice of arrays and computers making random (but smart) moves. However, one slight problem is that I need to put random playing cards in the deck array and make it shuffle every new hand. Is this possible?

What happens when when you mix coke, pop rocks, vinegar, and baking soda?
I am currently working on 2 projects: "Slot Machine Time!" and "Shoot 'em all up"
zenassem
23
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 15th May 2007 04:00 Edited at: 15th May 2007 04:08
Just keep an array of all the cards (which I'm sure you already have). To shuffle, generate a random number[1-52 or 0-51 depending on if you use the 0 as the first element of your arrays]. The random number represents the index to your card array. Now place that number on a stack data structure. The stack will be used to select each card during play. Keep getting random number you have all 52 card indexes.

*Be sure to seed the random number generator [The rnd() function] by placing the line

randomize timer()

at the beginning of your shuffle loop, or the start of the game.


Now you need to decide how you want to handle duplicate results from the random generator. I used a "brute force" method before and it really doesn't take to much time. Just keep track of the numbers already selected and if the rnd() function returns an index already selected, code an if condtion that looks for the closest "lowest" index number not selected. If there is no "lower" number have it either check for "higher" values or just have the search wrap around to the higher numbers.

There are other methods that are more efficient or complex, but I find the above works well enough that it's a non-issue.

TDK
Retired Moderator
23
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 15th May 2007 08:06
Sinani201:

Sorry to be blunt, but you keep asking questions that are covered in great detail in the tutorials at the top of the board - or can be found very easily with a search.

If you only read them, most of your questions wouldn't need asking and people like zenassem wouldn't have to waste their time repeating what's already been said many times before.

IF you have any problems after reading a tutorial, then by all means come back and ask for further info.

Quote: "7. Everything You Wanted To Know About Strings
Pretty much all you need to know about using strings in your DB programs. From cutting them up and joining them back together again to shuffling and dealing a pack of cards - it's all here!"

http://forum.thegamecreators.com/?m=forum_view&t=99497&b=10

I've also covered it here:

http://forum.thegamecreators.com/?m=forum_view&t=58901&b=7

...and dozens of others which showed up when I searched for the word 'cards'.

TDK_Man

Sinani201
19
Years of Service
User Offline
Joined: 16th Apr 2007
Location: Aperture Science Enrichment Center
Posted: 16th May 2007 02:12
TDK,
I see that you are mad about questions being asked multiple times. I'll use the search tool, then.

Aren't I full of questions?
What happens when when you mix coke, pop rocks, vinegar, and baking soda?
TDK
Retired Moderator
23
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 18th May 2007 04:29
No - not mad...

But it can be a bit annoying when you tell someone they would benefit from reading some tutorials, supply a link to them, and then a few days later they ask if something is possible that is actually covered in those tutorials...

99 times out of 100, using the search will point you to the answer to your problem far quicker than waiting for someone to answer - especially when they are probably like me, only going to moan at you for not using the search!

TDK_Man

Login to post a reply

Server time is: 2026-07-07 12:27:41
Your offset time is: 2026-07-07 12:27:41