mIRC Home    About    Download    Register    News    Help

Print Thread
#139880 19/01/06 08:18 AM
Joined: Mar 2005
Posts: 212
N
Fjord artisan
OP Offline
Fjord artisan
N
Joined: Mar 2005
Posts: 212
this will be hard to explain so bear with me
i want to use $rand to generate a random number
and then store that random number in a variable to be called later
how can this be done

#139881 19/01/06 08:32 AM
Joined: Jan 2003
Posts: 1,063
D
Hoopy frood
Offline
Hoopy frood
D
Joined: Jan 2003
Posts: 1,063
Code:
$rand(v1,v2)
This works in two ways. If you supply it with numbers for v1 and v2, it returns a random number between v1 and v2. If you supply it with letters, it returns a random letter between letters v1 and v2.

$rand(a,z)   returns a letter in the range a,b,c,...,z
$rand(A,Z)  returns a letter in the range A,B,C,...,Z
$rand(0,N)  returns a number in the range 0,1,2,...,N


which results in something like:

/set %myFirstVariable $rand(0,100)

that will set a variable called 'myFirstVariable' to a random value between 0 and 100


If it ain't broken, don't fix it!
#139882 19/01/06 08:41 AM
Joined: Mar 2005
Posts: 212
N
Fjord artisan
OP Offline
Fjord artisan
N
Joined: Mar 2005
Posts: 212
and when u call the variable you will get $rand(0,100)
and if you force a second evaluation you will get an entirely different randomly generated number than you did the first time thereby making it impossible to call the original number later.

i wasnt thorough enough in explaining what i wanted so you mistook it for a lack of effort on my part
thats my fault
let me try again

i want to randomly generate a number and then store that randomly generated number in a variable and then be able to call that variable and have it return that exact same randomly generated number

im trying to make a script that deals cards and once a cards dealt we cant very well have it turning up again in the same hand

Last edited by NeUtRoN_StaR; 19/01/06 08:45 AM.
#139883 19/01/06 09:43 AM
Joined: Aug 2004
Posts: 7,252
R
Hoopy frood
Offline
Hoopy frood
R
Joined: Aug 2004
Posts: 7,252
Answer for Intial question: set %var = $r(1,52)

More detailed possibility, per example given
2 hands, 5 cards each
Code:
 var %card = 1
while %card <= 5 {
  var %hand = 1
  while %hand <= 2 {
    while $istok(%deck,%draw,32) || !%draw {
      var %draw = $r(1,52)
      set %deck $addtok(%deck,%draw,32)
    }
    set $+(%,hand.,%hand) $addtok($($+(%,hand.,%hand),2),%draw,32)
    inc %hand
  }
  inc %card
}
 

#139884 19/01/06 05:49 PM
Joined: Oct 2004
Posts: 8,330
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,330
Personally, I think using hash tables for cards is much easier. You can easily remove the cards from the hash table as they are used. Then, you can reshuffle by just reloading the table. Quick, easy, efficient. laugh

If you're using a normal $rand like was mentioned in the first response, you can also just keep adding to the variable:

set %cardsused %cardsused $rand(1,52)

The results would be (for 3 "draws"):
5
5 8
5 8 51

You can then just do a check on the variable to see what numbers are used (use $istok to see if the number was used). smile

Still, RusselB's answer should work just fine as well (I can't test and didn't read through it, but he's usually right). laugh Just letting you know some other methods.


Invision Support
#Invision on irc.irchighway.net
#139885 19/01/06 07:05 PM
Joined: Mar 2005
Posts: 212
N
Fjord artisan
OP Offline
Fjord artisan
N
Joined: Mar 2005
Posts: 212
thats what i was doing
i did a quick sloppy bench test that just used $hget(cards,$rand(1,52)).data
to see if i could let the cards appearing more than once slide but i found the incidence of a card showing up more than once was pretty high

and ive just been struggling with a way to restrict that

ive also been having a hell of a time coming up with the way it will compare hands against each other and so that sort of thing

im going to have to give it criteria to recognize a hand rather than a list of all possible hands to check against since the list would contain about 2.3 million entries

im in between classes atm
but i was going to study russ' code for a bit later and try and incorporate it into what i was already using

Last edited by NeUtRoN_StaR; 19/01/06 07:16 PM.
#139886 19/01/06 07:15 PM
Joined: Mar 2005
Posts: 212
N
Fjord artisan
OP Offline
Fjord artisan
N
Joined: Mar 2005
Posts: 212
so .... how would we deal a card and then have it show up in %cards.used

can you give me an example because the way im still seeing it the number generate by rand for the deal and then the addition to %cards.used would be two different numbers

as you can see im having a hell of a time with this
and im just starting

i just cant think of a way to randomly deal a card from the hash table and then keep track of what item number that it was to check for it or remove it later

show me an example if you can

