mIRC Homepage
Posted By: Longspeak Card Drawing Script? - 03/05/05 04:56 PM
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
Posted By: Riamus2 Re: Card Drawing Script? - 03/05/05 05:13 PM
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
Posted By: Kelder Re: Card Drawing Script? - 03/05/05 07:50 PM
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.
Posted By: Riamus2 Re: Card Drawing Script? - 03/05/05 08:20 PM
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. smile

Hash tables are useful to know anyhow. Hehe.
Posted By: FiberOPtics Re: Card Drawing Script? - 03/05/05 08:25 PM
Quote:
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
Posted By: Kelder Re: Card Drawing Script? - 03/05/05 08:48 PM
aww, you're right blush
Let's blame it on fast scripting due to some sports event starting smile

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)?
Posted By: FiberOPtics Re: Card Drawing Script? - 03/05/05 08:55 PM
Quote:
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.
Posted By: mIRCManiac Re: Card Drawing Script? - 03/05/05 09:25 PM
ditto
© mIRC Discussion Forums