java - Debugging Conway's Game of Life Graphics? -


i trying make simple version of conway's game of life computer generates grid of rectangles , fills in rectangles represent "live" cells. problem having cannot grid clear after first pattern, patterns generated on same grid , looks big blob of colored rectangles.

here code:

public class gameoflife {     static jpanel panel;     static jframe frame;     public static void main(string[] args) throws interruptedexception{         int [][] array = new int [17][17];         /*          * set pattern conway's game of life manipulating array below.          */         array[2][4]=1;         array[2][5]=1;         array[2][6]=1;         panel = new jpanel();         dimension dim = new dimension(400,400);                 panel.setpreferredsize(dim);         frame = new jframe();         frame.setsize(1000, 500);         container contentpane =    frame.getcontentpane();         contentpane.add(panel);         frame.setvisible(true);         /*         * runs game of life simulation "a" number of times.         */         int [][] end = new int [array.length][array[0].length];         int a=0;         while(a<4){             for(int i=1; i<=array.length-2; i++)             {                 for(int j=1; j<=array[0].length-2; j++)                 {                     int counter = surround(array,i,j);                     if(array[i][j]==1 && counter<=2)                     {                         end[i][j]=0;                     }                     if(array[i][j]==1 && counter==3)                     {                         end[i][j]=1;                     }                     if(array[i][j]==1 && counter>4)                     {                         end[i][j]=0;                     }                     if(array[i][j]==0 && counter==3)                     {                         end[i][j]=1;                     }                     if(array[i][j]==1 && counter==4)                     {                         end[i][j]=1;                     }                 }             }             graphics g = panel.getgraphics();             graphics(array,g);             a++;             for(int i=0; i<array.length; i++)             {                 for(int j=0; j<array[0].length; j++)                 {                     array[i][j]=end[i][j];                     end[i][j]=0;                 }             }             thread.sleep(1000);             g.dispose();         }     }          public static int surround(int[][] initial, int i, int j){         int[][] surrounding = {{initial[i-1][j-1],initial[i-1][j],initial[i-1][j+1]},                 {initial[i][j-1],initial[i][j],initial[i][j+1]},                 {initial[i+1][j-1],initial[i+1][j],initial[i+1][j+1]}};         int counter = 0;         for(int a=0; a<=2; a++)         {             for(int b=0; b<=2; b++)             {                 if(surrounding[a][b]==1)                 {                     counter ++;                 }             }         }         return counter;     }          public static void graphics(int[][] array, graphics g)     {         int box_dim=10;         for(int i=0; i<array.length; i++)         {             for(int j=0; j<array[0].length; j++)             {                     g.drawrect(i*box_dim, j*box_dim, 10,10);                     g.setcolor(color.black);                     if(array[i][j]==1)                     {                         g.fillrect(i*box_dim, j*box_dim, 10, 10);                        }             }         }      } } 

any appreciated!

you need draw rectangles both "alive" , "dead" cells color them differently. live cells black , dead cells white if don't redraw every cell during every iteration you'll run issue you've described. being said...you seem have answered own question.


Comments

Popular posts from this blog

methods - python can't use function in submodule -

Java 3D LWJGL collision -

c# - ErrorThe type or namespace name 'AxWMPLib' could not be found (are you missing a using directive or an assembly reference?) -