Seite 1 von 5

Tic Tac Toe

Verfasst: 29.01.2006 03:29
von R4z0r1989
Da mir mal wieder langweilig war, hab ich mich mal hingesetzt und Tic Tac Toe geproggt!!!

Es ist zwar lang und komplieziert aufgebaucht(ich weiß ich es zu lang gemacht hab)

Wer Lust hat es zu kürzen aber keine Funktion verändert der soll ruhig machen!!!

Code: Alles auswählen

; PureBasic Visual Designer v3.80 build 1249


;- Window Constants
;
Enumeration
  #Window_0
EndEnumeration

;- Gadget Constants
;
Enumeration
  #OL
  #OM
  #OR
  #ML
  #MM
  #MR
  #UL
  #UM
  #UR
  #NAchricht
  #Spielende
  #Neustart
EndEnumeration

;- Fonts
;
Global FontID1
FontID1 = LoadFont(1, "Arial", 24)
Global FontID2
FontID2 = LoadFont(2, "Arial", 12)

Procedure Open_Window_0()
  If OpenWindow(#Window_0, 216, 0, 190, 260,  #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar , "Tic Tac Toe")
    If CreateGadgetList(WindowID())
      ButtonGadget(#OL, 10, 10, 50, 50, "")
      SetGadgetFont(#OL, FontID1)
      ButtonGadget(#OM, 70, 10, 50, 50, "")
      SetGadgetFont(#OM, FontID1)
      ButtonGadget(#OR, 130, 10, 50, 50, "")
      SetGadgetFont(#OR, FontID1)
      ButtonGadget(#ML, 10, 70, 50, 50, "")
      SetGadgetFont(#ML, FontID1)
      ButtonGadget(#MM, 70, 70, 50, 50, "")
      SetGadgetFont(#MM, FontID1)
      ButtonGadget(#MR, 130, 70, 50, 50, "")
      SetGadgetFont(#MR, FontID1)
      ButtonGadget(#UL, 10, 130, 50, 50, "")
      SetGadgetFont(#UL, FontID1)
      ButtonGadget(#UM, 70, 130, 50, 50, "")
      SetGadgetFont(#UM, FontID1)
      ButtonGadget(#UR, 130, 130, 50, 50, "")
      SetGadgetFont(#UR, FontID1)
      ButtonGadget(#Neustart, 10, 220, 80, 30, "Neues Spiel")
      ButtonGadget(#Spielende, 100, 220, 80, 30, "Spiel Beenden")
      TextGadget(#NAchricht, 10, 190, 170, 20, "", #PB_Text_Center)
      SetGadgetFont(#NAchricht, FontID2)
      
    EndIf
  EndIf
EndProcedure


Open_Window_0()

SetGadgetText(#Nachricht,"X ist dran")

Repeat 
  
  
  Event = WaitWindowEvent()
 
  If OL.l > 0 And OM.l > 0 And ORR.l > 0 And ML.l > 0 And MM.l > 0 And MR.l > 0 And UL.l > 0 And UM.l > 0 And UR.l > 0
    SetGadgetText(#Nachricht,"keiner hat gewonnen")
  EndIf
 
  
  If      OL.l = 1 And OM.l = 1 And ORR.l = 1
    SetGadgetText(#NAchricht,"O gewinnt")    
    DisableGadget(#OL,1) : DisableGadget(#OM,1) : DisableGadget(#OR,1) : DisableGadget(#ML,1) : DisableGadget(#MM,1) : DisableGadget(#MR,1) : DisableGadget(#UL,1) : DisableGadget(#UM,1) : DisableGadget(#UR,1)
  ElseIf ML.l = 1 And MM.l = 1 And MR.l = 1
    SetGadgetText(#NAchricht,"O gewinnt")    
    DisableGadget(#OL,1) : DisableGadget(#OM,1) : DisableGadget(#OR,1) : DisableGadget(#ML,1) : DisableGadget(#MM,1) : DisableGadget(#MR,1) : DisableGadget(#UL,1) : DisableGadget(#UM,1) : DisableGadget(#UR,1)
  ElseIf UL.l = 1 And UM.l = 1 And UR.l = 1
    SetGadgetText(#NAchricht,"O gewinnt")    
    DisableGadget(#OL,1) : DisableGadget(#OM,1) : DisableGadget(#OR,1) : DisableGadget(#ML,1) : DisableGadget(#MM,1) : DisableGadget(#MR,1) : DisableGadget(#UL,1) : DisableGadget(#UM,1) : DisableGadget(#UR,1)
  ElseIf OL.l = 1 And ML.l = 1 And UL.l = 1
    SetGadgetText(#NAchricht,"O gewinnt")    
    DisableGadget(#OL,1) : DisableGadget(#OM,1) : DisableGadget(#OR,1) : DisableGadget(#ML,1) : DisableGadget(#MM,1) : DisableGadget(#MR,1) : DisableGadget(#UL,1) : DisableGadget(#UM,1) : DisableGadget(#UR,1)
  ElseIf OM.l = 1 And MM.l = 1 And UM.l = 1
    SetGadgetText(#NAchricht,"O gewinnt")    
    DisableGadget(#OL,1) : DisableGadget(#OM,1) : DisableGadget(#OR,1) : DisableGadget(#ML,1) : DisableGadget(#MM,1) : DisableGadget(#MR,1) : DisableGadget(#UL,1) : DisableGadget(#UM,1) : DisableGadget(#UR,1)
  ElseIf ORR.l= 1 And MR.l = 1 And UR.l = 1
    SetGadgetText(#NAchricht,"O gewinnt")    
    DisableGadget(#OL,1) : DisableGadget(#OM,1) : DisableGadget(#OR,1) : DisableGadget(#ML,1) : DisableGadget(#MM,1) : DisableGadget(#MR,1) : DisableGadget(#UL,1) : DisableGadget(#UM,1) : DisableGadget(#UR,1)
  ElseIf OL.l = 1 And MM.l = 1 And UR.l = 1
    SetGadgetText(#NAchricht,"O gewinnt")    
    DisableGadget(#OL,1) : DisableGadget(#OM,1) : DisableGadget(#OR,1) : DisableGadget(#ML,1) : DisableGadget(#MM,1) : DisableGadget(#MR,1) : DisableGadget(#UL,1) : DisableGadget(#UM,1) : DisableGadget(#UR,1)
  ElseIf ORR.l= 1 And MM.l = 1 And UL.l = 1
    SetGadgetText(#NAchricht,"O gewinnt")    
    DisableGadget(#OL,1) : DisableGadget(#OM,1) : DisableGadget(#OR,1) : DisableGadget(#ML,1) : DisableGadget(#MM,1) : DisableGadget(#MR,1) : DisableGadget(#UL,1) : DisableGadget(#UM,1) : DisableGadget(#UR,1)    
  EndIf
  
  If      OL.l = 2 And OM.l = 2 And ORR.l = 2
    SetGadgetText(#NAchricht,"X gewinnt")    
    DisableGadget(#OL,1) : DisableGadget(#OM,1) : DisableGadget(#OR,1) : DisableGadget(#ML,1) : DisableGadget(#MM,1) : DisableGadget(#MR,1) : DisableGadget(#UL,1) : DisableGadget(#UM,1) : DisableGadget(#UR,1)
  ElseIf ML.l = 2 And MM.l = 2 And MR.l = 2
    SetGadgetText(#NAchricht,"X gewinnt")    
    DisableGadget(#OL,1) : DisableGadget(#OM,1) : DisableGadget(#OR,1) : DisableGadget(#ML,1) : DisableGadget(#MM,1) : DisableGadget(#MR,1) : DisableGadget(#UL,1) : DisableGadget(#UM,1) : DisableGadget(#UR,1)
  ElseIf UL.l = 2 And UM.l = 2 And UR.l = 2
    SetGadgetText(#NAchricht,"X gewinnt")    
    DisableGadget(#OL,1) : DisableGadget(#OM,1) : DisableGadget(#OR,1) : DisableGadget(#ML,1) : DisableGadget(#MM,1) : DisableGadget(#MR,1) : DisableGadget(#UL,1) : DisableGadget(#UM,1) : DisableGadget(#UR,1)
  ElseIf OL.l = 2 And ML.l = 2 And UL.l = 2
    SetGadgetText(#NAchricht,"X gewinnt")    
    DisableGadget(#OL,1) : DisableGadget(#OM,1) : DisableGadget(#OR,1) : DisableGadget(#ML,1) : DisableGadget(#MM,1) : DisableGadget(#MR,1) : DisableGadget(#UL,1) : DisableGadget(#UM,1) : DisableGadget(#UR,1)
  ElseIf OM.l = 2 And MM.l = 2 And UM.l = 2
    SetGadgetText(#NAchricht,"X gewinnt")    
    DisableGadget(#OL,1) : DisableGadget(#OM,1) : DisableGadget(#OR,1) : DisableGadget(#ML,1) : DisableGadget(#MM,1) : DisableGadget(#MR,1) : DisableGadget(#UL,1) : DisableGadget(#UM,1) : DisableGadget(#UR,1)
  ElseIf ORR.l= 2 And MR.l = 2 And UR.l = 2
    SetGadgetText(#NAchricht,"X gewinnt")    
    DisableGadget(#OL,1) : DisableGadget(#OM,1) : DisableGadget(#OR,1) : DisableGadget(#ML,1) : DisableGadget(#MM,1) : DisableGadget(#MR,1) : DisableGadget(#UL,1) : DisableGadget(#UM,1) : DisableGadget(#UR,1)
  ElseIf OL.l = 2 And MM.l = 2 And UR.l = 2
    SetGadgetText(#NAchricht,"X gewinnt")    
    DisableGadget(#OL,1) : DisableGadget(#OM,1) : DisableGadget(#OR,1) : DisableGadget(#ML,1) : DisableGadget(#MM,1) : DisableGadget(#MR,1) : DisableGadget(#UL,1) : DisableGadget(#UM,1) : DisableGadget(#UR,1)
  ElseIf ORR.l= 2 And MM.l = 2 And UL.l = 2
    SetGadgetText(#NAchricht,"X gewinnt")    
    DisableGadget(#OL,1) : DisableGadget(#OM,1) : DisableGadget(#OR,1) : DisableGadget(#ML,1) : DisableGadget(#MM,1) : DisableGadget(#MR,1) : DisableGadget(#UL,1) : DisableGadget(#UM,1) : DisableGadget(#UR,1)    
  EndIf
  

     
   
  If Event = #PB_EventCloseWindow 
    Quit = 1 
  ElseIf Event = #PB_Event_Gadget 
    
    Select EventGadgetID()
    
      Case #OL
        DisableGadget(#OL, 1)
        If     Pl.l = 0
          SetGadgetText(#OL,"X")
          OL.l = 2
          Pl.l = 1
          SetGadgetText(#Nachricht,"O ist dran")
        ElseIf PL.l = 1
          SetGadgetText(#OL,"O")
          OL.l = 1
          Pl.l = 0
          SetGadgetText(#Nachricht,"X ist dran")
        EndIf
      
      Case #OM
        DisableGadget(#OM, 1)
        If     Pl.l = 0
          SetGadgetText(#OM,"X")
          OM.l = 2
          Pl.l = 1
          SetGadgetText(#Nachricht,"O ist dran")
        ElseIf PL.l = 1
          SetGadgetText(#OM,"O")
          OM.l = 1
          Pl.l = 0
          SetGadgetText(#Nachricht,"X ist dran")
        EndIf
        
      Case #OR
        DisableGadget(#OR, 1)
        If     Pl.l = 0
          SetGadgetText(#OR,"X")
          ORR.l = 2
           Pl.l = 1
           SetGadgetText(#Nachricht,"O ist dran")
        ElseIf PL.l = 1
          SetGadgetText(#OR,"O")
          ORR.l = 1
           Pl.l = 0
           SetGadgetText(#Nachricht,"X ist dran")
        EndIf
      
      Case #ML
        DisableGadget(#ML, 1)
        If     Pl.l = 0
          SetGadgetText(#ML,"X")
          ML.l = 2
          Pl.l = 1
          SetGadgetText(#Nachricht,"O ist dran")
        ElseIf PL.l = 1
          SetGadgetText(#ML,"O")
          ML.l = 1
          Pl.l = 0
          SetGadgetText(#Nachricht,"X ist dran")
        EndIf

      Case #MM
        DisableGadget(#MM, 1)
        If Pl.l = 0
          SetGadgetText(#MM,"X")
          MM.l = 2
          Pl.l = 1
          SetGadgetText(#Nachricht,"O ist dran")
        ElseIf PL.l = 1
          SetGadgetText(#MM,"O")
          MM.l = 1
          Pl.l = 0
          SetGadgetText(#Nachricht,"X ist dran")
        EndIf
      
      Case #MR
        DisableGadget(#MR, 1)
        If Pl.l = 0
          SetGadgetText(#MR,"X")
          MR.l = 2
          Pl.l = 1
          SetGadgetText(#Nachricht,"O ist dran")
        ElseIf PL.l = 1
          SetGadgetText(#MR,"O")
          MR.l = 1
          Pl.l = 0
          SetGadgetText(#Nachricht,"X ist dran")
        EndIf
        
      Case #UL
        DisableGadget(#UL, 1)
        If Pl.l = 0
          SetGadgetText(#UL,"X")
          UL.l = 2
          Pl.l = 1
          SetGadgetText(#Nachricht,"O ist dran")
        ElseIf PL.l = 1
          SetGadgetText(#UL,"O")
          UL.l = 1
          Pl.l = 0
          SetGadgetText(#Nachricht,"X ist dran")
        EndIf
      
      Case #UM
        DisableGadget(#UM, 1)
        If Pl.l = 0
          SetGadgetText(#UM,"X")
          UM.l = 2
          Pl.l = 1
          SetGadgetText(#Nachricht,"O ist dran")
        ElseIf PL.l = 1
          SetGadgetText(#UM,"O")
          UM.l = 1
          Pl.l = 0
          SetGadgetText(#Nachricht,"X ist dran")
        EndIf
       
      Case #UR
        DisableGadget(#UR, 1)
        If Pl.l = 0
          SetGadgetText(#UR,"X")
          UR.l = 2
          Pl.l = 1
          SetGadgetText(#Nachricht,"O ist dran")
        ElseIf PL.l = 1
          SetGadgetText(#UR,"O")
          UR.l = 1
          Pl.l = 0
          SetGadgetText(#Nachricht,"X ist dran")
        EndIf
        
      Case #Spielende
        End
        
      Case #Neustart
        DisableGadget(#OL,0) : SetGadgetText(#OL,"") :  OL.l = 0
        DisableGadget(#OM,0) : SetGadgetText(#OM,"") :  OM.l = 0
        DisableGadget(#OR,0) : SetGadgetText(#OR,"") : ORR.l = 0
        DisableGadget(#ML,0) : SetGadgetText(#ML,"") :  ML.l = 0
        DisableGadget(#MM,0) : SetGadgetText(#MM,"") :  MM.l = 0
        DisableGadget(#MR,0) : SetGadgetText(#MR,"") :  MR.l = 0
        DisableGadget(#UL,0) : SetGadgetText(#UL,"") :  UL.l = 0
        DisableGadget(#UM,0) : SetGadgetText(#UM,"") :  UM.l = 0
        DisableGadget(#UR,0) : SetGadgetText(#UR,"") :  UR.l = 0
        SetGadgetText(#Nachricht,"X ist dran")
        Pl.l = 0
        
    EndSelect 
  EndIf 
  
Until Quit

Wer draus nen 20 Zeiler macht bekommt Respect von mir!!!!!!!!!!!!!!!!!

Verfasst: 29.01.2006 03:50
von ts-soft
Ganz nett, fehlt noch der Computergegner :wink:

Verfasst: 29.01.2006 04:16
von R4z0r1989
für das das ich noch ein echter n00b bin find ichs gut!!!!!

Aber wenn du meinst kannst du ja einen machen!!!!!!!!!!!!

Verfasst: 29.01.2006 04:30
von ts-soft
Ich finde es ja auch ganz gut :allright:

Vielleicht fängste erstmal mit nem Gegner an, der einfach per Random in ein freies Feld setzt. Und stück für stück, kriegste vielleicht auch nen Gegner hin.

Verfasst: 29.01.2006 04:36
von R4z0r1989
MAch mal bitte ein beispiel!!!!
also ein Feld per Random aktivieren!
Bitte tu mir den gefallen!
Ich hab keinen Plan wie ich so ne Art Activate_gadget()

Hinbeeekommen soll

Verfasst: 29.01.2006 05:04
von ts-soft
Nicht activate, sondern per Zufall ein Feld auswählen, prüfen ob es noch frei ist und dann disablen, als wenn der Spieler gedrückt hat.

Merk Dir in einem Array, welche Felder disabled sind, z.B. so:

Code: Alles auswählen

Dim Felder.b(2,2)
;     -1- -2- -3-
; -1- 0,0 0,1 0,2
; -2- 1,0 1,1 1,2
; -3- 0,0 2,1 2,2
Immer wenn ein Feld disabled wurde, schreibste eine 1 rein. Gültige Felder enthalten also eine 0

Oben Links ist also 0,0, wenn gedrückt dann:
Felder(0,0) = 1
Das nächste Feld daneben dann Felder(0,1) = 1

Der Computer ermittelt also 2 Koordinaten per Random, guckt nach ob noch 0, sonst nochmal, dann 1 rein und disablen, so in etwa.

Verfasst: 29.01.2006 05:09
von R4z0r1989
sorry das versteh ich nicht
hast du ICQ wenn ja meine Nummer:279007139
Wenn nein dann erklärs mir bitte hier nochmal ausführlich!!!

Danke!!!!!!!!!!!!!!!!!!!!!!!!! :allright:

Verfasst: 29.01.2006 05:18
von ts-soft
Du kannst auch für jeden Button eine Variable definieren (zB OR für ObenRechts), diese wird im Case Zweig immer gleich auf 1 gesetzt.
Jetzt ermittelste einen Wert, zwischen 1 + 9 per Random, guckst ob die Variable 0 ist, sonst nochmal, bis du einen Wert hast, dessen Variable 0 ist.

jeder dieser Werte von 1 bis 9 stellt einen Button dar, z.B. die 3 ist #OR, diesen jetzt disablen, und seine Variable ebenfalls auf 1 stellen.

Verfasst: 29.01.2006 05:22
von R4z0r1989
ABer mit Random(x)

kann man doch nur einen Wert von 0 bis eben x ermitteln oder gibt es da eine andere möglichkeit

kann ich nicht demm random z.B. sagen er soll durch zufall ein davon ausgeben: #a #b #c #d???

Bitte schreib mich in ICQ an bitte :praise:

Verfasst: 29.01.2006 15:18
von R4z0r1989
activategadget(#GadgetID) ist falsch den da setzt es ja bloß deen fokus darauf!!!!!

was für ein befehl betätigt den Button????


Danke imm voraus