View unanswered posts | View active topics It is currently Wed May 23, 2018 2:41 am

 Page 1 of 1 [ 2 posts ]
 Print view Previous topic | Next topic
the game of life
Author Message
Novice

Joined: Sat Apr 12, 2008 11:09 am
Posts: 60
Location: holland
the game of life
Hello

i have created some code playing the game of life. But the game dies out pretty fast. Anyone ideas what is wrong?
 Code:/*Game of life  Rules:   Life is played on a grid of square cells--like a chess board but extending   infinitely in every direction. A cell can be live or dead. A live cell is shown   by putting a marker on its square. A dead cell is shown by leaving the square empty.   Each cell in the grid has a neighborhood consisting of the eight cells in every   direction including diagonals.   To apply one step of the rules, we count the number of live neighbors for each cell.   What happens next depends on this number.   -A dead cell with exactly three live neighbors becomes a live cell (birth).   -A live cell with two or three live neighbors stays alive (survival).   -In all other cases, a cell dies or remains dead (overcrowding or loneliness).  act:    cells are shown with rectrangles of 2x2 and one empty pixel giving 33 by 21 cells    on the screen.    2 arrays are used. 1 with last life/death cells and 1 with new life/deth cells.    based on the values of 1 array, the other array is filled and the cells are    shown/deleted if neccesarry */int time=100;//to slow it downint x=0,y=0;int count=0;//count the number of life cells aroundbool array1[32][20];bool array2[32][20];task main(){  for(x=0;x<32;x++)  // fill array 1 with random cells and print them on the screen  {     for(y=0;y<20;y++)     {        if(random(1)==1)        {          array1[x][y]=true;          nxtDrawRect(x*3, y*3, x*3+1, y*3+1);        }       else          array1[x][y]=false;     }  }   while (true)// start the game   {      wait1Msec(time);      for(y=0;y<20;y++) //array1 is on the screen. calculate array 2 and put it on the screen     {        for(x=0;x<32;x++)        {        //   wait10Msec(10);           count=0;           if(x>0&&y<20&&array1[x-1][y+1])count++;//check for life neighbour cells           if(x>0&&array1[x-1][y])count++;           if(x>0&&y>0&&array1[x-1][y-1])count++;           if(y<20&&array1[x][y+1])count++;           if(y>0&&array1[x][y-1])count++;           if(x<32&&y<20&&array1[x+1][y+1])count++;           if(x<32&&array1[x+1][y])count++;           if(x<32&&y>0&&array1[x+1][y-1])count++;           if(count==3)// cell becomes alive           {             array2[x][y]=true;              nxtDrawRect(x*3, y*3, x*3+1, y*3+1);           }           else if(count==2)// a live cell stays alive, a deth cell             array2[x][y]=array1[x][y];           else // a live sell dies           {              array2[x][y]=false;             nxtEraseRect(x*3, y*3, x*3+1, y*3+1);           }      }    }    wait1Msec(time);    for(y=0;y<20;y++) //array2 is on the screen. calculate array 1 and put it on the screen    {        for(x=0;x<32;x++)        {        //wait10Msec(10);           count=0;           if(x>0&&y<20&&array2[x-1][y+1])count++;//check for life neighbour cells           if(x>0&&array2[x-1][y])count++;           if(x>0&&y>0&&array2[x-1][y-1])count++;           if(y<20&&array2[x][y+1])count++;           if(y>0&&array2[x][y-1])count++;           if(x<32&&y<20&&array2[x+1][y+1])count++;           if(x<32&&array2[x+1][y])count++;           if(x<32&&y>0&&array2[x+1][y-1])count++;           if(count==3)// cell becomes alive           {             array1[x][y]=true;              nxtDrawRect(x*3, y*3, x*3+1, y*3+1);           }           else if(count==2)// a live cell stays alive            array1[x][y]=array2[x][y];           else // a live cell dies           {              array1[x][y]=false;             nxtEraseRect(x*3, y*3, x*3+1, y*3+1);          }        }     }   }}

Mon Sep 08, 2008 3:14 pm
Expert

Joined: Sun Sep 09, 2007 10:12 am
Posts: 116
Re: the game of life
on:

for(y=0;y<20;y++)
for(x=0;x<32;x++)

you should use y<19 and x<31, cause of overflow...

_________________
http://www.apcsguarda.com
My Project: http://www.robotc.net/forums/viewtopic.php?f=15&t=712

Tue Sep 23, 2008 5:23 pm
Display posts from previous:  Sort by
 Page 1 of 1 [ 2 posts ]

#### Who is online

Users browsing this forum: No registered users and 2 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ ROBOTC Applications    ROBOTC for LEGO MINDSTORMS       Third-party sensors    ROBOTC for CORTEX & PIC    ROBOTC for VEX IQ    ROBOTC for Arduino    Robot Virtual Worlds    Multi-Robot Communications    Issues and Bugs Competitions & Partners    Mini Urban Challenge    CS2N Robot Virtual Worlds Competitions       VEX Skyrise Competition 2014-2015       VEX Toss Up 2013-2014       FTC Block Party! 2013-2014    Competitions using VEX - BEST, TSA, VEX, and RoboFest!    FTC Programming    RoboCup Junior and Other ROBOT Competitions Virtual Brick Robotics Discussions    General Discussions    Project Discussions Off-Topic ROBOTC Forum & ROBOTC.net Suggestions/Feedback    ROBOTC Forums Suggestions/Comments    ROBOTC.net Suggestions/Comments       NXT Programming: Tips for Beginning with ROBOTC       VEX Programming: Tips for Beginning with ROBOTC    2013 Robotics Summer Of Learning       VEX Toss Up Programming Challenge       FTC Ring It Up! Programming Challenge    International Forums       Spanish Forums          ROBOTC for MINDSTORMS          ROBOTC for VEX       French Forums          ROBOTC pour Mindstorms          ROBOTC pour IFI VEX       Japanese Forums （日本語のフォーラム）       German Forums    2015 Spring Carnival Event    PLTW (Project Lead The Way)    Robotics Merit Badge    2014 Robotics Academy Summer of Learning

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.