Пятнашки

Пример игры Пятнашки. Для компиляции нужна библиотека Window9
Платформы: Windows , Linux
Автор: Станислав Будинов 2010 - stabud@yandex.ru

Fifteeng.png

#INCLUDE "window9.bi"


Enum

Button_0
Button_1
Button_2
Button_3
Button_4
Button_5
Button_6
Button_7
Button_8
Button_9
Button_10
Button_11
Button_12
Button_13
Button_14
Button_15
End Enum


Dim Shared dvig As Integer

Dim As Integer event

Dim Shared As Long iSize = 75 , iXone = 5 , iYone

#IFDEF __FB_WIN32__
iYone = 10
#ELSE
iYone = 30
#ENDIF

Randomize Timer

SetGadgetFont(,LoadFont("arial",22,,,1))

Sub Open_Window_0()

   Var hwnd=OpenWindow("Пятнашки", 516, 346, 340, 390 )
   CenterWindow(hwnd)

   ButtonGadget(Button_1, iXone, iYone, iSize, iSize, "1")
   ButtonGadget(Button_2, iXone+80, iYone, iSize, iSize, "2")
   ButtonGadget(Button_3, iXone+160, iYone, iSize, iSize, "3")
   ButtonGadget(Button_4, iXone+240, iYone, iSize, iSize, "4")
   ButtonGadget(Button_5, iXone, iYone+80, iSize, iSize, "5")
   ButtonGadget(Button_6, iXone+80, iYone+80, iSize, iSize, "6")
   ButtonGadget(Button_7, iXone+160, iYone+80, iSize, iSize, "7")
   ButtonGadget(Button_8, iXone+240, iYone+80, iSize, iSize, "8")
   ButtonGadget(Button_9, iXone, iYone+160, iSize, iSize, "9")
   ButtonGadget(Button_10, iXone+80, iYone+160, iSize, iSize, "10")
   ButtonGadget(Button_11, iXone+160, iYone+160, iSize, iSize, "11")
   ButtonGadget(Button_12, iXone+240, iYone+160, iSize, iSize, "12")
   ButtonGadget(Button_13, iXone, iYone+240, iSize, iSize, "13")
   ButtonGadget(Button_14, iXone+80, iYone+240, iSize, iSize, "14")
   ButtonGadget(Button_15, iXone+160, iYone+240, iSize, iSize, "15")
   Var menu=Create_Menu()        'Создаем меню
   Var menName= MenuTitle(menu,"Меню")
   MenuItem(1,menName,"Новая игра")
   MenuItem(2,menName,"Об игре")
   MenuBar(menName)
   MenuItem(3,menName,"Выход")

End Sub


Sub Pustoe_mesto(a As Integer)

   Dim As Integer razmer_x,razmer_y,raz_x,raz_y,Schet,Schet1,Schet2,Schet3
   razmer_x = GadgetX(a)    'местонахождение гаджета
   razmer_y = GadgetY(a)

   For gad As Integer=1 To 15

      raz_x = GadgetX(gad)
      raz_y = GadgetY(gad)

      If razmer_x + 80 < 320 Then

         If razmer_x + 80 <> raz_x Or razmer_y <> raz_y Then    ' проверка справа
            Schet=Schet+1

         Endif
      Endif

      If razmer_x - 80 >= 5 Then

         If razmer_x - 80 <> raz_x  Or razmer_y <> raz_y  Then      'проверка слева
            Schet1=Schet1+1

         Endif
      Endif


      If razmer_y + 80 < 320+iYone Then

         If razmer_y + 80 <> raz_y  Or razmer_x <> raz_x Then        'проверка снизу
            Schet2=Schet2+1

         Endif
      Endif



      If razmer_y - 80 >= iYone Then

         If razmer_y - 80 <> raz_y  Or razmer_x <> raz_x Then          'проверка сверху
            Schet3=Schet3+1

         Endif
      Endif
   Next



   If Schet=15 Then

      dvig=1

   Endif

   If Schet1=15 Then

      dvig=2

   Endif

   If Schet2=15 Then

      dvig=3

   Endif

   If Schet3=15 Then

      dvig=4


   Endif

End Sub


Sub peremeshenie(a As Integer,b As Integer)


   Select Case a
      Case 1

         ResizeGadget(b, GadgetX(b)+80)              'Перемещение кнопок
      Case 2

         ResizeGadget(b, GadgetX(b)-80)
      Case 3

         ResizeGadget(b,,GadgetY(b)+80)
      Case 4

         ResizeGadget(b,,GadgetY(b)-80)
   End Select

   dvig=0

End Sub


Sub sluch()
   Dim  gadget As Integer

   For ww As Integer=1 To 2000


      gadget= Rnd*15              ' случайная расстановка
      If gadget=0 Then

         gadget=1

      Endif
      Pustoe_mesto(gadget)
      peremeshenie(dvig,gadget)
   Next


End Sub




Open_window_0()

sluch()

Do


   event=WindowEvent()

   If event=EventGadget  Then

      For gadget As Integer=1 To 15

         If gadget = EventNumber    Then              ' Идентификация гаджета
            Pustoe_mesto(gadget)
            peremeshenie(dvig,gadget)
            Exit For

         Endif
      Next

   Endif


   If GadgetX(1)+GadgetY(1)=iXone+iYone And GadgetX(2)+GadgetY(2)=iXone+iYone+80 And GadgetX(3)+GadgetY(3)=iXone+iYone+160 And GadgetX(4)+GadgetY(4)=iXone+iYone+240 Then

      If GadgetX(5)+GadgetY(5)=iXone+iYone+80 And GadgetX(6)+GadgetY(6)=iXone+iYone+160 And GadgetX(7)+GadgetY(7)=iXone+iYone+240 And GadgetX(8)+GadgetY(8)=iXone+iYone+320 Then

         If GadgetX(9)+GadgetY(9)=iXone+iYone+160 And GadgetX(10)+GadgetY(10)=iXone+iYone+240 And GadgetX(11)+GadgetY(11)=iXone+iYone+320 And GadgetX(12)+GadgetY(12)=iXone+iYone+400 Then

            If GadgetX(13)+GadgetY(13)=iXone+iYone+240 And GadgetX(14)+GadgetY(14)=iXone+iYone+320 And GadgetX(15)+GadgetY(15)=iXone+iYone+400 Then

               MessBox("Победа", "Молодец ты сумел(а) собрать пятнашку.")
               sluch()
            Endif
         Endif
      Endif
   Endif

   If event=EventMenu  Then 'события меню
      Select Case  EventNumber
         Case 1

            sluch()
         Case 2

            MessBox("Об игре", "Создано в Freebasic, автор: Станислав Будинов")
         Case 3

            End

      End Select

   Endif
   
Loop Until event=EventClose