Results 1 to 2 of 2

Thread: Conway's Game of Life [Script]

  1. #1
    Join Date
    May 2012
    Location
    Glorious Nippon
    Posts
    1,004
    Mentioned
    50 Post(s)
    Quoted
    502 Post(s)

    Thumbs up Conway's Game of Life [Script]

    Conway's Game of Life

    Simba Code:
    1. program GOL;
    2.  
    3. const
    4.   //calculation takes ~500 ns per pixel
    5.   {SETUP}
    6.   COL_MAX     = 150;     // width / SCALE
    7.   ROW_MAX     = 100;     // height / SCALE
    8.   DELAY_MS    = 090;     // ms delay between "frames"
    9.   LIFE_CHANCE = 10;      // value 1-100. chance that a pixel will start "alive"
    10.   LIFE_COLOR  = $0066ff; // color of "life"
    11.   SCALE       = 04;      // bitmap scale factor
    12.   {/SETUP}
    13.  
    14.   ROW_COUNT = COL_MAX + 2;
    15.  
    16. type
    17.   TRow = array[0..COL_MAX + 1] of Byte;
    18.   TMat = array[0..ROW_MAX + 1] of TRow;
    19.   PMat = ^TMat;
    20.   PMats = array[0..1] of PMat;
    21.   TBArr = array[0..2*ROW_COUNT + 2] of Byte;
    22.   PBArr = ^TBArr;
    23.  
    24. var
    25.   MatA, MatB: TMat;
    26.   Mats: PMats;
    27.   CurrentMat: Byte;
    28.  
    29. procedure Init();
    30. begin
    31.   Mats[0] := @MatA;
    32.   Mats[1] := @MatB;
    33.   CurrentMat := 0;
    34. end;
    35.  
    36. procedure InitRandom();
    37. var
    38.   b: Boolean;
    39.   x, y: Int32;
    40. begin
    41.   Init();
    42.   for y := 1 to ROW_MAX do
    43.     for x := 1 to COL_MAX do
    44.       if (Random(1, 100) <= LIFE_CHANCE) then MatA[y, x] := 1
    45.                                          else MatA[y, x] := 0;
    46. end;
    47.  
    48. procedure UpdateBitmap();
    49. var
    50.   PTemp: PBArr;
    51.   x, y, BMP: Int32;
    52. begin
    53.   BMP := CreateBitmap(COL_MAX, ROW_MAX);
    54.   try
    55.     for y := 1 to ROW_MAX do
    56.     begin
    57.       PTemp := @Mats[CurrentMat]^[y, 0];
    58.       for x := 1 to COL_MAX do
    59.         if (PTemp^[x]) then
    60.           FastSetPixel(bmp, x-1, y-1, LIFE_COLOR);
    61.     end;
    62.     StretchBitmapResize(bmp, COL_MAX * SCALE, ROW_MAX * SCALE);
    63.     DrawBitmapDebugImg(BMP);
    64.   finally
    65.     FreeBitmap(BMP);
    66.   end;
    67.   wait(DELAY_MS);
    68. end;
    69.  
    70. procedure Torus();
    71. var
    72.   PTemp: PBArr;
    73.   y: Int32;
    74. begin
    75.   PTemp := @Mats[CurrentMat]^[1, 0];
    76.   for y := 1 to ROW_MAX do
    77.   begin
    78.     PTemp^[0] := PTemp^[COL_MAX];
    79.     PTemp^[COL_MAX + 1] := PTemp^[1];
    80.     PTemp := Pointer(Int32(PtrUInt(PTemp) + SizeOf(TRow)));
    81.   end;
    82.   MemMove(Mats[CurrentMat]^[1, 0], Mats[CurrentMat]^[ROW_MAX + 1, 0], SizeOf(TRow));
    83.   MemMove(Mats[CurrentMat]^[ROW_MAX, 0], Mats[CurrentMat]^[0, 0], SizeOf(TRow));
    84. end;
    85.  
    86. function Survive(p: PBArr): byte;
    87. const
    88.   SURVIVES: array[False..True] of array[0..8] of Byte =
    89.                         [[0, 0, 0, 1, 0, 0, 0, 0, 0],
    90.                          [0, 0, 1, 1, 0, 0, 0, 0, 0]];
    91. var
    92.   Sum: int32;
    93. begin
    94.   Sum := Int32(p^[0]) + Int32(p^[1]) + Int32(p^[2]);
    95.   Sum := Sum + Int32(p^[ROW_COUNT]) + Int32(p^[ROW_COUNT + 2]);
    96.   Sum := Sum + Int32(p^[2 * ROW_COUNT]) + Int32(p^[2 * ROW_COUNT + 1] + Int32(p^[2 * ROW_COUNT + 2]));
    97.   result := SURVIVES[Boolean(p^[ROW_COUNT + 1]), Sum];
    98. end;
    99.  
    100. procedure NextGen();
    101. var
    102.   pA, pB: ^Byte;
    103. begin
    104.   Torus;
    105.   pA := @Mats[CurrentMat]^[0, 0];
    106.   pB := @Mats[1 - CurrentMat]^[1, 1];
    107.   for 1 to ROW_MAX do
    108.   begin
    109.     for 1 to COL_MAX do
    110.     begin
    111.       pB^ := Survive(PBarr(pA));
    112.       inc(pA);
    113.       inc(pB);
    114.     end;
    115.     inc(pA, 2);
    116.     inc(pB, 2);
    117.   end;
    118.   CurrentMat := 1 - CurrentMat;
    119. end;
    120.  
    121. begin
    122.   ClearDebug();
    123.   DisplayDebugImgWindow(COL_MAX * SCALE, ROW_MAX * SCALE);
    124.   InitRandom();
    125.   repeat
    126.     UpdateBitmap();
    127.     NextGen();
    128.   until false;
    129.   UpdateBitmap();
    130. end.
    The default settings should yield something like this:



    Have fun with it!
    Last edited by Citrus; 06-03-2017 at 11:16 AM.

  2. #2
    Join Date
    Mar 2016
    Location
    Canada
    Posts
    32
    Mentioned
    0 Post(s)
    Quoted
    3 Post(s)

    Default

    Lol, well this is interesting. Thanks!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •