#Include "Inc\RotateSprite.bas"
#Include "Inc\Class Dialog.bas"
#Ifdef __FB_WIN32__
Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" ( _
ByVal lpszSoundName As String, _
ByVal uFlags As Long) As Long
#EndIf

Using minigui

Type RotateGame
	M(1 To 16) As Integer =>{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}
	MSave(1 To 16) As Integer
	As Integer placeclick,Xrotate,Yrotate,flagcheckVictory
	As Double timer_
	As Any Ptr roto,bg,img,vic
	md As dialog Ptr = New Dialog(0,0,240,290,"RotateGame",&hFf0000)
	but As Button Ptr = New Button(95,260,50,17,"Reset",&hff0000,0)
	Declare Sub Intro()
	Declare Sub Drawbg()
	Declare Sub Place()
	Declare Function checkPlace(n As Integer) As Integer
	Declare Function checkVictory() As Integer
	Declare Sub setRotate(rot As Integer)
	Declare Sub rotate(n As Integer , rot As Integer)
	Declare Sub ResetMatrix()
	Declare Sub R_N_D()
	Declare Sub drawM()
	Declare Sub EventRotate()
	Declare Destructor()
	Declare Constructor()
End Type

Dim obj As RotateGame

Constructor RotateGame()
	Randomize Timer
	md->SetColorMiniframe(&hff0000)
	roto = ImageCreate(90,90)
	bg = ImageCreate(64,64)
	img =  ImageCreate(230,257)
	vic =  ImageCreate(199,199)
	BLoad "Res\bg.bmp",bg
	BLoad "Res\freebasic.bmp",img
	BLoad "Res\Victory.bmp",vic
	R_N_D()
	md->FrameClick()
	md->Update()
	Intro()
	EventRotate()
End Constructor

Destructor RotateGame()
ImageDestroy(roto)
ImageDestroy(bg)
ImageDestroy(img)
ImageDestroy(vic)
End Destructor

Sub RotateGame.Intro()
	For i As Integer = 1 To 100
		Put(5,28),img,Alpha,i\3
		Sleep(20)
		PCopy
	Next
	Color,&hBF005F
	Cls
End Sub

Sub RotateGame.Drawbg()
	For i As Integer = 0 To 5
		For j As Integer = 0 To 5
			Put (i*64+4,j*64+26),bg,Alpha
		Next
	Next
End Sub

Sub RotateGame.Place()
	Dim t As Integer=1
	If flagcheckVictory =0 Then
		For i As Integer = 43 To 242
			Line(20,i)-Step(199,1),&hAF0000 + i And &hffFFFF,BF
		Next
	Else
		Put(20,43),vic,Alpha
	EndIf
	For y As Integer = 65 To 170 Step 50
		For x As Integer = 42 To 142 Step 50
			If md->MX>=x And md->MX<x+50 And md->MY>=y And md->MY<y+50 Then
				If flagcheckVictory =0 Then
					Line(x-10,y-10)-Step(75,75),&hBF005F,BF
					Xrotate = x+25
					Yrotate = y+25
					placeclick=t
				EndIf
				Exit Sub
			EndIf
			t+=1
		Next
	Next
	placeclick=0
End Sub

Function RotateGame.checkPlace(n As Integer) As Integer
	If m(n)= n Then
		Return &hFF00ff00
	Else
		Return &hFFffffff
	EndIf
End Function
Sub sound()
	Sleep(200)
	#Ifdef __FB_WIN32__
	sndPlaySound( "Res/tada.wav", 0 )
	#EndIf
End Sub
Function RotateGame.checkVictory() As Integer
	Dim v As Integer
	For i As Integer = 1 To 16
		If m(i)=i Then
			v+=1
		EndIf
	Next
	If v=16 Then
		ThreadCreate(Cast(Any Ptr,@sound))
		Return 1
	EndIf
End Function

Sub RotateGame.setRotate(rot As Integer)
	Get (Xrotate-35,Yrotate-35)-Step(75,75),roto
	View(Xrotate-35,Yrotate-35)-(Xrotate+40,Yrotate+40)
	For i As Single = 0 To 95
		Cls(1)
		If rot = 1 Then
			RotateScale(,roto,Xrotate,Yrotate,-i,0.7)
		Else
			RotateScale(,roto,Xrotate,Yrotate,i,0.7)
		EndIf
		PCopy
		Sleep(6)
	Next
	View(0,0)-(240,290)
End Sub

Sub RotateGame.rotate(n As Integer , rot As Integer)
	Select Case n
		Case 4,5,6:n+=1
		Case 7,8,9:n+=2
	End Select
	If rot=1 Then
		Swap M(n),M(n+1)
		Swap M(n+1),M(n+5)
		Swap M(n+4),M(n+5)
	Else
		Swap M(n),M(n+1)
		Swap M(n),M(n+4)
		Swap M(n+5),M(n+4)
	EndIf
End Sub

Sub RotateGame.ResetMatrix()
	For i As Integer = 1 To 16
		M(i)=MSave(i)
	Next
End Sub

Sub RotateGame.R_N_D()
	For i As Integer = 1 To 30
		rotate(Rnd*9+1,Rnd*1+1 )
	Next
	For i As Integer = 1 To 16
		MSave(i)=M(i)
	Next
End Sub

Sub RotateGame.drawM()
	Dim t As Integer=1
	If flagcheckVictory=0 Then
		For y As Integer = 65 To 220 Step 50
			For x As Integer = 42 To 212 Step 50
				Draw String (x,y),Str(M(t)),checkPlace(t),,Alpha
				t+=1
			Next
		Next
	EndIf
End Sub

Sub RotateGame.EventRotate()
	Do
		Cls
		Drawbg()
		md->Update()
		but->update()
		Place()
		drawM()
		If (ScreenEvent(@AllEvent)) Then
			md->FrameClick()
			If but->Click Then
				If flagcheckVictory = 0 Then
					ResetMatrix()
				Else
					R_N_D()
					but->settext("Reset")
					flagcheckVictory = 0
				EndIf
			EndIf
			If AllEvent.type=EVENT_MOUSE_BUTTON_PRESS Then
				If flagcheckVictory =0 Then
					If placeclick <> 0 Then
						rotate(placeclick,AllEvent.button)
						setRotate(AllEvent.button)
						flagcheckVictory = checkVictory()
						If flagcheckVictory = 1 Then
							but->settext("New")
						EndIf
					EndIf
				EndIf
			EndIf
		EndIf
		PCopy
		Sleep(1)
		If MultiKey(SC_ESCAPE) Then Exit Do
	Loop
End Sub
