

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

New Topic/Question
Reply


MultiQuote







|