Conway's GAME OF LIFE.
#1
The Famous GAME OF LIFE (Not! working at the moment!) Sad

An important Game? regarding Computer A.I research!

http://en.wikipedia.org/wiki/Conway's_Game_of_Life

Anthony.


Code:
CLS
RANDOMIZE TIMER
SCREEN 12




COLOR 10
PRINT "Game of life."
INPUT "First coordinate of grid: ", co1
INPUT "Enter second coordinate of grid: ", co2
INPUT "Set color (0 to 15) : ", col
DIM oldgen(co2 - co1, co2 - co1) AS INTEGER 'declaring  old gen array
DIM newgen(co2 - co1, co2 - co1) AS INTEGER 'declaring new generation array


CLS
LINE (co1, co1 - 1)-(co2 + 1, co2 + 1), 15, B  'the frame of the whole autotmate
oldgen(75, 75) = 1 ' the following are the coordinates for a glider
oldgen(75, 76) = 1
oldgen(75, 77) = 1
oldgen(74, 77) = 1
oldgen(73, 76) = 1


FOR i = 1 TO (co2 - co1) 'displaying the old generation array (glider...)
   FOR j = 1 TO (co2 - co1)
  IF oldgen(i, j) > 0 THEN
   PSET(j + co1,i + co1),col
  END IF
   NEXT
NEXT

DO WHILE  true     'infinite outer loop repeating the whole algorithm  
counter = 0   'initializing counter
FOR i = 1 TO (co2 - co1)
   FOR j = 1 TO (co2 - co1)
    IF ((j + 1) < (co2 - co1)) AND ((i + 1) < (co2 - co1)) THEN 'see if cell is not on border, and if not      
      IF oldgen(i - 1, j - 1) > 0 THEN    'checking neighbours and incrementing
      counter = counter + 1  'counter for each "living" cell      
     END IF

      IF oldgen(i - 1, j) > 0 THEN
      counter = counter + 1
      END IF

      IF oldgen(i - 1, j + 1) > 0 THEN
      counter = counter + 1
      END IF

      IF oldgen(i, j - 1) > 0 THEN
      counter = counter + 1
      END IF

      IF oldgen(i, j + 1) > 0 THEN
      counter = counter + 1
      END IF

      IF oldgen(i + 1, j - 1) > 0 THEN
      counter = counter + 1
      END IF

      IF oldgen(i + 1, j) > 0 THEN
      counter = counter + 1
      END IF

      IF oldgen(i + 1, j + 1) > 0 THEN
      counter = counter + 1
      END IF
   END IF

   IF (oldgen(i, j) > 0) AND ((counter = 2) OR (counter = 3)) THEN 'if the cell
   newgen(i, j) = 1 ' is alive and has 2 or 3 neighbours it will be alive in the
   END IF 'next step too. so fill newgen array with a 1.
   IF (oldgen(i, j) = 0) AND (counter = 3) THEN 'if the cell is dead and has 3
   newgen(i, j) = 1 'neighbours, it will be alive in the next step.fill newgen
   END IF ' with a 1.
  NEXT
NEXT

FOR i = 1 TO (co2 - co1) ' display the new generation
   FOR j = 1 TO (co2 - co1)
  IF newgen(i, j) > 0 THEN
   PSET(j + co1,i + co1),col
  END IF
   NEXT
NEXT

FOR i = 1 TO (co2 - co1)
   FOR j = 1 TO (co2 - co1)
   oldgen(i, j) = newgen(i, j)'set oldgen array to newgen array
   newgen(i, j) = 0 'reinitialize newgen to 0
   NEXT
NEXT

LOOP
Reply
#2
[youtube]XcuBvj0pw-E[/youtube]

[youtube]FdMzngWchDk[/youtube]


[youtube]k2IZ1qsx4CM[/youtube]

[youtube]Q7BnNd866O8[/youtube]

[youtube]Zu5ORo3Im1g[/youtube]
Reply
#3
Cellular Automata..


Simple Rules lead to Complex Systems!

http://natureofcode.com/book/chapter-7-c...-automata/





Anthony.
Reply
#4
Here's a game of life with gospers glider gun that I made in QB64, you can also draw in it if you like using the mouse.

