Hallo leutz ... bin heute wieder aus dem Urlaub zurück und habe dort ein kleines Spiel geschrieben...Ich konnt nicht schlafen und dann kam mir der Einfall TicTacToe zu schreiben...Nun ja dass habe ich auch getan und hier ist das Ergebnis.
Ihr könnt gegen einen Freund spielen oder auch gegen den Computer...Ist jetzt war nicht das schwerste zum programmieren aber ihr könnt ja trotzdem mal eure Kritik äußern...
Spoiler anzeigen
#cs
[/autoit] [autoit][/autoit] [autoit]Autor: Marcel Böhm (Reaker)
Copyright 2010 by Marcel
Email: [email='boehm.marcel@gmail.com'][/email]
#ce
[/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#include <GuiButton.au3>
#include <Array.au3>
#include <GuiStatusBar.au3>
Global $Feld[10], $FeldSt[10]
Global $Turn
Global $GamePlay
Global $Gewonnen = 0
Global Const $GUI_Title = "TicTacToe by Reaker"
Global Const $Version = "1.0"
Global Const $GUI_BK = 0x307A8F
Global Const $SIGN_CO = 0xFF0000
Global Const $GUI_TRANS = 225
Global Const $Path_Ini = @ScriptDir & "\config.ini"
If Not FileExists($Path_Ini) Then
IniWrite($Path_Ini, "Settings", "GamePlay", "1")
EndIf
If IniRead($Path_Ini, "Settings", "GamePlay", "") = "1" Then
$GamePlay = 1
Else
$GamePlay = 2
EndIf
#Region ### START Koda GUI section ### Form=C:\Users\Marcel\Sonstiges\Skripte\Autoit3\Projekte\TicTacToe\Form1.kxf
Global $Form1 = GUICreate($GUI_Title, 425, 463, -1, -1)
GUISetCursor(3)
WinSetTrans($Form1, Default, $GUI_TRANS)
GUISetBkColor($GUI_BK)
Global $Button1 = GUICtrlCreateButton("Neues Spiel", 20, 405, 96, 26, $WS_GROUP)
Global $Button2 = GUICtrlCreateButton("Optionen", 125, 405, 91, 26, $WS_GROUP)
Global $Button3 = GUICtrlCreateButton("About", 230, 405, 91, 26, $WS_GROUP)
$Feld[1] = GUICtrlCreateButton("", 20, 15, 121, 121, $WS_GROUP)
$Feld[2] = GUICtrlCreateButton("", 152, 14, 121, 121, $WS_GROUP)
$Feld[3] = GUICtrlCreateButton("", 285, 14, 121, 121, $WS_GROUP)
$Feld[4] = GUICtrlCreateButton("", 20, 143, 121, 121, $WS_GROUP)
$Feld[5] = GUICtrlCreateButton("", 152, 143, 121, 121, $WS_GROUP)
$Feld[6] = GUICtrlCreateButton("", 285, 143, 121, 121, $WS_GROUP)
$Feld[7] = GUICtrlCreateButton("", 20, 273, 121, 121, $WS_GROUP)
$Feld[8] = GUICtrlCreateButton("", 152, 273, 121, 121, $WS_GROUP)
$Feld[9] = GUICtrlCreateButton("", 285, 273, 121, 121, $WS_GROUP)
For $i = 1 To 9
GUICtrlSetFont($Feld[$i], 100, 800, 0, "MS Sans Serif")
Next
Global $StatusBar1 = _GUICtrlStatusBar_Create($Form1)
Global $StatusBar1_PartsWidth[2] = [110, -1]
_GUICtrlStatusBar_SetParts($StatusBar1, $StatusBar1_PartsWidth)
_GUICtrlStatusBar_SetMinHeight($StatusBar1, 21)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
_GDIPlus_Startup()
[/autoit] [autoit][/autoit] [autoit]AdlibRegister("_DrawLine", 100)
[/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]#Region ### START Koda GUI section ### Form=C:\Users\Marcel\Sonstiges\Skripte\Autoit3\Projekte\TicTacToe\Form2.kxf
Global $Form2 = GUICreate("Optionen", 236, 158, 285, 182, Default, Default, $Form1)
GUISetBkColor($GUI_BK)
WinSetTrans($Form2, Default, $GUI_TRANS + 20)
Global $Radio1 = GUICtrlCreateRadio("Gegen Computer (1 Spieler)", 20, 20, 156, 16)
GUICtrlSetState(-1, $GUI_CHECKED)
Global $Radio2 = GUICtrlCreateRadio("Gegen Person (2 Spieler)", 20, 40, 161, 21)
Global $Button5 = GUICtrlCreateButton("Speichern", 140, 130, 86, 23, $WS_GROUP)
Global $Button4 = GUICtrlCreateButton("Abbrechen", 50, 130, 86, 23, $WS_GROUP)
#EndRegion ### END Koda GUI section ###
_NewGame()
[/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]While 1
$nMsg = GUIGetMsg(1)
Switch $nMsg[1]
Case $Form1
Switch $nMsg[0]
Case $GUI_EVENT_CLOSE
Exit
Case $Button1
_NewGame()
Case $Button2
GUISetState(@SW_SHOW, $Form2)
WinSetState($Form1, Default, @SW_DISABLE)
Case $Button3
MsgBox(0, "About", _
"TicTacToe - Game" & @LF & _
"" & @LF & _
"Autor: Marcel Böhm (Reaker) " & @LF & _
"Email: [email='boehm.marcel@gmail.com'][/email] " & @LF & _
"Version: " & $Version & " (20.02.2010)" & @LF & _
"Copyright by Marcel Böhm 2010")
Case $Feld[1]
_click(1)
Case $Feld[2]
_click(2)
Case $Feld[3]
_click(3)
Case $Feld[4]
_click(4)
Case $Feld[5]
_click(5)
Case $Feld[6]
_click(6)
Case $Feld[7]
_click(7)
Case $Feld[8]
_click(8)
Case $Feld[9]
_click(9)
EndSwitch
Case $Form2
Switch $nMsg[0]
Case $GUI_EVENT_CLOSE
GUISetState(@SW_HIDE, $Form2)
WinSetState($Form1, Default, @SW_ENABLE)
Case $Button4
GUISetState(@SW_HIDE, $Form2)
WinSetState($Form1, Default, @SW_ENABLE)
Case $Button5
If BitAND(GUICtrlRead($Radio1), $GUI_CHECKED) = $GUI_CHECKED Then
IniWrite($Path_Ini, "Settings", "GamePlay", "1")
Else
IniWrite($Path_Ini, "Settings", "GamePlay", "2")
EndIf
TrayTip("Info", "Erfolgreich gespeichert", 1, 1)
GUISetState(@SW_HIDE, $Form2)
WinSetState($Form1, Default, @SW_ENABLE)
EndSwitch
EndSwitch
WEnd
Func _click($flag)
$Gewonnen = 0
If $GamePlay = 1 Then
Switch $FeldSt[$flag]
Case 1 ;freies Feld
GUICtrlSetData($Feld[$flag], "X")
$FeldSt[$flag] = 2
If _Gewonnen() = 0 Then
;computer zug
If $Gewonnen = 0 Then
_ComZug()
_Gewonnen()
[/autoit] [autoit][/autoit] [autoit]EndIf
EndIf
Case 2
TrayTip("Info", "Auf diesem Feld ist bereits ein Kreuz", 1, 3)
Case 3
TrayTip("Info", "Auf diesem Feld ist bereits ein Kreis", 1, 3)
EndSwitch
Else
Switch $FeldSt[$flag]
Case 1 ;freies Feld
If $Turn = 1 Then
GUICtrlSetData($Feld[$flag], "X")
$FeldSt[$flag] = 2
Else
GUICtrlSetData($Feld[$flag], "O")
$FeldSt[$flag] = 3
EndIf
If $Turn = 1 Then
$Turn = 2
Else
$Turn = 1
EndIf
Case 2 ;markiert - Kreuz
TrayTip("Info", "Auf diesem Feld ist bereits ein Kreuz", 1, 3)
Case 3 ;markiert - Kreis
TrayTip("Info", "Auf diesem Feld ist bereits ein Kreis", 1, 3)
EndSwitch
_Gewonnen()
[/autoit] [autoit][/autoit] [autoit]EndIf
[/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]EndFunc ;==>_click
[/autoit] [autoit][/autoit] [autoit]Func _Gewonnen()
If $FeldSt[1] <> 1 And $FeldSt[2] <> 1 And $FeldSt[3] <> 1 And $FeldSt[4] <> 1 And $FeldSt[5] <> 1 And $FeldSt[6] <> 1 And $FeldSt[7] <> 1 And $FeldSt[8] <> 1 And $FeldSt[9] <> 1 Then
$Gewonnen = 3
EndIf
;##Kreuz gewonnen##
If $FeldSt[1] = 2 And $FeldSt[2] = 2 And $FeldSt[3] = 2 Then
$Gewonnen = 2
EndIf
If $FeldSt[4] = 2 And $FeldSt[5] = 2 And $FeldSt[6] = 2 Then
$Gewonnen = 2
EndIf
If $FeldSt[7] = 2 And $FeldSt[8] = 2 And $FeldSt[9] = 2 Then
$Gewonnen = 2
EndIf
If $FeldSt[1] = 2 And $FeldSt[4] = 2 And $FeldSt[7] = 2 Then
$Gewonnen = 2
EndIf
If $FeldSt[2] = 2 And $FeldSt[5] = 2 And $FeldSt[8] = 2 Then
$Gewonnen = 2
EndIf
If $FeldSt[3] = 2 And $FeldSt[6] = 2 And $FeldSt[9] = 2 Then
$Gewonnen = 2
EndIf
If $FeldSt[1] = 2 And $FeldSt[5] = 2 And $FeldSt[9] = 2 Then
$Gewonnen = 2
EndIf
If $FeldSt[3] = 2 And $FeldSt[5] = 2 And $FeldSt[7] = 2 Then
$Gewonnen = 2
EndIf
;##Kreis gewonnen##
If $FeldSt[1] = 3 And $FeldSt[2] = 3 And $FeldSt[3] = 3 Then
$Gewonnen = 1
EndIf
If $FeldSt[4] = 3 And $FeldSt[5] = 3 And $FeldSt[6] = 3 Then
$Gewonnen = 1
EndIf
If $FeldSt[7] = 3 And $FeldSt[8] = 3 And $FeldSt[9] = 3 Then
$Gewonnen = 1
EndIf
If $FeldSt[1] = 3 And $FeldSt[4] = 3 And $FeldSt[7] = 3 Then
$Gewonnen = 1
EndIf
If $FeldSt[2] = 3 And $FeldSt[5] = 3 And $FeldSt[8] = 3 Then
$Gewonnen = 1
EndIf
If $FeldSt[3] = 3 And $FeldSt[6] = 3 And $FeldSt[9] = 3 Then
$Gewonnen = 1
EndIf
If $FeldSt[1] = 3 And $FeldSt[5] = 3 And $FeldSt[9] = 3 Then
$Gewonnen = 1
EndIf
If $FeldSt[3] = 3 And $FeldSt[5] = 3 And $FeldSt[7] = 3 Then
$Gewonnen = 1
EndIf
If $Gewonnen = 1 Then
_GUICtrlStatusBar_SetText($StatusBar1, "Kreis hat gewonnen", 1)
MsgBox(0, "Gewonnen!!!", "Kreis hat gewonnen")
ElseIf $Gewonnen = 2 Then
_GUICtrlStatusBar_SetText($StatusBar1, "Kreuz hat gewonnen", 1)
MsgBox(0, "Gewonnen!!!", "Kreuz hat gewonnen")
ElseIf $Gewonnen = 0 Then
If $Turn = 1 Then
_GUICtrlStatusBar_SetText($StatusBar1, "Kreuz ist dran", 1)
Else
_GUICtrlStatusBar_SetText($StatusBar1, "Kreis ist dran", 1)
EndIf
Return 0
EndIf
If $Gewonnen <> 0 Then
If $Gewonnen = 3 Then
$msg = MsgBox(4, "Unentschieden!!!", "Soll ein neues Spiel gestartet werden?")
If $msg = 6 Then
_NewGame()
Else
Exit
EndIf
Else
$msg = MsgBox(4, "Neues Spiel?", "Soll ein neues Spiel gestartet werden?")
If $msg = 6 Then
_NewGame()
Else
Exit
EndIf
EndIf
EndIf
EndFunc ;==>_Gewonnen
Func _NewGame()
For $i = 1 To 9
$FeldSt[$i] = 1
GUICtrlSetData($Feld[$i], "")
Next
If IniRead($Path_Ini, "Settings", "GamePlay", "") = "1" Then
$GamePlay = 1
Else
$GamePlay = 2
EndIf
If $GamePlay = 1 Then
_GUICtrlStatusBar_SetText($StatusBar1, "Gegen Computer", 0)
Else
_GUICtrlStatusBar_SetText($StatusBar1, "Gegen Person", 0)
EndIf
$Turn = 1 ;Kreuz
If $Turn = 1 Then
_GUICtrlStatusBar_SetText($StatusBar1, "Kreuz ist dran", 1)
Else
_GUICtrlStatusBar_SetText($StatusBar1, "Kreis ist dran", 1)
EndIf
EndFunc ;==>_NewGame
Func _DrawLine()
$hGraphic = _GDIPlus_GraphicsCreateFromHWND($Form1)
$hPen = _GDIPlus_PenCreate(0xFF000000, 6)
_GDIPlus_GraphicsDrawLine($hGraphic, 146, 14, 146, 397, $hPen)
_GDIPlus_GraphicsDrawLine($hGraphic, 278, 14, 278, 397, $hPen)
_GDIPlus_GraphicsDrawLine($hGraphic, 18, 139, 405, 139, $hPen)
_GDIPlus_GraphicsDrawLine($hGraphic, 18, 268, 405, 268, $hPen)
EndFunc ;==>_DrawLine
Func _ComZug()
Local $FeldRem[1], $Random = ""
;###################### Bei welchen Möglichkeiten man Gewinnen kann (Liste nicht vollständig, damit man noch etwas Chance hat) #############
;wenn 2 Kreuze
If $FeldSt[1] = 2 And $FeldSt[3] = 2 And $FeldSt[2] = 1 Then _ArrayAdd($FeldRem, 2)
If $FeldSt[4] = 2 And $FeldSt[6] = 2 And $FeldSt[5] = 1 Then _ArrayAdd($FeldRem, 5)
If $FeldSt[7] = 2 And $FeldSt[9] = 2 And $FeldSt[8] = 1 Then _ArrayAdd($FeldRem,
If $FeldSt[1] = 2 And $FeldSt[7] = 2 And $FeldSt[4] = 1 Then _ArrayAdd($FeldRem, 4)
If $FeldSt[2] = 2 And $FeldSt[8] = 2 And $FeldSt[5] = 1 Then _ArrayAdd($FeldRem, 5)
If $FeldSt[3] = 2 And $FeldSt[9] = 2 And $FeldSt[6] = 1 Then _ArrayAdd($FeldRem, 6)
If $FeldSt[1] = 2 And $FeldSt[9] = 2 And $FeldSt[5] = 1 Then _ArrayAdd($FeldRem, 5)
If $FeldSt[3] = 2 And $FeldSt[7] = 2 And $FeldSt[5] = 1 Then _ArrayAdd($FeldRem, 5)
If $FeldSt[1] = 2 And $FeldSt[5] = 2 And $FeldSt[9] = 1 Then _ArrayAdd($FeldRem, 9)
If $FeldSt[3] = 2 And $FeldSt[5] = 2 And $FeldSt[7] = 1 Then _ArrayAdd($FeldRem, 7)
If $FeldSt[9] = 2 And $FeldSt[5] = 2 And $FeldSt[1] = 1 Then _ArrayAdd($FeldRem, 1)
If $FeldSt[7] = 2 And $FeldSt[5] = 2 And $FeldSt[3] = 1 Then _ArrayAdd($FeldRem, 3)
If $FeldSt[4] = 2 And $FeldSt[5] = 2 And $FeldSt[6] = 1 Then _ArrayAdd($FeldRem, 6)
If $FeldSt[6] = 2 And $FeldSt[5] = 2 And $FeldSt[4] = 1 Then _ArrayAdd($FeldRem, 4)
If $FeldSt[2] = 2 And $FeldSt[5] = 2 And $FeldSt[7] = 1 Then _ArrayAdd($FeldRem, 7)
If $FeldSt[7] = 2 And $FeldSt[5] = 2 And $FeldSt[3] = 1 Then _ArrayAdd($FeldRem, 3)
;wenn 2 Kreise
For $i = 1 To 3 ;zählt 3mal, damit die priorität höher ist
If $FeldSt[1] = 3 And $FeldSt[3] = 3 And $FeldSt[2] = 1 Then _ArrayAdd($FeldRem, 2)
If $FeldSt[4] = 3 And $FeldSt[6] = 3 And $FeldSt[5] = 1 Then _ArrayAdd($FeldRem, 5)
If $FeldSt[7] = 3 And $FeldSt[9] = 3 And $FeldSt[8] = 1 Then _ArrayAdd($FeldRem,
If $FeldSt[1] = 3 And $FeldSt[7] = 3 And $FeldSt[4] = 1 Then _ArrayAdd($FeldRem, 4)
If $FeldSt[2] = 3 And $FeldSt[8] = 3 And $FeldSt[5] = 1 Then _ArrayAdd($FeldRem, 5)
If $FeldSt[3] = 3 And $FeldSt[9] = 3 And $FeldSt[6] = 1 Then _ArrayAdd($FeldRem, 6)
If $FeldSt[1] = 3 And $FeldSt[9] = 3 And $FeldSt[5] = 1 Then _ArrayAdd($FeldRem, 5)
If $FeldSt[3] = 3 And $FeldSt[7] = 3 And $FeldSt[5] = 1 Then _ArrayAdd($FeldRem, 5)
If $FeldSt[1] = 3 And $FeldSt[5] = 3 And $FeldSt[9] = 1 Then _ArrayAdd($FeldRem, 9)
If $FeldSt[3] = 3 And $FeldSt[5] = 3 And $FeldSt[7] = 1 Then _ArrayAdd($FeldRem, 7)
If $FeldSt[9] = 3 And $FeldSt[5] = 3 And $FeldSt[1] = 1 Then _ArrayAdd($FeldRem, 1)
If $FeldSt[7] = 3 And $FeldSt[5] = 3 And $FeldSt[3] = 1 Then _ArrayAdd($FeldRem, 3)
If $FeldSt[4] = 3 And $FeldSt[5] = 3 And $FeldSt[6] = 1 Then _ArrayAdd($FeldRem, 6)
If $FeldSt[6] = 3 And $FeldSt[5] = 3 And $FeldSt[4] = 1 Then _ArrayAdd($FeldRem, 4)
If $FeldSt[2] = 3 And $FeldSt[5] = 3 And $FeldSt[7] = 1 Then _ArrayAdd($FeldRem, 7)
If $FeldSt[7] = 3 And $FeldSt[5] = 3 And $FeldSt[3] = 1 Then _ArrayAdd($FeldRem, 3)
Next
[/autoit] [autoit][/autoit] [autoit];~ _ArrayDisplay($FeldRem, "Felder die Gewinn entscheiden");zeigt die Felder an, bei denen jemand gewinnen kann
[/autoit] [autoit][/autoit] [autoit]If UBound($FeldRem) - 1 > 0 Then
If Random() > 0.25 Then; Wahrscheinlichkeit für geziehlten Zug (umso kleiner der Wert desto besser der Computer) min. 0 , max. 1
$Count = UBound($FeldRem) - 1
If $Count = 1 Then
$Random = 1
Else
$Random = Random(1, $Count, 1)
EndIf
$Random = $FeldRem[$Random]
Else
While 1
$Random = Random(1, 9, 1)
If $FeldSt[$Random] = 1 Then ExitLoop
WEnd
EndIf
Else
While 1
$Random = Random(1, 9, 1)
If $FeldSt[$Random] = 1 Then ExitLoop
WEnd
EndIf
GUICtrlSetData($Feld[$Random], "O")
$FeldSt[$Random] = 3
EndFunc ;==>_ComZug