'*******************************************************************************
' Авторы данной игры:
' Roland Chastain  - искуственный интеллект
' Станислав Будинов  - интерфейс
'*******************************************************************************
#Ifdef __FB_WIN32__
#include "inc\uFMOD.bi"
#Else
#include "inc\ALSAuFMOD.bi"
#EndIf
#Include "inc\Class Dialog.bas"
#Include "inc\Class ImagePushButton.bas"
#Include "inc\alg.bas"
#Include "inc\filework.bas"

Using minigui

Type four
	As UByte r,g,b
	As Integer newplace,oldplace,flagnew,Xnew,Ynew, _
	Finish,SoundFlag,graphicsflag,PressFlag,Releaseflag, ArrowID , drawgame
	As Any Ptr ImgC(14)
	md As dialog Ptr = New Dialog(0,0,648,511,"FOUR",&h000000)
	graphicsB As ImagePushButton Ptr
	soundB As ImagePushButton Ptr
	FileWork As FileWork
	SoundWork As SoundWork
	Declare Constructor ()
	Declare Destructor()
	Declare Sub Eventfour()
	Declare Sub GetX()
	Declare Sub GetY()
	Declare Sub drawall()
	Declare Sub AlgMove()
	Declare Sub Check()
	Declare Sub drawAnim()
	Declare Sub Draw_Arrow ()
	Declare Sub Copy()
	Declare Sub GetColor(Col As Integer)
	Declare Sub SoundPlay()
	Declare Sub DrawCircle(number As Integer, col As Integer)
End Type

Dim shared obj As four
obj.Eventfour()
Constructor four()
	ImgC(3)=ImageCreate(640,480,0)
	For i As Integer = 0 To 2
		ImgC(i)=ImageCreate(40,40,0)
		ImgC(i+4)=ImageCreate(23,21,0)
	Next
	ImgC(7)=ImageCreate(50,46,0)
	ImgC(8)=ImageCreate(50,46,0)
	ImgC(9)=ImageCreate(53,49,0)
	ImgC(10)=ImageCreate(53,49,0)
	ImgC(11)=ImageCreate(369,318,0)
	ImgC(12)=ImageCreate(251,40,0)
	ImgC(13)=ImageCreate(229,40,0)
	ImgC(14)=ImageCreate(321,35,0)
	FileWork.LoadIm(-1,3,ImgC(3))
	FileWork.LoadIm(-2,3,ImgC(11))

	BLoad("res/another/gr.bmp",ImgC(4))
	BLoad("res/another/sy.bmp",ImgC(5))
	BLoad("res/another/sn.bmp",ImgC(6))

	BLoad("res/message/lost.bmp",ImgC(12))
	BLoad("res/message/draw.bmp",ImgC(13))
	BLoad("res/message/win.bmp",ImgC(14))

	BLoad("res/arrow/strl.bmp",ImgC(7))
	BLoad("res/arrow/strr.bmp",ImgC(8))
	BLoad("res/arrow/strl1.bmp",ImgC(9))
	BLoad("res/arrow/strr1.bmp",ImgC(10))

	graphicsB = New ImagePushButton(582,479,27,25,ImgC(4),0,2,2)
	soundB = New ImagePushButton(614,479,27,25,ImgC(5),0,2,2)
	graphicsB->SetImage(ImgC(4),0)
	soundB->SetImage(ImgC(5),0)
	DrawCircle(0,&hFF0000)
	DrawCircle(2,&h0000FF)
	Initialise
	Randomize Timer
	SoundPlay()
End Constructor


Destructor four()
	For i As Integer = 0 To 14
		ImageDestroy(ImgC(i))
	Next
End Destructor

Sub four.SoundPlay()
	If SoundFlag=0 Then
		#Ifdef __FB_WIN32__
		uFMOD_PlaySong(@SoundWork.soundClip(SoundWork.CurentTrek), 0, XM_FILE)
		#Else
		uFMOD_ALSAPlaySong(@SoundWork.soundClip(SoundWork.CurentTrek), 0, XM_FILE, "plughw:0,0")
		#EndIf
		SoundWork.CurentTrek+=1
		if SoundWork.CurentTrek>SoundWork.totalNumberOfClips then SoundWork.CurentTrek=0
	Else
		uFMOD_StopSong()
	EndIf
