Conway's Spiel des Lebens

  • Hallo Leute,

    bin letztens auf diese Internetseite gestoßen: http://www.mathematische-basteleien.de/gameoflife.htm
    und dachte mir ich würde das gerne in AutoIt nachprogrammieren.

    Nun das hier ist das Ergebnis:

    [autoit]


    #include <GUIConstantsEx.au3>
    #include <StaticConstants.au3>
    #include <Array.au3>

    [/autoit][autoit][/autoit][autoit]

    Global $iGUIw = 500, $iGUIh = 600, $iWidth = 10, $iHeight = 10, $iBlack = 0x000000, $iWhite = 0xffffff, $iZeit = 1,$iAlleTod
    Global $iYSize = 50, $iXSize = 50, $aControlID[$iYSize][$iXSize], $aKolonie[$iYSize][$iXSize]
    $hGUI = GUICreate("SpielDesLebens", $iGUIw, $iGUIh)
    $hBtnStop = GUICtrlCreateButton("Stop",310,550,50,50)
    $hBtnStart = GUICtrlCreateButton("Start", 200, 550, 100, 50)
    $hLabel = GUICtrlCreateLabel("Zyklen:", 10, 550, 50, 50)
    $hZeit = GUICtrlCreateLabel($iZeit, 60, 550, 100, 50)

    [/autoit][autoit][/autoit][autoit]

    For $y = 0 To UBound($aControlID) - 1
    For $x = 0 To UBound($aControlID) - 1
    $aKolonie[$y][$x] = 0
    $aControlID[$y][$x] = GUICtrlCreateGraphic($x * $iWidth, $y * $iHeight, $iWidth, $iHeight)
    GUICtrlSetBkColor(-1, $iWhite)
    Next
    Next
    GUISetState(@SW_SHOW)

    [/autoit][autoit][/autoit][autoit]

    _Start()

    [/autoit][autoit][/autoit][autoit]

    Func _Start()
    While 1
    Sleep(10)
    Global $hMsg = GUIGetMsg()
    If $hMsg = $GUI_EVENT_CLOSE Then Exit
    For $y = 0 To UBound($aControlID) - 1
    For $x = 0 To UBound($aControlID) - 1
    If $hMsg = $aControlID[$y][$x] Then
    If $aKolonie[$y][$x] = 1 Then
    GUICtrlSetBkColor($aControlID[$y][$x], $iWhite)
    $aKolonie[$y][$x] = 0
    Else
    GUICtrlSetBkColor($aControlID[$y][$x], $iBlack)
    $aKolonie[$y][$x] = 1
    EndIf
    EndIf
    Next
    Next
    If $hMsg = $hBtnStart Then
    $iStop = 1
    Do
    _Leben()
    $iZeit += 1
    GUICtrlSetData($hZeit, $iZeit)
    _Zeichen()
    $hMsg = GUIGetMsg()
    Until $iAlleTod = 0 Or $hMsg = $hBtnStop
    EndIf
    WEnd
    EndFunc

    [/autoit][autoit][/autoit][autoit]

    Func _Zeichen()
    GUISetState(@SW_LOCK)
    For $y = 0 To UBound($aKolonie) - 1
    For $x = 0 To UBound($aKolonie) - 1
    If $aKolonie[$y][$x] = 1 Then
    GUICtrlSetBkColor($aControlID[$y][$x], $iBlack)
    EndIf
    If $aKolonie[$y][$x] = 2 Then
    GUICtrlSetBkColor($aControlID[$y][$x], $iWhite)
    EndIf
    Next
    Next
    GUISetState(@SW_UNLOCK)
    EndFunc

    [/autoit][autoit][/autoit][autoit]

    Func _Leben()
    Local $aNeueKolonie[$iYSize][$iXSize]
    $iAlleTod = 0
    For $y = 0 To $iYSize - 1
    For $x = 0 To $iXSize - 1
    Local $iNachbarn = 0

    [/autoit][autoit][/autoit][autoit]

    If $x - 1 <> -1 Then
    If $aKolonie[$y][$x - 1] = 1 Then $iNachbarn += 1
    EndIf;Links
    If $x + 1 <> UBound($aKolonie) Then
    If $aKolonie[$y][$x + 1] = 1 Then $iNachbarn += 1
    EndIf;Rechts
    If $y - 1 <> -1 Then
    If $aKolonie[$y - 1][$x] = 1 Then $iNachbarn += 1
    EndIf;Oben
    If $y + 1 <> UBound($aKolonie) Then
    If $aKolonie[$y + 1][$x] = 1 Then $iNachbarn += 1
    EndIf;Unten
    If $y - 1 <> -1 And $x - 1 <> -1 Then
    If $aKolonie[$y - 1][$x - 1] = 1 Then $iNachbarn += 1
    EndIf;Links Oben
    If $y + 1 <> UBound($aKolonie) And $x - 1 <> -1 Then
    If $aKolonie[$y + 1][$x - 1] = 1 Then $iNachbarn += 1
    EndIf;Links Unten
    If $y - 1 <> -1 And $x + 1 <> UBound($aKolonie) Then
    If $aKolonie[$y - 1][$x + 1] = 1 Then $iNachbarn += 1
    EndIf;Rechts Oben
    If $y + 1 <> UBound($aKolonie) And $x + 1 <> UBound($aKolonie) Then
    If $aKolonie[$y + 1][$x + 1] = 1 Then $iNachbarn += 1
    EndIf;Rechts Unten
    If $aKolonie[$y][$x] = 2 Or $aKolonie[$y][$x] = 0 Then
    If $iNachbarn = 3 Then
    $aNeueKolonie[$y][$x] = 1
    $iAlleTod = 1
    EndIf
    EndIf
    If $aKolonie[$y][$x] = 1 Then
    If $iNachbarn = 2 Or $iNachbarn = 3 Then
    $aNeueKolonie[$y][$x] = 1
    $iAlleTod = 1
    Else
    $aNeueKolonie[$y][$x] = 2
    EndIf
    EndIf
    Next
    Next
    $aKolonie = $aNeueKolonie
    EndFunc

    [/autoit]

    Die Geschwindigkeit lässt natürlich zu wünschen übrig, eventuell wäre hier der Einsatz von GDIPlus deutlich besser. Auch die Berechnung der nächsten "Kolonie" ist aus dem Kindergarten aber Hej! was soll's es funktioniert :D

    Ich empfehle euch besonders die verschiedenen Gleiter auszuprobieren. Bis dahin viel Spaß =O

    :thumbup:

  • Die Geschwindigkeit lässt natürlich zu wünschen übrig,

    Die vielen "If´s" kosten Geschwindigkeit.
    Fast alle kannst du dir sparen, wenn du einen "Rahmen" von permanenten "Einsen" rund um dein Spielfeld legst.
    11111111
    10000001
    10000001
    10000001
    11111111
    Somit brauchst du nur noch die
    If $aKolonie[$y + 1][$x] = 1 Then $iNachbarn += 1
    Und selbst die bekommst du alle in eine Zeile...
    $iNachbarn=...+....+...+...+.... (lass dir was einfallen 8) )