Blackjack MiniGame (VB6)

Started by
2 comments, last by gp_s 16 years, 1 month ago
OK, I'm on making a game in VB6, I'm going to be adding a mini-casino to it, the first game in the casino is going to be BlackJack. Its just going to be text (not pictures) I've never made a card game before as I'm quite new to VB, I was just wondering what would be the most suitable way of making it deal random cards? I was thinking something like ' Start of code Dim Roll As Integer Dim Card As String Randomize Roll = Int(52 * rnd) + 1 If Roll = 1 Then Card = "Ace" Else If Roll = 2 Then Card = "Two" and so on.... this will be a lot of code though, i was wondering if there would be an easier way? Cheers u1bd2005.
Advertisement
You're going to want to make a deck of 52 cards. Since in Blackjack the suit doesn't matter, you can ignore suit and just add 4 1's, 4 2's, etc to the deck which will be some sort of list or array. Then shuffle the array, make an index variable that points to the top of the deck and start dealing cards.

Actually simulating the thing you're trying to simulate is usually a good idea. OK, with blackjack this doesn't matter too much. About the only way simply choosing a random number from 1 through 13 would go wrong is if you dealt 5 aces or something. If you simulate a deck of cards, little things like that can't happen.

As for printing the name of a card, a lookup table would be easiest. Make an array of strings whose index is the name of the card. By that I means index 1 is "Ace", 2 is "Deuce", 3 is "Three", etc. That way you don't have to do the giant switch statement either. A giant switch statement is usually a sign that you could do it a better way.
I'm not very familiar with VB6, but you need to look into arrays.

You could have an array of suits and card names such as -
suit[4] = { "Hearts" , "Diamonds" , "Clubs" , "Spades" };
card[13] = { "Ace" , "one" ... "ten" , "Jack" , "Queen" , "King" };

You could then use something like -
cardname = card[cardno % 13] + "of" + cardsuit[cardno/13]
( % is modulo )

You could also have a deck array (of 52 cards), shuffle this deck in some way then draw a card from each successive element of the array.

The way you are doing it would allow the same card to be drawn more than once (and end up as a very bloated piece of code)
You should make a Card class and a Deck class which would contain 52 cards. Then you can use these for every card game you make. I made these for VB6 a while ago, I could give you the code if you want but its probably better if you do it yourself so you can udnerstand it better.

This topic is closed to new replies.

Advertisement