News:

Welcome to RetroCoders Community

Main Menu

Conway's game of life

Started by ron77_db, Apr 01, 2022, 12:02 PM

Previous topic - Next topic

ron77_db

SCREEN 19

DIM x AS INTEGER = 99, y AS INTEGER = 36
DIM SHARED AS INTEGER grid(0 TO 100,0 TO 37), _new_grid(0 TO 100, 0 TO 37)
DIM As Integer gen, n, nei, was


RANDOMIZE TIMER


FOR x AS INTEGER = 0 TO 100
   FOR y AS INTEGER = 0 TO 37
      grid(x,y) = INT(RND*2)      
   NEXT
NEXT

SUB game(arr() AS INTEGER, was AS INTEGER, nei AS INTEGER, arr2() AS INTEGER)
   DIM n AS INTEGER
   FOR x AS INTEGER =1 TO 99
      FOR y AS INTEGER = 1 TO 36
         'IF x-1 < 1 THEN x = 1
         'IF x +1 > 99 THEN x = 99
         'IF y-1 < 1 THEN y = 1
         'IF y +1> 36 THEN y = 36
         nei = arr( x - 1, y - 1) +arr( x , y - 1)
      nei = nei + arr( x + 1, y -1)
      nei = nei + arr( x - 1, y) + arr( x + 1, y)
      nei = nei + arr( x - 1, y + 1)
      nei = nei + arr( x, y + 1) +arr( x + 1, y + 1)
      was =arr( x, y)
      If was =0 Then
        If nei =3 Then n =1 Else n =0
      Else
        If nei =3  Or nei =2 Then n =1 Else n =0
      End IF
      arr2(x,y) = n
      NEXT
   NEXT
END SUB

SUB swap_grid(arr1() AS INTEGER, arr2() AS INTEGER)
   FOR x AS INTEGER = 0 TO 100
      FOR y AS INTEGER = 0 TO 37
         arr1(x,y) = arr2(x,y)
      NEXT
   NEXT
END SUB

SUB PRINT_TO_SCREEN(x AS INTEGER, y AS INTEGER, arr()AS integer)
   IF (arr(x,y)) = 1 THEN
      LOCATE y, x : PRINT CHR(219);
   ELSEIF (arr(x,y)) = 0 THEN
      LOCATE y, x : PRINT " ";
      
   ENDIF
END SUB


SUB print_grid(grid() AS INTEGER)
   FOR x AS INTEGER = 0 TO 100
      FOR y AS INTEGER = 0 TO 37
         PRINT_TO_SCREEN(x,y,grid())
      NEXT
   NEXT
END SUB

DIM s AS STRING = "in memory of JOHN HORTON CONWAY 1937 - 2020"
DIM t AS STRING = "CONWAY'S GAME OF LIFE"
LOCATE 15, (LOWORD(WIDTH) - LEN(s)) SHR 1 : PRINT s
LOCATE 18, (LOWORD(WIDTH) - LEN(t)) SHR 1 : PRINT t
SLEEP 
CLS

DO 
   GAME(grid(),was,nei, _new_grid())
   sleep 100
   SWAP_GRID(grid(),_new_grid())
   SLEEP 100
   PRINT_GRID(grid())
LOOP UNTIL INKEY = CHR(27)
SLEEP

stigma

Hi...

No, you need two arrays. It won't work with one array

At least, that is what I know you are welcome to prove me wrong

stigma

hi, aural :)

Take a look at these freebasic examples from FB main forum: https://www.freebasic.net/forum/viewtopic.php?t=29177

Tomaaz

Quote from: stigma on Jan 14, 2023, 03:47 PMNo, you need two arrays. It won't work with one array

It's absolutely possible to do it with a single one-dimensional array. An example in D (the language I use the most nowadays):

import raylib;
Color rgba(int re, int gr, int bl, int al) {
    return Color(cast(ubyte)re, cast(ubyte)gr, cast(ubyte)bl, cast(ubyte)al);
}

void main() {
    int x2, y2, l;
    uint[1000] tablica;
    for (auto x = 0; x < 399; x++) {
        l = GetRandomValue(1, 30);
        if (l == 1 && (x < 50 || x > 350)) {
            tablica[x] = 1;
        }
        else {
            tablica[x] = 0;
        }
    }
    InitWindow(420, 420, "Game of Life");
    SetTargetFPS(5);
    while (!WindowShouldClose()){
        BeginDrawing();
        ClearBackground(Color(0, 0, 0, 255));
        for (auto x = 0; x < 400; x += 20) {
            for (auto y = 0; y < 20; y += 1) {
                if (tablica[x + y] == 1) {
                    DrawCircle(x + 10, y * 20 + 10, 10, rgba(255, 0, 0, 255));
                }
                else {
                    DrawCircle(x + 10, y * 20 + 10, 10, rgba(0, 0, 0, 255));
                }
            }
        }
        EndDrawing();
        for (auto x = 0 ; x < 400; x += 20) {
            for (auto y = 0; y < 20; y += 1) {
                if (tablica[x + y] == 1) {
                    l = 0;
                    x2 = x + 20;
                    y2 = y + 1;
                    if (x2 <= 400 && y2 <= 20) {
                        if (tablica[x2 + y] == 1) {
                            l++;
                        }
                        if (tablica[x + y2] == 1) {
                            l++;
                        }
                    }
                    x2 = x - 20;
                    y2 = y - 1;
                    if (x2 >= 0 && y2 >= 0) {
                        if (tablica[x2 + y] == 1) {
                            l++;
                        }
                        if (tablica[x + y2] == 1) {
                            l++;
                        }
                    }
                    if (l == 3 || l ==2)  {
                        tablica[x + 400 + y] = 1;
                    }
                    else {
                        tablica[x + 400 + y] = 0;
                    }
                }
                else {
                    l = 0;
                    x2 = x + 20;
                    y2 = y + 1;
                    if (x2 <= 400 && y2 <= 20) {
                        if (tablica[x2 + y] == 0) {
                            l++;
                        }
                        if (tablica[x + y2] == 0) {
                            l++;
                        }
                    }
                    x2 = x - 20;
                    y2 = y - 1;
                    if (x2 >= 0 && y2 >= 0) {
                        if (tablica[x2 + y] == 0) {
                            l++;
                        }
                        if (tablica[x + y2] == 0) {
                            l++;
                        }
                    }
                    if (l == 3)  {
                        tablica[x + 400 + y] = 1;
                    }
                    else {
                        tablica[x + 400 + y] = 0;
                    }
                }
            }
        }
        for (auto x = 400; x < 799; x++) {
            tablica[x - 400] = tablica[x];
        }                    
    }
}


__blackjack__

@Tomaaz: Well that's cheating.  You are simply storing two arrays in one array.  At the expense of code readability.  And that's even unnecessary because you could have used one array with three dimensions.  One dimension each for X, Y, and "generation" for two generations.  That would make it possible to make the program even more efficient. Instead of copying the new generation to the old one, it would be possible just to switch the generation index then.
,,Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." —Brian W. Kernighan