1

I'm trying to get the values of outputArray that were initialized in the class LightsOutPuzzle in my class, solve. But after printing the elements of the array

1) before calling the class solve in LightsOutPuzzle 2) after calling solve, the elements are not equal, it's as if it has been initialized again.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.StringTokenizer;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class LightsOutPuzzle extends JFrame implements ActionListener {

    public JFrame f;
    public JPanel p1;
    public JPanel p2;
    public JButton[] buttonArray;
    public JButton solve;
    public int nRows = 5;
    public int nColumns = 5;
    public int nTotalButtons = nRows * nColumns;
    public int[] outputArray = new int[25];

    public void create() {
        init();
        gui();
    }

    public void gui() {
        f = new JFrame("Lights Out Puzzle");
        f.setVisible(true);
        f.setSize(500,500);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        p2 = new JPanel();

        solve = new JButton("Solve");

        p2.add(solve);

        f.add(p1);
        f.add(p2, BorderLayout.SOUTH);

        solve.addActionListener(new Action());
    }

    public void init() {    
        p1 = new JPanel();
        p1.setLayout(new GridLayout(nRows, nColumns, 0, 0));

        int[] inputArray = new int[25];
        int i=0;

        buttonArray = new JButton[nTotalButtons];
        for (int nNum = 0; nNum < nTotalButtons; nNum++) 
        {
            buttonArray[nNum] = new JButton("");
            add(buttonArray[nNum]);
            //clickability
            buttonArray[nNum].addActionListener(this);
            buttonArray[nNum].setBackground(Color.yellow);
            buttonArray[nNum].setActionCommand("" + nNum);
        }

        //reading config file
        BufferedReader br = null;
        try {
            String line;
            br = new BufferedReader(new FileReader("input.txt"));

            while ((line = br.readLine()) != null) {
               StringTokenizer stringTokenizer = new StringTokenizer(line, " ");
               while (stringTokenizer.hasMoreElements()) {
                    inputArray[i] = Integer.parseInt(stringTokenizer.nextElement().toString());
                    i++;
               }
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null)
                    br.close();

            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        //drawing text input
        for(i=0; i<nTotalButtons; i++){
            if(inputArray[i] == 1)
                buttonArray[i].setBackground(Color.white);
            p1.add(buttonArray[i]);
        }
    }

    public void save() {
        int i = 0;      
        for (i=0; i<25; i++) {
            if(buttonArray[i].getBackground() == Color.yellow)
                outputArray[i] = 1;
            else 
                outputArray[i] = 0;
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) 
    {
        //conditions for the puzzle
    }

    public class Action implements ActionListener {
        public void actionPerformed (ActionEvent e) {
            int i;

            save();
            for(i=0; i<25; i++)
                System.out.println(outputArray[i]);
            new solve();
        }
    }

    public static void main(String[] args) {
        LightsOutPuzzle lop = new LightsOutPuzzle();
        lop.create();
    }
}

At this point, all the elements of outputArray are equal to 1. However, when I print them using the other class, solve, all the elements of outputArray are now equal to 0.

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

public class solve extends LightsOutPuzzle {
private JFrame f;
private JPanel p1;

int[] inputArray = new int[25];
int[] zeroArray = new int[25];
int[] UIState = new int[25];
int[] boardState = new int[25];
Queue<int[]> queue = new LinkedList<int[]>();

public solve() {
    gui();
    init();
}

public void gui() {
    f = new JFrame("Solution");
    f.setVisible(true);
    f.setSize(500,500);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    p1 = new JPanel();
    p1.setLayout(new GridLayout(nRows, nColumns, 0, 0));

    buttonArray = new JButton[nTotalButtons];
    for (int nNum = 0; nNum < nTotalButtons; nNum++) {
        buttonArray[nNum] = new JButton("");
        add(buttonArray[nNum]);
        //buttonArray[nNum].setBackground(Color.white);
        buttonArray[nNum].setActionCommand("" + nNum);
        p1.add(buttonArray[nNum]);
    }

    f.add(p1);
}

public void init() {
    //drawing text input
    int i;
        for(i=0; i<25; i++)
            System.out.println(outputArray[i]);
    for(i=0; i<nTotalButtons; i++){
        if(outputArray[i] == 1)
            buttonArray[i].setBackground(Color.yellow);
    }
}

public static void main(String[] args) {
}

}

I would also appreciate tips about proper programming practices in Java. It's been a long time since I coded in this language so I just tried to create a program from what I can remember..

1

2 Answers 2

1

Even though class solve extends LightsOutPuzzle, it is a different object and so has its own copy of outputArray. In the first part of the code you are printing the outputArray instance in LightsOutPuzzle which has been initialized to ones:

ActionListener in LightsOutPuzzle

    public class Action implements ActionListener {
        public void actionPerformed (ActionEvent e) {
            int i;

            save();
            for(i=0; i<25; i++)
                    {
                      /////////////PRINTS OUT outputArray IN LightsOutPuzzle
                      System.out.println(outputArray[i]);
                    }

                    ////CREATES NEW solve OBJECT WITH ITS OWN outputArray COPY
                    new solve();
        }
    }

Then when you call the solve constructor, you are printing the solve copy which has not been initialized yet and are all zeros:

Init method in solve class

public void init() {
    //drawing text input
    int i;
        for(i=0; i<25; i++)
                     {
                         //////PRINTS OUT outputArray IN solve CLASS
                         System.out.println(outputArray[i]);
                     }
                     for(i=0; i<nTotalButtons; i++){
                        if(outputArray[i] == 1)
                           buttonArray[i].setBackground(Color.yellow);
                      }
      }
Sign up to request clarification or add additional context in comments.

4 Comments

They're different? Then how can I access the one in LightsOutPuzzle?
I'm not sure what your intent was for subclassing LightsOutPuzzle in the solve class, since you should subclass to share behavior between classes and you're not doing that. I think you are subclassing to share state, but state is not automatically shared among different copies of the same class or subclasses. At a glance, don't subclass LightsOutPuzzle, and pass the LightsOutPuzzle state (just outputArray if that's all you care about) to the solve constructor. One more tip - follow Java coding conventions and CamelCase class names. So class solve should be Solve.
Got it. But what is the simplest way to pass outputArray to the class Solve?
Simplest way is probably to create a Solve constructor that accepts an int[] and then set Solve's version of outputArray to the value passed in the constuctor.
1

The problem is that the variable that you base your decision on (the color of your buttons) gets reset because you are re-creating the buttons in the gui method which gets called from solve. As a result, your initial 1's turn to 0's again and that's what is printed during init.

Big tip: Don't use GUI properties for your core logic.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.