GIDForums  

Go Back   GIDForums > Computer Programming Forums > Miscellaneous Programming Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Apr-2007, 03:15
-XXX- -XXX- is offline
New Member
 
Join Date: Feb 2007
Posts: 24
-XXX- is on a distinguished road

Quick Basic and Game of Life


I'm pretty new to programming but I think that i could program the Game of Life, in it's simplest way. I've already programmed 1 dimensional cellular automata in quick basic, and now I would like to try to program conway's game of life. I think that it'll be best to start with a finite space.

I know that I should do it with a pair of two-dimensional arrays, one for the old generation and one for the new, and then set the newgen array to be the old one, and set the new gen to 0.

But I have problems with the loop and algorythm which should compare a cell with its neighbours in order to predict the new gen...

Can somebody help, please?

The best way would be if somebody could write the algorythm in pseudocode...

thank you very much...
-XXX-
  #2  
Old 03-Apr-2007, 08:41
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: quick basic and Game of life


Quote:
Originally Posted by -XXX-
pseudocode...

Code:
Given an array of integers. For row = r and column = c, If array(r, c) is equal to zero, we say that position is "dead" If array(r, c) is equal to one, we say that position is "alive" It is assumed that a function is available to count the number of "live" neighbors of any position, as follows In general, the function will investigate the eight cells in the range defined by (r-1 <= row <= r+1), (c-1 <= col <= c+1), row not equal to col The return value is the count of the number of live cells in this range. Assuming a finite grid, special care must be taken at the borders, to make sure you don't go out of the grid. Anyhow, with that function correctly implemented, each move is calculated as follows: Copy the existing array into a new array: next_array Here's the move: FOR r in the range of rows BEGIN LOOP FOR c in the range of columns BEGIN LOOP count = number of live neighbors of array(r, c) IF (array(r, c) is equal to zero) <== it's now dead THEN IF (count is equal to 3) THEN set next_array(r, c) to one <== it comes to life END IF ELSE <== it's now alive IF (count is less than 2) or (count is greater than 3) THEN set next_array(r, c) to zero <== it dies END IF END IF END LOOP <== columns END LOOP <== rows Then copy next_array into array to show the results of the move and to get ready for the next move

Regards,

Dave
  #3  
Old 04-Apr-2007, 01:13
-XXX- -XXX- is offline
New Member
 
Join Date: Feb 2007
Posts: 24
-XXX- is on a distinguished road

Re: quick basic and Game of life


Yes, thank you very much, it was very helpful.
  #4  
Old 04-Apr-2007, 08:32
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: quick basic and Game of life


Quote:
Originally Posted by davekw7x
Code:
It is assumed that a function is available to count the number of "live" neighbors of any position, as follows . . . In general, the function will investigate the eight cells in the range defined by (r-1 <= row <= r+1), (c-1 <= col <= c+1), row not equal to col . . .


I didn't define this correctly. The condition is that we examine the cells in the range defined by
Code:
(r-1 <= row <= r+1), (c-1 <= col <= c+1), (row,col) not equal to (r,c)

In other words don't include the cell array(r,c) in the count (we are counting the neighbors of array(r,c)):

(row not equal to r) OR (col not equal to c)

More precisely:

Code:
Here is a function to count the number of live neighbors of the element array(r, c), assuming the index values go from 0 through R-1 and 0 through C-1, respectively: BEGIN Integer FUNCTION CountLiveNeighbors(array, r, c) Declare a local integer variable "count", and set "count" to zero. Declare two local integer variables: "rr", and "cc" First Loop: let rr go from -1 to 1 in steps of 1 BEGIN LOOP1 Second loop: let cc go from -1 to 1 in steps of 1 BEGIN LOOP2 IF (((rr is not equal to zero) OR (cc is not equal to zero)) <-- self AND (r + rr) is greater than or equal to zero <-- check low row AND (c + cc) is greater than or equal to zero <-- check low col AND (r + rr) is less than R <-- check high row AND (c + cc) is less than C <-- check high col ) THEN SET count = count + array(r+rr, c+cc); <-- increment count if alive END IF END LOOP2 END LOOP1 RETURN count END FUNCTION CountLiveNeighbors

