As a school project in my navigator 12 class, I've decided to try and learn Java. I thought that I'd try and make the Game of Life, because it would be a good way to start learning Java (watched a bunch of Java tutorials).
Could I get your feedback on this program I wrote? I would like input on if everything is inputted properly and what I should've done differently, and if there is more effective and efficient ways to do a certain task. Also, just any type feedback would be helpful.
I made the game so the sides of the board would be connected to each other, and so the simulation would be contained(it will loop to the other side). I also have a slider to change the speed the game iterates. Other features are randomizing the board, and play and stop buttons. You can also, change the size of the board, with inputting the size you want it to generate.
Main
public class Main {
public static void main(String[] args) {
new Settingsboard();
}
}
Settings
public class Settingsboard extends JFrame{
JFrame Frame2 = new JFrame();
JLabel label1, label2, value1, value2;
JTextField x_Value, y_Value;
JButton button1, button2, button3, clear;
static JCheckBox random;
int run = 0;
static JSlider howManyTimes;
int delay = 1000; // 1000 ms == 1 second
javax.swing.Timer myTimer = new javax.swing.Timer(delay,
new MyTimerActionListener());
public Settingsboard(){
Frame2.setSize(400,400);
JPanel thePanel = new JPanel();
//JPanel thePanel2 = new JPanel();
label1 = new JLabel("Set Size Of Board: ");
thePanel.add(label1);
value1 = new JLabel(" Width: ");
thePanel.add(value1);
x_Value = new JTextField("", 5);
thePanel.add(x_Value);
value2 = new JLabel(" Height: ");
thePanel.add(value2);
y_Value = new JTextField("", 5);
thePanel.add(y_Value);
button1 = new JButton("Generate Board");
button1.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button1){
if(run == 0){
int number1 = Integer.parseInt(x_Value.getText());
int number2 = Integer.parseInt(y_Value.getText());
new Board(number1,number2);
run = 1;
}
else{JOptionPane.showMessageDialog(null, "Board is already running", "InfoBox: ", JOptionPane.INFORMATION_MESSAGE);}
}
}
}
);
thePanel.add(button1);
button2 = new JButton("Play");
button2.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button2){
myTimer.start();
}
}
}
);
thePanel.add(button2);
button3 = new JButton("Stop");
button3.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button3){
myTimer.stop();
}
}
}
);
thePanel.add(button3);
random = new JCheckBox("Randomize?");
thePanel.add(random);
label2 = new JLabel(" Speed of loop (in milliseconds): ");
thePanel.add(label2);
howManyTimes = new JSlider(0, 1000, 1000);
howManyTimes.setMinorTickSpacing(50);
howManyTimes.setMajorTickSpacing(250);
howManyTimes.setPaintTicks(true);
howManyTimes.setPaintLabels(true);
ListenForSlider lForSlider = new ListenForSlider();
howManyTimes.addChangeListener(lForSlider);
thePanel.add(howManyTimes);
clear = new JButton("Reset board");
clear.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getSource() == clear){
for(int y=0; y<Board.lengthT; y++){
for(int x=0; x<Board.widthT; x++){
if(Settingsboard.random.isSelected()) {
int random = (int )(Math.random() * 2);
if(random == 0){
Board.grid[x][y].setBackground(Color.red);
Board.boolBoard[x][y] = false;
}
else if(random == 1){
Board.grid[x][y].setBackground(Color.blue);
Board.boolBoard[x][y] = true;
}
}
else{
Board.grid[x][y].setBackground(Color.red);
Board.boolBoard[x][y] = false;
}
}
}
}
}
}
);
thePanel.add(clear);
Frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Frame2.add(thePanel);
Frame2.setVisible(true);
}
private class ListenForSlider implements ChangeListener{
@Override
public void stateChanged(ChangeEvent e) {
// Check if the source of the event was the button
if(e.getSource() == howManyTimes){
delay = howManyTimes.getValue();
String speed = Integer.toString(delay);
System.out.print(speed);
if(delay == 0){
System.out.print("hit");
speed = "As fast as it will go!";
}
label2.setText(" Speed of loop(in milliseconds): " + speed );
myTimer.setDelay(delay);
}
}
}
}
Board
public class Board extends JFrame{
JFrame Frame = new JFrame(); //creates frame
static JButton[][] grid; //names the grid of buttons
JTextArea textArea1;
static int lengthT = 0;;
static int widthT = 0;
static Boolean[][] boolBoard;
List<String> onXY_Values = new ArrayList<String>();
public Board(int width, int length){ //constructor
boolBoard = new Boolean [width][length];
Frame.setSize(1000,1000);
Frame.setLocationRelativeTo(null);
Frame.setTitle("The Game of Life");
ListenForKeys lForKeys = new ListenForKeys();
widthT = width;
lengthT = length;
Frame.setLayout(new GridLayout(width,length)); //set layout
grid = new JButton[width][length]; //allocate the size of grid
for(int y=0; y<length; y++){
for(int x=0; x<width; x++){
grid[x][y]=new JButton("("+x+","+y+")"); //creates new button
Frame.add(grid[x][y]); //adds button to grid
grid[x][y].setBorderPainted(false);
grid[x][y].setContentAreaFilled(false);
if(Settingsboard.random.isSelected()) {
int random = (int )(Math.random() * 2);
if(random == 0){
grid[x][y].setBackground(Color.red);
boolBoard[x][y] = false;
}
else if(random == 1){
grid[x][y].setBackground(Color.blue);
boolBoard[x][y] = true;
}
}
else{
grid[x][y].setBackground(Color.red);
boolBoard[x][y] = false;
}
grid[x][y].setForeground(Color.black);
grid[x][y].setOpaque(true);
grid[x][y].setPreferredSize(new Dimension(30, 30));
grid[x][y].setBorder(BorderFactory.createLineBorder(Color.black, 2));
grid[x][y].setMargin(new Insets(0, 0, 0, 0));
ListenForButton lForButton = new ListenForButton();
grid[x][y].addActionListener(lForButton);
grid[x][y].addKeyListener(lForKeys);
}
}
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Frame.pack(); //sets appropriate size for frame
Frame.setVisible(true); //makes frame visible
}
public static void main(String[] args) {
}
private class ListenForButton implements ActionListener{
public void actionPerformed(ActionEvent e){
String xString = e.getActionCommand();
String yString = e.getActionCommand();
int index = xString.indexOf(',');
xString = xString.substring(0, index);
String yStringRemove = xString;
xString = xString.replace("(", "");
int x = Integer.parseInt(xString);
yString = yString.replace(yStringRemove, "");
yString = yString.replace(",", "");
yString = yString.replace(")", "");
int y = Integer.parseInt(yString);
((JButton)e.getSource()).setEnabled(true);
if(e.getSource() == grid[x][y]){
grid[x][y].getActionCommand();
if(!boolBoard[x][y]){//turning on
grid[x][y].setBackground(Color.blue);
onXY_Values.add(x + "," + y);
boolBoard[x][y] = true;
}
else if(boolBoard[x][y]){//turning off
grid[x][y].setBackground(Color.red);
onXY_Values.remove(x + "," + y);
boolBoard[x][y] = false;
}
}
}
}
private class ListenForKeys implements KeyListener{
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
if( e.getKeyChar() == 10 ){
Logic.logic(lengthT,widthT,grid,boolBoard);
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}
}
Timer
public class MyTimerActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
int widthT = Board.widthT;
int lengthT = Board.lengthT;
JButton[][] grid = Board.grid;
Boolean [][] boolBoard = Board.boolBoard;
Logic.logic(lengthT, widthT, grid, boolBoard);
}
}
Logic
public class Logic {
public static void logic(int lengthT, int widthT, JButton[][] grid, Boolean[][] boolBoard) {
List<Integer> turnOnX = new ArrayList<Integer>();
List<Integer> turnOnY = new ArrayList<Integer>();
List<Integer> turnOffX = new ArrayList<Integer>();
List<Integer> turnOffY = new ArrayList<Integer>();
int fullspace = 0;
for(int w = 0; w < 1; w++){//right bottom piece
int j = lengthT - 1;
int i = widthT - 1;
if(!boolBoard[i][j]){//off
fullspace = 0;
if(boolBoard[i- 1][j-1]){
fullspace++;
}
if(boolBoard[i- 1][j]){
fullspace++;
}
if(boolBoard[i][j-1]){
fullspace++;
}
if(boolBoard[i-widthT+1][j-lengthT+1]){
fullspace++;
}
if(boolBoard[i-widthT+1][j]){
fullspace++;
}
if(boolBoard[i][j-lengthT+1]){
fullspace++;
}
if(boolBoard[i-widthT+1][j-1]){
fullspace++;
}
if(boolBoard[i-1][j-lengthT+1]){
fullspace++;
}
if(fullspace == 3){
turnOnX.add(i);
turnOnY.add(j);
}
}
else if(boolBoard[i][j]){
fullspace = 0;
if(boolBoard[i- 1][j-1]){
fullspace++;
}
if(boolBoard[i- 1][j]){
fullspace++;
}
if(boolBoard[i][j-1]){
fullspace++;
}
if(boolBoard[i-widthT+1][j-lengthT+1]){
fullspace++;
}
if(boolBoard[i-widthT+1][j]){
fullspace++;
}
if(boolBoard[i][j-lengthT+1]){
fullspace++;
}
if(boolBoard[i-widthT+1][j-1]){
fullspace++;
}
if(boolBoard[i-1][j-lengthT+1]){
fullspace++;
}
if(fullspace < 2){
turnOffX.add(i);
turnOffY.add(j);
}
else if(fullspace > 3){
turnOffX.add(i);
turnOffY.add(j);
}
}
}
for(int w = 0; w < 1; w++){//left bottom piece
int j = lengthT - 1;
int i = w;
if(!boolBoard[i][j]){//off
fullspace = 0;
if(boolBoard[i+widthT- 1][j-1]){
fullspace++;
}
if(boolBoard[i+widthT- 1][j]){
fullspace++;
}
if(boolBoard[i][j-1]){
fullspace++;
}
if(boolBoard[i+1][j-lengthT+1]){
fullspace++;
}
if(boolBoard[i+1][j]){
fullspace++;
}
if(boolBoard[i][j-lengthT+1]){
fullspace++;
}
if(boolBoard[i+1][j-1]){
fullspace++;
}
if(boolBoard[i+widthT-1][j-lengthT+1]){
fullspace++;
}
if(fullspace == 3){
turnOnX.add(i);
turnOnY.add(j);
}
}
else if(boolBoard[i][j]){
fullspace = 0;
if(boolBoard[i+widthT- 1][j-1]){
fullspace++;
}
if(boolBoard[i+widthT- 1][j]){
fullspace++;
}
if(boolBoard[i][j-1]){
fullspace++;
}
if(boolBoard[i+1][j-lengthT+1]){
fullspace++;
}
if(boolBoard[i+1][j]){
fullspace++;
}
if(boolBoard[i][j-lengthT+1]){
fullspace++;
}
if(boolBoard[i+1][j-1]){
fullspace++;
}
if(boolBoard[i+widthT-1][j-lengthT+1]){
fullspace++;
}
if(fullspace < 2){
turnOffX.add(i);
turnOffY.add(j);
}
else if(fullspace > 3){
turnOffX.add(i);
turnOffY.add(j);
}
}
}
for(int w = 0; w < 1; w++){//right top piece
int j = 0;
int i = widthT - 1;
if(!boolBoard[i][j]){//off
fullspace = 0;
if(boolBoard[i- 1][j+lengthT-1]){
fullspace++;
}
if(boolBoard[i- 1][j]){
fullspace++;
}
if(boolBoard[i][j+lengthT-1]){
fullspace++;
}
if(boolBoard[i-widthT+1][j+1]){
fullspace++;
}
if(boolBoard[i-widthT+1][j]){
fullspace++;
}
if(boolBoard[i][j+1]){
fullspace++;
}
if(boolBoard[i-widthT+1][j+lengthT-1]){
fullspace++;
}
if(boolBoard[i-1][j+1]){
fullspace++;
}
if(fullspace == 3){
turnOnX.add(i);
turnOnY.add(j);
}
}
else if(boolBoard[i][j]){
fullspace = 0;
if(boolBoard[i- 1][j+lengthT-1]){
fullspace++;
}
if(boolBoard[i- 1][j]){
fullspace++;
}
if(boolBoard[i][j+lengthT-1]){
fullspace++;
}
if(boolBoard[i-widthT+1][j+1]){
fullspace++;
}
if(boolBoard[i-widthT+1][j]){
fullspace++;
}
if(boolBoard[i][j+1]){
fullspace++;
}
if(boolBoard[i-widthT+1][j+lengthT-1]){
fullspace++;
}
if(boolBoard[i-1][j+1]){
fullspace++;
}
if(fullspace < 2){
turnOffX.add(i);
turnOffY.add(j);
}
else if(fullspace > 3){
turnOffX.add(i);
turnOffY.add(j);
}
}
}
for(int w = 0; w < 1; w++){//left top piece
int j = 0;
int i = w;
if(!boolBoard[i][j]){//off
fullspace = 0;
if(boolBoard[i + widthT - 1][j+lengthT-1]){
fullspace++;
}
if(boolBoard[i + widthT- 1][j]){
fullspace++;
}
if(boolBoard[i][j+lengthT-1]){
fullspace++;
}
if(boolBoard[i+1][j+1]){
fullspace++;
}
if(boolBoard[i+1][j]){
fullspace++;
}
if(boolBoard[i][j+1]){
fullspace++;
}
if(boolBoard[i+1][j+lengthT-1]){
fullspace++;
}
if(boolBoard[i+ widthT-1][j+1]){
fullspace++;
}
if(fullspace == 3){
turnOnX.add(i);
turnOnY.add(j);
}
}
else if(boolBoard[i][j]){
fullspace = 0;
if(boolBoard[i+ widthT - 1][j+lengthT-1]){
fullspace++;
}
if(boolBoard[i+ widthT- 1][j]){
fullspace++;
}
if(boolBoard[i][j+lengthT-1]){
fullspace++;
}
if(boolBoard[i+1][j+1]){
fullspace++;
}
if(boolBoard[i+1][j]){
fullspace++;
}
if(boolBoard[i][j+1]){
fullspace++;
}
if(boolBoard[i+1][j+lengthT-1]){
fullspace++;
}
if(boolBoard[i+ widthT-1][j+1]){
fullspace++;
}
if(fullspace < 2){
turnOffX.add(i);
turnOffY.add(j);
}
else if(fullspace > 3){
turnOffX.add(i);
turnOffY.add(j);
}
}
}
for(int w = 1; w < widthT - 1; w++){//top bar
int j = 0;
int i = w;
if(!boolBoard[i][j]){//off
fullspace = 0;
if(boolBoard[i - 1][j+lengthT-1]){
fullspace++;
}
if(boolBoard[i - 1][j]){
fullspace++;
}
if(boolBoard[i][j+lengthT-1]){
fullspace++;
}
if(boolBoard[i+1][j+1]){
fullspace++;
}
if(boolBoard[i+1][j]){
fullspace++;
}
if(boolBoard[i][j+1]){
fullspace++;
}
if(boolBoard[i+1][j+lengthT-1]){
fullspace++;
}
if(boolBoard[i-1][j+1]){
fullspace++;
}
if(fullspace == 3){
turnOnX.add(i);
turnOnY.add(j);
}
}
else if(boolBoard[i][j]){
fullspace = 0;
if(boolBoard[i - 1][j+lengthT-1]){
fullspace++;
}
if(boolBoard[i - 1][j]){
fullspace++;
}
if(boolBoard[i][j+lengthT-1]){
fullspace++;
}
if(boolBoard[i+1][j+1]){
fullspace++;
}
if(boolBoard[i+1][j]){
fullspace++;
}
if(boolBoard[i][j+1]){
fullspace++;
}
if(boolBoard[i+1][j+lengthT-1]){
fullspace++;
}
if(boolBoard[i-1][j+1]){
fullspace++;
}
if(fullspace < 2){
turnOffX.add(i);
turnOffY.add(j);
}
else if(fullspace > 3){
turnOffX.add(i);
turnOffY.add(j);
}
}
}
for(int w = 1; w < widthT - 1; w++){//bottom bar
int j = lengthT - 1;
int i = w;
if(!boolBoard[i][j]){//off
fullspace = 0;
if(boolBoard[i - 1][j - 1]){
fullspace++;
}
if(boolBoard[i - 1][j]){
fullspace++;
}
if(boolBoard[i][j - 1]){
fullspace++;
}
if(boolBoard[i+1][j-lengthT+1]){
fullspace++;
}
if(boolBoard[i+1][j]){
fullspace++;
}
if(boolBoard[i][j-lengthT+1]){
fullspace++;
}
if(boolBoard[i+1][j - 1]){
fullspace++;
}
if(boolBoard[i-1][j-lengthT+1]){
fullspace++;
}
if(fullspace == 3){
turnOnX.add(i);
turnOnY.add(j);
}
}
else if(boolBoard[i][j]){
fullspace = 0;
if(boolBoard[i - 1][j-1]){
fullspace++;
}
if(boolBoard[i - 1][j]){
fullspace++;
}
if(boolBoard[i][j-1]){
fullspace++;
}
if(boolBoard[i+1][j-lengthT+1]){
fullspace++;
}
if(boolBoard[i+1][j]){
fullspace++;
}
if(boolBoard[i][j-lengthT+1]){
fullspace++;
}
if(boolBoard[i+1][j-1]){
fullspace++;
}
if(boolBoard[i-1][j-lengthT+1]){
fullspace++;
}
if(fullspace < 2){
turnOffX.add(i);
turnOffY.add(j);
}
else if(fullspace > 3){
turnOffX.add(i);
turnOffY.add(j);
}
}
}
for(int l = 1; l < lengthT - 1; l++){//left bar
int i = 0;
int j = l;
if(!boolBoard[i][j]){//off
fullspace = 0;
if(boolBoard[i + widthT -1][j-1]){
fullspace++;
}
if(boolBoard[i + widthT -1][j]){
fullspace++;
}
if(boolBoard[i][j-1]){
fullspace++;
}
if(boolBoard[i+1][j+1]){
fullspace++;
}
if(boolBoard[i+1][j]){
fullspace++;
}
if(boolBoard[i][j+1]){
fullspace++;
}
if(boolBoard[i+1][j-1]){
fullspace++;
}
if(boolBoard[i + widthT -1][j+1]){
fullspace++;
}
if(fullspace == 3){
turnOnX.add(i);
turnOnY.add(j);
}
}
else if(boolBoard[i][j]){
fullspace = 0;
if(boolBoard[i + widthT -1][j-1]){
fullspace++;
}
if(boolBoard[i + widthT -1][j]){
fullspace++;
}
if(boolBoard[i][j-1]){
fullspace++;
}
if(boolBoard[i+1][j+1]){
fullspace++;
}
if(boolBoard[i+1][j]){
fullspace++;
}
if(boolBoard[i][j+1]){
fullspace++;
}
if(boolBoard[i+1][j-1]){
fullspace++;
}
if(boolBoard[i + widthT -1][j+1]){
fullspace++;
}
if(fullspace < 2){
turnOffX.add(i);
turnOffY.add(j);
}
else if(fullspace > 3){
turnOffX.add(i);
turnOffY.add(j);
}
}
}
for(int l = 1; l < lengthT - 1; l++){//right bar
int i = widthT -1;
int j = l;
if(!boolBoard[i][j]){//off
fullspace = 0;
if(boolBoard[i-1][j-1]){
fullspace++;
}
if(boolBoard[i-1][j]){
fullspace++;
}
if(boolBoard[i][j-1]){
fullspace++;
}
if(boolBoard[i-widthT+1][j+1]){
fullspace++;
}
if(boolBoard[i-widthT+1][j]){
fullspace++;
}
if(boolBoard[i][j+1]){
fullspace++;
}
if(boolBoard[i-widthT+1][j-1]){
fullspace++;
}
if(boolBoard[i-1][j+1]){
fullspace++;
}
if(fullspace == 3){
turnOnX.add(i);
turnOnY.add(j);
}
}
else if(boolBoard[i][j]){
fullspace = 0;
if(boolBoard[i-1][j-1]){
fullspace++;
}
if(boolBoard[i-1][j]){
fullspace++;
}
if(boolBoard[i][j-1]){
fullspace++;
}
if(boolBoard[i-widthT+1][j+1]){
fullspace++;
}
if(boolBoard[i-widthT+1][j]){
fullspace++;
}
if(boolBoard[i][j+1]){
fullspace++;
}
if(boolBoard[i-widthT+1][j-1]){
fullspace++;
}
if(boolBoard[i-1][j+1]){
fullspace++;
}
if(fullspace < 2){
turnOffX.add(i);
turnOffY.add(j);
}
else if(fullspace > 3){
turnOffX.add(i);
turnOffY.add(j);
}
}
}
for(int i = 1; i < widthT-1; i++)//checking empty spaces *main body*
{
for(int j = 1; j < lengthT-1; j++)
{
if(!boolBoard[i][j]){//off
fullspace = 0;
if(boolBoard[i - 1][j-1]){
fullspace++;
}
if(boolBoard[i - 1][j]){
fullspace++;
}
if(boolBoard[i][j-1]){
fullspace++;
}
if(boolBoard[i+1][j+1]){
fullspace++;
}
if(boolBoard[i+1][j]){
fullspace++;
}
if(boolBoard[i][j+1]){
fullspace++;
}
if(boolBoard[i+1][j-1]){
fullspace++;
}
if(boolBoard[i-1][j+1]){
fullspace++;
}
if(fullspace == 3){
turnOnX.add(i);
turnOnY.add(j);
}
}
else if(boolBoard[i][j]){
fullspace = 0;
if(boolBoard[i - 1][j-1]){
fullspace++;
}
if(boolBoard[i - 1][j]){
fullspace++;
}
if(boolBoard[i][j-1]){
fullspace++;
}
if(boolBoard[i+1][j+1]){
fullspace++;
}
if(boolBoard[i+1][j]){
fullspace++;
}
if(boolBoard[i][j+1]){
fullspace++;
}
if(boolBoard[i+1][j-1]){
fullspace++;
}
if(boolBoard[i-1][j+1]){
fullspace++;
}
if(fullspace < 2){
turnOffX.add(i);
turnOffY.add(j);
}
else if(fullspace > 3){
turnOffX.add(i);
turnOffY.add(j);
}
}
}
}
for (int i = 0; i < turnOnX.size(); i++){
grid[ turnOnX.get(i) ][ turnOnY.get(i) ].setBackground(Color.blue);
boolBoard[turnOnX.get(i)][turnOnY.get(i)] = true;
}
for (int i = 0; i < turnOffX.size(); i++){
grid[ turnOffX.get(i) ][ turnOffY.get(i) ].setBackground(Color.red);
boolBoard[turnOffX.get(i)][turnOffY.get(i)] = false;
}
}
}