Code:
DIM xx(319 * 199), yy(319 * 199), co(319 * 199) 

'graphics: 
SCREEN 7, 0, 1, 0 
high = 1 
pbase = 15 - high 
 
 
' gospers glider gun (taken from wiki). 
PSET (100, 104), pbase + 1 ' 
PSET (100, 105), pbase + 1 ' 
PSET (101, 104), pbase + 1 
PSET (101, 105), pbase + 1 
PSET (110, 104), pbase + 1 ' 
PSET (110, 105), pbase + 1 ' 
PSET (110, 106), pbase + 1 ' 
PSET (111, 103), pbase + 1 
PSET (111, 107), pbase + 1 
PSET (112, 102), pbase + 1 ' 
PSET (112, 108), pbase + 1 ' 
PSET (113, 102), pbase + 1 
PSET (113, 108), pbase + 1 
PSET (114, 105), pbase + 1 ' 
PSET (115, 103), pbase + 1 
PSET (115, 107), pbase + 1 
PSET (116, 104), pbase + 1 ' 
PSET (116, 105), pbase + 1 ' 
PSET (116, 106), pbase + 1 ' 
PSET (117, 105), pbase + 1 
PSET (120, 102), pbase + 1 ' 
PSET (120, 103), pbase + 1 ' 
PSET (120, 104), pbase + 1 ' 
PSET (121, 102), pbase + 1 
PSET (121, 103), pbase + 1 
PSET (121, 104), pbase + 1 
PSET (122, 101), pbase + 1 ' 
PSET (122, 105), pbase + 1 ' 
PSET (124, 100), pbase + 1 
PSET (124, 101), pbase + 1 
PSET (124, 105), pbase + 1 ' 
PSET (124, 106), pbase + 1 ' 
PSET (134, 102), pbase + 1 
PSET (134, 103), pbase + 1 
PSET (135, 102), pbase + 1 ' 
PSET (135, 103), pbase + 1 ' 
 
'Conway's game of life. 
 
'rules; 
'1) any live cell with < 2 neighbors dies. 2 
'2) live cell with > 3 neighbors dies. 
'3) live cell with 2 or 3 neighbors lives on. 
'4) Dead cell with 3 neighbors becomes a live cell. 
 
 

PCOPY 1, 0 
DO 
PSET (xmus, ymus), POINT(xmus, ymus) 
DO WHILE _MOUSEINPUT 
xmus = _MOUSEX 
ymus = _MOUSEY 
bmus = _MOUSEBUTTON(1) 
LOOP 
 
 
PCOPY 1, 0 
 
IF bmus <> 0 THEN LINE -(xmus, ymus), 15 
LOOP WHILE bmus <> 0 
 
 
 
FOR x = 1 TO 319 
FOR y = 1 TO 199 
 
 
l = POINT(x - 1, y) - pbase 
r = POINT(x + 1, y) - pbase 
u = POINT(x, y - 1) - pbase 
d = POINT(x, y + 1) - pbase 
ul = POINT(x - 1, y - 1) - pbase 
ur = POINT(x + 1, y - 1) - pbase 
dl = POINT(x - 1, y + 1) - pbase 
dr = POINT(x + 1, y + 1) - pbase 
you = POINT(x, y) - pbase 
 
IF l < 1 THEN l = 0 
IF r < 1 THEN r = 0 
IF u < 1 THEN u = 0 
IF d < 1 THEN d = 0 
IF ul < 1 THEN ul = 0 
IF ur < 1 THEN ur = 0 
IF dl < 1 THEN dl = 0 
IF dr < 1 THEN dr = 0 
IF you < 1 THEN you = 0 
 
c = 0 
 
IF l = 1 THEN c = c + 1 
IF r = 1 THEN c = c + 1 
IF u = 1 THEN c = c + 1 
IF d = 1 THEN c = c + 1 
IF ul = 1 THEN c = c + 1 
IF ur = 1 THEN c = c + 1 
IF dl = 1 THEN c = c + 1 
IF dr = 1 THEN c = c + 1 
 
 
IF you = 0 THEN 
IF c = 3 THEN count = count + 1: xx(count) = x: yy(count) = y: co(count) = pbase + 1 
END IF 
 
 
IF you = 1 THEN 
IF c = 2 OR c = 3 THEN count = count + 1: xx(count) = x: yy(count) = y: co(count) = pbase + 1 
END IF 
 
