;Chris Turek
;

;
;10 November 2004





;*********************************************************
;CONTROLS for the TOWERS OF HANOI
;
;
;up arrow -- alternate between the 3 discs
;
;left/right arrows -- alternate the currently selected disc between the 3 towers
;
;
;down arrow -- release/drop the currently selected disc onto the currently selected tower
;
;X key -- exit and reset the puzzle
;
;
;*********************************************************






Include "BlitzGeoWallStereoLib.bb"
Include "ChaseCameraLib.bb"
Include "carsAI.bb";ai for the cars to run around the city
Include "pellets.bb";display and handle the pellets the car can collect










;variables for the logical representation of the towers and discs
;
;towerx_y = tower number x in the y position
;ex: tower1_1 = tower 1, the first(lowest) level of the tower
Global tower1_3, tower1_2, tower1_1
Global tower2_3, tower2_2, tower2_1
Global tower3_3, tower3_2, tower3_1
Global spindle1, spindle2, spindle3

;the discs
Global SMALL = 1
Global MEDIUM = 2
Global LARGE = 3


;graphical representations of the discs and towers
Global tower1, tower2, tower3
Global discSmall, discMedium, discLarge
Global ds, dm, dl;"shadow" versions of the discs for when they are "selected"


;to keep tabs on the disc currently selected/highlit
;boolean values for the discs
Global sB, mB, lB

;set the initial configuration of the puzzle -- and hide the models
Function setupTowers()

;the first tower will have the discs in their original configurations
tower1_1 = LARGE 
tower1_2 = MEDIUM
tower1_3 = SMALL

;the other towers are empty
tower2_1 = 0 ;0 = no disc present
tower2_2 = 0
tower2_3 = 0
tower3_1 = 0 
tower3_2 = 0
tower3_3 = 0



discSmall = LoadMesh("disc_small.x")
discTex = LoadTexture("discs.jpg")
EntityTexture discSmall, discTex
ScaleEntity discSmall, .3, .3 , .3
EntityOrder discSmall, 0

discMedium = LoadMesh("disc_medium.x")
EntityTexture discMedium, discTex
ScaleEntity discMedium, .3, .3,.3
EntityOrder discMedium, 0

discLarge = LoadMesh("disc_large.x")
EntityTexture discLarge, discTex
ScaleEntity discLarge, .3, .3, .3
EntityOrder discLarge, 0



ds = LoadMesh("disc_small.x")
cubeTex = LoadTexture("discs_glow.jpg")
EntityTexture ds, cubeTex
ScaleEntity ds, .3, .3 , .3
EntityOrder ds, 0

dm = LoadMesh("disc_medium.x")
EntityTexture dm, cubeTex
ScaleEntity dm, .3, .3 , .3
EntityOrder dm, 0

dl = LoadMesh("disc_large.x")
EntityTexture dl, cubeTex
ScaleEntity dl, .3, .3 , .3
EntityOrder dl, 0


spindle1 = LoadMesh("spindle.x")
spindleTex = LoadTexture("spindle.jpg")
EntityTexture spindle1, spindleTex
ScaleEntity spindle1, .17, .17, .17
EntityOrder spindle1, 0

spindle2 = CopyMesh(spindle1)
EntityTexture spindle2, spindleTex
ScaleEntity spindle2, .17, .17, .17
EntityOrder spindle2, 0

spindle3 = CopyMesh(spindle1)
EntityTexture spindle3, spindleTex
ScaleEntity spindle3, .17, .17, .17
EntityOrder spindle3, 0


;hide all of the puzzle items
HideEntity discSmall
HideEntity discMedium
HideEntity discLarge
HideEntity ds
HideEntity dm
HideEntity dl
HideEntity spindle1
HideEntity spindle2
HideEntity spindle3

;the current disc that is selected is small, the others are not selected
sB = 1 
mB = 0
lB = 0




End Function






;hide all of the puzzle objects/meshes
Function hideTowers()