#139887 19/01/06 08:10 PM
Joined: Dec 2002
Posts: 1,245
M
Hoopy frood
Offline
Hoopy frood
M
Joined: Dec 2002
Posts: 1,245
this might be kind of crude, but I think it does what you want
Code:
alias deal {
  var %cardplayer = $r(1,52)
  cardcheck %cardplayer
}
alias cardcheck {
  var %cardplayer = $1
  if (%cardplayer isin %cardsused) { deal | return }
  if (%cardplayer !isin %cardsused) { set %cardsused %cardsused %cardplayer }
  ;for testing echo to status
  echo card is %cardplayer of %cardsused
  ;whatever other code
} 

#139888 19/01/06 08:11 PM
Joined: Dec 2002
Posts: 1,245
M
Hoopy frood
Offline
Hoopy frood
M
Joined: Dec 2002
Posts: 1,245
Riamus2
Welcome back, all moved in and organized?

#139889 19/01/06 08:32 PM
Joined: Oct 2004
Posts: 8,330
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,330
Yes. I've been moved in for 2 months, but didn't have the money to get online again. I'll actually have internet at home starting Monday. I'm just using this from work for now. Thanks for the welcome back. smile

Now, back to the questions at hand...

MikeChat, you can't use isin because "1" isin "15" and such... you'll need $istok. Still the same idea as what you coded above. I don't have mIRC here and it's been 2 months since I've used it, so I can't recall the exact $istok format, so I won't give an example. Sorry. smile

