好的,这是我的代码。到目前为止,我已将其设置为显示正确的模式3秒,进入输入屏幕供用户输入,当用户点击屏幕6次时,它采用用户输入的模式并将其存储到数组输入中,然后检查输入数组和模式数组是否相同。现在的问题是,当它检查数组是否与百分比模式正确工作时相同但它对任何其他模式都无法正常工作。
如何比较两个2D阵列?
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}
};
}
答案 0 :(得分:2)
尝试使用:
Arrays.equals(); // <---- One Dimensional Arrays
或
ArrayUtils.isEquals(); // <---- Multi Dimensional Arrays
这些方法将比较两个数组的实际内容(如果它们是相同的引用或相同的对象),并且您不必单独逐步执行每个元素。
希望它有所帮助!