HideEntity discSmall
HideEntity discMedium
HideEntity discLarge
HideEntity ds
HideEntity dm
HideEntity dl
HideEntity spindle1
HideEntity spindle2
HideEntity spindle3
HideEntity flyer1
HideEntity flyer2
HideEntity flyer3
HideEntity flyer4

ShowEntity g_model
ShowEntity building2_1
ShowEntity grass
ShowEntity building2_2
ShowEntity building1


ShowEntity building3_1
ShowEntity building3_2
ShowEntity building4_1
ShowEntity building4_2
ShowEntity building5_1
ShowEntity building5_2
ShowEntity building6_1
ShowEntity building6_2
ShowEntity building6_3

ShowEntity record_store
ShowEntity sidewalk
ShowEntity sidewalk2


End Function







;show all of the puzzle objects/meshes
Function showTowers()

ShowEntity discSmall
ShowEntity discMedium
ShowEntity discLarge
ShowEntity spindle1
ShowEntity spindle2
ShowEntity spindle3
ShowEntity flyer1
ShowEntity flyer2
ShowEntity flyer3
ShowEntity flyer4

HideEntity g_model
HideEntity building2_1
HideEntity grass
HideEntity building2_2
HideEntity building1


HideEntity building3_1
HideEntity building3_2
HideEntity building4_1
HideEntity building4_2
HideEntity building5_1
HideEntity building5_2
HideEntity building6_1
HideEntity building6_2
HideEntity building6_3


HideEntity record_store
HideEntity sidewalk
HideEntity sidewalk2





PositionEntity discSmall, -70, 9, 115
PositionEntity discMedium, -70, 6, 115
PositionEntity discLarge, -70, 3, 115
PositionEntity ds, -70, 9, 115
PositionEntity dm, -70, 6, 115
PositionEntity dl, -70, 3, 115
PositionEntity spindle1, -70, 0, 115
PositionEntity spindle2, -63, 0, 115
PositionEntity spindle3, -56, 0, 115


If g_screenXSize=2048 Then

	PositionEntity SL_GetLeftCamera(), -63, 5, 95
	ChaseCamera(SL_GetLeftCamera(), spindle2, 0,5,3)	
		
	
ElseIf g_screenXSize=1280 Then

	PositionEntity SL_GetLeftCamera(), -63, 4, 100
	ChaseCamera(SL_GetLeftCamera(), spindle2, 0,5,3)	
	

Else

	PositionEntity SL_GetLeftCamera(), -63, 4, 101
	ChaseCamera(SL_GetLeftCamera(), spindle2, 0,5,3)	
		

EndIf


End Function







