' Игра "Быки-коровы" Автор: Станислав Будинов
#Include "window9.bi"
IncludeBinary ("Res/bik.png",Background)
IncludeBinary ("Res/KOBI.png",Image3)
IncludeBinary ("Res/pe.wav",PE)
IncludeBinary ("Res/tada.wav",TADA)
IncludeBinary ("Res/ding.wav",DING)
IncludeBinary ("Res/VER.wav",VER)
IncludeBinary ("Res/UD.wav",UD)

#Define PLAYS(a) (sndPlaySound(@a(0),SND_ASYNC Or SND_MEMORY))
Dim Shared As HFONT font
Dim As Integer b=11,BIK,event,VOPROS
Dim As String Sluch_VALUE
Dim Shared As HWND abouthwnd,mainhwnd
font=LoadFont("Courier",16,,1)
Randomize Timer
'Получение случайного числа
Function SLUCH() As String
	Dim As Integer ONE,TWO,THREE,FOUR
	ONE=Int(Rnd*9)+1
	MET1:
	TWO=Int(Rnd*9)+1
	If TWO=ONE Then GoTo MET1
	MET2:
	THREE=Int(Rnd*9)+1
	If THREE=ONE Or THREE=TWO Then GoTo MET2
	MET3:
	FOUR=Int(Rnd*9)+1
	If FOUR=ONE Or FOUR=TWO Or FOUR=THREE Then GoTo MET3
	Return ONE & TWO & THREE & FOUR
End Function

'Ввод числа с проверкой на дубликаты
Sub VVOD_CHISLA(a As Integer,b As Integer)
	If InStr(GetGadgetText(b),Str(a))=0 Then
		SetGadgetText(b,GetGadgetText(b)+Str(a))
		PLAYS(DING)
	EndIf
End Sub
'Сравнение случайного числа и вводимого
Function Podschet(s As String,v As String,b As Integer) As Integer
	Dim As Integer BIK,KOR
	If Mid(s,1,1)=Mid(v,1,1) Then BIK+=1
	If Mid(s,1,1)=Mid(v,2,1) Or Mid(s,1,1)=Mid(v,3,1) Or Mid(s,1,1)=Mid(v,4,1) Then KOR+=1
	If Mid(s,2,1)=Mid(v,2,1) Then BIK+=1
	If Mid(s,2,1)=Mid(v,1,1) Or Mid(s,2,1)=Mid(v,3,1) Or Mid(s,2,1)=Mid(v,4,1)Then KOR+=1
	If Mid(s,3,1)=Mid(v,3,1) Then BIK+=1
	If Mid(s,3,1)=Mid(v,1,1) Or Mid(s,3,1)=Mid(v,2,1) Or Mid(s,3,1)=Mid(v,4,1)Then KOR+=1
	If Mid(s,4,1)=Mid(v,4,1) Then BIK+=1
	If Mid(s,4,1)=Mid(v,1,1) Or Mid(s,4,1)=Mid(v,2,1) Or Mid(s,4,1)=Mid(v,3,1)Then KOR+=1
	SetGadgetText(b,BIK & "Б" & KOR & "К")
	Return BIK
End Function
'Окно "О программе"
Sub Open_About()
	abouthwnd=OpenWindow("Об игре", 347, 125, 550, 620)
	ImageGadget(100, 0, 0, 550, 584, Catch_Image(@Image3()))
End Sub
'Главное окно
Sub OpenWindow_Window_0()
	Dim As Integer c,d,e,a,b
	mainhwnd=OpenWindow("Window_0", 449, 200, 300, 499,WS_POPUP Or WS_VISIBLE)
	WindowBackgroundImage(mainhwnd,Catch_Image(@Background()))
	c=55
	d=363
	e=1
	For a=1 To 2
		For b=1 To 5
			ButtonGadget(e,c,d,36,36,Str(e))
			SetGadgetFont(e,Cast(Integer,Font))
			c+=40
			e+=1
		Next
		d+=50
		c=55
	Next
	SetGadgetText(10,"X")
	c=78
	d=88
	e=11
	For a=1 To 2
		For b=1 To 7
			TextGadget(e,c,d,50,20,"")
			SetGadgetFont(e,Cast(Integer,Font))
			SetGadgetColor(e,&hFFFF00,&hFFFF00,1)
			d+=30
			e+=1
		Next
		d=88
		c+=105
	Next
	TextGadget(26,275,15,10,13,"Х")
	SetGadgetColor(26,&hFFFF00,&hFFFF00,1)
	TextGadget(30,263,15,10,13,"?")
	SetGadgetColor(30,&hFFFF00,&hFFFF00,1)
	ButtonGadget(25,132,320,47,20,"Верно")
	DisableGadget(25,1)
End Sub

OpenWindow_Window_0()
Sluch_VALUE=SLUCH()
' Главный цикл
Do
	Event = WaitEvent()
	Select Case Event

		Case EventGadget
			Select Case EventNumber
				Case 1 To 9           ' тест кнопок с 1-9
					VVOD_CHISLA(EventNumber,b)
					If Len(GetGadgetText(b))=4 Then
						For g As Integer=1 To 9
							DisableGadget(g,1)
						Next
						DisableGadget(25,0)
					EndIf
				Case 10       'тест 10 кнопки "удаление"
					If Len(GetGadgetText(b))=4 Then
						SetGadgetText(b,Mid(GetGadgetText(b),1,3))
						For g As Integer=1 To 9
							DisableGadget(g,0)
						Next
						DisableGadget(25,1)
						PlayS(UD)
					ElseIf Len(GetGadgetText(b))=3 Then
						SetGadgetText(b,Mid(GetGadgetText(b),1,2))
						PlayS(UD)
					ElseIf Len(GetGadgetText(b))=2 Then
						SetGadgetText(b,Mid(GetGadgetText(b),1,1))
						PlayS(UD)
					ElseIf Len(GetGadgetText(b))=1 Then
						SetGadgetText(b,"")
						PlayS(UD)
					EndIf
				Case 25    'тест кнопки "верно"
					BIK=Podschet(Sluch_VALUE,GetGadgetText(b),b+7)
					If BIK<>4 And b<>17 Then
						PlayS(VER)
					EndIf
					If BIK=4 Then
						PlayS(TADA)
						VOPROS=MessBox("Поздравление","Вы отгадали число,примите наши поздравления! Хотите сыграть еще раз!",MB_YESNO)
					ElseIf b=17 Then
						PlayS(PE)
						VOPROS=MessBox("Сообщение","Вы не смогли отгадать число! Хотите сыграть еще раз!",MB_YESNO)
					EndIf
					b+=1
					If VOPROS=IDYES Then
						For g As Integer=11 To 24
							SetGadgetText(g,"")
						Next
						b=11
						VOPROS=0
						Sluch_VALUE=SLUCH()
					ElseIf VOPROS=IDNo Then
						Exit Do
					EndIf
					For g As Integer=1 To 9
						DisableGadget(g,0)
					Next
					DisableGadget(25,1)
				Case 26
					Exit Do
				Case 30
					Open_About()
			End Select
		Case EventClose
			Close_Window(abouthwnd)
	End Select
Loop