Regards,

Dave
  #5  
Old 05-Apr-2007, 01:11
-XXX- -XXX- is offline
New Member
 
Join Date: Feb 2007
Posts: 24
-XXX- is on a distinguished road

Re: quick basic and Game of life


Firstly here is my code, in QuickBasic.

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


Comments are preceeded by a " ' " and are in blue.

PSET(y,x),col sets a pixel in the given color col.

The problem is that it doesn't do what a normal glider should do but draw lines from one side of the frame to another...

Help, please?

-XXX-
  #6  
Old 05-Apr-2007, 09:14
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: quick basic and Game of life


Quote:
Originally Posted by -XXX-
Firstly here is my code, in QuickBasic.

PSET(y,x),col sets a pixel in the given color col.


The problem is that it doesn't do what a normal glider should do but draw lines from one side of the frame to another...

No way am I going to figure out the QBasic (I'm not even going to look at it), but: Shouldn't you erase the old picture before drawing the new picture at each generation? (PSET the old array points to the background color or some such thing?)

Regards,

Dave
Last edited by davekw7x : 05-Apr-2007 at 10:28.
  #7  
Old 05-Apr-2007, 10:59
WaltP's Avatar
WaltP WaltP is online now
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: quick basic and Game of life


Your code looks OK, as far as I checked it. The problem is your concept of the arrays.

Rather than dealing with pixels, use characters. Define oldgen and newgen to be the size of the screen and just output "*" for each 1 and SPACE for each 0. This will help you get the concept down.

Once that's done, then you can try the pixel thing. And Dave is correct. You have to erase the old screen when using the pixels.
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #8  
Old 05-Apr-2007, 13:07
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: quick basic and Game of life


Quote:
Originally Posted by davekw7x
No way am I going to figure out the QBasic

Well, I couldn't resist a little peek (An old BASIC pun. Get it? PEEK? No? Oh, well...)

What's up with this?

Code:
DO WHILE true 'infinite outer loop repeating the whole algorithm counter = 0 'initializing counter <--- Huh? FOR i = 1 TO (co2 - co1) FOR j = 1 TO (co2 - co1) . . . 'increment counter for each live neighbor of oldgen(i, j) 'update newgen(i,j), depending on value of counter . . . NEXT 'j NEXT 'i . . .
For each (i, j), you use "counter" to indicate the number of live neighbors. But you initialize counter to zero at the beginning of each pass through the array. Shouldn't you initialize it for each point?

Code:
DO WHILE true 'infinite outer loop repeating the whole algorithm FOR i = 1 TO (co2 - co1) FOR j = 1 TO (co2 - co1) counter = 0 'initializing counter . . . 'increment counter for each live neighbor of oldgen(i, j) 'update newgen(i,j), depending on value of counter . . . NEXT 'j NEXT 'i . . .

Regards,

Dave
  #9  
Old 05-Apr-2007, 13:51
-XXX- -XXX- is offline
New Member
 
Join Date: Feb 2007
Posts: 24
-XXX- is on a distinguished road

Re: quick basic and Game of life


s**t am I stupid!!

Thank You very much, really !!

works as it should now....

thx,
-XXX-
  #10  
Old 05-Apr-2007, 14:16
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: quick basic and Game of life


Quote:
Originally Posted by -XXX-
works as it should now....
I am always amazed at the apparent complexity arriving from a (deterministic) finite automaton with such a simple algorithm. Every time I go back and look at it, I am amazed all over again.

Having a program of your very own to use to investigate the possibilities is waaaay better than just running someone else's java (or whatever...) on some web site somewhere (but there are lots of good ideas out there).

Regards,

Dave
 
 

Recent GIDBlogProblems with the Navy (Enlisted) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Computer Game Design HELP NEEDED! dcallito C++ Forum 3 01-Jun-2007 12:29
HELP!!!!HELP!!!! Design a game call “Games of Guessing” HELP!!!! tianurn C++ Forum 1 26-Mar-2007 15:38
Help on Basic Design C++ maliquenavidad C++ Forum 4 28-Nov-2005 21:24

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:59.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.