help with logic

BlitzMax Forums/BlitzMax Programming/help with logic

I'm having trouble seeing where the problem lies in some code i'm working on.

its a friut/slot machine thing, which should simply randomize the reels, and if they are all the same then the program will do something. Also, the spin method takes a fixed result parameter, so that the outcome can be rigged to a certain result.

while the fixed result part works fine, the random numbers don't, and instead of printing the right message, it re-randomizes the reels. why is this?

10 points to whoever can tell me what i'm doing wrong.

Type FruitMachine
	
	'stop variables, given a different time each to stagger the stopping of each reel
	Field stop1:Float = 0
	Field stop2:Float = 0
	Field stop3:Float = 0
      
       'are the reels spinning
      Field spin:Int = False
      
       'index, used to store the number of each bonus (also the array index of images in the actual program this is from)
	Field index1:Int 
	Field index2:Int 
	Field index3:Int 

        'result is for making sure that the reels all stop on the same index  in order to fix the result
	Field result:Int
	
'create a new object
	Function Create:FruitMachine()
		Local n:FruitMachine = New FruitMachine
		n.index1:Int = Rand(0,8)
		n.index2:Int = Rand(0,8)
		n.index3:Int = Rand(0,8)
		Return n
	End Function
	
'start spinning reels, also takes the result as a parameter if the result is not to be random
	Method spinReels(inResult:Int)
		spin = True
		result = inResult
	End Method

'update deals with the logic of the object
	Method update()
     
               start spinning if necessary, giving different times to the stop variables to stagger stopping
		If spin = True
			stop1 = 30
			stop2 = 60
			stop3 = 90
			spin = False
		End If
		
'while the stop variable for each index is greater than 0, randomise the indexes (this is for visual effect when images are drawn)
		If stop1 > 0
			stop1:-1 
			index1 = Rand(0, 8)
		End If
		
		If stop2 > 0
			stop2:-1 
			index2 = Rand(0, 8)
		End If
		
		If stop3 > 0
			stop3:-1 
			index3 = Rand(0, 8)
		End If
	
'once the last reel has stopped spinning, if the result is to be fixed, do this now
		If stop3<=0
			If result >= 0
				index1 = result
				index2 = result
				index3 = result
			
			End If
		End If
		
	End Method
End Type

SeedRnd MilliSecs()

Local slots:fruitmachine=New fruitmachine.Create()
Local slotsused:Int=False
Local slotResult:Int=-1

Graphics 10,10,0,60

While Not KeyHit(key_escape)
	
	If KeyHit(key_space)
'if the reels aren't spinning, set them off
			If slotsused = False
				
				If slots.stop3 <= 0
'you can change the numbers to any thing from 1 to 8 to get a fixed result, of -1 to get a random one.
					slots.spinReels(-1)'Rand(- 1, 8))
					
					slotsUsed = True
					Print"using Slots"
				End If
'if the reels have finished spinning, print the appropriate message
			ElseIf slotsused = True
				If slots.stop3 <= 0
						If slotresult = 0
							Print "fire"
							slotsused = False
						Else If slotresult = 1
							Print "coins"
							slotsused = False
						Else If slotresult = 2
							Print"invincible"
							slotsused = False
						Else If slotresult = 3
							Print "lightning"
							slotsused = False
						Else If slotresult = 4
							Print"slow"
							slotsused = False
						Else If slotresult = 5
							Print "drones"
							slotsused = False
						Else If slotresult = 6
							Print"coins2"
							slotsused = False
						Else If slotresult = 7
							Print"mines"
							slotsused = False
						Else If slotresult = 8
							Print"lives"
							slotsused = False
						End If
					End If
				EndIf
		End If
		
		If slotsused = True
			If slots.stop3 <= 0
				
				Print"index1:"+slots.index1
				Print"index2:"+slots.index2
				Print"index3:"+slots.index3
				
				If (slots.index1 = slots.index2) And (slots.index1 = slots.index3)
					slotResult = slots.index1
					
				Else
					slotsused = False
				End If
			End If
		End If
		
	
	slots.update()
	
	
