#Include "XO.bi"

Obj.Gui()

Sub XO.NewGame()Export

	Dim As Integer g=1

	For y As Integer=0 To lastSize

		For x As Integer=0 To lastSize

			If g<>0 Then FreeGadget(g)

			g+=1

		Next

	Next

	g=1

	For y As Integer=0 To size_y

		For x As Integer=0 To size_x

			TextGadget(g,x*20+2,y*20+2,20,20,"",WS_BORDER Or SS_CENTER Or SS_CENTERIMAGE)

			fields(x,y)=0

			calc_fields(x,y)=0

			g+=1

		Next

	Next
	
	ResizeWindow(GetParent(GadgetID(1)),,,(size_x+1)*20+20,(size_x+1)*20+62)

	lastSize=size_x

	flag=0

	attack_factor=1

	valuation_factor=3

End Sub

Sub XO.Gui()Export

	size_x=19:size_y=19

	Var hwnd=OpenWindow("XO , Autor: A.S.Lebedev, Adapted: S.Budinov",10,10,(size_x+1)*20+20,(size_y+1)*20+42)
	
	Var icon=LoadIcon(GetModuleHandle(0),MAKEINTRESOURCE(100))
	
	SendMessage(hwnd,WM_SETICON,ICON_BIG,Cast(Lparam,icon))
	
	SendMessage(hwnd,WM_SETICON,ICON_SMALL,Cast(Lparam,icon))
	
	CenterWindow(hwnd)
   
	NewGame

	hMenu=Create_Menu()

	MenName=MenuTitle(hMenu,"New Game")

	MenNameCHild_1=OpenSubMenu(MenName,"10õ10")
	
	MenuItem(1,MenNameCHild_1,"The average opponent")
	
	MenuItem(2,MenNameCHild_1,"The strong opponent")
	
	MenNameCHild_2=OpenSubMenu(MenName,"20õ20")
	
	MenuItem(3,MenNameCHild_2,"The average opponent")
	
	MenuItem(4,MenNameCHild_2,"The strong opponent")	

	Do

		Var event=WaitEvent

		Select Case event

			Case eventgadget

				Select Case EventNumber

					Case 1 To (size_x+1)*(size_x+1)

						If flag=0 Then

							If GetGadgetText(EventNumber)="" Then

								SetGadgetText(EventNumber,"X")

								fields((EventNumber-1) Mod (size_x+1),(EventNumber-1)\(size_x+1))=1

								If end_analyze()=0 Then
									
									ii
									
								Else
									
									Beep
									
								EndIf
								
							EndIf

						EndIf

				End Select

			Case eventmenu

				Select Case EventNumber

					Case 1
						
						size_x=9 : size_y=9 : comp_level=1 : NewGame
						
					Case 2

						size_x=9 : size_y=9 : comp_level=0 : NewGame

					Case 3
						
						size_x=19 : size_y=19 : comp_level=1 : NewGame
						
					Case 4	

						size_x=19 : size_y=19 : comp_level=0 : NewGame

				End Select

			Case eventclose

				Exit Do

		End Select

	Loop

End Sub
