Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ MUSHclient ➜ VBscript ➜ Cards

Cards

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by WRTIII   Canada  (76 posts)  Bio
Date Sun 12 Jan 2003 02:00 PM (UTC)
Message
Hmm ok got this so far,

Sub DealCards(deal, the, game)
Dim cards(52)
cards(1)="Ace of Hearts"
cards(2)="Ace of Spades"
cards(3)="Ace of Clubs"
cards(4)="Ace of Diamonds"
cards(5)="Two of Hearts"
cards(6)="Two of Spades"
cards(7)="Two of Clubs"
cards(8)="Two of Diamonds"
cards(9)="Three of Hearts"
cards(10)="Three of Spades"
cards(11)="Three of Clubs"
cards(12)="Three of Diamonds"
cards(13)="Four of Hearts"
cards(14)="Four of Spades"
cards(15)="Four of Clubs"
cards(16)="Four of Diamonds"
cards(17)="Five of Hearts"
cards(18)="Five of Spades"
cards(19)="Five of Clubs"
cards(20)="Five of Diamonds"
cards(21)="Six of Hearts"
cards(22)="Six of Spades"
cards(23)="Six of Clubs"
cards(24)="Six of Diamonds"
cards(25)="Seven of Hearts"
cards(26)="Seven of Spades"
cards(27)="Seven of Clubs"
cards(28)="Seven of Diamonds"
cards(29)="Eight of Hearts"
cards(30)="Eight of Spades"
cards(31)="Eight of Clubs"
cards(32)="Eight of Diamonds"
cards(33)="Nine of Hearts"
cards(34)="Nine of Spades"
cards(35)="Nine of Clubs"
cards(36)="Nine of Diamonds"
cards(37)="Ten of Hearts"
cards(38)="Ten of Spades"
cards(39)="Ten of Clubs"
cards(40)="Ten of Diamonds"
cards(41)="Jack of Hearts"
cards(42)="Jack of Spades"
cards(43)="Jack of Clubs"
cards(44)="Jack of Diamonds"
cards(45)="Queen of Hearts"
cards(46)="Queen of Spades"
cards(47)="Queen of Clubs"
cards(48)="Queen of Diamonds"
cards(49)="King of Hearts"
cards(50)="King of Spades"
cards(51)="King of Clubs"
cards(52)="King of Diamonds"
Randomize
A = Int(52 * Rnd) + 1
world.note cards(A)
End Sub

Gives me a random cards each time, Now I've been sitting here for the last two hours trying to think the rest though. I am not worried about shuffling it yet,

1. How am I going to use say 2 decks at once.. If I want two decks do I have to use a 102 card arry .. or hmm.. might work into my second question..

2. I can make it generate a random number between 1 and 52 but how do I make it so it picks a random number between 1 and 52 and does not use that number again until I "Shuffle" the deck,

and thinking of which if I was to use two decks per say.. I could just use the same array I have there but make it allow a number to be used twice before it stops it from being used..

Hope this makes sense to someone so far cause It's all sounding like mumble jumbo to me hehe
Top

Posted by Guest1   USA  (256 posts)
Date Reply #1 on Sun 12 Jan 2003 05:26 PM (UTC)
Message
dunno if this is the easiest way, buy what about having variables for each card that set to either yes or no depending whether they've been picked or not already? when a card is picked, the variable is set to yes. if you try to pick a card that has variable = yes then it picks again.
Top

Posted by Nick Gammon   Australia  (23,120 posts)  Bio   Forum Administrator
Date Reply #2 on Sun 12 Jan 2003 11:06 PM (UTC)
Message
I went through this a while ago, in another context. It is easier to do what I suggested recently, shuffle them by swapping first using the simple loop I described, that way you don' t have to remember which ones have been "dealt".

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Shadowfyr   USA  (1,788 posts)  Bio
Date Reply #3 on Sun 12 Jan 2003 11:24 PM (UTC)

Amended on Sun 12 Jan 2003 11:34 PM (UTC) by Shadowfyr

Message
Ok.. Here is what I would do.

'Place 'card' array definition here so it autoloads and is used by all subs that need it.

sub CreateDeck (Number)
  redim Deck(52 * Number) 'Since we 'redim' Deck it is dynamic and we can resize it any way we want.
  dim count, count2
  for count = 0 to Number - 1
    for count2 = 0 to 51
      Deck(count) = count2
    next
  next
  world.setvariable "Deck", join(Deck,",")
end sub

