Conway's Game Of Life


Closed Thread
Results 1 to 40 of 46

Hybrid View

  1. #1
    Join Date
    Feb 2007
    Posts
    55


    Did you find this post helpful? Yes | No

    Default

    here is the code for it, using 2 arrays for the led data.

    Code:
    ' 8X8 LED MATRIX -- GAME OF LIFE --
    ' PIC16F872 External 4MHz Xtal Osc
    ' LED Matrix columns are conected to PORTC via ULN2004AN to 0v.
    ' RC0 - 1st column on left of display
    ' LED Matrix rows conected to PORTB via 100 ohm resistors (Resistor value dependant on LEDs used)
    ' RB0 - top row
    ' MCLR (pin1) connected via 4K7 to +5v supply.
    ' Push buttons conected to PORTA 3 and 4 with pull up resistors to +5v rail.
    ' By: DJW 20 June 2009
    ' Compiled with PicBasicPro
    '************************************************************************************************
    
    ADCON1 = %00000110	'all digital
    
    TRISA = %00011000 :TRISB = %00000000 :TRISC = %00000000
    
    SYMBOL pushA	=	PORTA.3		'change pattern
    SYMBOL pushB	=	PORTA.4		'start/stop game
    
    leddata var byte [8] :led var bit [64] :lednew var bit [64]
    population var byte :topedge var bit :botedge var bit :lefedge var bit :rigedge var	bit 
    alive var byte :temp var word :ledA var byte :ledB var byte :ledD var byte :ledC var byte
    ledE var byte :ledF var byte :ledG var byte :ledH var byte :counter var byte :scan var byte
    
    start:
    	RANDOM temp
    	leddata [0] = temp.lowbyte
    	leddata [1] = temp.highbyte
    	RANDOM temp
    	leddata [2] = temp.lowbyte
    	leddata [3] = temp.highbyte
    	RANDOM temp
    	leddata [4] = temp.lowbyte
    	leddata [5] = temp.highbyte
    	RANDOM temp
    	leddata [6] = temp.lowbyte
    	leddata [7] = temp.highbyte
    loop:	
    
    	FOR temp = 0 TO 1500
    		IF pushA = 0 THEN
    			PAUSE 200
    			GOTO START
    		ENDIF
    		IF pushB = 0 THEN 
    			PAUSE 200
    			GOTO game
    		ENDIF
    		GOSUB display
    	NEXT
    	GOTO game
    
    '-------------------------- DISPLAY LOOP ROUTINE -------------------------------------------------
    display:
    			PORTC = 1
    			FOR counter = 0 TO 7
    				PORTB = leddata [counter]
    				PAUSE 1
    				PORTB = 0		
    				PORTC = PORTC * 2
    			NEXT
    			RETURN
    
    '------------------------ Read Cell data from display and store data bits in led array --------------------
    GAME:
    	ledA = leddata[0] : ledB = leddata[1] : ledC = leddata[2] : ledD = leddata[3]
    	ledE = leddata[4] : ledF = leddata[5] : ledG = leddata[6] : ledH = leddata[7]
    	led [0] = ledA.0 :led [1] = ledA.1 :led [2] = ledA.2 :led [3] = ledA.3 :led [4] = ledA.4 
    	led [5] = ledA.5 :led [6] = ledA.6 :led [7] = ledA.7 :led [8] = ledB.0 :led [9] = ledB.1 
    	led [10] = ledB.2 :led [11] = ledB.3 :led [12] = ledB.4 :led [13] = ledB.5 :led [14] = ledB.6 
    	led [15] = ledB.7 :led [16] = ledC.0 :led [17] = ledC.1 :led [18] = ledC.2 :led [19] = ledC.3 
    	led [20] = ledC.4 :led [21] = ledC.5 :led [22] = ledC.6 :led [23] = ledC.7 :led [24] = ledD.0
    	led [25] = ledD.1 :led [26] = ledD.2 :led [27] = ledD.3 :led [28] = ledD.4 :led [29] = ledD.5 
    	led [30] = ledD.6 :led [31] = ledD.7 :led [32] = ledE.0 :led [33] = ledE.1 :led [34] = ledE.2 
    	led [35] = ledE.3 :led [36] = ledE.4 :led [37] = ledE.5 :led [38] = ledE.6 :led [39] = ledE.7 
    	led [40] = ledF.0 :led [41] = ledF.1 :led [42] = ledF.2 :led [43] = ledF.3 :led [44] = ledF.4 
    	led [45] = ledF.5 :led [46] = ledF.6 :led [47] = ledF.7 :led [48] = ledG.0 :led [49] = ledG.1 
    	led [50] = ledG.2 :led [51] = ledG.3 :led [52] = ledG.4 :led [53] = ledG.5 :led [54] = ledG.6
    	led [55] = ledG.7 :led [56] = ledH.0 :led [57] = ledH.1 :led [58] = ledH.2 :led [59] = ledH.3 
    	led [60] = ledH.4 :led [61] = ledH.5 :led [62] = ledH.6 :led [63] = ledH.7 
    	temp = 0
    '------------------------- Resets after 500 evolutions ---------------------------------------------------
    gamestart:
    	temp = temp + 1	
    	IF temp > 500 THEN
    		GOTO start
    	ENDIF
    '------------------------ Checking each cells neighbours, counting neighbours in population ---------------
    '----------------------- Also creating a newled data array to be displayed on next generation -------------
    	alive = 0		
    FOR counter = 0 TO 63
    	population = 0 :topedge	= 0 :botedge = 0 :lefedge = 0 :rigedge = 0
    	IF counter < 8 THEN
    		lefedge = 1
    	ENDIF
    	IF counter > 55 THEN
    		rigedge = 1
    	ENDIF
    	IF counter = 0 OR counter = 8 OR counter = 16 OR counter = 24 OR counter = 32 OR counter = 40 OR counter = 48 OR counter = 56 THEN
    		topedge = 1
    	ENDIF
    	IF counter = 7 OR counter = 15 OR counter = 23 OR counter = 31 OR counter = 39 OR counter = 47 OR counter = 55 OR counter = 63 THEN
    		botedge = 1
    	ENDIF
    	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 lefedge = 0 THEN
    		IF led [counter-8] = 1 THEN
    			population = population + 1
    		ENDIF
    		IF led [counter-9] = 1 AND topedge = 0 THEN
    			population = population + 1
    		ENDIF
    		IF led [counter-7] = 1 THEN
    			population = population + 1
    		ENDIF
    	ENDIF
    	IF rigedge = 0 THEN
    		IF led [counter+8] = 1 THEN
    			population = population + 1
    		ENDIF
    		IF led [counter+9] = 1 AND botedge = 0 THEN
    			population = population + 1
    		ENDIF
    		IF led [counter+7] = 1 THEN
    			population = population + 1
    		ENDIF
    	ENDIF
    	lednew [counter] = led [counter]
    	IF led [counter] = 1 THEN
    		IF population > 3 OR population < 2 THEN
    			lednew [counter] = 0
    		ENDIF
    	ENDIF
    	IF led [counter] = 0 THEN
    		IF population = 3 THEN
    			lednew [counter] = 1
    		ENDIF
    	ENDIF
    NEXT
    '-------------------------- Reloads led array with new generation data -----------------------------------
    FOR counter = 0 TO 63
    	led [counter] = lednew [counter]
    	IF led [counter] = 1 THEN
    		alive = alive + 1
    	ENDIF
    NEXT
    '------------------------------ IF display blank, all cells dead then reset ------------------------------
    IF alive = 0 THEN
    	GOTO start
    ENDIF
    '------------------------------ Covert led array bits into display column bytes -------------------------
    '---------------------------------------- and send data to display --------------------------------------
    ledA.0 = led [0] :ledA.1 = led [1] :ledA.2 = led [2] :ledA.3 = led [3] :ledA.4 = led [4] :ledA.5 = led [5]
    ledA.6 = led [6] :ledA.7 = led [7] :ledB.0 = led [8] :ledB.1 = led [9] :ledB.2 = led [10] :ledB.3 = led [11]
    ledB.4 = led [12] :ledB.5 = led [13] :ledB.6 = led [14] :ledB.7 = led [15] :ledC.0 = led [16]
    ledC.1 = led [17] :ledC.2 = led [18] :ledC.3 = led [19] :ledC.4 = led [20] :ledC.5 = led [21]
    ledC.6 = led [22] :ledC.7 = led [23] :ledD.0 = led [24] :ledD.1 = led [25] :ledD.2 = led [26]
    ledD.3 = led [27] :ledD.4 = led [28] :ledD.5 = led [29] :ledD.6 = led [30] :ledD.7 = led [31]
    ledE.0 = led [32] :ledE.1 = led [33] :ledE.2 = led [34] :ledE.3 = led [35] :ledE.4 = led [36]
    ledE.5 = led [37] :ledE.6 = led [38] :ledE.7 = led [39] :ledF.0 = led [40] :ledF.1 = led [41]
    ledF.2 = led [42] :ledF.3 = led [43] :ledF.4 = led [44] :ledF.5 = led [45] :ledF.6 = led [46]
    ledF.7 = led [47] :ledG.0 = led [48] :ledG.1 = led [49] :ledG.2 = led [50] :ledG.3 = led [51]
    ledG.4 = led [52] :ledG.5 = led [53] :ledG.6 = led [54] :ledG.7 = led [55] :ledH.0 = led [56]
    ledH.1 = led [57] :ledH.2 = led [58] :ledH.3 = led [59] :ledH.4 = led [60] :ledH.5 = led [61]
    ledH.6 = led [62] :ledH.7 = led [63]
    leddata [0] = ledA :leddata [1] = ledB :leddata [2] = ledC :leddata [3] = ledD
    leddata [4] = ledE :leddata [5] = ledF :leddata [6] = ledG :leddata [7] = ledH
    		FOR scan = 0 TO 20
    			IF pushB = 0 THEN
    				PAUSE 200
    				GOTO start
    			ENDIF
    			GOSUB display
    		NEXT 
    GOTO gamestart	
    END
    Last edited by wellyboot; - 21st June 2009 at 20:44.

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


    Did you find this post helpful? Yes | No

    Default

    That was fast.

    Did it make a significant difference?
    With it wrapping at the edges, I would imagine things stay "alive" much longer.
    <br>
    DT

  3. #3
    Join Date
    Feb 2007
    Posts
    55


    Did you find this post helpful? Yes | No

    Default

    maybe too fast, ive noticed a mistake!

    i didnt try it without the wrap around effect, as i do believe it would die a lot quicker.

    but i did for example stop checking cell 40 as a neighbour to cell 31 as it over a column away from each other.

    so for cell 31, i only check 30,22,23,38,39 and 24,32 (not cell+9 = 40)

    and here is the mistake: I should check cell 16 (cell - 15)

    '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

    but checking cell 40 wouldnt be such a bad thing as it would be a slightly diagonal wrapping around effect.


  4. #4


    Did you find this post helpful? Yes | No

    Default Re: Conway's Game Of Life NeoPixel

    Thread resurrection. Code help please.

    I'm working on this using a sequential 256 led neopixel string which is laid out in a prebuilt 16x16 matrix panel.

    The neopixel string works fine but is like a snakes and ladder board in that starting from bottom left corner you go along the bottom up at the end of the row then back along then up a row and along etc etc. (Back and forth)

    This arrangement is of course different to the actual LIFE array in PIC memory which is arranged as per the diagram.

    So I want to transfer data from the Life byte array into the NeoPixel byte array adjusting the NeoPixel(address) when necessary.

    The diagram might make it easier to see what i mean..

    Code:
    'Life Array 16x16 in memory.     'Life Array 16x16 Neopixel Matrix String.  
    
    '         Top                                  Top
    '240 -------------- 255             '255 -------------- 240
    '224 -------------- 239             '224 -------------- 239
    '208 -------------- 223             '223 -------------- 208
    '192 -------------- 207             '192 -------------- 207
    '176 -------------- 191             '191 -------------- 176
    '160 --x----------- 175             '160 --x----------- 175
    '144 ---x---------- 159             '159 ----------x--- 144
    '128 -xxx---------- 143             '128 -xxx---------- 143
    '112 -------------- 127             '127 -------------- 112
    '96  -------------- 111              '96  -------------- 111
    '80  -------------- 95               '95  -------------- 80
    '64  -------------- 79               '64  -------------- 79
    '48  -------------- 63               '63  -------------- 48
    '32  -------------- 47               '32  -------------- 47
    '16  -------------- 31               '31  -------------- 16
    '0   -------------- 15                '0   -------------- 15   
    '        Bottom                              Bottom
    Hopefully you see the problem, I can't see an efficient way to transfer the data from the LIFE to the NEOPIXEL array whilst correcting the address on the alternate lines..

    A simple Pixel(A) = Life(A) works for 0-15, 32-47 etc etc but not for 16-31, or 48-63 etc etc as the data ends up on the wrong part of the screen..

    Any brilliant ideas.

    You can see the problem on 144-159 with test data, it ends up on the wrong part of the screen.
    Last edited by retepsnikrep; - 15th May 2020 at 10:41.

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: Conway's Game Of Life

    Here's an idea, untested, don't know if it compiles.
    Code:
    InPtr VAR BYTE
    OutPtr VAR BYTE
    
    Row VAR BYTE
    Col VAR BYTE
    
    Even VAR BIT
    
    
    Translate:
      InPtr = 0
    
      FOR Row = 0 to 15			' Cycle thru 16 rows
         FOR Col = 0 to 15			' of 16 pixels each
           IF Row.0 = 0 THEN		' If the row number is even     
             OutPtr = (Row*16) + Col        ' Output pixels in true order       
           ELSE
             OutPtr = (Row*16) + (15-Col)   ' Output pixels in reversed order        
           ENDIF
           NeoArray[OutPtr] = LifeArray[InPtr]
         NEXT
      NEXT
      InPtr = InPtr + 1
    RETURN

  6. #6


    Did you find this post helpful? Yes | No

    Default Re: Conway's Game Of Life

    Thanks Henrik that was just the kick start I needed.

    Now to get the rest sorted..

    Last edited by retepsnikrep; - 15th May 2020 at 13:06.

  7. #7


    Did you find this post helpful? Yes | No

    Default Re: Conway's Game Of Life

    Still working on this.

    Thanks to help with example code on this and other NeoPixel threads I now have a 1024 led.

    Driving 1024 leds with (PIC 18F26K80) at just over 3hz

    Need to work on optimizing the code, adding colour and generally enhancing the experience.

    The four 8x32 leds strip are connected in series and fixed on the backboard of this nice neat white deep photo frame using double sided adhesive sheet.

    The LEDS are a perfect fit in this $10 ebay frame so highly recommended for similar projects.

    White Shadow Box Photo Frame with Deep Mount 32 X 32cm


Similar Threads

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

Members who have read this thread : 2

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