Algorithm problem for slot machine

Started by
5 comments, last by Zahlman 19 years ago
Hi I'm using VB6 to create a slot machine. So far it's working well but I have 1 small problem. The slot machine has nine boxes. It pays out if any 3 of these boxes rolls 7. I can't figure out how to check for this. if i use the following it keeps finding the same 7 over 3 times. for i = 0 to 8 if box(i).picture = imaage(7) and box(i).picture = imaage(7)And box(i).picture = imaage(7) Then you win end if next i I am sure this is simple for you long time programers but for a newbie like me it's got me baffeled. I would greatly appreciate some insight. Please use layman's terms. thank you.
Advertisement
Just count how many sevens you've got. Your code only tests for a single box being a 7 (if box i is a 7, then all 3 box(i).picture == imaage(7) are true!)

count = 0for i = 0 to 8   if box(i).picture == imaage(7) Then count = count + 1next iif count >= 3 Then   you winend if
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
[edit] Good God you have fast typing skills Fruny [lol] And it's in VB code [sad]

Quote:Original post by Marz
if i use the following it keeps finding the same 7 over 3 times.

for i = 0 to 8

if box(i).picture = imaage(7) and box(i).picture = imaage(7)And box(i).picture = imaage(7) Then
you win
end if
next i


Right now this is what's happening:

You start by looping through from 0 - 8
for i = 0 to 8

Howeverm then you check to see if that box's image is 7, to which if it is, then you display that the player has won:
if box(i).picture = imaage(7) and box(i).picture = imaage(7)And box(i).picture = imaage(7) Then you win

So if the first box is a 7, then:
box(i).picture == 7
box(i).picture == 7
box(i).picture == 7

You are checking the same index 3 times.

To solve this, what you need to do is something like this - I don't know VB that well so this is pseudo code:
ctr = 0FOR i 0 to 8if box(i).picture = imaage(7) Thenctr = ctr + 1end ifnext iif ctr > 0 then  you win


Instead you will keep track of the total number of 7's or in this case, just the fact the player rolled one.
Awesome that solved the problem completely. Thank you both.
darn that does let me know when the player has turned up any 3 sevens but I also need to know where they are so I can light up the squares around them.

sorry darn I thought we had it.

i would assume that all the pictures you are drawing have coordaninths (y wouldnt they) so all you have to do is

if box(i).picture == imaage(7) Then count = count + 1

then

box(i).picture = Light_Up_7Box.

i think this is what your asking (i dont know VB but hopefully you can inturpret what i mean

____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
Quote:Original post by Marz
darn that does let me know when the player has turned up any 3 sevens but I also need to know where they are so I can light up the squares around them.

sorry darn I thought we had it.


If the current box is a 7, you need to increment the count and also light up that box - just do that in one step:

for i = 0 to 8   if box(i).picture == imaage(7) Then       count = count + 1       box(i).picture = 'fill in whatever image index here 

This topic is closed to new replies.

Advertisement