sub Shuffle (AName, Output, wilds)
  dim TDeck
  TDeck = split(world.getvariable ("Deck"), ",")
  if wilds(1) = "new" then
    if wilds(2) > 0 then
      CreateDeck (wilds(2))
    else
      'This is for reshuffling the same deck. If you don't plan to
      'allow a reshuffle 'during' play, then if can be romoved,
      'since the intent is to replace any -1 entries with the proper
      'cards.
      CreateDeck (int((ubound(TDeck) + 1) / 52))
      TDeck = split(world.getvariable ("Deck"), ",")
    end if
  end if
  dim count, rn, Temp
  randomize
  'Add a world.send "Say ..." or the like here to indicate you are shuffling.
  for count = 1 to wilds(1) 'Shuffle this many times.
    for count2 = 0 to ubound(TDeck)
      rn = int (ubound(TDeck) * Rnd) + 1
      Temp = TDeck(rn) 'Gotta love VBScript... :P Would have been nice 'if' you could use
                       'Swap TDeck(rn), TDeck(count2), but they left out the command. Sigh...
      TDeck(rn) = TDeck(count2)
      TDeck(count2) = Temp
    next
  next
  world.setvariable "Deck", join(TDeck,",")
  world.setvariable "CardPos",0
end if

sub PullACard (AName, Output, Wilds)
  Dim TDeck, CardPos
  TDeck = split(world.getvariable ("Deck"), ",")
  CardPos = world.getvariable ("CardPos")
  '...
end sub


In PullACard you use CardPos to keep track of where in the deck you are. You can then increase it each time you 'pull' a card and even drop the value you get from TDeck into a hands. Keeping track of the hands is probably quite a bit more complex, since an array like Hand(player,cards), which would be easiest to define like Dim Hand(max players, max cards), is needed, but I am not sure how well you can join and split parts of an array (or if you can??). It should be possible though to adapt things to automatically fill hands for each player this way as well, etc. The above may turn out to be the easy part. lol

If you choose to allow something like reshuffling during the game, then I would suggest placing a -1 into any used card position in the deck, then just testing for it and skipping to the next card in the deck if it contains one after a reshuffle. The Shuffle sub is already set up to handle the existance of -1s in the array by regenerating the deck. The commands would be:

Yourshufflealias new - Resets the existing deck (in case of those nasty -1s and reshuffles it.

Yourshufflealias new # - Creates and shuffles a new deck with # 52 card decks in it.

Yourshufflealias - Shuffles the existing deck

You will also need to add a warning for when you start to drop below a certain number of cards left in the deck of course. ;)

As for the card names themselves. Just use the same array you are already, but place it with the before the rest and outside of the subs, that way it is automatically created and global to all routines. You then use the number from the randomized 'Deck' to retrieve the name.

NOTE: in modern versions of basic however, you have to specify if arrays will start with 1, if you do then all of them do and it is much easier to just remember to use 0. This means you should make your cards array 0 to 51, not 1 to 52. ;) Hope this helps.
Top

Posted by Nick Gammon   Australia  (23,120 posts)  Bio   Forum Administrator
Date Reply #4 on Mon 13 Jan 2003 03:01 AM (UTC)

Amended on Mon 13 Jan 2003 03:05 AM (UTC) by Nick Gammon

Message
OK then, here is my card shuffler - it just displays the results at the end, but you could modify the last couple of lines to remove "n" cards from the pack, shuffled. (eg. 13 for a bridge hand, 10 for a 500 hand, 5 for a poker hand etc.)



Randomize  ' Initialize random-number generator.

sub ShuffleCards
dim i, j, k, m, swap, cards (52)

'
'  generate all 52 cards names in a loop
'

for i = 1 to 4     ' do all 4 suits
  select case i   ' convert suit number to name
    case 1 : k = "Spades"
    case 2 : k = "Hearts"
    case 3 : k = "Diamonds"
    case 4 : k = "Clubs"
  end select

  ' do each card in the suit
  for j = 1 to 13  ' do all 13 cards
    select case j 
      case 1  : m = "Ace"
      case 11 : m = "Jack"
      case 12 : m = "Queen"
      case 13 : m = "King"
      case else: m = cstr (j)
    end select

    cards ((i - 1) * 13 + j) = m & " of " & k 
  next  ' card in this suit

next  ' suit

world.note " ... shuffling ..."

'
'  shuffle them (swap each one with another one at random)
'
for k = 1 to 5  ' do 5 shuffle passes
  for i = 1 to 52
    j = Fix (rnd * 52) + 1
    swap = cards (i)
    cards (i) = cards (j)
    cards (j) = swap
  next  ' next card in this pass
next  ' next pass

'
'  display them
'
for i = 1 to 52
  world.note cards (i)
next

end sub
  





Here is some example output ...


... shuffling ...
9 of Spades
10 of Diamonds
8 of Spades
Jack of Spades
Queen of Spades
6 of Spades
4 of Hearts
5 of Spades
3 of Clubs
2 of Hearts
King of Hearts
Queen of Hearts
7 of Diamonds
3 of Hearts
9 of Clubs
5 of Hearts
10 of Spades
Ace of Diamonds
Queen of Diamonds
8 of Diamonds
9 of Hearts
3 of Spades
10 of Clubs
2 of Spades
6 of Hearts
4 of Diamonds
7 of Hearts
4 of Clubs
Jack of Diamonds
Queen of Clubs
8 of Clubs
9 of Diamonds
2 of Clubs
4 of Spades
Ace of Hearts
King of Spades
3 of Diamonds
Ace of Clubs
6 of Diamonds
Ace of Spades
7 of Spades
Jack of Clubs
10 of Hearts
Jack of Hearts
2 of Diamonds
7 of Clubs
5 of Clubs
King of Diamonds
6 of Clubs
5 of Diamonds
King of Clubs
8 of Hearts


- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by WRTIII   Canada  (76 posts)  Bio
Date Reply #5 on Mon 13 Jan 2003 04:11 AM (UTC)
Message
Hmm I don't follow the

Select Case
Case #
End Select

Stuff...
Top

Posted by WRTIII   Canada  (76 posts)  Bio
Date Reply #6 on Mon 13 Jan 2003 05:16 PM (UTC)
Message
Ok I either missed a point or am just kinda lost...

say I want to deal a hand of blackjack now.. I can't just do

'for i = 1 to 1
' world.note cards (i)
'next

each time it is they want a card cause I will get duplicates..

so would I do?

'for i = 1 to 52
' And Assign Each Card into an Array of 52
'next

Now use a varible to remember which card number was dealt out last, then when card 52 gets dealt it runs the shuffling sub again and assign a new 52 cards to the array.
That way I just have the sub to shuffle and put the cards in an array, than a sub for each game type to deal out the cards as needed..?
Top

Posted by WRTIII   Canada  (76 posts)  Bio
Date Reply #7 on Mon 13 Jan 2003 05:28 PM (UTC)
Message
Ick Ok what I need to know is how I make a array a variable? or something like that lol

'
' display them
'
for i = 1 to 52
shuffledcards(i) = cards(i)
next

End Sub

Sub todeal(lll, llll, lllll)

world.note shuffledcards(13)

end sub

I put the shuffled 52 cards into an array instead of displaying them, then in the next sub I can setit up to work of triggers and the such for lets say blackjack
If they say hitme I give them another card and the varible of shuffledcards changes +1 meaning another card that is out of play until it gets to 52 then I run the shuffling script again..

Hmm... Can I have a sub within a sub? Cause I am going to need subs for hit, doubledown, stand, split etc. or will I just need a milllion single subs for all my games..

Or is there a way to say have all my Blackjack subs in whole .vbs file and all my poker ones in another, then when a person choose the game to play it refers to the correct file?

Top

Posted by Shadowfyr   USA  (1,788 posts)  Bio
Date Reply #8 on Mon 13 Jan 2003 09:51 PM (UTC)
Message
As I stated.. My version allows for a wide range of versitility. The mechanics of the game and how to keep track of the cards in the players hand I left up to someone else, but creating a deck and randomizing the cards in it is something it can already handle. It also uses the array of card names you proposed at the start, but in a slightly different way that you had planned.

Nick, not to be a pill or anything, but keeping track of the actual names in the deck is never a particularly efficient way to do things. It is even less so if you are dealing with 3-4 decks. Mine will handle any number of decks, can maintain the game even if something causes mushclient to crash and only leaves out the hand management you need to keep track of what each player has, something that is likely to differ from game to game anyway.
Top

Posted by Nick Gammon   Australia  (23,120 posts)  Bio   Forum Administrator
Date Reply #9 on Mon 13 Jan 2003 09:51 PM (UTC)
Message
You can't put a sub inside a sub, but you can put variables *outside* a sub. Thus you might have one function that simply shuffles the pack (basically 90% of my sub) however put the cards array outside. eg.


dim cards (52)
Randomize ' Initialize random-number generator.

sub ShuffleCards
dim i, j, k, m, swap

'
' generate all 52 cards names in a loop
'
... rest the same ...


Now to "deal" a card simply have another variable (eg. nextcard) that is also outside the sub. When you shuffle set that to 1. Then to deal a card you would do this ...


world.note cards (nextcard) ' deal next random card
nextcard = nextcard + 1 ' get another one next time
if nextcard > 52 then ' used them all - reshuffle
ShuffleCards
end if


I don't see why you would need a million subs for a blackjack game - just think carefully and you should be able to code it fairly simply. I think there were Blackjack games written in Basic in the 1970s, see if you can dig up one of those as an example.

As for "select" here is an example:


select case i   ' convert suit number to name
    case 1 : k = "Spades"
    case 2 : k = "Hearts"
    case 3 : k = "Diamonds"
    case 4 : k = "Clubs"
  end select


This is the same as (but easier to read) ...


if i = 1 then k = "Spades"
else if i = 2 then k = "Hearts"
else if i = 3 then k = "Diamonds"
else if i = 4 then k = "Clubs"


The select case has a "selector" (in this case the variable i) which is compared to the numbers for each "case" and if it matches then the statement on that line is executed.

Using the "if" approach you have to repeat "if i = " for each line which looks messy.




- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


28,663 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.