-3

Ok this is my code. Now I have it set up so far to display the correct pattern for 3 seconds, go to an input screen for the user to input, when the user clicks the screen 6 times it takes the pattern the user inputs and stores it into the array input, then checks if the input array and pattern array are the same. Now the problem is that when it checks for the arrays to be the same that it works correctly for the percent pattern but it doesn't work correctly for any other pattern.

How can I compare the two 2D arrays?

package game;

import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;

public class InputGame implements Properties,Listeners{
    public static JFrame gf = new JFrame();
    public static JButton a1,a2,a3,a4;
    public static JButton b1,b2,b3,b4;
    public static JButton c1,c2,c3,c4;
    public static JButton d1,d2,d3,d4;
    public static int height = 800;
    public static int width = 600;
    public static int gsize = 4;
    public static int order =1;
    public static Dimension size = new Dimension(height,width);
    public static menu Menu = new menu();
    public static GridLayout Ggrid = new GridLayout(gsize,gsize);
    public static int numOfClicks =0;
    public static int numCorrect=0;
    public static int[][] input = new int[][]{
        {0,0,0,0},
        {0,0,0,0},
        {0,0,0,0},
        {0,0,0,0}

    };

    //public static Thread d;

    public static void clear()
    {
        gf.dispose();
    }

    public static void setup() {
        gf.dispose();
        gf = new JFrame();
        gf.setLocation(300,100);
        gf.setSize(size);
        gf.setResizable(false);
        gf.setLayout(Ggrid);
        gf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        PatternGame.clear();
        blank();
        gf.setVisible(true);
        System.out.print(numOfClicks);

    }

    public static void blank()
    {
        {
            a1 = new JButton("");
            a2 = new JButton("");
            a3 = new JButton("");
            a4 = new JButton("");
            a1.addActionListener(new input());
            a2.addActionListener(new input());
            a3.addActionListener(new input());
            a4.addActionListener(new input());
            gf.add(a1);
            gf.add(a2);
            gf.add(a3);
            gf.add(a4);

        }
        {
            b1 = new JButton("");
            b2 = new JButton("");
            b3 = new JButton("");
            b4 = new JButton("");
            b1.addActionListener(new input());
            b2.addActionListener(new input());
            b3.addActionListener(new input());
            b4.addActionListener(new input());
            gf.add(b1);
            gf.add(b2);
            gf.add(b3);
            gf.add(b4);

        }
        {
            c1 = new JButton("");
            c2 = new JButton("");
            c3 = new JButton("");
            c4 = new JButton("");
            c1.addActionListener(new input());
            c2.addActionListener(new input());
            c3.addActionListener(new input());
            c4.addActionListener(new input());
            gf.add(c1);
            gf.add(c2);
            gf.add(c3);
            gf.add(c4);

        }
        {
            d1 = new JButton("");
            d2 = new JButton("");
            d3 = new JButton("");
            d4 = new JButton("");
            d1.addActionListener(new input());
            d2.addActionListener(new input());
            d3.addActionListener(new input());
            d4.addActionListener(new input());
            gf.add(d1);
            gf.add(d2);
            gf.add(d3);
            gf.add(d4);

        }



    }
    public static void input()
    {

        {
            String x = a1.getText();
            if (x.equals("X"))
            {
                input[0][0] = 1;
            }
            x = a2.getText();
            if (x.equals("X"))
            {
                input[0][1] = 1;
            }
             x = a3.getText();
            if (x.equals("X"))
            {
                input[0][2] = 1;
            }
             x = a4.getText();
            if (x.equals("X"))
            {
                input[0][3] = 1;
            }
        }
        {
            String x = b1.getText();
            if (x.equals("X"))
            {
                input[1][0] = 1;
            }
            x = b2.getText();
            if (x.equals("X"))
            {
                input[1][1] = 1;
            }
             x = b3.getText();
            if (x.equals("X"))
            {
                input[1][2] = 1;
            }
             x = b4.getText();
            if (x.equals("X"))
            {
                input[1][3] = 1;
            }
        }
        {
            String x = c1.getText();
            if (x.equals("X"))
            {
                input[2][0] = 1;
            }
            x = c2.getText();
            if (x.equals("X"))
            {
                input[2][1] = 1;
            }
             x = c3.getText();
            if (x.equals("X"))
            {
                input[2][2] = 1;
            }
             x = c4.getText();
            if (x.equals("X"))
            {
                input[2][3] = 1;
            }
        }
        {
            String x = d1.getText();
            if (x.equals("X"))
            {
                input[3][0] = 1;
            }
            x = d2.getText();
            if (x.equals("X"))
            {
                input[3][1] = 1;
            }
             x = d3.getText();
            if (x.equals("X"))
            {
                input[3][2] = 1;
            }
             x = d4.getText();
            if (x.equals("X"))
            {
                input[3][3] = 1;
            }
        }


    }
    public static boolean checkCorrect()
    {
        input();
        for (int a=0;a<4;a++)
        {
            for(int b=0;b<4;b++)
            {
                if (order == 1)
                {
                    if (handlebars[a][b] == input[a][b])
                    {

                        InputGame.numCorrect++;

                    }

                }
                if (order == 2)
                {
                    if (ys[a][b] == input[a][b])
                    {

                        InputGame.numCorrect++;

                    }
                }
                if (order == 3)
                {
                    if (spaceShip[a][b] == input[a][b])
                    {

                        InputGame.numCorrect++;

                    }
                }
                if (order == 4)
                {
                    if (flock[a][b] == input[a][b])
                    {

                        InputGame.numCorrect++;

                    }
                }
                if (order == 5)
                {
                    if (percent[a][b] == input[a][b])
                    {

                        InputGame.numCorrect++;

                    }
                }
                if (order == 6)
                {
                    if (square[a][b] == input[a][b])
                    {

                        InputGame.numCorrect++;

                    }
                }
            }
        }
        System.out.println(numCorrect);
        for (int a =0;a<4;a++)
        {
            System.out.println();
            for(int b=0;b<4;b++)
            {
                System.out.print(input[a][b]);

            }
        }
        for (int a =0;a<4;a++)
        {
            System.out.println();
            for(int b=0;b<4;b++)
            {
                System.out.print(ys[a][b]);

            }
        }
        if (numCorrect == 16)
        {
            return true;
        }
        else
        {
            return false;

        }
    }


}
package game;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JOptionPane;


