简单的java连连看代码 如何用java写一个连连看游戏( 六 )


diamondsButton[cols][rows] = new JButton(String
.valueOf(grid[cols + 1][rows + 1]));
diamondsButton[cols][rows].addActionListener(this);
centerPanel.add(diamondsButton[cols][rows]);
}
}
exitButton = new JButton("退出");
exitButton.addActionListener(this);
resetButton = new JButton("重列");
resetButton.addActionListener(this);
newlyButton = new JButton("再来一局");
newlyButton.addActionListener(this);
southPanel.add(exitButton);
southPanel.add(resetButton);
southPanel.add(newlyButton);
fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable
.getText())));
northPanel.add(fractionLable);
mainFrame.setBounds(280, 100, 500, 450);
mainFrame.setVisible(true);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void randomBuild() {
int randoms, cols, rows;
for (int twins = 1; twins = 15; twins++) {//一共15对button,30个
【简单的java连连看代码 如何用java写一个连连看游戏】randoms = (int) (Math.random() * 25 + 1);//button上的数字
for (int alike = 1; alike = 2; alike++) {
cols = (int) (Math.random() * 6 + 1);
rows = (int) (Math.random() * 5 + 1);
while (grid[cols][rows] != 0) {//等于0说明这个空格有了button
cols = (int) (Math.random() * 6 + 1);
rows = (int) (Math.random() * 5 + 1);
}
this.grid[cols][rows] = randoms;
}
}
}
public void fraction() {
fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable
.getText()) + 100));
}
public void reload() {
int save[] = new int[30];
int n = 0, cols, rows;
int grid[][] = new int[8][7];
for (int i = 0; i = 6; i++) {
for (int j = 0; j = 5; j++) {
if (this.grid[i][j] != 0) {
save[n] = this.grid[i][j];//记下每个button的数字
n++;//有几个没有消去的button
}
}
}
n = n - 1;
this.grid = grid;
while (n = 0) {//把没有消去的button重新放一次
cols = (int) (Math.random() * 6 + 1);
rows = (int) (Math.random() * 5 + 1);
while (grid[cols][rows] != 0) {
cols = (int) (Math.random() * 6 + 1);
rows = (int) (Math.random() * 5 + 1);
}
this.grid[cols][rows] = save[n];
n--;
}
mainFrame.setVisible(false);
pressInformation = false; // 这里一定要将按钮点击信息归为初始
init();
for (int i = 0; i6; i++) {
for (int j = 0; j5; j++) {
if (grid[i + 1][j + 1] == 0)
diamondsButton[i][j].setVisible(false);
}
}
}
public void estimateEven(int placeX, int placeY, JButton bz) {
if (pressInformation == false) {
x = placeX;
y = placeY;
secondMsg = grid[x][y];
secondButton = bz;
pressInformation = true;
} else {
x0 = x;
y0 = y;
fristMsg = secondMsg;
firstButton = secondButton;
x = placeX;
y = placeY;
secondMsg = grid[x][y];
secondButton = bz;
if (fristMsg == secondMsgsecondButton != firstButton) {
xiao();
}
}
}
public void xiao() { // 相同的情况下能不能消去 。仔细分析,不一条条注释
if ((x0 == x(y0 == y + 1 || y0 == y - 1))
|| ((x0 == x + 1 || x0 == x - 1)(y0 == y))) { // 判断是否相邻
remove();
} else {
for (j = 0; j7; j++) {
if (grid[x0][j] == 0) { // 判断和第一个按钮同行的哪个按钮为空
//如果找到一个为空的 , 就按列值的三种情况比较第二个按钮与空按钮的位置
if (yj) {//第二个按钮在空按钮右边
for (i = y - 1; i = j; i--) { //检测从第二个按钮横向左边到空格所在列为止是否全是空格
if (grid[x][i] != 0) {
k = 0;
break;//存在非空格的就退出 , 这一退出就不可能k==2了,所以就会到下而215行出同理的判断列

推荐阅读