Conway's Game Of Life


Closed Thread
Results 1 to 40 of 46

Hybrid View

  1. #1
    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!

  2. #2
    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

  3. #3
    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.

  4. #4
    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..

  5. #5
    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

  6. #6
    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 21:44.

  7. #7
    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

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 : 1

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