As for the hash tables... (if there is anything coded incorrectly, remember it's been 2 months since I've scripted and I can't view the format from here to check it... just ask if something isn't working or mention what's coded wrong):
Code:
alias ShuffleDeck {
  hmake Deck 5
  hadd Deck 1 AH
  hadd Deck 2 2H
  hadd Deck 3 3H
  [color:red]...etc...[/color]
}

alias MakeHands {
  hmake Hand1 1
  hmake Hand2 1
}

alias Draw {
  if ($hget(Deck,0) == 0) { ShuffleDeck }
  var %card = $rand(1,$hget(Deck,0))
  hadd Hand $+ $1 $hget(Deck,%card)
  hdel Deck %card
}


Use Draw with the hand number who is drawing the card:

/draw 1
/draw 2
etc.

That should get you started. Note that you may also want to add in a check to make sure the deck and the hand hash tables are created to prevent errors. And, right now, it will reshuffle a FULL deck. You'll want to change that part to have it remove the cards in each hand after shuffling. This should give you a general idea, though. I don't have time to write a full card drawing script right now. smile


Invision Support
#Invision on irc.irchighway.net
#139890 19/01/06 09:00 PM
Joined: Sep 2003
Posts: 4,230
D
Hoopy frood
Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
Depending on what ya doing you could move away from a hashtable all together, you have a fixed set of cards which isnt overly large (52) so you can just use a string.

Code:
  var %deck = SAce,S2,S3,S4,S5,S6,S7,S8,S9,S10,SJack,SQueen,SKing,CAce,C2,C3,C4,C5,C6,C7,C8,C9,C10,CJack,CQueen,CKing,DAce,D2,D3,D4,D5,D6,D7,D8,D9,D10,DJack,DQueen,DKing,HAce,H2,H3,H4,H5,H6,H7,H8,H9,H10,HJack,HQueen,HKing
  var %shuffled.deck, %i = 52
  while (%i) {
    var %n = $rand(1,$numtok(%Deck,44))
    var %shuffled.deck = $addtok(%shuffled.deck,$gettok(%deck,%n,44),44)
    var %deck = $deltok(%deck,%n,44)
    dec %i
  }


Result is %shuffled.deck being 52 random ordered cards comma seperated with cards being tokens that are <suit><card>. <suit> being SCDH and <card> being ace,1-10,jack,queen,king

This was only an example, as your need might better suit using SA,S2,S3...,S9,ST,SJ,SQ,SK or some such setup using 2 charachetr comma seperated values u could shuffle up to 6 fuill decks

Using even tighter compacting asc values (101 to 152*, representing the 52 cards) , you can shuffle up to 18 decks, using $mid() etc rather than tokenizers to access them.
(i used 101 to 152 here becuase 32 [space] well not attach to a string with out some oddities occuring)

or go mentally high using a binary var.

I beleive 21 (black jack) uses multiple decks like this.

#139891 19/01/06 09:31 PM
Joined: Mar 2005
Posts: 212
N
Fjord artisan
OP Offline
Fjord artisan
N
Joined: Mar 2005
Posts: 212
the way im generating the table since i thought
doing 52 hadds would be in bad taste

on *:start:{
if ($isfile(cards.hsh)) {
hmake cards 6
hload -o cards cards.hsh
}
else {
hmake cards 6
var %card.install = 1
while ($read(cards.txt,%card.install)) {
hadd cards %card.install $read(cards.txt,%card.install)
inc %card.install
}
}
}

contents of cards.txt
A-S
A-C
A-H
A-D
K-S
K-C
K-H
K-D
Q-S
Q-C
Q-H
Q-D
J-S
J-C
J-H
J-D
10-S
10-C
10-H
10-D
9-S
9-C
9-H
9-D
8-S
8-C
8-H
8-D
7-S
7-C
7-H
7-D
6-S
6-C
6-H
6-D
5-S
5-C
5-H
5-D
4-S
4-C
4-H
4-D
3-S
3-C
3-H
3-D
2-S
2-C
2-H
2-D

#139892 19/01/06 10:07 PM
Joined: Oct 2004
Posts: 8,330
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,330
Heh. It is. laugh

I just used that to show how to do it. I actually do it with two nested loops...

Code:
var %rank = 1
while (%rank &lt;= 13) {
  var %suit = 1
  while (%suit &lt;= 4) {
    hadd Deck $replacex(%rank,10,10,11,J,12,Q,13,K,1,A) $+ $replace(%suit,1,C,2,S,3,H,4,D)
    hinc %suit
  }
  hinc %rank
}

(note that this code works only in 6.16+ as it uses $replacex... to use it in earlier versions, the $replacex would have to be edited to work properly using $replace or another method)

If I'm displaying them in the channel, I also add in control codes to color the cards right into the hash table (black/red). I think I have this written better in my own code, but I don't have that in front of me to look at and as I've said, it's been awhile. laugh

Of course, you can always load the hash whenever you need to if you have it saved and then just have a code in there to generate it if it's not there. Much faster that way. smile

Also, as DaveC pointed out, you can also just do it with tokens like he displayed. Either way will work just fine. I prefer the tables just because you can easily convert it for any card game you want (extra decks, smaller decks like 9-A, etc) by just changing the while loops. Of course, that's really only helpful to you if you're making a lot of card games. laugh


Invision Support
#Invision on irc.irchighway.net
#139893 20/01/06 12:21 AM
Joined: Mar 2005
Posts: 212
N
Fjord artisan
OP Offline
Fjord artisan
N
Joined: Mar 2005
Posts: 212
i thre together a dealing thing based on the remove from the hash table idea

i really didnt need to be confused this whole time i was just testing the wrong way

but having it remove an item from the table after its dealt and then go back randomly and try to find an item that doesnt return null obviously

makes for some long gaps after youve exausted 20 or 30 cards

i dont know im getting ready to give up on this idea i think its a bit to far over my head

alias deal {
set %cnum 1
var %cards = $hget(cards,$rand(1,$hget(cards,0).item))
if (%cards == $null) {
}
elseif (%cnum <= 5) {
msg $chan %cards
inc %cnum
var %dis = $hfind(cards,%cards).data
hdel cards %dis
deal
}
}
^ is the bit i was tinkering with it doesnt have a reshuffle feature in it i think the tokens might be the way to go because then you can shrink the pool it draws from as the choice become fewer rather than having it draw blanks constantly

#139894 20/01/06 12:23 AM
Joined: Mar 2005
Posts: 212
N
Fjord artisan
OP Offline
Fjord artisan
N
Joined: Mar 2005
Posts: 212
feel like making a hold'em bot wink

#139895 20/01/06 04:09 AM
Joined: Sep 2003
Posts: 4,230
D
Hoopy frood
Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
Code:
;
;assumes a hashtable "cards" has been loaded with itemnames 1 to 52, each holding a card name ie: 1 = AS, 2 = AC, 3 = AH, 4 = AD, 5 = 2S etc etc
;
alias deal.card {
  if ($hget(cards,0).item) {
    var %n = $rand(1,$hget(cards,0).item)
    var %r = $hget(cards,$hget(cards,%n).item)
    hdel cards $hget(cards,%n).item
    return %r
  }
}

* untested coded *

it gets called as $deal.card and that comes back with $null for no deck, or a card like "6D" for 6 dimonds, see note on hashtable it needs.
What it does is.. checks if there are cards left, and if so, then generates a randoim number between 1 and the number of cards left (%n), then it saves in %r the contents of the selected card, understand that here the %n refers to the position in the pack of the Nth card, not the card numbers 1 to 52 themselfs. It next deletes that card from the pack, and lastely returns the card contents.

#139896 20/01/06 07:13 PM
Joined: Oct 2004
Posts: 8,330
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,330
DaveC's code should do what you need for the card dealing. I see that I forgot to include .item on the $hget when doing my checks of total cards remaining. It's been too long... frown

Anyhow, I think you were missing the idea that the hash table, after deleting a card, will not need to be checked to see if a card is gone. Think of the hash table like a deck of cards... you take one out and you don't have to then go back into the deck and see what card was removed... it's gone from the deck, so it won't be dealt again.

As for making a Hold'em bot... probably not anytime soon. I'm working on a couple other games right now (at least, I was before going offline for 2 months). I need to complete those first... Scattergories and Cribbage.


Invision Support
#Invision on irc.irchighway.net

Link Copied to Clipboard