Conway's Game Of Life


Closed Thread
Results 1 to 40 of 46

Hybrid View

  1. #1
    Join Date
    Feb 2007
    Posts
    55

    Default Conway's Game Of Life

    hello

    after seeing a few youtube videos on 'Conway's game of life' id like to have a go at building one on a 8x8 led matrix to start with, then maybe something bigger later.

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

    im just looking for some advice on how to check each of the cell's neighbours are alive or dead without writing a shed load of IF/THEN statments for each cell (on a 8x8 led matrix there would be 64 cells with upto 8 neighbours for each cell)

  2. #2
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    If each LED is represented in a Bit Array, numbered from 0-63, from 0 at top left to 63 at bottom right, then, for any given LED 'x', it's neighbours are simply x-9, x-8, x-7, x-1, x+1, X+7, x+8 and x+9. The same routine can then be used on any cell in the array. Some extra code will have to account for display edges otherwise the patterns will 'wrap' out of one side of the screen and re-enter from the other... but then again that then will look like an interesting 'perpetual' colony!

  3. #3
    Join Date
    Feb 2007
    Posts
    55


    Did you find this post helpful? Yes | No

    Default

    Thanks for your reply, this is roughly what i had in mind.

    so far i have come up with this bit of untryed code,
    from the grid below if cell (x) 31 is alive, and you wish to check its neighbours: x-9, x-8, x-7, x-1, x+1, X+7, x+8 and x+9
    you will end up checking 22, 23, 24,30,32,38,39,40
    which almost works for wrapping around except 40 is too far away.

    '0 8 16 24 32 40 48 56
    '1 9 17 25 33 41 49 57
    '2 10 18 26 34 42 50 58
    '3 11 19 27 35 43 51 59
    '4 12 20 28 36 44 52 60
    '5 13 21 29 37 45 53 61
    '6 14 22 30 38 46 54 62
    '7 15 23 31 39 47 55 63

    led var bit [64]
    counter var byte
    population var byte ' <<number of neighbours

    FOR counter = 0 TO 63
    population = 0
    IF led [counter-1] = 1 AND counter > 0 THEN
    population = population + 1
    ENDIF
    IF led [counter+1] = 1 AND counter < 63 THEN
    population = population + 1
    ENDIF
    IF counter > 7 THEN
    IF led [counter-8] = 1 THEN
    population = population + 1
    ENDIF
    IF led [counter-9] = 1 THEN
    population = population + 1
    ENDIF
    IF led [counter-7] = 1 THEN
    population = population + 1
    ENDIF
    ENDIF
    IF counter < 56 THEN
    IF led [counter+8] = 1 THEN
    population = population + 1
    ENDIF
    IF led [counter+9] = 1 THEN
    population = population + 1
    ENDIF
    IF led [counter+7] = 1 THEN
    population = population + 1
    ENDIF
    ENDIF
    IF led [counter] = 1 THEN
    IF population > 3 OR population < 2 THEN
    led [counter] = 0
    ENDIF
    ENDIF
    IF led [counter] = 0 THEN
    IF population = 3 THEN
    led [counter] = 1
    ENDIF
    ENDIF
    NEXT

  4. #4
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Well, 40 could be valid because (like for example the old Atari Asteroids game) you can wrap vertically as well as horizontally... if an Asteroid went off the top, it appeared at the bottom, and likewise when it went off one side, then it appeared on the other.

  5. #5
    Join Date
    Feb 2007
    Posts
    55


    Did you find this post helpful? Yes | No

    Default

    Thanks for your reply

    have come up with this:



    i ended up allowing it to wrap vertically only which does keep the sequence of life going a bit longer..

  6. #6
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    I think you'll need to create 2 arrays.
    One for the current generation, and one for the Next generation.

    The way it is now, you scan across each location and determine whether it's alive or dead and immediately change it's state.

    Then when you check the next location, it's making a decision based on the bit you just changed, which can't be considered until the next generation.

    As you scan 1 generation, place the results in a holding array. Once the entire board has been scanned for a single generation, update the LED's, and use that array as the "current generation", or copy it back to the first array.

    hth,
    DT

  7. #7
    Join Date
    May 2013
    Location
    australia
    Posts
    2,383


    Did you find this post helpful? Yes | No

    Default Re: Conway's Game Of Life

    i had a bit of a "google" for stagnant life pattern detection . the answer was obvious CRC
    i resurrected my old code , added crc16 stored the last 8 crc's in a ring buffer. i then go on to check every new generation's
    crc for existence in the buffer, more than two counts and i call it a repeat and begin a new pattern
    works like a charm.
    Warning I'm not a teacher

  8. #8
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,517


    Did you find this post helpful? Yes | No

    Default Re: Conway's Game Of Life

    The array transform routine, as posted above executes in 4.66ms on my test hardware (18F46K42 @64MHz).
    It's a small improvement but I managed to reduce that to 4.28ms by changing it into two FOR/NEXT loops, one for odd, one for even rows.
    Code:
        FOR Col = 0 To 30 Step 2
            InPtr = (TempW + Col)
            led[InPtr] = lednew[InPtr]
            OutPtr = InPtr
           
            IF led[InPtr] > 0 THEN
               NeoLed(OutPtr) = Led[InPtr]	
               alive = alive + 1
               dead = dead + InPtr
            ELSE
                NeoLED(OutPtr) = 0
            ENDIF
        NEXT
        
        FOR Col = 1 To 31 Step 2    ' Odd rows
            InPtr = (TempW + Col)
            led[InPtr] = lednew[InPtr]
            OutPtr = (TempW) + (31-Col)   'Reversed order
            
            IF led[InPtr] > 0 THEN
               NeoLed(OutPtr) = Led[InPtr]	
               alive = alive + 1
               dead = dead + InPtr
            ELSE
               NeoLED(OutPtr) = 0
            ENDIF
        NEXT
    I must point out though that I don't have the arrays populated with anything so their values are always zero which obviously will have an effect.

  9. #9


    Did you find this post helpful? Yes | No

    Default Re: Conway's Game Of Life

    Could I have a look at your Life CRC16 code please Richard?

  10. #10
    Join Date
    May 2013
    Location
    australia
    Posts
    2,383


    Did you find this post helpful? Yes | No

    Default Re: Conway's Game Of Life

    this is my ccitt16 crc code , i feed every byte in the cell array into it and store result in ring buffer after first testing to see if the new crc exists more than once in the buffer buffer, if its there more than once [or twice if you like] thats it game over.


    Code:
    this is a ccit 16 crc routine
    
    
    crc var word
    crc_in var byte
    j var byte
    
    
    
    
    to use   set crc  to $ffff
    
    
    then set crc_in to each value to be crc'ed
    and gosub crc16
    
    when finished crc hold the crc value
    
    
    don't forget to reset crc to $ffff for next time
    
    
    
    
    
    
    
    
    
    
    
    
    crc16:     
          crc= crc ^  crc_in 
          for j=0 TO 7
             if (crc&1)  THEN
               crc= ((crc>>1) ^ $a001 )   ;
             else
               crc= crc>>1;
             ENDIF  
          NEXT
    RETURN
    Last edited by richard; - 24th May 2020 at 13:10.
    Warning I'm not a teacher

  11. #11


    Did you find this post helpful? Yes | No

    Default 256 Led Matrix running on 12F1840 Pic.

    As video.

    Colour 'Life' on tiny 12F1840.


  12. #12
    Join Date
    May 2013
    Location
    australia
    Posts
    2,383


    Did you find this post helpful? Yes | No

    Default Re: Conway's Game Of Life

    my first venture into youtube

    life on pic18f26k22@64mhz on 32x64 red,green p10 led panel , with word seeds

    Warning I'm not a teacher

  13. #13
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,795


    Did you find this post helpful? Yes | No

    Default Re: Conway's Game Of Life

    Just amazing!

    Although I never fully understood that "game" your work looks impressive, especially on this P10 display in wide format.

    Nice job that video on YouTube also!

    Ioannis

  14. #14


    Did you find this post helpful? Yes | No

    Default Re: Conway's Game Of Life

    Very nice Richard. What LED's are those?

    Did my thread resurrection encourage you.. LOL

    Can we have a look at your 2048 led code? In particular the word seeds....

    Thanks and very cool.

Similar Threads

  1. programming jumping game
    By bokken in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 17th February 2012, 07:40
  2. Simon Game
    By flotulopex in forum Code Examples
    Replies: 1
    Last Post: - 4th November 2010, 07:25
  3. EEPROM life expectancy?
    By muddy0409 in forum General
    Replies: 3
    Last Post: - 1st May 2007, 13:44
  4. home brew game console W interface
    By PICMAN in forum General
    Replies: 1
    Last Post: - 15th March 2005, 23:25
  5. Game port to USB adaptor
    By Squibcakes in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 24th September 2004, 04:16

Members who have read this thread : 3

You do not have permission to view the list of names.

Posting Permissions

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