|
Joined: May 2005
Posts: 6
Nutrimatic drinks dispenser
|
OP
Nutrimatic drinks dispenser
Joined: May 2005
Posts: 6 |
I'm writing a script to draw cards. The basics are easy, although any suggestions as to how I can do it without a lengthy series of IF statements would be helpful.
What I REALLY need help with is the memory. I want the script to remember, until I tell it to reset, what cards have been drawn previously.
It's not playing cards, but playing cards make a good example. If the Ace of Spades is drawn, I want the script to remember that the next time it is called, and have no chance of drawing another Ace of Spades. When I enter the proper command, I want the deck to "shuffle", making all the cards available again.
Is this possible?
Longspeak
|
|
|
|
Joined: Oct 2004
Posts: 8,330
Hoopy frood
|
Hoopy frood
Joined: Oct 2004
Posts: 8,330 |
Hash tables are the easiest way to handle cards. Simply fill the hash table with the cards, then draw them randomly and remove them from the table. When the table is empty, refill it. It can be done with any type of cards.
/help /hmake /help /hadd /help $hget
Invision Support #Invision on irc.irchighway.net
|
|
|
|
Joined: Apr 2003
Posts: 701
Hoopy frood
|
Hoopy frood
Joined: Apr 2003
Posts: 701 |
Another way could be: alias shuffle { set %deck var %i = 4 while (%i) { var %suit = $gettok(H.D.S.C,%i,46) var %j = 13 while (%j) var %num = $gettok(1.2.3.4.5.6.7.8.9.10.J.Q.K,%j,46) var %deck = $addtok(%deck,$+(%suit,%num),46) dec %j } dec %i } } -> results in %deck having all cards
Then use this to draw and remove a card: alias drawcard { if (!$numtok(%deck,46)) return ERROR: no cards left! var %rn = $rand(1,$v1) var %card = $gettok(%deck,%rn,46) set %deck $deltok(%deck,%rn,46) return %card }
If the length of all items plus the . inbetween them is larger than about 900 characters, it will fail.
|
|
|
|
Joined: Oct 2004
Posts: 8,330
Hoopy frood
|
Hoopy frood
Joined: Oct 2004
Posts: 8,330 |
Except he doesn't want it for a standard playing deck. Still, you can use that method as well. I personally use hash tables for decks as it is an easy way to manage the deck and find out the number of cards left and sort them as needed and such. Using that method helps when you start making card games that require more than just dealing cards or drawing cards. An example would be a game where you want to have the ability to look through the deck for a specific card and then shuffle it. That could be done with the other way as well, but it starts getting complicated once you start doing that. So, I stick to hash tables so that I don't have to change how I handle decks if I need more control over them. Hash tables are useful to know anyhow. Hehe.
Invision Support #Invision on irc.irchighway.net
|
|
|
|
Joined: Feb 2004
Posts: 2,019
Hoopy frood
|
Hoopy frood
Joined: Feb 2004
Posts: 2,019 |
if (!$numtok(%deck,46)) return ERROR: no cards left! var %rn = $rand(1,$v1) Little note, $v1 will be 0 if there were tokens left to check, which creates the range 1,0 for rand to pick out. As an example: //if !$numtok(foo bar,32) { } | echo -a v1: $v1
Gone.
|
|
|
|
Joined: Apr 2003
Posts: 701
Hoopy frood
|
Hoopy frood
Joined: Apr 2003
Posts: 701 |
aww, you're right Let's blame it on fast scripting due to some sports event starting fix: if ($numtok(%deck,46) == 0) return ERROR: no cards left! var %rn = $rand(1,$v1) ps: would other people also like $rand(1,0) to return 0/$null instead of $rand(0,1)?
|
|
|
|
Joined: Feb 2004
Posts: 2,019
Hoopy frood
|
Hoopy frood
Joined: Feb 2004
Posts: 2,019 |
ps: would other people also like $rand(1,0) to return 0/$null instead of $rand(0,1)? Nope, since the parameters relative location should not matter imo. $rand takes the random between two characters. In the case of numbers, I don't think the first number should be the lowest one.
Gone.
|
|
|
|
Joined: Feb 2005
Posts: 681
Fjord artisan
|
Fjord artisan
Joined: Feb 2005
Posts: 681 |
|
|
|
|
|