NEXT 
NEXT 
CLS 
FOR k = 1 TO count 
PSET (xx(k), yy(k)), co(k) 
NEXT 
count = 0 
IF INKEY$ = CHR$(27) THEN END 
GOTO 1 
Reply
#5
I have no idea why ARB filed this under AI Pathfinding Algos, because it sounds so impressive?

ARB's code not working for me, does it work anyone else?

Well here is some code you can modify to your hearts content:
Code:
_TITLE "Quick Life trans for QB64 11-06.82  2017-11-11by bplus"
' From: quick life.bas SmallBASIC (not MS) B+ G7 stripped down to favorite setting
'
' To: the one out there who has checked out Conway's Life the last couple of days.
' For you, a working version (albeit highly modified) of Conway's Life code in QB64.
'
' Quote Rules (from Wiki):
' The universe of the Game of Life is an infinite two-dimensional orthogonal grid of square cells,
' each of which is in one of two possible states, alive or dead, or "populated" or "unpopulated".
' Every cell interacts with its eight neighbours, which are the cells that are horizontally,
' vertically, or diagonally adjacent. At each step in time, the following transitions occur:
' 1) Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
' 2) Any live cell with two or three live neighbours lives on to the next generation.
' 3) Any live cell with more than three live neighbours dies, as if by overpopulation.
' 4) Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
' The initial pattern constitutes the seed of the system.
' The first generation is created by applying the above rules simultaneously to every cell in the
' seed—births and deaths occur simultaneously, and the discrete moment at which this happens is
' sometimes called a tick (in other words, each generation is a pure function of the preceding one).
' The rules continue to be applied repeatedly to create further generations.
' (End Quote)

' Alas in practical applications we do not have infinite board to play Life, so at boundries rules
' break down as neighbor counts are only 5 max on edge and only 3 max at corners.

'This code is very easy to modify into other tests / demos:
' Try coloring by neighbor counts.
' Try other rules besides Classic 2,3 neighbors = survive, 3 neighbors = birth.
' Try regenration along the borders every other generation, which causes symetric beauties!
' Change an = the number of cells per side, even amounts that divide 700 (pixels per board side) work best.

CONST xmax = 700
CONST ymax = 700

SCREEN _NEWIMAGE(xmax, ymax, 32)
_SCREENMOVE 360, 20

'DEFINT A-Z
DIM qb&(15) 'thanks Andy Amaya for use with his sub qColor fore, back
qb&(0) = _RGB(0, 0, 0) '       black
qb&(1) = _RGB(0, 0, 128) '     blue
qb&(2) = _RGB(8, 128, 8) '     green
qb&(3) = _RGB(0, 128, 128) '   cyan
qb&(4) = _RGB(128, 0, 0) '     red
qb&(5) = _RGB(128, 0, 128) '   magenta
qb&(6) = _RGB(128, 64, 32) '   brown
qb&(7) = _RGB(168, 168, 168) ' white
qb&(8) = _RGB(128, 128, 128) ' grey
qb&(9) = _RGB(84, 84, 252) '   light blue
qb&(10) = _RGB(42, 252, 42) '  light green
qb&(11) = _RGB(0, 220, 220) '  light cyan
qb&(12) = _RGB(255, 0, 0) '    light red
qb&(13) = _RGB(255, 84, 255) ' light magenta
qb&(14) = _RGB(255, 255, 0) '  yellow
qb&(15) = _RGB(255, 255, 255) 'bright white

'test colors
'FOR i = 0 TO 15
'    PRINT i,
'    LINE (100, 100)-(500, 500), qb&(i), BF
'    _LIMIT 1
'NEXT

'for Mandala Life an = 140 in next line an = number of cells on sides of board
an = 70: s = INT(ymax / an): bigBlock = an * s: g = 0
DIM a(1 TO an, 1 TO an), ng(1 TO an, 1 TO an), ls(1 TO an, 1 TO an)