End Sub

Private Sub four.GetColor(Col As Integer)
	r = CUByte(HiWord(Col))
	g = CUByte(LoWord(HiByte(Col)))
	b = CUByte(LoWord(LoByte(Col)))
End Sub

Sub four.DrawCircle(number As Integer, col As Integer)
	GetColor(col):Dim As Integer rad = 20
	Dim K As Integer = Sqr(5*rad)
	For i As Integer = 0 To rad*2-1
		For j As Integer = 0 To rad*2-1
			Dim delta2 As Integer = rad*rad - (i-rad)*(i-rad) - (j-rad+1)*(j-rad+1)
			If delta2 >= 0 Then
				If delta2 >= k*k Then
					delta2 = k*k
				End If
				PSet ImgC(number), (i,j),RGBA(r,g,b,&hA7*(delta2/K/K)*(delta2/K/K)*(delta2/K/K))
			End If
		Next j
	Next i
	For i As Integer = 0 To rad-K/2
		Circle ImgC(1),(rad,rad-K/2-i),rad-K/2-i,RGBA(255,255,255,i*((300-i)\rad)),,,1,F
	Next
End Sub

Sub four.GetX()
	If md->MY > 30 Then
		For i As Integer = 0 To 6
			If md->MX >=142+52*i And md->MX < 194+52*i Then
				newplace = i
				If newplace <> oldplace Then
					oldplace = newplace
				EndIf
			EndIf
		Next
	EndIf
End Sub

Sub four.GetY()
	For y As Integer = 0 TO 5
		if CouranteCopy.c(Xnew,y)=0 Then
			Ynew = 5-y
			exit Sub
		EndIf
	Next
End Sub

Sub four.Copy()
	For y As Integer = 0 To 5
		For x As Integer = 0 To 6
			CouranteCopy.c(x,y)=Courante.c(x,y)
		Next
	Next
End Sub

Sub four.Draw_Arrow ()
	Put(143,244),ImgC(7),Alpha
	Put(455,244),ImgC(8),Alpha
	Put(250,444),ImgC(7),Alpha
	Put(347,444),ImgC(8),Alpha
	If AllEvent.type = EVENT_MOUSE_BUTTON_PRESS Then
		If Releaseflag = 1 Then
			PressFlag = 1 : Releaseflag = 0
		Else
			PressFlag = 2
		EndIf
	ElseIf AllEvent.type = EVENT_MOUSE_BUTTON_RELEASE Then
		PressFlag = 0 : Releaseflag = 1
	EndIf
	Select Case md->MY
		Case 242 To 295
			If md->MX>=143 And md->MX<=196 Then
				If PressFlag = 0 Then
					Put(143,244),ImgC(9),Alpha
				ElseIf PressFlag = 1 Then
					FileWork.LoadIm(2,1,ImgC(11))
				EndIf
			ElseIf md->MX>=455 And md->MX<=508 Then
				If PressFlag = 0 Then
					Put(455,244),ImgC(10),Alpha
				ElseIf PressFlag = 1 Then
					FileWork.LoadIm(2,2,ImgC(11))
				EndIf
			EndIf
		Case 442 To 495
			If md->MX>=250 And md->MX<=303 Then
				If PressFlag = 0 Then
					Put(250,444),ImgC(9),Alpha
				ElseIf PressFlag = 1 Then
					FileWork.LoadIm(1,3,ImgC(3))
				EndIf
			ElseIf md->MX>=347 And md->MX<=500 Then
				If PressFlag = 0 Then
					Put(347,444),ImgC(10),Alpha
				ElseIf PressFlag = 1 Then
					FileWork.LoadIm(1,4,ImgC(3))
				EndIf
			EndIf
	End Select
End Sub

