Noch einmal Conways Game of Life!

Spiele, Demos, Grafikzeug und anderes unterhaltendes.
Benutzeravatar
Mijikai
Beiträge: 754
Registriert: 25.09.2016 01:42

Noch einmal Conways Game of Life!

Beitrag von Mijikai »

Motiviert durch aktuelle Threads & Posts zu Conways Game of Life
habe ich mich auch mal an eine Umsetzung gewagt :)

Bild

Farben:

Code: Alles auswählen

Lila -> Zelle wird neu geboren
Grün -> Zelle überlebt
Blau -> Zelle stirbt
Tasten:

Code: Alles auswählen

[Escape] -> beendet das Spiel
[Space] -> beginnt eine neue Runde
Viel Spaß :D

Code:

Code: Alles auswählen

EnableExplicit

;------------------------------
;Compact Conway's Game of Life
;CCGOL (c) 2019 by Mijikai
;Version: DRAFT
;------------------------------
;ToDo:
;- GUI & Controls
;- ...
;------------------------------

Structure COLONIE_STRUCT
  *cell.CELL_STRUCT[8]
EndStructure

Structure CELL_STRUCT
  state.b
  mark.b
  colonie.COLONIE_STRUCT
EndStructure

Structure WORLD_STRUCT
  cell.CELL_STRUCT[65044]
EndStructure

Global world.WORLD_STRUCT

Procedure.i CCGOL(Render.b = #False)
  Protected index.i
  Protected x.i
  Protected y.i
  Protected a.i
  Protected b.i
  Protected count.i
  index = 640
  If Render
    For y = 1 To 200
      For x = 1 To 320
        count = world\cell[index]\colonie\cell[0]\state
        count + world\cell[index]\colonie\cell[1]\state
        count + world\cell[index]\colonie\cell[2]\state
        count + world\cell[index]\colonie\cell[3]\state
        count + world\cell[index]\colonie\cell[4]\state
        count + world\cell[index]\colonie\cell[5]\state
        count + world\cell[index]\colonie\cell[6]\state
        count + world\cell[index]\colonie\cell[7]\state
        If world\cell[index]\state = #False
          world\cell[index]\mark = Bool(count = 3)
          If world\cell[index]\mark
            Plot(x - 1,y - 1,$FF00FF) 
          EndIf
        Else
          world\cell[index]\mark = Bool(count = 2 Or count = 3)
          If world\cell[index]\mark
            Plot(x - 1,y - 1,$00FF00)
          Else
            Plot(x - 1,y - 1,$FF0000)
          EndIf
        EndIf
        index + 1
      Next
    Next
    For index = 640 To 64639
      world\cell[index]\state = world\cell[index]\mark
    Next
  Else
    For y = 1 To 200
      For x = 1 To 320
        If y = 1
          a = 64320 - 640
          b = 0
        ElseIf y = 200
          a = 0
          b = 640 - 64320
        Else
          a = 0
          b = 0
        EndIf
        world\cell[index]\colonie\cell[0] = @world\cell[index + a - 001]
        world\cell[index]\colonie\cell[1] = @world\cell[index + a - 320]
        world\cell[index]\colonie\cell[2] = @world\cell[index + b + 001]
        world\cell[index]\colonie\cell[3] = @world\cell[index + b + 320]
        world\cell[index]\colonie\cell[4] = @world\cell[index + a - 319]
        world\cell[index]\colonie\cell[5] = @world\cell[index + a - 321]
        world\cell[index]\colonie\cell[6] = @world\cell[index + b + 321]
        world\cell[index]\colonie\cell[7] = @world\cell[index + b + 319]
        world\cell[index]\mark = #False
        world\cell[index]\state = Random(1)
        index + 1
      Next
    Next
  EndIf
EndProcedure

If InitKeyboard() And InitSprite()
  CCGOL()
  If OpenScreen(320,200,32,"test") 
    Repeat
      ClearScreen($0)
      If StartDrawing(ScreenOutput())
        CCGOL(#True)
        StopDrawing()
      EndIf
      FlipBuffers()
      ExamineKeyboard()
      If KeyboardReleased(#PB_Key_Space)
        CCGOL()
      EndIf
    Until KeyboardPushed(#PB_Key_Escape)
  EndIf
EndIf

End