;move a disc based on the arrow keys
;
Function moveDisc(direction)


	Select direction

	Case U ;up key
	
	
		;SMALL disc
		;move from the small disc to the large disc
		;
		;hide the selected version of the small disc
		;show the non selected version of the small disc
		;hide the non selected version of the large disc
		;show the selected version of the large disc
		If selected (SMALL) Then
			HideEntity ds
			ShowEntity discSmall
			HideEntity discLarge
			ShowEntity dl
			lB = 1
			sB = 0
			 
		;MEDIUM disc
		ElseIf selected (MEDIUM) Then
			HideEntity dm
			ShowEntity discMedium
			HideEntity discSmall
			ShowEntity ds
			sB = 1
			mB = 0
			
		;LARGE disc	
		ElseIf selected (LARGE) Then
			HideEntity dl
			ShowEntity discLarge
			HideEntity discMedium
			ShowEntity dm
			lB = 0
			mB = 1
			
		EndIf
	
	
	
	
	Case D ;down -- same as the up key, but in the opposite direction(obviously)
	
		If selected (SMALL) Then
			HideEntity ds
			ShowEntity discSmall
			HideEntity discMedium
			ShowEntity dm
			mB = 1
			sB = 0
			
			 
		ElseIf selected (MEDIUM) Then
			HideEntity dm
			ShowEntity discMedium
			HideEntity discLarge
			ShowEntity dl
			lB = 1
			mB = 0
			
			
		ElseIf selected (LARGE) Then
			HideEntity dl
			ShowEntity discLarge
			HideEntity discSmall
			ShowEntity ds
			lB = 0
			sB = 1
			EndIf
	

	
	
	
	
	
	Case L  ;left
		;move the selected disc one tower to the left
	

	
		;small disc
		If selected (SMALL) Then
			
			
			x# = EntityX#(ds)
			
			;the towers are at the following X postions: T1:-70, T2:-63, T3:-56
			;generate the x# for the disc as it moves to the left
			If x# = -70 Then
				x# = -56
				Else
				x#  = x# - 7
			EndIf
			
			;the disc can only move sideways if 1 of 2 things are true
			;1) the disc is on the top of the current tower
			;2) the disc is already at Y=11, meaning that it is already on top of the puzzle,
			;having been moved sideways already.
			;NOTE: when a disc is moved to a different tower sideways i elevate it to Y=11 to
			;show that it is "above" the puzzle and being moved
			If isTop(SMALL) Or EntityY#(ds) = 11 Then
			
				movingFlag = 1 ;this means that since the disc has been moved to a new tower
								 ;it must be "released" (R key) before another disc can be selected.
								 ;this boolean is handled in mappoint.bb
				PositionEntity ds, x#, 11, 115
				PositionEntity discSmall, x#, 11, 115
				
			Else 
					;error noise is played to show that the move is illegal
					channel = PlaySound(collideNoise)	
					PlaySound(collideNoise)	
			
			EndIf
		
			
			 ;medium disc
		ElseIf selected (MEDIUM) Then
		
			x# = EntityX#(dm)
			
			If x# = -70 Then
				x# = -56
				Else
				x#  = x# - 7
			EndIf
		
			
			If isTop(MEDIUM) Or EntityY#(dm) = 11 Then
			
				movingFlag = 1
				PositionEntity dm, x#, 11, 115
				PositionEntity discMedium, x#, 11, 115
				
			Else 
					
					channel = PlaySound(collideNoise)	
					PlaySound(collideNoise)
			
			EndIf
			
			;large disc
		ElseIf selected (LARGE) Then
			
			x# = EntityX#(dl)
			
			If x# = -70 Then
				x# = -56
				Else
				x#  = x# - 7
			EndIf
			
			
			If isTop(LARGE) Or EntityY#(dl) = 11 Then
			
				movingFlag = 1
				PositionEntity dl, x#, 11, 115
				PositionEntity discLarge, x#, 11, 115
				
			Else 
					
					channel = PlaySound(collideNoise)	
					PlaySound(collideNoise)
			
			EndIf 
			
			
		EndIf
	
	
	
	Case R  ;right
	;this is handled the same as the left movement above, but with everything moving to the right instead
	
	
		If selected (SMALL) Then
			
			
			x# = EntityX#(ds)
			
			If x# = -56 Then
				x# = -70
				Else
				x#  = x# + 7
			EndIf
			
			
			If isTop(SMALL) Or EntityY#(ds) = 11 Then
			
				movingFlag = 1
				PositionEntity ds, x#, 11, 115
				PositionEntity discSmall, x#, 11, 115
						
			Else 
					
					channel = PlaySound(collideNoise)	
					PlaySound(collideNoise)
			 
			EndIf
			
			
		ElseIf selected (MEDIUM) Then
			
			x# = EntityX#(dm)
			
			If x# = -56 Then
				x# = -70
				Else
				x#  = x# + 7
			EndIf
			
			
			If isTop(MEDIUM) Or EntityY#(dm) = 11 Then
			
				movingFlag = 1
				PositionEntity dm, x#, 11, 115
				PositionEntity discMedium, x#, 11, 115
				
			Else 
					
					channel = PlaySound(collideNoise)	
					PlaySound(collideNoise)
			
			EndIf
			
			
		ElseIf selected (LARGE) Then
		
			x# = EntityX#(dl)
			
			If x# = -56 Then
				x# = -70
				Else
				x#  = x# + 7
			EndIf
		
			If isTop(LARGE) Or EntityY#(dl) = 11 Then
			
				movingFlag = 1
				PositionEntity dl, x#, 11, 115
				PositionEntity discLarge, x#, 11, 115
				
			Else 
					
					channel = PlaySound(collideNoise)	
					PlaySound(collideNoise)
				
			
			EndIf 
			
			
			
		EndIf

	
	
