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 / random counter single numbers card game.

Author
Message
HomerS
16
Years of Service
User Offline
Joined: 8th Apr 2007
Location:
Posted: 3rd Nov 2012 13:51
HEllow

i made a random couter for a card game.

How can i put this in a function?

like the 10 numbers need to be stored in a variable.

see the code also in CODEBASE


LBFN
16
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 3rd Nov 2012 19:48
Hi HomerS,

I wrote some code to shuffle a 52 card deck and you can pull ten cards out of it at a time and wrote a few functions with it. Even though this may not be exactly what you are looking for, hopefully it will help.



It uses manually SYNCing, as it shows up better on-screen. I noticed I joined these forums the day before you did.

So many games to code.....so little time.
HomerS
16
Years of Service
User Offline
Joined: 8th Apr 2007
Location:
Posted: 3rd Nov 2012 22:21
Hi LBFN (stands for?)

Thanks for your help. I van learn a lot here. I have DBC a long time and like a lot of people thinking of making games but it never gets to the end of programming a complete game. Lack of motivation to go on on errors for me then.

I am trying to make a "simple" game like memory, just for fun. So i have 2 x 24 cards. So I use all the cards that I have. Now i use gosub, that works fine, but try to get this piece of code in a function.

I wil make it 3d and skip the 3d animations of the cards rotating etc. For me it is the trick to make AND finish a game that works. Try to finish it befor i am 65 years! lol.

When it works, i will pimp it up with the animations.


It cost me a lot of work, just get the right (free) 3d program to make the cards (uv-mapping). So most time is in learning.

In wings 3d I made a card. The UV shows good, but in DBC it seems to be mirrord (not a big problem at the moment).


Greetings!

Toedeledoki
Libervurto
17
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 3rd Nov 2012 22:21 Edited at: 3rd Nov 2012 22:43
Hi Homer,
I've given you some tips and changed a few things around.



Since arrays are global in DBC you could simply wrap the whole thing up in function tags to turn it into a function, but I think we could optimise the code before doing that.

There's an issue with the code in that choosing a card is "in theory" a potential infinite loop! That's because we're choosing a random number and then checking if we've already had that number, so in theory we could pick 4 first and then on the next card we might continue picking 4 for eternity! Of course that's not really going to happen but the problem remains that picking cards is slowed down by the possibility of duplicates.
Just for a demonstration I'm going to increase the deck to 2000 cards!

There's now a few seconds delay before the routine finishes.

So how can we remove the chance of duplicates and speed this up? Well we need a way to remove a card from the possible choices once it has been chosen; we're essentially giving the cards an identity. A simple way to do this is to make an array for the cards (like a deck) and check off the ones that have been selected (removed from the deck), then when we pick a new card we cycle through the deck to find one that hasn't been taken already.


Now it's much faster! But hang on a second, we can see from the output that all we're really doing is shuffling the numbers so isn't there a better way of achieving that without randomly picking cards? Yes let's shuffle the deck then the cards will already be in a random order when we deal them!

So what happens when we shuffle cards? We swap the position of two randomly chosen cards in the deck, and we repeat the process until we're happy the deck is randomized. First we need to figure out how to pick two cards. We could do this as we've done before but there's a simpler and cleverer way to do it.

If we take one card out of 20 (1 to 20 for simplicity but we'll actually use 0 to 19 in the program) that leaves 19 cards remaining; so our next selection will be out of 19 cards. But if the first card picked isn't 20 we might accidentally choose the same card again , but here's the clever part: if we remember which card we picked first we can simply count on from it to find the second card and if we go over the maximum amount of cards we simply wrap back around to the start, so it's impossible to pick the same card again! (This counting doesn't waste time like in the previous code example because we're not actually testing anything we're just adding two values together.) Then we just swap the values at those two positions in the array and hey presto, our first swap is complete!


So there you go. Now you've learned some neat tricks with arrays and how to manipulate them. Hope that helps.

Shh... you're pretty.
HomerS
16
Years of Service
User Offline
Joined: 8th Apr 2007
Location:
Posted: 3rd Nov 2012 22:29
Hi OBase87,

Thanks for you help!

Quote: "
I removed the line "read CARD(r)"
READ is not used in this way, it is used to read data from data statements and store in the given variable.
Since you have no data statements I'm not sure what happens but it's not good.
"


I used read becouse the database is empty and gives zero (0) as a value. I let de code compare each variable if there is a nuber already given to the card variable.

So the read option works with me.



Greetings!

Toedeledoki
Silverman
17
Years of Service
User Offline
Joined: 18th Jan 2007
Location: France
Posted: 4th Nov 2012 14:33
hi HomerS,

here a small tutorial; what you need to know about the functions:



DirectX 9.0c (February 2010)/ DBClassic v1.20

Login to post a reply

Server time is: 2024-03-29 14:16:30
Your offset time is: 2024-03-29 14:16:30