Ok i finished making Tic-Tac-Toe yesterday in VB just for the fun of it so today i am making blackjack but ran into a weird problem... i made a function to return an integer between 1 and 52, but also excluding all other cards that have been played. here it is:
VB Code:
Private Function RndCard() As Integer
lop:
RndCard = Rnd * 52
If RndCard = 0 Then
GoTo lop
ElseIf RndCard = D1 Then
GoTo lop
ElseIf RndCard = D2 Then
GoTo lop
ElseIf RndCard = D3 Then
GoTo lop
ElseIf RndCard = D4 Then
GoTo lop
ElseIf RndCard = D5 Then
GoTo lop
ElseIf RndCard = P1 Then
GoTo lop
ElseIf RndCard = P2 Then
GoTo lop
ElseIf RndCard = P3 Then
GoTo lop
ElseIf RndCard = P4 Then
GoTo lop
ElseIf RndCard = P5 Then
GoTo lop
End If
End Function
this works but what i initially tried to do was this
VB Code:
Private Function RndCard() As Integer
lop:
RndCard = Rnd * 52
If RndCard = 0 Or D1 Or D2 Or D3 Or D4 Or D5 Or P1 Or P2 Or P3 Or P4 Or P5 Then
GoTo lop
End If
End Function
but forsome reason when i used my test button to display RndCard on a label it crashed every time, after i put an elseif before them all it worked, but i just dont understand why the other way kept crashing me.
any answers would be much appreciated
Last edited by Cruncher; Feb 12th, 2009 at 06:34 AM.
Your syntax turned the "or" into a bitwise operator. If you want to do a comparison your syntax should have been
Code:
if (RndCard = 0 Or RndCard = D1 Or RndCard =D2 Or RndCard =D3 Or RndCard =D4 Or RndCard =D5 Or RndCard =P1 Or RndCard =P2 Or RndCard =P3 Or RndCard =P4 Or RndCard =P5 ) then
but that's a mess.
Try doing a shuffle of array instead of random when using card games.
Kinda like this:
Code:
Dim rand(1 To 52) As Integer
Dim x As Integer, i As Integer, j As Integer
'load the deck
For i = 1 To 52
rand(i) = i
Next
'shuffle
For i = 1 To 52
x = Int(Rnd() * 52) + 1
j = rand(i)
rand(i) = rand(x)
rand(x) = j
Next
'shuffle again
For i = 1 To 52
x = Int(Rnd() * 52) + 1
j = rand(i)
rand(i) = rand(x)
rand(x) = j
Next
Now you can go down the deck in order 'til you reach the end. Just like a real deck of cards.
you gave me some code that loads and shuffles a deck, but when i tried using it i couldnt figure it out, could you possibly tell me how i get a card from the top of the deck
thanks
Please check out the attached code it will answer everything. Click on the card box to deal a card!
the key is in this part of the code
Code:
Image1(Index).Picture = cards(rand(currentCard))
rand's indexed value is the true card value. This form of referencing is called Indirect Indexed. Basically you are pointing to a pointer.
This looks like the start of a draw poker game but it's just an example - no underlying game.
Also, let me give credit where credit is due, the free card images are downloaded from http://www.jfitz.com/cards/
Last edited by technorobbo; May 3rd, 2009 at 09:02 PM.
Thank but uhh, when u used the case for it to load pictures (dont understand how that works really) but you must of had them in a ifferant location on your computer because im getting an error as soon as i try to run and its in the code where it looks for the pictures, its a command ive never used so i dont know how to fix it. in case you dont see this ill quote it in a pm too
Thank but uhh, when u used the case for it to load pictures (dont understand how that works really) but you must of had them in a ifferant location on your computer because im getting an error as soon as i try to run and its in the code where it looks for the pictures, its a command ive never used so i dont know how to fix it. in case you dont see this ill quote it in a pm too
3 Questions
what command does vb highlight when the error appears
Does the exec work?
What error is reported
I use the app.path for the directory so it looks for the location where the project was loaded from. So my thinking is that OLE automation is not checked in your references. Make sure it is, it's required for the stdpicture object.
Last edited by technorobbo; Feb 17th, 2009 at 03:12 PM.
i got it working, and yes the exe always worked, its because i didnt extract it, forgot that vb couldnt read archived images, but when i tried to use this i looked at your line that said
Image1(i).Picture = LoadPicture()
so i used
imgD1.Picture = LoadPicture() (not using an array because i dont like them)
i copied what you have for form load and all ur variables and everything, and when i click deal it doesnt load any picture
EDIT: just to clarify, there is no run-time error, this is simply a logic error, i can usually debug logic errors pretty good, but since im using foreign code I dont understand why.
EDIT2: figured it out
1 more problem, i think this should be the last one now, how do i get it to recognize all the cards and how many points they are worth, i noticed when you load the form you have case 1 to 10 and that kinda stuff is there a way i can use that to easily determine how many points each card is worth?
Last edited by Cruncher; Feb 18th, 2009 at 03:44 PM.
That's where the array comes into play.
With a 1 to 52 array you can divide the index by 12 and find out its value and suit!!! It goes something like this
Code:
cardval=(index-1) mod 13
suit=int((index-1)/13)
cardval would then yield a number between 0 and 12 (zero being ace and 12 king)
suit would yield a value between 0 and 3 (0=spades,1=hearts,2=clubs,3=diamonds)
You may want to put the cards index in the picturebox tag property to easily identify it.
You see theres a method to this array madness.
Last edited by technorobbo; Feb 18th, 2009 at 05:11 PM.
Ok, in order to explain what I just said - I' modified the code in post six to include text boxes above the cards that tell you exactly what the card is.
Here's a link to that upload http://www.vbforums.com/attachment.p...8&d=1235010588
explanation:
The tag property of an object is the programers storage space that let's him(her) store a value within that control. In this case I stored the cards value (1 thru 52) in the picturebox's tag property.
Using the code I described in my last post I name the cards as I deal them (see code below)
"x = Image1(index).Tag" retrieves the code I stored in the tag
"cardvalue = (x - 1) Mod 13" looks for the remainder of a division by the number 13. Basically it loops every 13 numbers but since it's a remainder it never gets to 13 so the results is always 0 thru 12.
"suit = Int((x - 1) / 13)" divides by thirteen and get the whole number answer. the results are 0 thru 3. this gives me the suits because the cards are organized in order of suits.
the choose function takes these results and assigns text to them.
You can use the cardvalue to do your blackjack math just add 1 to the number first because ace is zero (math drives that). and anything over 10 make it 10. you can use "Choose" function to do all that.
Code:
Private Sub NameCard(index As Integer)
Dim x As Integer, cardvalue As Integer, suit As Integer
x = Image1(index).Tag
cardvalue = (x - 1) Mod 13
suit = Int((x - 1) / 13)
Text1(index) = Choose(cardvalue + 1, "Ace", 2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King") _
& " of " & Choose(suit + 1, "Spades", "Clubs", "Hearts", "Diamonds")
End Sub
I attached what i managed to make with your help, some of the code is identical to what you gave me, but i had to write lots new code and revise a lot of what you gave me to fit my needs,
if you could tell me what you think of my code that would be cool (Im starting to figure out this array stuff)
EDIT: attachment removed look further down.
Last edited by Cruncher; Feb 20th, 2009 at 08:20 PM.
K, ill do that by the way i was wondering, how can i make the dealer pause between each card he plays, like whats the line of code to make visual basic stop running for 1 seconds then continue?
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub Form_Load()
Me.Show
MsgBox "I'll be back in 5 seconds"
Sleep 5000 '5 second delay
MsgBox "I'm Back!"
End Sub
ill stick with what i have, uhm for a small thing is there an operator for doesnt equal 0? for instance at the end of the hit buttons code i want to put a line that makes it so if i have played 5 cards without busting, regardless of my points you win, so i need it when a(9) (which is the last card) doesnt not equal 0 (this means that its not blank) then i win.
thanks in advance
cruncher
EDIT: nvm its <>
Last edited by Cruncher; Feb 20th, 2009 at 04:30 PM.
OK, im gonna wrap this project up, i did everything except for the split, i am going to upload it for reference for anyone if they wanna look at it... my brother suggested that i make Yahtzee should be fun.