Wend



Cheers
Charlie

Without any kind of useful comments it's too hard to work out what your program is doing. What are the stops and the indexes and what does each method do?

point taken, i've added a few comments now.

Cheers
Charlie

You're setting the slotresult after you checked it, so it's not likely to work out too well.
also I think the nesting of Ifs is a little jumbled.

This seems to work:
Type FruitMachine
	
	'stop variables, given a different time each to stagger the stopping of each reel
	Field stop1:Float = 0
	Field stop2:Float = 0
	Field stop3:Float = 0
      
       'are the reels spinning
      Field spin:Int = False
      
       'index, used to store the number of each bonus (also the array index of images in the actual program this is from)
	Field index1:Int 
	Field index2:Int 
	Field index3:Int 

        'result is for making sure that the reels all stop on the same index  in order to fix the result
	Field result:Int
	
'create a new object
	Function Create:FruitMachine()
		Local n:FruitMachine = New FruitMachine
		n.index1:Int = Rand(0,8)
		n.index2:Int = Rand(0,8)
		n.index3:Int = Rand(0,8)
		Return n
	End Function
	
'start spinning reels, also takes the result as a parameter if the result is not to be random
	Method spinReels(inResult:Int)
		spin = True
		result = inResult
	End Method

'update deals with the logic of the object
	Method update()
     
               'start spinning If necessary, giving different times To the stop variables To stagger stopping
		If spin = True
			stop1 = 30
			stop2 = 60
			stop3 = 90
			spin = False
		End If
		
'while the stop variable for each index is greater than 0, randomise the indexes (this is for visual effect when images are drawn)
		If stop1 > 0
			stop1:-1 
			index1 = Rand(0, 8)
		End If
		
		If stop2 > 0
			stop2:-1 
			index2 = Rand(0, 8)
		End If
		
		If stop3 > 0
			stop3:-1 
			index3 = Rand(0, 8)
		End If
	
'once the last reel has stopped spinning, if the result is to be fixed, do this now
		If stop3<=0
			If result >= 0
				index1 = result
				index2 = result
				index3 = result
			
			End If
		End If
		
	End Method
End Type

SeedRnd MilliSecs()

Local slots:fruitmachine=New fruitmachine.Create()
Local slotsused:Int=False
Local slotResult:Int=-1

Graphics 10,10,0,60

While Not KeyHit(key_escape)
	
	If KeyHit(key_space)
'if the reels aren't spinning, set them off
		If slotsused = False
				
			If slots.stop3 <= 0 Then
'you can change the numbers to any thing from 1 to 8 to get a fixed result, of -1 to get a random one.
				slots.spinReels(-1)'Rand(- 1, 8))
					
				slotsUsed = True
				Print"using Slots"
			End If
'if the reels have finished spinning, print the appropriate message
		EndIf
		If slotsused = True Then
			If slots.stop3 <= 0 Then
				
				Print"index1:"+slots.index1
				Print"index2:"+slots.index2
				Print"index3:"+slots.index3
				
				If (slots.index1 = slots.index2) And (slots.index1 = slots.index3)
					slotResult = slots.index1
					
				Else
					slotsused = False
				End If
			End If

			If slots.stop3 <= 0
				If slotresult = 0
					Print "fire"
					slotsused = False
				Else If slotresult = 1
					Print "coins"
					slotsused = False
				Else If slotresult = 2
					Print"invincible"
					slotsused = False
				Else If slotresult = 3
					Print "lightning"
					slotsused = False
				Else If slotresult = 4
					Print"slow"
					slotsused = False
				Else If slotresult = 5
					Print "drones"
					slotsused = False
				Else If slotresult = 6
					Print"coins2"
					slotsused = False
				Else If slotresult = 7
					Print"mines"
					slotsused = False
				Else If slotresult = 8
					Print"lives"
					slotsused = False
				End If
			End If
		EndIf
		
	End If
	slotresult =-1'reset the slot result

		
	
	slots.update()
	
	
Wend