Sub four.Eventfour()
	Do
		Cls
		md->Update()
		Put(4,27),ImgC(3),PSet
		Put(139,106),ImgC(11),PSet
		If (ScreenEvent(@AllEvent)) Then
			md->FrameClick()
			If graphicsB->Click()Then
				graphicsflag Xor=1
			EndIf
			If soundB->Click() Then
				SoundFlag Xor= 1
				SoundPlay()
				soundB->SetImage(ImgC((5+SoundFlag)))
			EndIf
			If graphicsflag = 0 Then
				GetX()
				If AllEvent.type = EVENT_MOUSE_BUTTON_PRESS And Trait <> Neant Then
					AlgMove()
				ElseIf AllEvent.type = EVENT_KEY_PRESS And Trait = Neant Then
					finish = 0
					Initialise
					SoundPlay()
				EndIf
			EndIf
		EndIf
		graphicsB->Update()
		soundB->Update()
		If graphicsflag = 0 Then
			drawall()
			If Trait <> Neant Then
				Put(oldplace*52+148,60),ImgC(0),Alpha
				Put(oldplace*52+148,60),ImgC(1),Alpha
			EndIf
			If Finish = 1 Then
				Put(165,450),ImgC(14),Alpha
				uFMOD_StopSong()
			ElseIf Finish = 2 Then
				Put(225,450),ImgC(12),Alpha
				uFMOD_StopSong()
			ElseIf Finish = 3 Then
				Put(233,450),ImgC(13),Alpha
				uFMOD_StopSong()
			EndIf 
		Else
			Draw_Arrow ()
		EndIf
		PCopy
		Sleep(2)
		If MultiKey(SC_ESCAPE) Then Exit Do
	Loop
End Sub

Sub four.drawall()
	For y As Integer = 5 To 0 Step -1
		For x As Integer = 0 To 6
			if flagnew = -1 Then
				If CouranteCopy.c (x, y)=1 Then
					Put(x*52+148,(5-y)*52+117),ImgC(0),Alpha
					Put(x*52+148,(5-y)*52+117),ImgC(1),Alpha
				ElseIf CouranteCopy.c (x, y)=-1 Then
					Put(x*52+148,(5-y)*52+117),ImgC(2),Alpha
					Put(x*52+148,(5-y)*52+117),ImgC(1),Alpha
				EndIf
			Else
				If Courante.c (x, y)=1 Then
					Put(x*52+148,(5-y)*52+117),ImgC(0),Alpha
					Put(x*52+148,(5-y)*52+117),ImgC(1),Alpha
				ElseIf Courante.c (x, y)=-1 Then
					Put(x*52+148,(5-y)*52+117),ImgC(2),Alpha
					Put(x*52+148,(5-y)*52+117),ImgC(1),Alpha
				EndIf
			EndIf
		Next
	Next
End Sub

Sub four.drawAnim()
	For i As Integer = 60 To 117+Ynew*52 Step 3
		If (ScreenEvent(@AllEvent)) Then
			md->FrameClick()
		EndIf
		Cls
		md->Update()
		Put(4,27),ImgC(3),PSet
		Put(139,106),ImgC(11),PSet
		graphicsB->Update()
		soundB->Update()
		drawall()
		Put(Xnew*52+148,i),ImgC(Automate),Alpha
		Put(Xnew*52+148,i),ImgC(1),Alpha
		Sleep(2)
		PCopy
	Next
End Sub

Sub four.Check()
	Pions -= 1
	If (Score (Courante, Trait) > 3) Then
		Trait = Neant
	ElseIf Pions = 0 then
		Trait = Neant
		drawgame = 1
	Else
		Trait *= -1
	End If
	GetY()
	drawAnim()
	Copy()
End Sub

Sub four.AlgMove()
	If md->MY < 30 Or (md->MY>=478 And md->MX>580) Then Exit Sub
	flagnew = Ajoute (Courante, Trait, oldplace)
	If flagnew=-1 Then
		Xnew = oldplace
		Automate = 0
		Check()
		If  Trait = Neant Then
			Finish = 1
			If drawgame = 1 Then
				Finish = 3
				drawgame = 0
			EndIf
			Exit Sub
		EndIf
		Xnew = MeilleurCoup
		Ajoute (Courante, Trait,Xnew)
		Automate = 2
		Check()
		If  Trait = Neant Then
			Finish = 2
			If drawgame = 1 Then
				Finish = 3
				drawgame = 0
			EndIf
			Exit Sub
		EndIf
		flagnew=0
	EndIf
End Sub

