YaK:: Basic09 demo : Conway's Life 50x20 torus (two) [Changes]   [Calendar]   [Search]   [Index]   [PhotoTags]   
[mega_changes]
[photos]

Basic09 demo: Conway's Life 50x20 torus (two)


(* Conway's Game of Life for Basic09 / NitrOS-9 *)
PROCEDURE life
PARAM
TYPE grid_type = grid(50,20):BYTE

DIM cur_grid, nxt_grid: grid_type
DIM x, y, dx, dy, nx, ny: INTEGER
DIM count, gen: INTEGER
DIM row_str: STRING[50]

(* Clear both buffers *)
FOR y = 1 TO 20
  FOR x = 1 TO 50
    cur_grid.grid(x, y) = 0
    nxt_grid.grid(x, y) = 0
  NEXT x
NEXT y

(* Seed the R-Pentomino near center (X: 25..27, Y: 9..11) *)
(* Pattern layout:                                       *)
(*    . # #                                              *)
(*    # # .                                              *)
(*    . # .                                              *)
cur_grid.grid(26, 9)  = 1
cur_grid.grid(27, 9)  = 1
cur_grid.grid(25, 10) = 1
cur_grid.grid(26, 10) = 1
cur_grid.grid(26, 11) = 1

gen = 0

(* Main simulation loop *)
LOOP
  (* Optional VT52 / ANSI home-cursor & clear screen *)
  (* PRINT CHR$(27);"[H";CHR$(27);"[2J"; *)

  PRINT "--- Generation: "; gen; " ---"

  (* Print board line by line *)
  FOR y = 1 TO 20
    row_str = ""
    FOR x = 1 TO 50
      IF cur_grid.grid(x, y) = 1 THEN
        row_str = row_str + "#"
      ELSE
        row_str = row_str + "-"
      ENDIF
    NEXT x
    PRINT row_str
  NEXT y

  (* Calculate next generation with toroidal wrap *)
  FOR y = 1 TO 20
    FOR x = 1 TO 50
      count = 0

      FOR dy = -1 TO 1
        FOR dx = -1 TO 1
          IF (dx <> 0) OR (dy <> 0) THEN
            nx = x + dx
            ny = y + dy

            (* Torus boundary wrapping *)
            IF nx < 1 THEN
              nx = 50
            ELSE
              IF nx > 50 THEN
                nx = 1
              ENDIF
            ENDIF

            IF ny < 1 THEN
              ny = 20
            ELSE
              IF ny > 20 THEN
                ny = 1
              ENDIF
            ENDIF

            IF cur_grid.grid(nx, ny) = 1 THEN
              count = count + 1
            ENDIF
          ENDIF
        NEXT dx
      NEXT dy

      (* Apply Conway's rules *)
      IF cur_grid.grid(x, y) = 1 THEN
        IF (count = 2) OR (count = 3) THEN
          nxt_grid.grid(x, y) = 1
        ELSE
          nxt_grid.grid(x, y) = 0
        ENDIF
      ELSE
        IF count = 3 THEN
          nxt_grid.grid(x, y) = 1
        ELSE
          nxt_grid.grid(x, y) = 0
        ENDIF
      ENDIF

    NEXT x
  NEXT y

  (* Copy nxt_grid to cur_grid *)
  FOR y = 1 TO 20
    FOR x = 1 TO 50
      cur_grid.grid(x, y) = nxt_grid.grid(x, y)
    NEXT x
  NEXT y

  gen = gen + 1
ENDLOOP

(unless otherwise marked) Copyright 2002-2014 YakPeople. All rights reserved.
(last modified 2026-10-03)       [Login]
(No back references.)