public interface Listeners 
{
    static PatternGame game = new PatternGame();
    InputGame game2 = new InputGame();
    static class inst implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            if ("inst".equals(e.getActionCommand()))
            {

            }
        }
    }
    static class play implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            if ("play".equals(e.getActionCommand()))
            {
                menu.mf.dispose();
                PatternGame.gameStart();

            }
        }
    }
    static class exit implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            if ("exit".equals(e.getActionCommand()))
            {
                System.exit(0);
            }
        }
    }
    static class input implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            if (InputGame.numOfClicks !=1)
            {
                JButton x = (JButton) e.getSource();

                x.setText("X");
                InputGame.numOfClicks--;
            }
            else if (InputGame.numOfClicks ==1)
            {
                JButton x = (JButton) e.getSource();
                x.setText("X");
                InputGame.numOfClicks--;
                if (InputGame.checkCorrect())
                {
                    int yesno = JOptionPane.showConfirmDialog(null, "<html> Correct!" +"<br>Would you like another pattern?");
                    if (yesno == JOptionPane.YES_OPTION)
                    {
                        PatternGame.order++;
                        InputGame.clear();
                        PatternGame.gameStart();
                    }
                    else
                    {
                        InputGame.clear();
                        menu.start();
                    }
                }
                else
                {
                    JOptionPane.showMessageDialog(null, "Incorrect!");
                    InputGame.clear();
                    menu.start();
                }
            }


        }
    }
}
package game;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JButton;
import javax.swing.JFrame;

public class PatternGame implements Properties
{
    public static JFrame df = new JFrame();
    public static int height = 800;
    public static int width = 600;
    public static int gsize = 4;
    public static int order =1;
    public static Dimension size = new Dimension(height,width);
    public static menu Menu = new menu();
    public static GridLayout Ggrid = new GridLayout(gsize,gsize);

