Conways Game of Life

  • Hi Community,

    ich habe mal das System von zellulären Automaten "Spiel des Lebens" (engl. Conway's Game of Life) vom Mathematiker John Horton Conway für Autoit gemacht. Game of Life ist eine einfache Umsetzung der Automaten-Theorie von Stanisław Marcin Ulam.

    Wer noch Näheres wissen möchte, kann >Hier< schauen.

    Zum Script:
    Es gibt einen Editor, indem Formen erstellt werden können. Wie diese Formen sich in der Folgegeneration verhält, kann man über "Ausführen"->Start sich anzeigen lassen.

    Unter "Öffnen"-> Ordner Formen - können vorgefertigte Formen geladen werden, die bereits spezielle Namen haben und sich auch auf bestimmte Weise verhalten.

    Ein Video: >Hier<

    Das Script: (In der zip-Datei im Anhang befinden sich Beispieldateien zu verschiedenen Formen)

    Spoiler anzeigen
    [autoit]

    ;by Jautois - 2010

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

    #include <GDIPlus.au3>
    #include <Array.au3>
    #include <GUIConstantsEx.au3>
    #include <GuiStatusBar.au3>
    #include <StaticConstants.au3>
    #include <WindowsConstants.au3>
    #include <File.au3>

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

    HotKeySet("{F5}", "_TRANSLATETOARRAY")

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

    Opt("GUIOnEventMode", 1)

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

    _GDIPlus_Startup()

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

    Global $iStatus = True
    Const $pTitel = "Game of Life v.1 - by Jautois"
    Const $iWidth = 500
    Const $iHeight = 500
    Const $iStep = 1
    Const $iRectsize = 10
    Global $Matrix[Round($iWidth / $iRectsize)][Round($iHeight / $iRectsize)]
    Global $Pos[2]
    Global $Max[2] = [UBound($Matrix, 1) - 1, UBound($Matrix, 2) - 1]
    Global $iGeneration
    Global $iLim[2] = [0, $Max[0]]
    Global $Graphic[144]
    Global $aGrid[12][12]
    Global $Brushes[2] = [_GDIPlus_BrushCreateSolid("0xFF000000"), _GDIPlus_BrushCreateSolid("0xFFFFFFFF")]

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

    For $x = 0 To $Max[0] Step 1
    For $y = 0 To $Max[1] Step 1
    $Matrix[$x][$y] = False
    Next
    Next

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

    $Form1_1 = GUICreate("Game of Life - Editor - by Jautois", 448, 490, (@DesktopWidth / 2) + 153, (@DesktopHeight / 2) - 283)
    GUISetOnEvent(-3, "_EXIT")
    GUISetBkColor(0xB4B4B4)
    $MenuItem1 = GUICtrlCreateMenu("&Datei")
    $MenuItem2 = GUICtrlCreateMenuItem("Öffnen", $MenuItem1)
    GUICtrlSetOnEvent(-1, "_LOADGOL")
    $MenuItem3 = GUICtrlCreateMenuItem("", $MenuItem1)
    $MenuItem4 = GUICtrlCreateMenuItem("Speichern", $MenuItem1)
    GUICtrlSetOnEvent(-1, "_SAVEGOL")
    $MenuItem6 = GUICtrlCreateMenu("Ausführen")
    $MenuItem7 = GUICtrlCreateMenuItem("Start", $MenuItem6)
    GUICtrlSetOnEvent(-1, "_TRANSLATETOARRAY")
    $MenuItem8 = GUICtrlCreateMenu("Editor")
    $MenuItem9 = GUICtrlCreateMenuItem("Alle Felder leeren", $MenuItem8)
    GUICtrlSetOnEvent(-1, "_SETGRID")
    $MenuItem10 = GUICtrlCreateMenuItem("Alle Felder füllen", $MenuItem8)
    GUICtrlSetOnEvent(-1, "_SETGRID")
    $MenuItem10 = GUICtrlCreateMenuItem("Zufällige Belegung", $MenuItem8)
    GUICtrlSetOnEvent(-1, "_RANDOMSTARTUPGENERATION")

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

    For $i = 0 To 143
    $Graphic[$i] = GUICtrlCreateGraphic(8 + Mod($i, 12) * 36, 8 + Int($i / 12) * 36, 36, 36, BitOR($SS_CENTER, $SS_RIGHT, $SS_BLACKRECT, $SS_GRAYRECT, $SS_WHITERECT, $SS_BLACKFRAME, $SS_NOTIFY))
    GUICtrlSetOnEvent(-1, "_CHOOSE")
    Next

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

    Global $iLastID = 13

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

    GUISetState(@SW_SHOW)

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

    Global $Graphics = _GDIPlus_GraphicsCreateFromHWND(GUICreate($pTitel, $iWidth, $iHeight))
    Global $Bitmap = _GDIPlus_BitmapCreateFromGraphics($iWidth, $iHeight, $Graphics)
    Global $hBack_Buffer = _GDIPlus_ImageGetGraphicsContext($Bitmap)

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

    $hBrush = _GDIPlus_BrushCreateSolid(0xFFFF0000)
    $hFormat = _GDIPlus_StringFormatCreate(0)
    $hFamily = _GDIPlus_FontFamilyCreate("Arial")
    $hFont = _GDIPlus_FontCreate($hFamily, 10)
    $tLayout = _GDIPlus_RectFCreate(5, 5, 200, 50)

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

    GUISetOnEvent($GUI_EVENT_CLOSE, "_EXIT")
    GUISetBkColor("0xFFFFFF")

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

    AdlibRegister("_GROW", $iStep)

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

    GUISetState(@SW_SHOW)
    While 1
    Sleep(1000)
    WEnd

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

    Func _RANDOMSTARTUPGENERATION()
    Local $iSet = 0
    $iGeneration = 0
    Do
    If $Matrix[Random(0, $Max[0], 1)][Random(0, $Max[1], 1)] = False Then
    $Matrix[Random(0, $Max[0], 1)][Random(0, $Max[1], 1)] = True
    EndIf
    $iSet += 1
    Until $iSet = 1000
    EndFunc ;==>_RANDOMSTARTUPGENERATION

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

    Func _GROW()
    For $x = 0 To $Max[0] Step 1
    For $y = 0 To $Max[1] Step 1
    If $Matrix[$x][$y] = True Then
    _GDIPlus_GraphicsFillRect($hBack_Buffer, $x * $iRectsize, $y * $iRectsize, $iRectsize, $iRectsize, $Brushes[1])
    Else
    _GDIPlus_GraphicsFillRect($hBack_Buffer, $x * $iRectsize, $y * $iRectsize, $iRectsize, $iRectsize, $Brushes[0])
    EndIf
    Next
    Next
    $Matrix = _MANIPULATE()
    _GDIPlus_GraphicsDrawImageRect($Graphics, $Bitmap, 0, 0, $iWidth, $iHeight)
    $aInfo = _GDIPlus_GraphicsMeasureString($Graphics, "Generation: " & $iGeneration, $hFont, $tLayout, $hFormat)
    _GDIPlus_GraphicsDrawStringEx($Graphics, "Generation: " & $iGeneration, $hFont, $aInfo[0], $hFormat, $hBrush)
    EndFunc ;==>_GROW

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

    Func _NEIGHBORS($iX, $iY)
    Global $iCount = 0

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

    If $iX > 0 And $iY > 0 Then
    If $Matrix[$iX - 1][$iY - 1] Then $iCount += 1
    If $Matrix[$iX][$iY - 1] Then $iCount += 1
    If $Matrix[$iX - 1][$iY] Then $iCount += 1
    EndIf

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

    If $iX < $Max[0] And $iY < $Max[0] Then
    If $Matrix[$iX + 1][$iY + 1] Then $iCount += 1
    If $Matrix[$iX][$iY + 1] Then $iCount += 1
    If $Matrix[$iX + 1][$iY] Then $iCount += 1
    EndIf

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

    If $iX > 0 And $iY < $Max[0] Then
    If $Matrix[$iX - 1][$iY + 1] Then $iCount += 1
    EndIf

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

    If $iX < $Max[0] And $iY > 0 Then
    If $Matrix[$iX + 1][$iY - 1] Then $iCount += 1
    EndIf

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

    Return $iCount
    EndFunc ;==>_NEIGHBORS

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

    Func _MANIPULATE()
    Local $aNextGen[Round($iWidth / $iRectsize)][Round($iHeight / $iRectsize)]
    Local $iNeighbors

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

    For $x = 0 To $Max[0] Step 1
    For $y = 0 To $Max[1] Step 1
    $iNeighbors = _NEIGHBORS($x, $y)
    If $iNeighbors = 2 Or $iNeighbors = 3 Then
    If $iNeighbors = 2 Then
    $aNextGen[$x][$y] = $Matrix[$x][$y]
    Else
    $aNextGen[$x][$y] = True
    EndIf
    Else
    $aNextGen[$x][$y] = False
    EndIf
    Next
    Next

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

    $iGeneration += 1

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

    Return $aNextGen
    EndFunc ;==>_MANIPULATE

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

    Func _CHOOSE()
    $x = Mod((@GUI_CtrlId - $iLastID), 12)
    $y = Int((@GUI_CtrlId - $iLastID) / 12)

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

    If $aGrid[$x][$y] Then
    $aGrid[$x][$y] = False
    GUICtrlSetBkColor(@GUI_CtrlId, 0xB4B4B4)
    Else
    $aGrid[$x][$y] = True
    GUICtrlSetBkColor(@GUI_CtrlId, 0x0)
    EndIf
    EndFunc ;==>_CHOOSE

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

    Func _TRANSLATETOARRAY()
    _RESETARRAY()
    Do
    Sleep(200)
    Until $iStatus = True
    For $x = 0 To $Max[0] Step 1
    For $y = 0 To $Max[1] Step 1
    If $x >= 18 And $x <= 29 And $y >= 18 And $y <= 29 Then
    If $aGrid[$x - 18][$y - 18] = "" Then $Matrix[$x][$y] = False
    If $aGrid[$x - 18][$y - 18] Then $Matrix[$x][$y] = True
    EndIf
    Next
    Next
    $iGeneration = 0
    _GROW()
    EndFunc ;==>_TRANSLATETOARRAY

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

    Func _SETGRID()
    Local $iCol[2] = [0xB4B4B4, 0x0]

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

    $iID = @GUI_CtrlId - 10
    ;GUICtrlSetState($Form1_1)
    If $iID = 0 Then
    For $x = 0 To 11 Step 1
    For $y = 0 To 11 Step 1
    $aGrid[$x][$y] = False
    Next
    Next
    ElseIf $iID = 1 Then
    For $x = 0 To 11 Step 1
    For $y = 0 To 11 Step 1
    $aGrid[$x][$y] = True
    Next
    Next
    EndIf

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

    For $i = 0 To 143
    GUICtrlSetBkColor($Graphic[$i], $iCol[$iID])
    Next
    EndFunc ;==>_SETGRID

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

    Func _LOADGOL()
    Local $iCol[2] = [0xB4B4B4, 0x0]
    Local $aRawData
    Local $aRawGrid[12][12]
    Local $iRun
    $sFileName = FileOpenDialog("Datei laden ...", @ScriptDir & "\Formen\", "Game Of Life Data (*.gol)", 3)
    If @error Then
    MsgBox(4096, "", "Keine Datei(en) ausgewählt")
    Else
    _FileReadToArray($sFileName, $aRawData)
    ;_ArrayDisplay($aRawData)
    For $i = 1 To 12
    $sS = StringSplit($aRawData[$i], "")
    For $k = 1 To 12
    If $sS[$k] = 1 Then
    $aRawGrid[$k - 1][$i - 1] = True
    GUICtrlSetBkColor($Graphic[$iRun], $iCol[1])
    Else
    $aRawGrid[$k - 1][$i - 1] = False
    GUICtrlSetBkColor($Graphic[$iRun], $iCol[0])
    EndIf
    $iRun += 1
    Next
    Next
    EndIf
    $aGrid = $aRawGrid
    EndFunc ;==>_LOADGOL

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

    Func _SAVEGOL()
    $sFileName = FileSaveDialog("Wähle einen Namen.", @ScriptDir & "\Formen\", "Game Of Life Data (*.gol)", 18) & ".gol"
    If Not @error Then
    For $x = 0 To 11
    For $y = 0 To 11
    If $aGrid[$x][$y] Then
    FileWrite($sFileName, "1")
    Else
    FileWrite($sFileName, "0")
    EndIf
    Next
    FileWrite($sFileName, @CRLF)
    Next
    EndIf
    EndFunc ;==>_SAVEGOL

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

    Func _RESETARRAY()
    $iStatus = False
    For $x = 0 To $Max[0] Step 1
    For $y = 0 To $Max[1] Step 1
    $Matrix[$x][$y] = False
    Next
    Next
    $iStatus = True
    EndFunc ;==>_RESETARRAY

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

    Func _EXIT()
    _GDIPlus_Shutdown()
    Exit
    EndFunc ;==>_EXIT

    [/autoit]
  • Schön das es Euch gefällt.

    Wenn einer eine erste Generation erstellt hat, die sich richtig cool weiterentwickelt, kann er sie ja posten 8)

  • WOW, nicht schlecht :thumbup:
    Die hier funzt ganz gut.

    Dateien

    Spoiler anzeigen

    Grundkenntnisse in: C++, JavaScript
    Sehr gute Kenntnisse: PHP, JAVA, C und näturlich AutoIt


    Klaviatur, Anhang UDF, GDI+ Mühle

    Zitat

    "Wenn einen um 20h der Pizzadienst anruft und fragt, ob man's nur vergessen hat und ob man das gleiche
    möchte wie immer -- dann sollte man sein Bestellverhalten evtl überdenken"

    Einmal editiert, zuletzt von Cartan12 (15. Februar 2010 um 18:15)

  • Zitat

    Bei mir kommt beim öffnen deiner Datei gar nichts :huh:

    In der Datei sind alle Felder auf 0. Also sind alle Felder leer. Cartan12 muss nochmal schaun, was er da gespeichert hat. :)

  • Sieht gut aus. :thumbup:
    Denkst du dir so was aus oder Probiertst du einfach.
    Ich probier immer nur rum. :rolleyes:

    mfg Ubuntu