End Select 




End Function










;boolean function to determine which disc is currently selected
;returns true or false
;
Function selected (disc)

	If disc = SMALL And sB = 1 Then
		result = True
	
	ElseIf disc = MEDIUM And mB = 1 Then
		result = True

	ElseIf disc = LARGE And lB = 1 Then
		result = True
		
	Else
		result = False
		
	EndIf
		
	Return result
			


End Function







;is a tower empty based on the number (1-3) given
;returns true or false
Function emptyTower(num)


Select num

	Case 1
	
		If tower1_1 = 0 And tower1_2 = 0 And tower1_3 = 0 Then
			result = True
		EndIf	
	
	Case 2
	
		If tower2_1 = 0 And tower2_2 = 0 And tower2_3 = 0 Then
			result = True
		EndIf
	
	Case 3
	
		If tower3_1 = 0 And tower3_2 = 0 And tower3_3 = 0 Then
			result = True
		EndIf
	
	Default
	
		result = False
		
End Select


Return result



End Function









;is a tower full based on the tower number given
;returns true or false
Function fullTower(num)

Select num

	Case 1
	
		If tower1_1 > 0 And tower1_2 > 0 And tower1_3 > 0 Then
			result = True
		EndIf
	
	Case 2
	
		If tower2_1 > 0 And tower2_2 > 0 And tower2_3 > 0 Then
			result = True
		EndIf
	
	Case 3
	
		If tower3_1 > 0 And tower3_2 > 0 And tower3_3 > 0 Then
			result = True
		EndIf
	
	Default
	
		result = False
		
		
End Select

Return result



End Function




;does a tower have certain disc?
Function towerHasDisc(towerNumber, discSize)

Select towerNumber

	Case 1
	
		If tower1_1 = discSize Or tower1_2 = discSize Or tower1_3 = discSize Then
			result = True
			EndIf
			
	Case 2
	
		If tower2_1 = discSize Or tower2_2 = discSize Or tower2_3 = discSize Then
			result = True
			EndIf
	
	Case 3
	
	
		If tower3_1 = discSize Or tower3_2 = discSize Or tower3_3 = discSize Then
			result = True
			EndIf
	
	
	Default
	
		result = False
	
	
End Select


Return result
	


End Function




;is a move legal?
Function moveLegal(discSize, towernumber)

Select discSize

	Case SMALL
		
		If (Not(fullTower(towernumber))) Then
			result = True
		Else
			result = False
		EndIf
			
	
	
	Case MEDIUM
	
		If (Not(fullTower(towerNumber))) And (Not(towerHasDisc(towerNumber, SMALL))) Then
			result = True
		Else
			result = False
		EndIf
		
	
	Case LARGE
	
		If (Not(fullTower(towerNumber))) And (Not(towerHasDisc(towerNumber, SMALL))) And (Not(towerHasDisc(towerNumber, MEDIUM))) Then
			result = True
		Else
			result = False
		EndIf
		
	
	
	
	
End Select


Return result


End Function






;boolean function to determine the correctness of the puzzle
;TRUE if the puzzle is correct, FALSE otherwise
Function isSolved()

	Return tower3_1 = LARGE And tower3_2 = MEDIUM And tower3_3 = SMALL

End Function




;get the tower number of the current selected disc
Function getTower#()

	If selected(SMALL) Then
			
			xtemp = EntityX#(ds)
			 
	ElseIf selected(MEDIUM) Then
		
			xtemp = EntityX#(dm)
			
	ElseIf selected(LARGE) Then
			
			xtemp = EntityX#(dl)
			
	EndIf

	
	
	Select xtemp
	
		Case -70
		
			result = 1 
		
		Case -63
		
			result = 2
		
		Case -56
		
			result = 3
		
	End Select


	Return result
	

