Author Topic: Game of Life!  (Read 9477 times)

0 Members and 1 Guest are viewing this topic.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Game of Life!
« Reply #30 on: September 25, 2019, 03:12:00 am »
Hi coming back to the thread

I think I have made something wrong because with example posted at starting by Cobalt I got  overpopulation errors...
see here
results got going Step by Step
 
GoL Cobalt sovrappopulation.jpg

result got going AUTO
 
GoL Cobalt sovrappopulation2.jpg


Thanks to show how correct this!
Programming isn't difficult, only it's  consuming time and coffee

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Game of Life!
« Reply #31 on: September 27, 2019, 05:26:49 pm »
Hi coming back to the thread

I think I have made something wrong because with example posted at starting by Cobalt I got  overpopulation errors...
see here
results got going Step by Step
result got going AUTO
Thanks to show how correct this!

Using 1.3 right? why I don't use it yet.

Give this a try, all I did was separate the CONST with the colors into their own lines. I think that should help, beyond that I have no idea, as nobody else has said anything like that.

Code: QB64: [Select]
  1.     'so called Game of Life, though not a real game.
  2.     'Based on mathematician, John H. Conway's 1970 simulation that he called
  3.     'the Game of Life. There is no winning or losing, just experimenting.
  4.     'Cells are kept alive with 2-3 living cells around them
  5.     'Cells are brought to life if there are 3 live cells around
  6.     'Cells Die of loneliness if there are 1-0 live cells around
  7.     'Cells are smoothered if there are 4 or more live cells around.
  8.      
  9.      
  10.     SCREEN _NEWIMAGE(640, 480, 32): CLS: RANDOMIZE TIMER
  11.     Mix& = _NEWIMAGE(640, 480, 32)
  12.     Cell& = _NEWIMAGE(640, 480, 32)
  13.     _DELAY .15
  14.     _TITLE "Game Of Life? V0.1"
  15.     _SCREENMOVE 20, 20
  16.      
  17.     CONST TRUE = -1, FALSE = NOT TRUE
  18.     CONST Blue = _RGB32(0, 16, 224)
  19.     CONST Black = _RGB32(8, 8, 8)
  20.     CONST  White = _RGB32(240, 240, 240)
  21.     CONST MaxX = 50 'number of columns 127 max
  22.     CONST SX = 500 'size of board in pixels X, 510 max(cause instruction area)
  23.     CONST MaxY = 30 'number of rows 127 max
  24.     CONST SY = 470 'size of board in pixels Y,480 max(screen size limitation)
  25.     CONST Pass = 85 'random chance of life 0-100%(0=All live, 100=all dead, I know its backwards)
  26.      
  27.     DIM SHARED Board(MaxX, MaxY) AS _BYTE, Speed!
  28.     T1& = _FREETIMER
  29.     ON TIMER(T1&, .05) AgeBoard
  30.     _DEST Cell&
  31.     ClearBoard Board()
  32.     MakeRandom Board()
  33.     Speed! = .25
  34.      
  35.     DO
  36.      Nul = _MOUSEINPUT
  37.      '--------------------INPUT-------------------------
  38.      KBD$ = UCASE$(INKEY$)
  39.      SELECT CASE KBD$
  40.       CASE CHR$(27)
  41.        ExitFlag%% = TRUE
  42.       CASE "A" 'Turn on Automatic cycle
  43.        TIMER(T1&) ON
  44.        Auto = TRUE
  45.       CASE "S" 'Step 1 turn/Turn off Automatic cycle
  46.        IF Auto THEN TIMER(T1&) OFF: Auto = FALSE
  47.        AgeBoard
  48.       CASE "C" 'Clear Board/Turn off Automatic Cycle
  49.        IF Auto THEN TIMER(T1&) OFF: Auto = FALSE
  50.        ClearBoard Board()
  51.       CASE "F" 'random Fill board grid/Turn off Automatic Cycle
  52.        IF Auto THEN TIMER(T1&) OFF: Auto = FALSE
  53.        MakeRandom Board()
  54.       CASE "Q"
  55.        Speed! = Speed! + .05
  56.        IF Speed! >= 1.5 THEN Speed! = 1.5
  57.       CASE "P"
  58.        Speed! = Speed! - .05
  59.        IF Speed! <= .05 THEN Speed! = .05
  60.      END SELECT
  61.      '-------------------------------------------------------
  62.      
  63.      '----------------------MOUSE----------------------------
  64.      IF _MOUSEBUTTON(1) THEN
  65.       CheckCell _MOUSEX, _MOUSEY, Board()
  66.      END IF
  67.      '-------------------------------------------------------
  68.      
  69.      '--------------------Display----------------------------
  70.      DrawGrid MaxX, MaxY
  71.      DrawCells Board()
  72.      _PUTIMAGE , Cell&, Mix&
  73.      _PUTIMAGE , Mix&, _DISPLAY
  74.      _PRINTSTRING (520, 10), "A-automatic", _DISPLAY
  75.      _PRINTSTRING (520, 30), "S-Step by Step", _DISPLAY
  76.      _PRINTSTRING (520, 50), "C-Clear Board", _DISPLAY
  77.      _PRINTSTRING (520, 70), "F-Random Fill", _DISPLAY
  78.      _PRINTSTRING (520, 90), "Q-Slower cycles", _DISPLAY
  79.      _PRINTSTRING (520, 110), "P-Faster cycles", _DISPLAY
  80.      _PRINTSTRING (520, 130), "Speed:" + STR$(Speed!) + " sec   ", _DISPLAY
  81.      IF Auto THEN _PRINTSTRING (520, 150), "Auto:  ON", _DISPLAY ELSE _PRINTSTRING (520, 150), "Auto: OFF", _DISPLAY
  82.      _PRINTSTRING (520, 200), "Or select cells", _DISPLAY
  83.      _PRINTSTRING (520, 220), "with the mouse", _DISPLAY
  84.      '-------------------------------------------------------
  85.      IF Auto THEN _DELAY Speed! ELSE _DELAY .016
  86.     LOOP UNTIL ExitFlag%% = TRUE
  87.      
  88.     SUB AgeBoard ()
  89.      FOR x%% = 0 TO MaxX
  90.       FOR y%% = 0 TO MaxY
  91.      
  92.        Live%% = Check_Live(x%%, y%%) 'how many cells around the current one are alive?
  93.        IF Live%% = 3 AND (NOT Board(x%%, y%%)) THEN GiveLife x%%, y%% 'just right for NEW life!
  94.        IF Live%% <= 1 AND Board(x%%, y%%) THEN TakeLife x%%, y%% 'not enough friends
  95.        IF Live%% >= 4 AND Board(x%%, y%%) THEN TakeLife x%%, y%% 'TOO many friends
  96.      
  97.      NEXT y%%, x%%
  98.     END SUB
  99.      
  100.     SUB ClearBoard (B() AS _BYTE)
  101.      FOR x%% = 0 TO MaxX
  102.       FOR y%% = 0 TO MaxY
  103.        B(x%%, y%%) = FALSE
  104.      NEXT y%%, x%%
  105.     END SUB
  106.      
  107.     SUB MakeRandom (B() AS _BYTE)
  108.      FOR x%% = 0 TO MaxX
  109.       FOR y%% = 0 TO MaxY
  110.        IF INT(RND * 100) > Pass%% THEN B(x%%, y%%) = TRUE
  111.      NEXT y%%, x%%
  112.     END SUB
  113.      
  114.     FUNCTION ToggleCell (X%%, Y%%, B() AS _BYTE)
  115.      ToggleCell = NOT B(X%%, Y%%)
  116.      
  117.     FUNCTION Check_Live%% (X%%, Y%%)
  118.      Result%% = 0
  119.      StartX%% = 0: StartY%% = 0
  120.      EndX%% = 0: EndY%% = 0
  121.      'check that current cells neighbors are within the grid array
  122.      IF X%% - 1 > LBOUND(Board, 1) THEN StartX%% = X%% - 1 ELSE StartX%% = X%%
  123.      IF Y%% - 1 > LBOUND(Board, 2) THEN StartY%% = Y%% - 1 ELSE StartY%% = Y%%
  124.      IF X%% + 1 <= UBOUND(Board, 1) THEN EndX%% = X%% + 1 ELSE EndX%% = X%%
  125.      IF Y%% + 1 <= UBOUND(Board, 2) THEN EndY%% = Y%% + 1 ELSE EndY%% = Y%%
  126.      FOR Ix%% = StartX%% TO EndX%%
  127.       FOR Iy%% = StartY%% TO EndY%%
  128.        'dont bother with current cell being checked so dont process it
  129.        IF Ix%% = X%% AND Iy%% = Y%% THEN ELSE IF Board(Ix%%, Iy%%) THEN Result%% = Result%% + 1
  130.       NEXT
  131.      NEXT
  132.      'return the number of live cells around current cell
  133.      Check_Live = Result%%
  134.      
  135.     SUB GiveLife (X%%, Y%%)
  136.      Board(X%%, Y%%) = ToggleCell(X%%, Y%%, Board())
  137.     END SUB
  138.      
  139.     SUB TakeLife (X%%, Y%%)
  140.      Board(X%%, Y%%) = ToggleCell(X%%, Y%%, Board())
  141.     END SUB
  142.      
  143.     SUB DrawGrid (X%%, Y%%)
  144.      ScaleY = SY \ MaxY
  145.      ScaleX = SX \ MaxX
  146.      LINE (0, 0)-(SX, SY), White, B
  147.      FOR Ix% = 0 TO SX STEP ScaleX
  148.       LINE (Ix%, 0)-(Ix%, SY), White
  149.      NEXT
  150.      FOR Iy% = 0 TO SY STEP ScaleY
  151.       LINE (0, Iy%)-(SX, Iy%), White
  152.      NEXT
  153.     END SUB
  154.      
  155.     SUB DrawCells (B() AS _BYTE)
  156.      ScaleY = SY \ MaxY
  157.      ScaleX = SX \ MaxX
  158.      FOR X%% = 0 TO MaxX - 1
  159.       FOR Y%% = 0 TO MaxY
  160.        IF B(X%%, Y%%) THEN LINE (1 + X%% * ScaleX, 1 + Y%% * ScaleY)-STEP(ScaleX - 1, ScaleY - 1), Blue, BF ELSE LINE (1 + X%% * ScaleX, 1 + Y%% * ScaleY)-STEP(ScaleX - 2, ScaleY - 2), Black, BF
  161.      NEXT Y%%, X%%
  162.     END SUB
  163.      
  164.     SUB CheckCell (X%, Y%, B() AS _BYTE)
  165.      ScaleY = SY \ MaxY
  166.      ScaleX = SX \ MaxX
  167.      MX%% = X% \ ScaleX
  168.      MY%% = Y% \ ScaleY
  169.      B(MX%%, MY%%) = ToggleCell(MX%%, MY%%, B())
  170.      _DELAY .15
  171.     END SUB
  172.      
Granted after becoming radioactive I only have a half-life!

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Game of Life!
« Reply #32 on: September 27, 2019, 06:38:29 pm »
Hi Cobalt
Thanks for the upgrade! It runs well now!

Sorry for next my feedback... the mouseinput pass parameters to program also when click is made out of grid of life resulting in an error in the index of the grid.
Programming isn't difficult, only it's  consuming time and coffee