'seed for Conway's Life Classic
FOR y = 2 TO an - 1
   FOR x = 2 TO an - 1

       ' a(x, y) = INT(RND * 2)  'for random mess

       'for symmetric line
       IF y = an / 2 OR y = an / 2 + 1 THEN a(x, y) = 1

   NEXT
NEXT

WHILE INKEY$ <> " "

   ' Mandala Life regeneration of Mandala like arrays, seeds every other generation along edges
   'IF g MOD 2 = 0 THEN
   'FOR x = 1 TO an
   '    a(x, 1) = 1: a(x, an) = 1: a(1, x) = 1: a(an, x) = 1
   'NEXT
   'END IF

   FOR x = 2 TO an - 1
       FOR y = 2 TO an - 1
           pc = a(x - 1, y - 1) + a(x - 1, y) + a(x - 1, y + 1) + a(x, y - 1) + a(x, y + 1) + a(x + 1, y - 1) + a(x + 1, y) + a(x + 1, y + 1)
           ls(x, y) = pc
           r$ = RIGHT$(STR$(pc), 1)
           IF a(x, y) THEN 'cell is alive so what is surviveRule

               'Bplus favorite Mandala Life Rules for survival and birth
               'IF INSTR("2346", r$) THEN ng(x, y) = 1 ELSE ng(x, y) = 0

               'Classic Conway's Life Rules
               IF INSTR("23", r$) THEN ng(x, y) = 1 ELSE ng(x, y) = 0

           ELSE 'birth?

               'Bplus favorite Mandala Life Rules for survival and birth
               'IF INSTR("34", r$) THEN ng(x, y) = 1 ELSE ng(x, y) = 0

               'Classic Conway's Life Rules
               IF INSTR("3", r$) THEN ng(x, y) = 1 ELSE ng(x, y) = 0
           END IF
       NEXT
   NEXT

   'Bplus favorite Mandala Life Rules for survival and birth
   'LINE (1, 1)-(bigBlock, bigBlock), qb&(0), BF

   'Classic Conway's Life Rules
   LINE (1, 1)-(bigBlock, bigBlock), qb&(1), BF
   FOR y = 1 TO an
       FOR x = 1 TO an
           IF a(x, y) THEN 'show old a with it's neighbor counts br yellow or black

               'Bplus favorite Mandala Life Rules for survival and birth
               'LINE ((x - 1) * s + 1, (y - 1) * s + 1)-STEP(s, s), qb&(0), BF

               'this separates into individual cells for Classic look
               LINE ((x - 1) * s + 1, (y - 1) * s + 1)-STEP(s - 2, s - 2), qb&(15), BF

               'Mandala Life coloring by neighbor counts
               'ELSE
               '    lc = ls(x, y)
               '    SELECT CASE lc
               '        CASE 0: cl = 15 ' br white
               '        CASE 1: cl = 11 ' cyan
               '        CASE 2: cl = 7 '  low white, br gray
               '        CASE 3: cl = 10 ' light green
               '        CASE 4: cl = 9 '  blue
               '        CASE 5: cl = 13 ' violet
               '        CASE 6: cl = 12 ' br red
               '        CASE 7: cl = 4 '  dark red
               '        CASE 8: cl = 0 '  black
               '    END SELECT
               '    LINE ((x - 1) * s + 1, (y - 1) * s + 1)-STEP(s, s), qb&(cl), BF

           END IF
       NEXT
   NEXT
   _DISPLAY
   FOR y = 1 TO an
       FOR x = 1 TO an
           a(x, y) = ng(x, y) 'load a() with next generation data
       NEXT
   NEXT
   g = g + 1
   _LIMIT 10
WEND

First screen shot is when seed fills screen
2nd when 4 "gliders" form in center
3rd final stabilized life with 16 "blinkers"
This was seeded with a double line of cells across the center.

EDIT: Fix commented Mandala settings to match code in the next post.


Attached Files Thumbnail(s)

