5 Replies - 1131 Views - Last Post: 09 September 2013 - 05:07 PM Rate Topic: -----

#1 extremeblueness   User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 188
  • Joined: 22-October 12

Game of Life and Cellular State Automaton

Posted 08 September 2013 - 06:47 PM

I'm creating a 100x100 Game of Life cellular automata in Java. One problem - it produces a long string of errors, and fails to get past generation 0:
Attached Image
Attached Image
The code works for a ten by ten where the starting cells are independent of input, but it doesn't work now. Here's my code for all three classes:

public class Main
{
    public static void main(String[] args)
    {
        Game g = new Game();
        g.init();
        g.run();
    }
}


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Game implements ActionListener
{
    JFrame f;
    JFrame g;
    Location[][] l;
    JPanel c;
    JTextField x;
    JTextField y;
    JButton change;
    JButton objPlay;
    public void init()
    {
        f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        g = new JFrame();
        g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(new GridLayout(5,1));
        l = new Location[100][100];
        c = new JPanel(new GridLayout(100, 100));
        for(int i = 0; i < 100; i++)
        {
            for(int j = 0; j < 100; j++)
            {
                l[i][j] = new Location();
                c.add(l[i][j]);
            }
        }
        x = new JTextField(3);
        y = new JTextField(3);
        change = new JButton("Change");
        objPlay = new JButton("Play");
        g.setSize(400, 400);
        g.add(c);
        f.add(x);
        f.add(y);
        f.add(change);
        f.add(objPlay);
        change.addActionListener(this);
        objPlay.addActionListener(this);
    }
    
    public void actionPerformed(ActionEvent e)
    {
        if("Change".equals(e.getActionCommand()))
        {
            Integer i = Integer.parseInt(x.getText());
            Integer j = Integer.parseInt(y.getText());
            change(i, j);
        }
        else if ("Play".equals(e.getActionCommand()))
        {
            change.removeActionListener(this);
            objPlay.removeActionListener(this);
            play();
        }
    }
    
    public void run()
    {
        f.setVisible(true);
    }
    
    public void change(Integer i, Integer j)
    {
        if(l[i][j].alive)
        {
            l[i][j].die();
        }
        else
        {
            l[i][j].live();
        }
        c.repaint();
    }
    
    public void play()
    {
        g.setVisible(true);
        for(int i = 0; i < 10; i++)
        {
            check();
            try
            {
                f.setVisible(true);
                Thread.sleep(500);
            }
            catch(Exception e){}
            c.repaint();
            g.repaint();
        }
    }
    
    public void check()
    {
        for(int i = 0; i < 100; i++)
        {
            for(int j = 0; j < 100; j++)
            {
                l[i][j].checkingAlive = l[i][j].alive;
            }
        }
        for(int i = 0; i < 100; i++)
        {
            for(int j = 0; j < 100; j++)
            {
                byte surroundingLifes = 0;
                try
                {
                    if(l[i-1][j-1].checkingAlive)
                        surroundingLifes++;
                } catch(ArrayIndexOutOfBoundsException e) {}
                try
                {
                    if(l[i-1][j].checkingAlive)
                        surroundingLifes++;
                } catch(ArrayIndexOutOfBoundsException e) {}
                try
                {
                    if(l[i-1][j+1].checkingAlive)
                        surroundingLifes++;
                } catch(ArrayIndexOutOfBoundsException e) {}
                try
                {
                    if(l[i][j-1].checkingAlive)
                        surroundingLifes++;
                } catch(ArrayIndexOutOfBoundsException e) {}
                try
                {
                    if(l[i][j+1].checkingAlive)
                        surroundingLifes++;
                } catch(ArrayIndexOutOfBoundsException e) {}
                try
                {
                    if(l[i+1][j-1].checkingAlive)
                        surroundingLifes++;
                } catch(ArrayIndexOutOfBoundsException e) {}
                try
                {
                    if(l[i+1][j].checkingAlive)
                        surroundingLifes++;
                } catch(ArrayIndexOutOfBoundsException e) {}
                try
                {
                    if(l[i+1][j+1].checkingAlive)
                        surroundingLifes++;
                } catch(ArrayIndexOutOfBoundsException e) {}
                if(surroundingLifes < 2)
                {
                    l[i][j].die();
                }
                else if(surroundingLifes == 3)
                {
                    l[i][j].live();
                }
                else if(surroundingLifes > 3)
                {
                    l[i][j].die();
                }
            }
        }
    }
}


