vendredi 29 mai 2015

Debugging Conway's Game of Life Graphics?

I am trying to make a simple version of Conway's Game of Life where the computer generates a grid of rectangles and fills in the rectangles that represent "live" cells. The problem I am having is that I cannot get the grid to clear after the first pattern, so all the patterns are generated on the same grid and it looks like a big blob of colored rectangles.

Here is my 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 the pattern for Conway's Game of Life by manipulating the 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 the 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 help is much appreciated!

Aucun commentaire:

Enregistrer un commentaire