B += x
Reply
#6
Same code as above transformed into Mandala Life:
Code:
_TITLE "Mandala Life trans for QB64 11-06.82  2017-11-11 by bplus"
' From: quick life.bas SmallBASIC (not MS) B+ G7 stripped down to favorite setting
'
' To: the one out there who has checked out Conway's Life the last couple of days.
' For you, a working version (albeit highly modified) of Conway's Life code in QB64.
'
' Quote Rules (from Wiki):
' The universe of the Game of Life is an infinite two-dimensional orthogonal grid of square cells,
' each of which is in one of two possible states, alive or dead, or "populated" or "unpopulated".
' Every cell interacts with its eight neighbours, which are the cells that are horizontally,
' vertically, or diagonally adjacent. At each step in time, the following transitions occur:
' 1) Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
' 2) Any live cell with two or three live neighbours lives on to the next generation.
' 3) Any live cell with more than three live neighbours dies, as if by overpopulation.
' 4) Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
' The initial pattern constitutes the seed of the system.
' The first generation is created by applying the above rules simultaneously to every cell in the
' seed—births and deaths occur simultaneously, and the discrete moment at which this happens is
' sometimes called a tick (in other words, each generation is a pure function of the preceding one).
' The rules continue to be applied repeatedly to create further generations.
' (End Quote)

' Alas in practical applications we do not have infinite board to play Life, so at boundries rules
' break down as neighbor counts are only 5 max on edge and only 3 max at corners.

'This code is very easy to modify into other tests / demos:
' Try coloring by neighbor counts.
' Try other rules besides Classic 2,3 neighbors = survive, 3 neighbors = birth.
' Try regenration along the borders every other generation, which causes symetric beauties!
' Change an = the number of cells per side, even amounts that divide 700 (pixels per board side) work best.

CONST xmax = 700
CONST ymax = 700

SCREEN _NEWIMAGE(xmax, ymax, 32)
_SCREENMOVE 360, 20

'DEFINT A-Z
DIM qb&(15) 'thanks Andy Amaya for use with his sub qColor fore, back
qb&(0) = _RGB(0, 0, 0) '       black
qb&(1) = _RGB(0, 0, 128) '     blue
qb&(2) = _RGB(8, 128, 8) '     green
qb&(3) = _RGB(0, 128, 128) '   cyan
qb&(4) = _RGB(128, 0, 0) '     red
qb&(5) = _RGB(128, 0, 128) '   magenta
qb&(6) = _RGB(128, 64, 32) '   brown
qb&(7) = _RGB(168, 168, 168) ' white
qb&(8) = _RGB(128, 128, 128) ' grey
qb&(9) = _RGB(84, 84, 252) '   light blue
qb&(10) = _RGB(42, 252, 42) '  light green
qb&(11) = _RGB(0, 220, 220) '  light cyan
qb&(12) = _RGB(255, 0, 0) '    light red
qb&(13) = _RGB(255, 84, 255) ' light magenta
qb&(14) = _RGB(255, 255, 0) '  yellow
qb&(15) = _RGB(255, 255, 255) 'bright white

'test colors
'FOR i = 0 TO 15
'    PRINT i,
'    LINE (100, 100)-(500, 500), qb&(i), BF
'    _LIMIT 1
'NEXT

an = 140: s = INT(ymax / an): bigBlock = an * s: g = 0
DIM a(1 TO an, 1 TO an), ng(1 TO an, 1 TO an), ls(1 TO an, 1 TO an)

'seed for Conway's Life Classic
'FOR y = 2 TO an - 1
'    FOR x = 2 TO an - 1

'        ' a(x, y) = INT(RND * 2)  'for random mess

'        'for symmetric line
'        IF y = an / 2 OR y = an / 2 + 1 THEN a(x, y) = 1

'    NEXT
'NEXT