import javax.swing.JPanel;
import java.awt.Color;

public class Location extends JPanel
{
    boolean alive;
    boolean checkingAlive;
    
    public Location()
    {
        checkingAlive = false;
        alive = false;
        this.setBackground(Color.BLACK);
    }
    
    public void live()
    {
        alive = true;
        this.setBackground(Color.GREEN);
    }
    
    public void die()
    {
        alive = false;
        this.setBackground(Color.BLACK);
    }
}

This post has been edited by macosxnerd101: 08 September 2013 - 06:56 PM
Reason for edit:: Renamed title to be more descriptive


Is This A Good Question/Topic? 0
  • +

Replies To: Game of Life and Cellular State Automaton

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Game of Life and Cellular State Automaton

Posted 08 September 2013 - 06:53 PM

It looks like the problem comes from a sort() call. Please post the relevant code and the actual portion of the stack trace that will allow us to trace through your errors.

Also, I'm renaming your title to be more descriptive. Please don't use titles like "Help me" and "Errors."
Was This Post Helpful? 1
  • +
  • -

#3 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Game of Life and Cellular State Automaton

Posted 08 September 2013 - 07:11 PM

and this

catch(ArrayIndexOutOfBoundsException e) {}

is surely not a good idea and is actually ridiculous and scary

try/catch blocks exist to catch unexpected errors not to catch lazy programming errors

You wrote the code you should write code that does not exceed array dimension that is the minimum we can expect from normal code.

If somebody else writes code that call your code, and if that somebody else does not trust your code (which seems for the moment a reasonnable assestment) it will be his responsability to call your code within an ArrayoutOfBoundException try/catch clause.

This post has been edited by pbl: 08 September 2013 - 07:41 PM

Was This Post Helpful? 3
  • +
  • -

#4 extremeblueness   User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 188
  • Joined: 22-October 12

Re: Game of Life and Cellular State Automaton

Posted 09 September 2013 - 05:29 AM

View Postmacosxnerd101, on 09 September 2013 - 01:53 AM, said:

It looks like the problem comes from a sort() call. Please post the relevant code and the actual portion of the stack trace that will allow us to trace through your errors.

Also, I'm renaming your title to be more descriptive. Please don't use titles like "Help me" and "Errors."


I posted all my code, simply because I have no experience with strings of errors which don't contain any references to any classes outside the library, and its a really long list of errors.

Here's the full list of errors, which is what I think is called a stack trace, simply because I can't determine which portion is relevant due to the fact that it doesn't mention any of my classes once:

Spoiler

Also, I'm not aware of any sorting methods that my program calls. The only comparisons that my program calls are basic logic statements.

View Postpbl, on 09 September 2013 - 02:11 AM, said:

and this

catch(ArrayIndexOutOfBoundsException e) {}

is surely not a good idea and is actually ridiculous and scary

try/catch blocks exist to catch unexpected errors not to catch lazy programming errors

You wrote the code you should write code that does not exceed array dimension that is the minimum we can expect from normal code.

If somebody else writes code that call your code, and if that somebody else does not trust your code (which seems for the moment a reasonnable assestment) it will be his responsability to call your code within an ArrayoutOfBoundException try/catch clause.


Yes, I'm aware that this is a bad idea, and I'm sorry for my laziness. However, this doesn't do any harm in this particular program, as its basically being used to check if its using data that it should be using, and doing nothing more. Now could we please focus on my actual errors? I promise I'll change it before I finish the program.
Was This Post Helpful? 0
  • +
  • -

#5 Mylo   User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: Game of Life and Cellular State Automaton

Posted 09 September 2013 - 07:29 AM

After some experimenting, I can only come to the conclusion that you are adding too many JPanels, which swing (or maybe the layout manager) may not like.

Anything after 36x36 panels would throw the error, even in my own test application.

Perhaps you should try not have 10000 JPanels, but instead have 10000 Cell objects, and use the paint() methods to draw them in their state.

This post has been edited by Mylo: 09 September 2013 - 07:34 AM

Was This Post Helpful? 1
  • +
  • -

#6 extremeblueness   User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 188
  • Joined: 22-October 12

Re: Game of Life and Cellular State Automaton

Posted 09 September 2013 - 05:07 PM

Wow. You're right. A 36x36 works. Now I just want to know why a 100x100 doesn't work...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1