    public static void DisplayPat()
    {
        df.dispose();
        df = new JFrame();
        df.setLocation(300,100);
        df.setSize(size);
        df.setResizable(false);
        df.setLayout(Ggrid);
        df.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        df.setVisible(true);


    }
    public static boolean StayIn = true;
    public static InputGame game = new InputGame();
    public static int flag =0;
    public static void clear()
    {
        df.dispose();
    }
    public static void gameStart()
    {

        DisplayPat();
        getpattern();
        schedule(3);



    //notifyAll();
    }
    public static void blank()
    {
        for (int a=0;a<4;a++)
        {
            for(int b=0;b<4;b++)
            {

                JButton button = new JButton(" ");
                df.add(button);


            }
        }
    }
    public static void getpattern()
    {
        InputGame.numOfClicks=0;
        if (order == 1)
            {
            for (int a=0;a<4;a++)
            {
                for(int b=0;b<4;b++)
                {
                    if (handlebars[a][b] == 1)
                    {
                    JButton button = new JButton("X");
                    df.add(button);
                    InputGame.numOfClicks++;

                    }
                    else
                    {
                        JButton button = new JButton("");
                        df.add(button);
                    }
                    flag =1;

                }
            }
            }
        if (order == 2)
            {
            for (int a=0;a<4;a++)
            {
                for(int b=0;b<4;b++)
                {
                    if (ys[a][b] == 1)
                    {
                    JButton button = new JButton("X");
                    df.add(button);
                    InputGame.numOfClicks++;

                    }
                    else
                    {
                        JButton button = new JButton("");
                        df.add(button);
                    }
                }
            }
            }
        if (order == 3)
        {
        for (int a=0;a<4;a++)
        {
            for(int b=0;b<4;b++)
            {
                if (spaceShip[a][b] == 1)
                {
                JButton button = new JButton("X");
                df.add(button);
                InputGame.numOfClicks++;

                }
                else
                {
                    JButton button = new JButton("");
                    df.add(button);
                }
            }
        }
        }   if (order == 4)
        {
        for (int a=0;a<4;a++)
        {
            for(int b=0;b<4;b++)
            {
                if (flock[a][b] == 1)
                {
                JButton button = new JButton("X");
                df.add(button);
                InputGame.numOfClicks++;

                }
                else
                {
                    JButton button = new JButton("");
                    df.add(button);
                }
            }
        }
        }   if (order == 5)
        {
        for (int a=0;a<4;a++)
        {
            for(int b=0;b<4;b++)
            {
                if (percent[a][b] == 1)
                {
                JButton button = new JButton("X");
                df.add(button);
                InputGame.numOfClicks++;

                }
                else
                {
                    JButton button = new JButton("");
                    df.add(button);
                }
            }
        }
        }
        if (order == 6)
        {
        for (int a=0;a<4;a++)
        {
            for(int b=0;b<4;b++)
            {
                if (square[a][b] == 1)
                {
                JButton button = new JButton("X");
                df.add(button);
                InputGame.numOfClicks++;

                }
                else
                {
                    JButton button = new JButton("");
                    df.add(button);
                }
            }
        }
        }
        df.setVisible(true);
    }

    public static Timer timer = new Timer(); //timer object

    public static void schedule(int seconds) {
        timer.schedule(new ScheduleTask(), seconds*1000);
    }

    static class ScheduleTask extends TimerTask {
        public void run() {
            InputGame.setup();

           // timer.cancel(); //Terminate Timer thread
        }
    }

}
package game;

public interface Properties 
{

    int[][] percent = new int[][]{
            {1,0,0,1},             
            {0,0,1,0},
            {0,1,0,0},
            {1,0,0,1}
    };
    int[][] spaceShip = new int[][]{
            {0,1,1,0},
            {0,0,0,0},
            {0,1,1,0},
            {1,0,0,1}       
    };
    int[][] flock = new int[][]{
            {0,0,0,0},
            {1,0,1,0},
            {0,1,0,1},
            {1,0,1,0}
    };
    int[][] ys = new int[][]{
            {0,0,1,0},
            {1,0,1,0},
            {0,1,0,1},
            {0,1,0,0}
    };
    int[][] handlebars = new int[][]{
            {1,0,0,0},
            {0,1,1,0},
            {0,1,1,0},
            {0,0,0,1}
    };
    int[][] square = new int[][]{
            {0,1,0,1},
            {1,0,0,0},
            {0,0,0,1},
            {1,0,1,0}
    };

}
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • 3
    Do we need to see all that code? – Sotirios Delimanolis Dec 22 '13 at 03:47
  • No, we don't need to see all that code, but we *do* need to see a question. I'm still waiting... – Bohemian Dec 22 '13 at 03:49
  • 1
    Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See [SSCCE.org](http://SSCCE.org) for guidance. – PM 77-1 Dec 22 '13 at 03:50
  • The question is why does my program act like it does it doesn't seem logical why it works once but doesn't for for any other pattern. And this reason for all the code is to show everyone where all the pattern arrays and input array is used. – Mikhail_Bjornson Dec 22 '13 at 03:54
  • 1
    Without looking at that huge mess of code I'm guessing you are using == to compare arrays which is not exactly the way to go... also +1 @PM for the link – takendarkk Dec 22 '13 at 03:59
  • Yes I am could you tell me how to do it then. – Mikhail_Bjornson Dec 22 '13 at 04:00
  • Have a look at the answer here: http://stackoverflow.com/questions/8777257/equals-vs-arrays-equals-in-java – PM 77-1 Dec 22 '13 at 04:05
  • possible duplicate of [Compare two Byte Arrays? (Java)](http://stackoverflow.com/questions/5440039/compare-two-byte-arrays-java) – Kevin Panko Dec 22 '13 at 06:19
  • That should fix the question (second to last sentence). – Mikhail_Bjornson Dec 22 '13 at 22:40

1 Answers1

2

Try using:

Arrays.equals(); // <---- One Dimensional Arrays

or

ArrayUtils.isEquals(); // <---- Multi Dimensional Arrays

These methods will compare the actual contents of the two arrays (not if they are the same reference or same object) and you wont have to step through each element individually.

Hope it helps!

takendarkk
  • 3,347
  • 8
  • 25
  • 37