WHILE INKEY$ <> " "

   ' Mandala Life regeneration of Mandala like arrays, seeds every other generation along edges
   IF g MOD 2 = 0 THEN
       FOR x = 1 TO an
           a(x, 1) = 1: a(x, an) = 1: a(1, x) = 1: a(an, x) = 1
       NEXT
   END IF

   FOR x = 2 TO an - 1
       FOR y = 2 TO an - 1
           pc = a(x - 1, y - 1) + a(x - 1, y) + a(x - 1, y + 1) + a(x, y - 1) + a(x, y + 1) + a(x + 1, y - 1) + a(x + 1, y) + a(x + 1, y + 1)
           ls(x, y) = pc
           r$ = RIGHT$(STR$(pc), 1)
           IF a(x, y) THEN 'cell is alive so what is surviveRule

               'Bplus favorite Mandala Life Rules for survival and birth
               IF INSTR("2346", r$) THEN ng(x, y) = 1 ELSE ng(x, y) = 0

               'Classic Conway's Life Rules
               'IF INSTR("23", r$) THEN ng(x, y) = 1 ELSE ng(x, y) = 0

           ELSE 'birth?

               'Bplus favorite Mandala Life Rules for survival and birth
               IF INSTR("34", r$) THEN ng(x, y) = 1 ELSE ng(x, y) = 0

               'Classic Conway's Life Rules
               'IF INSTR("3", r$) THEN ng(x, y) = 1 ELSE ng(x, y) = 0
           END IF
       NEXT
   NEXT

   'Bplus favorite Mandala Life Rules for survival and birth
   LINE (1, 1)-(bigBlock, bigBlock), qb&(0), BF

   'Classic Conway's Life Rules
   'LINE (1, 1)-(bigBlock, bigBlock), qb&(1), BF
   FOR y = 1 TO an
       FOR x = 1 TO an
           IF a(x, y) THEN 'show old a with it's neighbor counts br yellow or black

               'Bplus favorite Mandala Life Rules for survival and birth
               LINE ((x - 1) * s + 1, (y - 1) * s + 1)-STEP(s, s), qb&(0), BF

               'this separates into individual cells for Classic look
               'LINE ((x - 1) * s + 1, (y - 1) * s + 1)-STEP(s - 2, s - 2), qb&(15), BF

               'Mandala Life coloring by neighbor counts
           ELSE
               lc = ls(x, y)
               SELECT CASE lc
                   CASE 0: cl = 15 ' br white
                   CASE 1: cl = 11 ' cyan
                   CASE 2: cl = 7 '  low white, br gray
                   CASE 3: cl = 10 ' light green
                   CASE 4: cl = 9 '  blue
                   CASE 5: cl = 13 ' violet
                   CASE 6: cl = 12 ' br red
                   CASE 7: cl = 4 '  dark red
                   CASE 8: cl = 0 '  black
               END SELECT
               LINE ((x - 1) * s + 1, (y - 1) * s + 1)-STEP(s, s), qb&(cl), BF

           END IF
       NEXT
   NEXT
   _DISPLAY
   FOR y = 1 TO an
       FOR x = 1 TO an
           a(x, y) = ng(x, y) 'load a() with next generation data
       NEXT
   NEXT
   g = g + 1
   IF g > 70 THEN _LIMIT 1
WEND


Attached Files Thumbnail(s)

B += x
Reply
#7
His code doesn't work? Let me show you my shocked face...


[Image: shocked-smiley.png]

Note to Walter. Please put shocked face emoji on the Christmas wish list.

Pete Big Grin
Reply
#8
and Walter, Pete will probably want it inflatable too.
B += x
Reply
#9
@Pete

Merry Christmas!

ShockedShockedShockedShockedShockedShockedShockedShockedShockedShockedShockedShockedShockedShockedShockedShockedShockedShockedShockedShockedShockedShockedShockedShockedShockedShockedShockedShocked
Dedicated to empowering computer programming hobbyists, tinkerers, amateurs, and enthusiasts.
profile for Walter Whitman at Stack Overflow, Q&A for professional and enthusiast programmers


Reply
#10
If anyone would like to have any specific emoji on the forum, please send them to me and I will add them.
Dedicated to empowering computer programming hobbyists, tinkerers, amateurs, and enthusiasts.
profile for Walter Whitman at Stack Overflow, Q&A for professional and enthusiast programmers


Reply
#11
(11-12-2017, 10:38 PM)bplus Wrote: and Walter, Pete will probably want it inflatable too.

Oh com'on  bplus. If I had wanted Walt to add an inflatable empji, I'd have asked for this one...

[Image: Shocked-Emoji.png]
Pete Shocked

PS Thanks Walt!!!
Reply
#12
OK Pete, time to quit playing with your bowling balls...
B += x
Reply
#13
Probably a good idea. I've been playing with them all morning but still can't seem to get that one pin down.

Pete Big Grin

If I wanted intelligent conversation, I'd just go talk to myself... except we're not on speaking terms.
Reply