'-------------------------------------------------
'---alfakilo Pair game 2012	alfakilo@email.ee---
'-------------------------------------------------
#Include "fbgfx.bi"
ScreenRes 400, 400, 32
Color Rgb(0, 50, 60), Rgb(111, 111, 111)
'
Declare Sub Load_images()
'
Dim Shared As Integer mx, my, mb,ff,fg,new_hiscore,hidetime
Dim Shared As Integer selectedalready,pair1,pair2,punktid,winy=400,mouseklicks,main_punktid
Dim Shared As Integer xvar,yvar,blockekokku
'IMAGE POINTERS
Dim Shared As Any Ptr taust,hideblock,win ,MENU,HISCORE
Dim Shared As Any Ptr blocknr1,blocknr2,blocknr3,blocknr4,blocknr5,blocknr6,blocknr7,blocknr8,blocknr9,blocknr10
Dim Shared As Any Ptr blocknr11,blocknr12,blocknr13,blocknr14,blocknr15,blocknr16,blocknr17,blocknr18,blocknr19,blocknr20

Type cardtype
     osx As Integer
     osy As Integer
     number As Integer
     isselected As Integer
     avatud As Integer
End Type
Dim Shared card() As cardtype

Load_images()

Declare Sub check_score(gamem As Integer,score As String)
'start game with this board
Declare Sub board_setup(board As String)
Dim Shared As String game_lev
Dim Shared As Integer gm=1'for hiscore check
Dim bm As Integer'for menu showing when restart
'GOING TO START MENU
Declare Sub menu_g()
menu_g()




Do  'DO MAIN LOOP
Sleep 10

Getmouse( mx, my, , mb )
'MOUSE CONTROL
For i As Integer = 1 To Ubound(card)
If card(i).avatud=0 And card(i).isselected=0 And selectedalready<2 Then
If mx > card(i).osx And mx < card(i).osx +30 And my > card(i).osy And my < card(i).osy +40 And mb =1 Then
selectedalready=selectedalready+1: card(i).isselected=selectedalready+1:mouseklicks=mouseklicks+1
EndIf
EndIf
Next

'Deselected automaticalli 
if selectedalready=2 Then
hidetime+=1
if hidetime>15 then
For i As Integer = 1 To Ubound(card)
card(i).isselected=0:selectedalready=0:pair1=0: pair2=0:hidetime=0
next
endif
endif

ScreenLock
    Cls
Put (0,0),taust,Trans
For i As Integer = 1 To Ubound(card)
If card(i).avatud=0 Then'kui pilt on avamata
If card(i).number=1 Then Put (card(i).osx,card(i).osy),blocknr1,Trans
If card(i).number=2 Then Put (card(i).osx,card(i).osy),blocknr2,trans
If card(i).number=3 Then Put (card(i).osx,card(i).osy),blocknr3,trans
If card(i).number=4 Then Put (card(i).osx,card(i).osy),blocknr4,trans
If card(i).number=5 Then Put (card(i).osx,card(i).osy),blocknr5,trans
If card(i).number=6 Then Put (card(i).osx,card(i).osy),blocknr6,trans
If card(i).number=7 Then Put (card(i).osx,card(i).osy),blocknr7,Trans
If card(i).number=8 Then Put (card(i).osx,card(i).osy),blocknr8,Trans
If card(i).number=9 Then Put (card(i).osx,card(i).osy),blocknr9,Trans
If card(i).number=10 Then Put (card(i).osx,card(i).osy),blocknr10,Trans
If card(i).number=11 Then Put (card(i).osx,card(i).osy),blocknr11,Trans
If card(i).number=12 Then Put (card(i).osx,card(i).osy),blocknr12,Trans
If card(i).number=13 Then Put (card(i).osx,card(i).osy),blocknr13,Trans
If card(i).number=14 Then Put (card(i).osx,card(i).osy),blocknr14,Trans
If card(i).number=15 Then Put (card(i).osx,card(i).osy),blocknr15,Trans
If card(i).number=16 Then Put (card(i).osx,card(i).osy),blocknr16,trans
If card(i).number=17 Then Put (card(i).osx,card(i).osy),blocknr17,Trans
If card(i).number=18 Then Put (card(i).osx,card(i).osy),blocknr18,Trans
If card(i).number=19 Then Put (card(i).osx,card(i).osy),blocknr19,Trans
If card(i).number=20 Then Put (card(i).osx,card(i).osy),blocknr20,Trans
EndIf