End Function







;release the selected disc on the current tower
Function releaseDisc(tower#)

	
	;SMALL disc
	If selected(SMALL) Then
			
			;we now have the SMALL disc, now determine if it can move onto the given tower
			Select tower#


				Case 1;tower 1
				
					;the tower is not full AND the move is legal
					If (Not(fullTower(tower#))) And moveLegal(SMALL, tower#) Then
						
						moveDiscToTower(SMALL, tower#)
						
					Else 
						;error noise
						channel = PlaySound(collideNoise)	
						PlaySound(collideNoise)
						
					EndIf
					
				
				
				Case 2;tower 2
				
					If (Not(fullTower(tower#))) And moveLegal(SMALL, tower#) Then
						
						
						moveDiscToTower(SMALL, tower#)
						
					Else 
					
						channel = PlaySound(collideNoise)	
						PlaySound(collideNoise)
						
					EndIf
				
				
				Case 3;tower3
				
					If (Not(fullTower(tower#))) And moveLegal(SMALL, tower#) Then
						
						moveDiscToTower(SMALL, tower#)
						
						
					Else 
					
						channel = PlaySound(collideNoise)	
						PlaySound(collideNoise)
						
					EndIf
				
				
			End Select
			


			 ;same fore medium tower
	ElseIf selected(MEDIUM) Then
		
			
			Select tower#


				Case 1
				
					If (Not(fullTower(tower#))) And moveLegal(MEDIUM, tower#) Then
						
						moveDiscToTower(MEDIUM, tower#)
						
					Else 
					
						channel = PlaySound(collideNoise)	
						PlaySound(collideNoise)
						
					EndIf
					
				
				
				Case 2
				
					If (Not(fullTower(tower#))) And moveLegal(MEDIUM, tower#) Then
						
						
						moveDiscToTower(MEDIUM, tower#)
						
					Else 
					
						channel = PlaySound(collideNoise)	
						PlaySound(collideNoise)
						
					EndIf
				
				
				Case 3
				
					If (Not(fullTower(tower#))) And moveLegal(MEDIUM, tower#) Then
						
						moveDiscToTower(MEDIUM, tower#)
						
					Else 
					
						channel = PlaySound(collideNoise)	
						PlaySound(collideNoise)
						
						
					EndIf
				
				
			End Select
			
			
			
			;and the large tower
	ElseIf selected(LARGE) Then
			
			
			
			Select tower#


				Case 1
				
					If (Not(fullTower(tower#))) And moveLegal(LARGE, tower#) Then
						
						moveDiscToTower(LARGE, tower#)
						
					Else 
					
						channel = PlaySound(collideNoise)	
						PlaySound(collideNoise)
						
					EndIf
					
				
				
				Case 2
				
					If (Not(fullTower(tower#))) And moveLegal(LARGE, tower#) Then
						
						
						moveDiscToTower(LARGE, tower#)
						
					Else 
					
						channel = PlaySound(collideNoise)	
						PlaySound(collideNoise)
						
					EndIf
				
				
				Case 3
				
					If (Not(fullTower(tower#))) And moveLegal(LARGE, tower#) Then
						
						moveDiscToTower(LARGE, tower#)
						
					Else 
					
						channel = PlaySound(collideNoise)	
						PlaySound(collideNoise)
						
						
					EndIf
				
				
			End Select
			
			
			
	EndIf
			
			
			





End Function



;move the correctly sized disc to the  tower requested
Function moveDiscToTower(sz, t#)


Select sz

	Case SMALL;small disc
		

		;tower number 1

		If t# = 1 Then
			
			;the tower is empty to place the disc on the bottom of the tower
			If tower1_1 = 0 And tower1_2 = 0 And tower1_3 = 0 Then
				y# = 3
				clearDisc(SMALL)
				tower1_1 = SMALL
				;the bottom of the tower is either large or medium, so place the small disc in the second slot
			Else If tower1_2 = 0  And (tower1_1 = LARGE Or tower1_1 = MEDIUM) And tower1_3 = 0 Then
				y# = 6
				clearDisc(SMALL)
				tower1_2 = SMALL
				;the medium and large discs are on the tower so place the small disc on top
			ElseIf tower1_3 = 0 And tower1_2 = MEDIUM And tower1_1 = LARGE Then
				y# = 9
				clearDisc(SMALL)
				tower1_3 = SMALL
				
			EndIf 
			
			PositionEntity ds, -70, y#, 115
			PositionEntity discSmall, -70, y#, 115
		
		
		
		;tower # 2
		ElseIf t# = 2 Then
		
			If tower2_1 = 0 And tower2_2 = 0 And tower2_3 = 0 Then
				y# = 3
				clearDisc(SMALL)
				tower2_1 = SMALL
			Else If tower2_2 = 0 And (tower2_1 = LARGE Or tower2_1 = MEDIUM) And tower2_3 = 0 Then
				y# = 6
				clearDisc(SMALL)
				tower2_2 = SMALL
			ElseIf tower2_3 = 0 And tower2_2 = MEDIUM And tower2_1 = LARGE Then
				y# = 9
				clearDisc(SMALL)
				tower2_3 = SMALL
				
			EndIf 
		
			PositionEntity ds, -63, y#, 115
			PositionEntity discSmall, -63, y#, 115
			
		
		
		;tower #3
		ElseIf t# = 3 Then
		 
			If tower3_1 = 0 And tower3_2 = 0 And tower3_3 = 0Then
				y# = 3
				clearDisc(SMALL)
				tower3_1 = SMALL
			Else If tower3_2 = 0 And (tower3_1 = LARGE Or tower3_1 = MEDIUM) And tower3_3 = 0Then
				y# = 6
				clearDisc(SMALL)
				tower3_2 = SMALL
			ElseIf tower3_3 = 0 And tower3_2 = MEDIUM And tower3_1 = LARGE Then
				y# = 9
				clearDisc(SMALL)
				tower3_3 = SMALL
				
			EndIf 
		
		
			PositionEntity ds, -56, y#, 115
			PositionEntity discSmall, -56, y#, 115
		
		
		
		EndIf
		

	
	
	Case MEDIUM;medium disc
	
		
		If t# = 1 Then;tower 1
		
			;the tower is empty so place it on the bottom
			If tower1_1 = 0 And tower1_2 = 0 And tower1_3 = 0 Then
				y# = 3
				clearDisc(MEDIUM)
				tower1_1 = MEDIUM
				;the tower has the large disc in it, so place the medium disc in the middle
			Else If tower1_2 = 0  And tower1_1 = LARGE And tower1_3 = 0 Then
				y# = 6
				clearDisc(MEDIUM)
				tower1_2 = MEDIUM
				
			EndIf
			
			PositionEntity dm, -70, y#, 115
			PositionEntity discMedium, -70, y#, 115
			
		
		
		
		ElseIf t# = 2 Then
		
			If tower2_1 = 0 And tower2_2 = 0 And tower2_3 = 0 Then
				y# = 3
				clearDisc(MEDIUM)
				tower2_1 = MEDIUM
			Else If tower2_2 = 0  And tower2_1 = LARGE And tower2_3 = 0 Then
				y# = 6
				clearDisc(MEDIUM)
				tower2_2 = MEDIUM
				
			EndIf
		
			PositionEntity dm, -63, y#, 115
			PositionEntity discMedium, -63, y#, 115
			
		
		
		
		ElseIf t# = 3 Then
		
			If tower3_1 = 0 And tower3_2 = 0 And tower3_3 = 0 Then
				y# = 3
				clearDisc(MEDIUM)
				tower3_1 = MEDIUM
			Else If tower3_2 = 0  And tower3_1 = LARGE And tower3_3 = 0 Then
				y# = 6
				clearDisc(MEDIUM)
				tower3_2 = MEDIUM
				
			EndIf
		 
			PositionEntity dm, -56, y#, 115
			PositionEntity discMedium, -56, y#, 115
			
		
		
		
		EndIf
		
		
	
	Case LARGE;large disc
		
	
		If t# = 1 Then ;tower 1
			
			;no boolean needed since the large disc can only be placed in the bottom of any given tower
			PositionEntity dl, -70, 3, 115
			PositionEntity discLarge, -70, 3, 115
			clearDisc(LARGE)
			tower1_1 = LARGE
			
		
		
		
		ElseIf t# = 2 Then
		
			PositionEntity dl, -63, 3, 115
			PositionEntity discLarge, -63, 3, 115
			clearDisc(LARGE)
			tower2_1 = LARGE
		
		
		
		ElseIf t# = 3 Then
		 
			PositionEntity dl, -56, 3, 115
			PositionEntity discLarge, -56, 3, 115
			clearDisc(LARGE)
			tower3_1 = LARGE
		
		
		
		EndIf

	
	
	
End Select




End Function







;clear the last place of the given disc in the tower data
Function clearDisc(sz)


	Select sz
	
		Case SMALL
		
			If tower1_1 = SMALL Then
				tower1_1 = 0
			ElseIf tower2_1 = SMALL Then
				tower2_1 = 0
			ElseIf tower3_1 = SMALL Then
				tower3_1 = 0
			ElseIf tower1_2 = SMALL Then
				tower1_2 = 0
			ElseIf tower2_2 = SMALL Then
				tower2_2 = 0
			ElseIf tower3_2 = SMALL Then
				tower3_2 = 0
			ElseIf tower1_3 = SMALL Then
				tower1_3 = 0
			ElseIf tower2_3 = SMALL Then
				tower2_3 = 0
			ElseIf tower3_3 = SMALL Then
				tower3_3 = 0
			EndIf	
			

		
		Case MEDIUM
		
			If tower1_1 = MEDIUM Then
				tower1_1 = 0
			ElseIf tower2_1 = MEDIUM Then
				tower2_1 = 0
			ElseIf tower3_1 = MEDIUM Then
				tower3_1 = 0
			ElseIf tower1_2 = MEDIUM Then
				tower1_2 = 0
			ElseIf tower2_2 = MEDIUM Then
				tower2_2 = 0
			ElseIf tower3_2 = MEDIUM Then
				tower3_2 = 0
			ElseIf tower1_3 = MEDIUM Then
				tower1_3 = 0
			ElseIf tower2_3 = MEDIUM Then
				tower2_3 = 0
			ElseIf tower3_3 = MEDIUM Then
				tower3_3 = 0
			EndIf
		
		
		
		Case LARGE

			If tower1_1 = LARGE Then
				tower1_1 = 0
			ElseIf tower2_1 = LARGE Then
				tower2_1 = 0
			ElseIf tower3_1 = LARGE Then
				tower3_1 = 0
			ElseIf tower1_2 = LARGE Then
				tower1_2 = 0
			ElseIf tower2_2 = LARGE Then
				tower2_2 = 0
			ElseIf tower3_2 = LARGE Then
				tower3_2 = 0
			ElseIf tower1_3 = LARGE Then
				tower1_3 = 0
			ElseIf tower2_3 = LARGE Then
				tower2_3 = 0
			ElseIf tower3_3 = LARGE Then
				tower3_3 = 0
			EndIf


	End Select
	


End Function









;is the given disc the top of the tower it is currently on?
Function isTop(d)


	result = False
	

	Select d
	
		Case SMALL;small disc
	
			x# = EntityX#(ds)
			
			
			
			If x# = -70 Then ;tower 1
			
				
				;the disc is on the top of the tower 1
				If tower1_3 = SMALL And tower1_2 = MEDIUM And tower1_1 = LARGE Then
					result = True
				;the disc is in the middle slot, and the top slot is empty	
				ElseIf tower1_1 > 0 And tower1_3 = 0 And tower1_2 = SMALL Then
					result = True
				;the disc is on the bottom and the top 2 slots are empty
				ElseIf tower1_1 = SMALL And tower1_3 = 0 And tower1_2 = 0 Then
					result = True	
				
				EndIf
				
			
			
			ElseIf x# = -63 Then ;tower 2
			
				If tower2_3 = SMALL And tower2_2 = MEDIUM And tower2_1 = LARGE Then
					result = True
					
				ElseIf tower2_1 > 0 And tower2_3 = 0 And tower2_2 = SMALL Then
					result = True
					
				ElseIf tower2_1 = SMALL And tower2_3 = 0 And tower2_2 = 0 Then
					result = True
					
				EndIf
			
			
			ElseIf x# = -56 Then ;tower 3
			
			
				If tower3_3 = SMALL And tower3_2 = MEDIUM And tower3_1 = LARGE Then
					result = True
					
				ElseIf tower3_1 > 0 And tower3_3 = 0 And tower3_2 = SMALL Then
					result = True
					
				ElseIf tower3_1 = SMALL And tower3_3 = 0 And tower3_2 = 0 Then
					result = True
			
				EndIf
				
			
			
			EndIf
			
		
		
		Case MEDIUM ;medium disc
		
		
			x# = EntityX#(dm)
			
			
			
			If x# = -70 Then ;tower 1
			
			
				;the disc is in the middle and the top slot is empty
				If tower1_3 = 0 And tower1_2 = MEDIUM And tower1_1 = LARGE Then
					result = True
				;the disc is on the bottom and the top 2 slots are empty
				ElseIf tower1_1 = MEDIUM And tower1_3 = 0 And tower1_2 = 0 Then
					result = True	
				
				EndIf
				
			
			
			ElseIf x# = -63 Then ;tower2
			
				If tower2_3 = 0 And tower2_2 = MEDIUM And tower2_1 = LARGE Then
					result = True
			
				ElseIf tower2_1 = MEDIUM And tower2_3 = 0 And tower2_2 = 0 Then
					result = True	
				
				EndIf
			
			
			ElseIf x# = -56 Then ;tower 3
			
			
				If tower3_3 = 0 And tower3_2 = MEDIUM And tower3_1 = LARGE Then
					result = True
			
				ElseIf tower3_1 = MEDIUM And tower3_3 = 0 And tower3_2 = 0 Then
					result = True	
				
				EndIf
			
			
			
			EndIf
			
		
		
		Case LARGE; large disc
		
			x# = EntityX#(dl)
			
			
			
			If x# = -70 Then ;tower 1
			
				;the large disc is on the bottom and the top 2 slots are empty
				If tower1_1 = LARGE And tower1_3 = 0 And tower1_2 = 0 Then
					result = True	
				
				EndIf
				
			
			
			ElseIf x# = -63 Then ;tower 2
			
				If tower2_1 = LARGE And tower2_3 = 0 And tower2_2 = 0 Then
					result = True	
				
				EndIf
			
			
			ElseIf x# = -56 Then; tower 3
			
			
				If tower3_1 = LARGE And tower3_3 = 0 And tower3_2 = 0 Then
					result = True	
				
				EndIf
			
			
			
			EndIf
		
		
	End Select
	
	
	Return result
	
End Function




;reset the towers to their original status -- unsolved and unmoved
Function resetTowers()


tower1_1 = LARGE 
tower1_2 = MEDIUM
tower1_3 = SMALL

tower2_1 = 0 
tower2_2 = 0
tower2_3 = 0
tower3_1 = 0 
tower3_2 = 0
tower3_3 = 0


PositionEntity discSmall, -70, 9, 115
PositionEntity discMedium, -70, 6, 115
PositionEntity discLarge, -70, 3, 115
PositionEntity ds, -70, 9, 115
PositionEntity dm, -70, 6, 115
PositionEntity dl, -70, 3, 115


End Function