'peidab paarid pildiga
If card(i).isselected=0 And card(i).avatud=0 Then Put (card(i).osx,card(i).osy),hideblock,Trans

'DETECT PAIR-----------------------------
'GET SELECTED NUMBERS
If card(i).isselected=2 And selectedalready=1 Then pair1=card(i).number
If card(i).isselected=3 And selectedalready=2 Then pair2=card(i).number

Next
'DETECT PAIR
For i As Integer = 1 To Ubound(card)
If pair1=pair2 Then
If card(i).number=pair2 And card(i).isselected>1 Then card(i).avatud=1:card(i).isselected=0:punktid=punktid+1
EndIf
Next'------------------------------------
       
         
'level completed?
If punktid/2 = blockekokku/2 Then
Put (120,winy),win,Trans

'CHECKING HISCORE HERE
If winy=400 Then check_score(gm,Str(punktid *10 - mouseklicks) )

If winy>20 Then winy=winy-1
If Multikey(FB.SC_ENTER) Then winy=400:mouseklicks=0:punktid=0:bm=1

If new_hiscore=1 Then
	Draw String (120, winy-5), "NEW_HI_SCORE=" & punktid *10 - mouseklicks
	Put (160,winy+100),hiscore,Trans
EndIf

EndIf


Draw String (10, 350), "OPENED " & punktid/2 
Draw String (10, 365), "CLICKS " & mouseklicks
Draw String (10, 380), "SCORE=" & punktid *10 - mouseklicks

ScreenUnLock

If bm=1 Then menu_g():bm=0
Loop Until Multikey(FB.SC_ESCAPE)


'------------
'LOAD_IMAGES
Sub Load_images()
taust = ImageCreate( 400, 400 ) :BLoad "Data/tt.bmp", taust
hideblock = ImageCreate( 30, 40 ) :BLoad "Data/hideblock.bmp", hideblock
win = ImageCreate( 200, 200 ) :BLoad "Data/WIN.bmp", win
blocknr1 = ImageCreate( 30, 40 ) :BLoad "Data/1.bmp", blocknr1
blocknr2 = ImageCreate( 30, 40 ) :BLoad "Data/2.bmp", blocknr2
blocknr3 = ImageCreate( 30, 40 ) :BLoad "Data/3.bmp", blocknr3
blocknr4 = ImageCreate( 30, 40 ) :BLoad "Data/4.bmp", blocknr4
blocknr5 = ImageCreate( 30, 40 ) :BLoad "Data/5.bmp", blocknr5
blocknr6 = ImageCreate( 30, 40 ) :BLoad "Data/6.bmp", blocknr6
blocknr7 = ImageCreate( 30, 40 ) :BLoad "Data/7.bmp", blocknr7
blocknr8 = ImageCreate( 30, 40 ) :BLoad "Data/8.bmp", blocknr8
blocknr9 = ImageCreate( 30, 40 ) :BLoad "Data/9.bmp", blocknr9
blocknr10 = ImageCreate( 30, 40 ) :BLoad "Data/10.bmp", blocknr10
blocknr11 = ImageCreate( 30, 40 ) :BLoad "Data/11.bmp", blocknr11
blocknr12 = ImageCreate( 30, 40 ) :BLoad "Data/12.bmp", blocknr12
blocknr13 = ImageCreate( 30, 40 ) :BLoad "Data/13.bmp", blocknr13
blocknr14 = ImageCreate( 30, 40 ) :BLoad "Data/14.bmp", blocknr14
blocknr15 = ImageCreate( 30, 40 ) :BLoad "Data/15.bmp", blocknr15
blocknr16 = ImageCreate( 30, 40 ) :BLoad "Data/16.bmp", blocknr16
blocknr17 = ImageCreate( 30, 40 ) :BLoad "Data/17.bmp", blocknr17
blocknr18 = ImageCreate( 30, 40 ) :BLoad "Data/18.bmp", blocknr18
blocknr19 = ImageCreate( 30, 40 ) :BLoad "Data/19.bmp", blocknr19
blocknr20 = ImageCreate( 30, 40 ) :BLoad "Data/20.bmp", blocknr20
MENU = ImageCreate( 137, 100 ) :BLoad "Data/MEN.bmp", MENU
HISCORE = ImageCreate( 50, 50 ) :BLoad "Data/plank.bmp", HISCORE
End Sub'----------------------------

'------------
'SETUP BOARD
Sub board_setup(board As String)
'Leia kaartide hulk ,anna positsioonid
ff = Freefile
Open board For Input As #ff
If Err>0 Then
Close #ff
end
EndIf
Input #ff,blockekokku
ReDim card(1 To blockekokku)
For i As Integer= 1 To Ubound(card)
Input #ff,xvar,yvar
 card(i).osx=xvar
 card(i).osy=yvar
Next
Close #ff

Dim As Integer pair_count=blockekokku/2

Randomize Timer, 1
'PAIR SIDE 111111111111
Dim nums(blockekokku) As Integer
For t As Integer=1 To pair_count
	nums(t)=t
Next
Dim sd As Integer
For i As Integer=1 To pair_count
Do
sd=nums(Rnd(pair_count)*pair_count)
If sd<>0 Then
card(i).number=nums(sd)
nums(sd)=0
EndIf
Loop Until sd<>0
Next
'PAIR SIDE 222222222222
Dim nums2(blockekokku) As Integer
For t As Integer=1 To pair_count
	nums2(t)=t
Next
For i As Integer=pair_count+1 To blockekokku
Do
sd=nums2(Rnd(pair_count)*pair_count)
If sd<>0 Then
card(i).number=nums2(sd)
nums2(sd)=0
EndIf
Loop Until sd<>0
Next

End Sub

'------------------
'HI SCORE CHECKING
Sub check_score(gamem As Integer,score As String)
Dim best(3) As String
Dim As Integer overwrite=0
ff = Freefile
Open "Data/Hiscore.o" For Input As #ff
If Err>0 Then
Close #ff
end
EndIf
Input #ff,best(1)
Input #ff,best(2)
Input #ff,best(3)
Close #ff

	If Val(score)>Val(best(gamem)) Then
		overwrite=1
		best(gamem)=score
		new_hiscore=1
	Else
		overwrite=0
		new_hiscore=0
	EndIf
	
	If overwrite=1 Then
fg = Freefile
Open "Data/Hiscore.o" For Output As  #fg
Write #fg,best(1)
Write #fg,best(2)
Write #fg,best(3)
Close #fg
	EndIf

End Sub


'----------------
'DIFFICULTY MENU
Sub menu_g()

Dim As Integer START_GAME=0
Do
Sleep 100
ScreenLock
    Cls
Getmouse( mx, my, , mb )
   Put (0,0),TAUST,trans
   Put (120,100),MENU,Trans
ScreenUnLock
If MB=1 Then
   'EASY	
   If MX>120 And MX<220 And MY >100 And MY <120 Then
   game_lev="Data/Boards/simple.o"
   board_setup(game_lev)	:gm=1:START_GAME=1
   EndIf
   'MEDIUM
   If MX>120 And MX<220 And MY >130 And MY <146 Then
   game_lev="Data/Boards/medium.o"
   board_setup(game_lev)	:gm=2:START_GAME=1
   EndIf
   'HARD
   If MX>120 And MX<220 And MY >155 And MY <176 Then
   game_lev="Data/Boards/hard.o"
   board_setup(game_lev)	:gm=3:START_GAME=1
   EndIf
EndIf 
If Multikey(FB.SC_ESCAPE) Then end
Loop Until START_GAME=1

For I As INTEGER=1 To 100
   Sleep 5
   Locate 1:Print "Please wait " & I
Next
	
End Sub
'E_O_F