123456789101112131415161718192021222324252627282930????import?java.io.File;import?java.util.Scanner;?public?class?FileTest?{?????/**?????*?@param?args?????*/????public?static?void?main(String[]?args)?{????????Scanner?sc?=?new?Scanner(System.in);????????String?fileName?=?sc.next();????????File?file?=?new?File(fileName);????????if?(file.exists())?{????????????if?(file.isDirectory())?{????????????????String[]?list?=?file.list();????????????????System.out.println(list.length);????????????????for?(String?string?:?list)?{????????????????????System.out????????????????????????????.println(string????????????????????????????????????.substring(string.lastIndexOf(".")?=?0???string????????????????????????????????????????????.lastIndexOf(".")?:?0));????????????????}????????????}?else?{????????????????System.out.println("不是文件夾");????????????}????????}?else?{????????????System.out.println("文件不存在");????????}????}
創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供碑林網(wǎng)站建設(shè)、碑林做網(wǎng)站、碑林網(wǎng)站設(shè)計、碑林網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、碑林企業(yè)網(wǎng)站模板建站服務(wù),10年碑林做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class lianliankan implements ActionListener
{
JFrame mainFrame; //主面板
Container thisContainer;
JPanel centerPanel,southPanel,northPanel; //子面板
JButton diamondsButton[][] = new JButton[6][5];//游戲按鈕數(shù)組
JButton exitButton,resetButton,newlyButton; //退出,重列,重新開始按鈕
JLabel fractionLable=new JLabel("0"); //分?jǐn)?shù)標(biāo)簽
JButton firstButton,secondButton; //分別記錄兩次被選中的按鈕
int grid[][] = new int[8][7];//儲存游戲按鈕位置
static boolean pressInformation=false; //判斷是否有按鈕被選中
int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戲按鈕的位置坐標(biāo)
int i,j,k,n;//消除方法控制
public void init(){
mainFrame=new JFrame("JKJ連連看");
thisContainer = mainFrame.getContentPane();
thisContainer.setLayout(new BorderLayout());
centerPanel=new JPanel();
southPanel=new JPanel();
northPanel=new JPanel();
thisContainer.add(centerPanel,"Center");
thisContainer.add(southPanel,"South");
thisContainer.add(northPanel,"North");
centerPanel.setLayout(new GridLayout(6,5));
for(int cols = 0;cols 6;cols++){
for(int rows = 0;rows 5;rows++ ){
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);
}
public void randomBuild() {
int randoms,cols,rows;
for(int twins=1;twins=15;twins++) {
randoms=(int)(Math.random()*25+1);
for(int alike=1;alike=2;alike++) {
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]=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];
n++;
}
}
}
n=n-1;
this.grid=grid;
while(n=0) {
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;i 6;i++){
for(int j = 0;j 5;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==secondMsg secondButton!=firstButton){
xiao();
}
}
}
public void xiao() { //相同的情況下能不能消去。仔細(xì)分析,不一條條注釋
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) { //如果第二個按鈕的Y坐標(biāo)大于空按鈕的Y坐標(biāo)說明第一按鈕在第二按鈕左邊
for (i=y-1;i=j;i-- ){ //判斷第二按鈕左側(cè)直到第一按鈕中間有沒有按鈕
if (grid[x][i]!=0) {
k=0;
break;
}
else //K=1說明通過了第一次驗證
}
if (k==1) {
linePassOne();
}
}
if (yj){ //如果第二個按鈕的Y坐標(biāo)小于空按鈕的Y坐標(biāo)說明第一按鈕在第二按鈕右邊
for (i=y+1;i=j ;i++ ){ //判斷第二按鈕左側(cè)直到第一按鈕中間有沒有按鈕
if (grid[x][i]!=0){
k=0;
break;
}
else
}
if (k==1){
linePassOne();
}
}
if (y==j ) {
linePassOne();
}
}
if (k==2) {
if (x0==x) {
remove();
}
if (x0x) {
for (n=x0;n=x-1;n++ ) {
if (grid[n][j]!=0) {
k=0;
break;
}
if(grid[n][j]==0 n==x-1) {
remove();
}
}
}
if (x0x) {
for (n=x0;n=x+1 ;n-- ) {
if (grid[n][j]!=0) {
k=0;
break;
}
if(grid[n][j]==0 n==x+1) {
remove();
}
}
}
}
}
for (i=0;i8;i++ ) { //列
if (grid[i][y0]==0) {
if (xi) {
for (j=x-1;j=i ;j-- ) {
if (grid[j][y]!=0) {
k=0;
break;
}
else
}
if (k==1) {
rowPassOne();
}
}
if (xi) {
for (j=x+1;j=i;j++ ) {
if (grid[j][y]!=0) {
k=0;
break;
}
else
}
if (k==1) {
rowPassOne();
}
}
if (x==i) {
rowPassOne();
}
}
if (k==2){
if (y0==y) {
remove();
}
if (y0y) {
for (n=y0;n=y-1 ;n++ ) {
if (grid[i][n]!=0) {
k=0;
break;
}
if(grid[i][n]==0 n==y-1) {
remove();
}
}
}
if (y0y) {
for (n=y0;n=y+1 ;n--) {
if (grid[i][n]!=0) {
k=0;
break;
}
if(grid[i][n]==0 n==y+1) {
remove();
}
}
}
}
}
}
}
public void linePassOne(){
if (y0j){ //第一按鈕同行空按鈕在左邊
for (i=y0-1;i=j ;i-- ){ //判斷第一按鈕同左側(cè)空按鈕之間有沒按鈕
if (grid[x0][i]!=0) {
k=0;
break;
}
else //K=2說明通過了第二次驗證
}
}
if (y0j){ //第一按鈕同行空按鈕在與第二按鈕之間
for (i=y0+1;i=j ;i++){
if (grid[x0][i]!=0) {
k=0;
break;
}
else
}
}
}
public void rowPassOne(){
if (x0i) {
for (j=x0-1;j=i ;j-- ) {
if (grid[j][y0]!=0) {
k=0;
break;
}
else
}
}
if (x0i) {
for (j=x0+1;j=i ;j++ ) {
if (grid[j][y0]!=0) {
k=0;
break;
}
else
}
}
}
public void remove(){
firstButton.setVisible(false);
secondButton.setVisible(false);
fraction();
pressInformation=false;
k=0;
grid[x0][y0]=0;
grid[x][y]=0;
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==newlyButton){
int grid[][] = new int[8][7];
this.grid = grid;
randomBuild();
mainFrame.setVisible(false);
pressInformation=false;
init();
}
if(e.getSource()==exitButton)
System.exit(0);
if(e.getSource()==resetButton)
reload();
for(int cols = 0;cols 6;cols++){
for(int rows = 0;rows 5;rows++ ){
if(e.getSource()==diamondsButton[cols][rows])
estimateEven(cols+1,rows+1,diamondsButton[cols][rows]);
}
}
}
public static void main(String[] args) {
lianliankan llk = new lianliankan();
llk.randomBuild();
llk.init();
}
}
//old 998 lines
//new 318 lines
參考一下吧。
import?java.awt.FlowLayout;
import?java.awt.Font;
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
import?javax.swing.JButton;
import?javax.swing.JFrame;
import?javax.swing.JLabel;
public?class?Program?{
static?int?seconds?=?150;
private?TimeThread?tt?=?null;
private?boolean?ttFlag?=?false;
private?void?init()?{
final?JLabel?tip?=?new?JLabel();
final?JButton?start?=?new?JButton("開始");
final?JButton?end?=?new?JButton("結(jié)束");
JFrame?f?=?new?JFrame();
f.setLayout(new?FlowLayout(5));
f.add(tip);
f.add(start);
f.add(end);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setSize(300,?150);
f.setLocationRelativeTo(null);
start.addActionListener(new?ActionListener()?{
@Override
public?void?actionPerformed(ActionEvent?e)?{
start.setEnabled(false);
tip.setFont(new?Font("宋體",Font.BOLD,27));
ttFlag?=?true;
tt?=?new?TimeThread(tip);
tt.start();
}
});
end.addActionListener(new?ActionListener()?{
@Override
public?void?actionPerformed(ActionEvent?e)?{
start.setEnabled(true);
tip.setText("");
Program.seconds?=?150;
ttFlag?=?false;
}
});
}
/**
?*?@param?args
?*/
public?static?void?main(String[]?args)?{
new?Program().init();
}
class?TimeThread?extends?Thread?{
private?JLabel?tip;
TimeThread(JLabel?tip)?{
this.tip?=?tip;
}
@Override
public?void?run()?{
int?seconds?=?Program.seconds;
tip.setText(seconds+"");
while?(seconds--??0??ttFlag)?{
tip.setText(seconds+"");
try?{
Thread.sleep(1000);
}?catch?(InterruptedException?e)?{
e.printStackTrace();
}
}
}
};
}
我以前自己寫一個玩的。沒有把你要求的功能全部實現(xiàn),不過你看了后可以改一下就好了。參考一下吧,我給了注解:
package mybase.programe;
/*
* lianliankan總體算法思路:由兩個確定的按鈕。若這兩個按鈕的數(shù)字相等,就開始找它們相連的路經(jīng)。這個找路經(jīng)
* 分3種情況:(從下面的這三種情況,我們可以知道,需要三個檢測,這三個檢測分別檢測一條直路經(jīng)。這樣就會有
* 三條路經(jīng)。若這三條路經(jīng)上都是空按鈕,那么就剛好是三種直線(兩個轉(zhuǎn)彎點)把兩個按鈕連接起來了)
* 1.相鄰
*
* 2. 若不相鄰的先在第一個按鈕的同行找一個空按鈕。1).找到后看第二個按鈕橫向到這個空按鈕
* 所在的列是否有按鈕。2).沒有的話再看第一個按鈕到與它同行的那個空按鈕之間是否有按鈕。3).沒有的話,再從
* 與第一個按鈕同行的那個空按鈕豎向到與第二個按鈕的同行看是否有按鈕。沒有的話路經(jīng)就通了,可以消了.
*
* 3.若2失敗后,再在第一個按鈕的同列找一個空按鈕。1).找到后看第二個按鈕豎向到這個空按鈕所在的行是否有按鈕。
* 2).沒有的話,再看第一個按鈕到與它同列的那個空按鈕之間是否有按鈕。3).沒有的話,再從與第一個按鈕同列的
* 那個空按鈕橫向到與第二個按鈕同列看是否有按鈕。沒有的話路經(jīng)就通了,可以消了。
*
* 若以上三步都失敗,說明這兩個按鈕不可以消去。
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class LianLianKan implements ActionListener {
JFrame mainFrame; // 主面板
Container thisContainer;
JPanel centerPanel, southPanel, northPanel; // 子面板
JButton diamondsButton[][] = new JButton[6][5];// 游戲按鈕數(shù)組
JButton exitButton, resetButton, newlyButton; // 退出,重列,重新開始按鈕
JLabel fractionLable = new JLabel("0"); // 分?jǐn)?shù)標(biāo)簽
JButton firstButton, secondButton; // 分別記錄兩次被選中的按鈕
// 儲存游戲按鈕位置(這里其實只要6行,5列。但是我們用了8行,7列。是等于在這個面板按鈕的周圍還圍
//了一層是0的按鈕,這樣就可以實現(xiàn)靠近面板邊緣的兩個按鈕可以消去)
int grid[][] = new int[8][7];
static boolean pressInformation = false; // 判斷是否有按鈕被選中
int x0 = 0, y0 = 0, x = 0, y = 0, fristMsg = 0, secondMsg = 0, validateLV; // 游戲按鈕的位置坐標(biāo)
int i, j, k, n;// 消除方法控制
public void init() {
mainFrame = new JFrame("JKJ連連看");
thisContainer = mainFrame.getContentPane();
thisContainer.setLayout(new BorderLayout());
centerPanel = new JPanel();
southPanel = new JPanel();
northPanel = new JPanel();
thisContainer.add(centerPanel, "Center");
thisContainer.add(southPanel, "South");
thisContainer.add(northPanel, "North");
centerPanel.setLayout(new GridLayout(6, 5));
for (int cols = 0; cols 6; cols++) {
for (int rows = 0; rows 5; rows++) {
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個
randoms = (int) (Math.random() * 25 + 1);//button上的數(shù)字
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的數(shù)字
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; i 6; i++) {
for (int j = 0; j 5; 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 == secondMsg secondButton != firstButton) {
xiao();
}
}
}
public void xiao() { // 相同的情況下能不能消去。仔細(xì)分析,不一條條注釋
if ((x0 == x (y0 == y + 1 || y0 == y - 1))
|| ((x0 == x + 1 || x0 == x - 1) (y0 == y))) { // 判斷是否相鄰
remove();
} else {
for (j = 0; j 7; j++) {
if (grid[x0][j] == 0) { // 判斷和第一個按鈕同行的哪個按鈕為空
//如果找到一個為空的,就按列值的三種情況比較第二個按鈕與空按鈕的位置
if (y j) {//第二個按鈕在空按鈕右邊
for (i = y - 1; i = j; i--) { //檢測從第二個按鈕橫向左邊到空格所在列為止是否全是空格
if (grid[x][i] != 0) {
k = 0;
break;//存在非空格的就退出,這一退出就不可能k==2了,所以就會到下而215行出同理的判斷列
} else {
k = 1;
} // K=1說明全是空格通過了第一次驗證,也就是從第二個按鈕橫向左邊到空格所在列為止全是空格
}
if (k == 1) {
linePassOne();//進(jìn)入第二次驗證,也就是從第一個按鈕到它同行的空格之間的空格判斷
}
}
if (y j) { // 第二個按鈕在空按鈕左邊
for (i = y + 1; i = j; i++) {//檢測從第二個按鈕橫向右邊到空格所在列為止是否全是空格
if (grid[x][i] != 0) {
k = 0;
break;
} else {
k = 1;
}
}
if (k == 1) {
linePassOne();
}
}
if (y == j) {//第二個按鈕和空按鈕同列
linePassOne();
}
}
//第三次檢測,檢測確定為空的第j列的那個按鈕豎向到第二個按鈕,看是不是有按鈕
if (k == 2) {
if (x0 == x) {//第一,二按鈕在同行
remove();
}
if (x0 x) {//第一按鈕在第二按鈕下邊
for (n = x0; n = x - 1; n++) {//從空按鈕豎向到第二個按鈕所在行是否有按鈕
if (grid[n][j] != 0) {
k = 0;
break;
}
//沒有按鈕,說明這條路經(jīng)就通了
if (grid[n][j] == 0 n == x - 1) {
remove();
}
}
}
if (x0 x) {//第一按鈕在第二按鈕上邊
for (n = x0; n = x + 1; n--) {
if (grid[n][j] != 0) {
k = 0;
break;
}
if (grid[n][j] == 0 n == x + 1) {
remove();
}
}
}
}
}//-------------------------------------for
//當(dāng)上面的檢測與第一個按鈕同行的空格按鈕失敗后(不能找到與第二個按鈕的相連路經(jīng)),下面就執(zhí)行
//檢測與第一個按鈕同列的空格按鈕
for (i = 0; i 8; i++) {
if (grid[i][y0] == 0) {// 判斷和第一個按鈕同列的哪個按鈕為空
if (x i) {//第二個按鈕在這個空按鈕的下面
for (j = x - 1; j = i; j--) {
if (grid[j][y] != 0) {
k = 0;
break;
} else {
k = 1;
}
}
if (k == 1) {
rowPassOne();
}
}
if (x i) {//第二個按鈕在這個空按鈕的上面
for (j = x + 1; j = i; j++) {
if (grid[j][y] != 0) {
k = 0;
break;
} else {
k = 1;
}
}
if (k == 1) {
rowPassOne();
}
}
if (x == i) {//第二個按鈕與這個空按鈕同行
rowPassOne();
}
}
if (k == 2) {
if (y0 == y) {//第二個按鈕與第一個按鈕同列
remove();
}
if (y0 y) {//第二個按鈕在第一個按鈕右邊
for (n = y0; n = y - 1; n++) {
if (grid[i][n] != 0) {
k = 0;
break;
}
if (grid[i][n] == 0 n == y - 1) {
remove();
}
}
}
if (y0 y) {//第二個按鈕在第一個按鈕左邊
for (n = y0; n = y + 1; n--) {
if (grid[i][n] != 0) {
k = 0;
break;
}
if (grid[i][n] == 0 n == y + 1) {
remove();
}
}
}
}
}//--------------------------------for
}//-------------else
}//------------xiao
public void linePassOne() {
if (y0 j) { // 第一按鈕同行空按鈕在左邊
for (i = y0 - 1; i = j; i--) { // 判斷第一按鈕同左側(cè)空按鈕之間有沒按鈕
if (grid[x0][i] != 0) {
k = 0;
break;
} else {
k = 2;
} // K=2說明通過了第二次驗證
}
}
if (y0 j) { // 第一按鈕同行空按鈕在右邊
for (i = y0 + 1; i = j; i++) {
if (grid[x0][i] != 0) {
k = 0;
break;
} else {
k = 2;
}
}
}
}
public void rowPassOne() {
if (x0 i) {//第一個按鈕在與它同列的那個空格按鈕下面
for (j = x0 - 1; j = i; j--) {
if (grid[j][y0] != 0) {
k = 0;
break;
} else {
k = 2;
}
}
}
if (x0 i) {//第一個按鈕在與它同列的那個空格按鈕上面
for (j = x0 + 1; j = i; j++) {
if (grid[j][y0] != 0) {
k = 0;
break;
} else {
k = 2;
}
}
}
}
public void remove() {
firstButton.setVisible(false);
secondButton.setVisible(false);
fraction();
pressInformation = false;
k = 0;
grid[x0][y0] = 0;
grid[x][y] = 0;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == newlyButton) {
int grid[][] = new int[8][7];
this.grid = grid;
randomBuild();
mainFrame.setVisible(false);
pressInformation = false;
init();
}
if (e.getSource() == exitButton)
System.exit(0);
if (e.getSource() == resetButton)
reload();
for (int cols = 0; cols 6; cols++) {
for (int rows = 0; rows 5; rows++) {
if (e.getSource() == diamondsButton[cols][rows])
estimateEven(cols + 1, rows + 1, diamondsButton[cols][rows]);
}
}
}
public static void main(String[] args) {
LianLianKan llk = new LianLianKan();
llk.randomBuild();
llk.init();
}
}
連連看的代碼(基本算法)加了部分注釋
import java.awt.*;
import java.awt.event.*;
public class lianliankan implements ActionListener
{
static String s="no"; //用來紀(jì)錄點擊按鈕的信息
int x0=0,y0=0,x=0,y=0,n1=0,n2=0; //用來紀(jì)錄按鈕的位置信息
Frame f,f1;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10; //用比較笨的方法添加了
Button b11,b12,b13,b14,b15,b16,b17,b18; //30個按鈕來實現(xiàn)游戲界面
Button b19,b20,b21,b22,b23,b24,b25; //可以用數(shù)組實現(xiàn),這是本人
Button b26,b27,b28,b29,b30,bc; //學(xué)java時,入門的聯(lián)系,所以
Button b,ba,br,bt1,bt2; //有些東西很業(yè)余!!嘻嘻
Panel p1,p2,p3;
TextField t; //用來顯示一些隨機(jī)信息,方法是下面的guli().
Label l;
int d[][]={ //用來和界面的按鈕建立映射關(guān)系
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0}
};
public static void main(String[] args)
{
lianliankan t=new lianliankan();
t.suiji();
t.go();
}
public void actionPerformed(ActionEvent e) //再來一次按鈕的響應(yīng)事件。
{
int d[][]={
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0}
};
this.d=d;
suiji();
f.setVisible(false);
f1.setVisible(false);
s="no";
go();
}
public void go()//初始化界面
{
l=new Label("親愛的玩家,");
f=new Frame("連連看");
t=new TextField();
p2=new Panel();
p1=new Panel();
p3=new Panel();
bc=new Button("退出");
br=new Button("重列");
b=new Button();
b1=new Button(String.valueOf(d[1][1]));
b2=new Button(String.valueOf(d[1][2]));
b3=new Button(String.valueOf(d[1][3]));
b4=new Button(String.valueOf(d[1][4]));
b5=new Button(String.valueOf(d[1][5]));
b6=new Button(String.valueOf(d[2][1]));
b7=new Button(String.valueOf(d[2][2]));
b8=new Button(String.valueOf(d[2][3]));
b9=new Button(String.valueOf(d[2][4]));
b10=new Button(String.valueOf(d[2][5]));
b11=new Button(String.valueOf(d[3][1]));
b12=new Button(String.valueOf(d[3][2]));
b13=new Button(String.valueOf(d[3][3]));
b14=new Button(String.valueOf(d[3][4]));
b15=new Button(String.valueOf(d[3][5]));
b16=new Button(String.valueOf(d[4][1]));
b17=new Button(String.valueOf(d[4][2]));
b18=new Button(String.valueOf(d[4][3]));
b19=new Button(String.valueOf(d[4][4]));
b20=new Button(String.valueOf(d[4][5]));
b21=new Button(String.valueOf(d[5][1]));
b22=new Button(String.valueOf(d[5][2]));
b23=new Button(String.valueOf(d[5][3]));
b24=new Button(String.valueOf(d[5][4]));
b25=new Button(String.valueOf(d[5][5]));
b26=new Button(String.valueOf(d[6][1]));
b27=new Button(String.valueOf(d[6][2]));
b28=new Button(String.valueOf(d[6][3]));
b29=new Button(String.valueOf(d[6][4]));
b30=new Button(String.valueOf(d[6][5]));
p3.setLayout(null);
p1.setSize(250,300);
p2.setSize(100,40);
p3.setSize(300,30);
t.setSize(60,30);
l.setSize(70,30);
p1.setLayout(new GridLayout(6,5));
p1.setBackground(Color.pink);
p1.setLocation(100,100);
p2.setLocation(0,400);
p3.setLocation(50,50);
t.setLocation(230,2);
l.setLocation(150,2);
bc.setLocation(0,40);
br.setLocation(0,100);
f.add(p1);
f.add(p2);
f.add(p3);
p3.add(l);
p3.add(t);
p2.add(bc);
p2.add(br);
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b4);
p1.add(b5);
p1.add(b6);
p1.add(b7);
p1.add(b8);
p1.add(b9);
p1.add(b10);
p1.add(b11);
p1.add(b12);
p1.add(b13);
p1.add(b14);
p1.add(b15);
p1.add(b16);
p1.add(b17);
p1.add(b18);
p1.add(b19);
p1.add(b20);
p1.add(b21);
p1.add(b22);
p1.add(b23);
p1.add(b24);
p1.add(b25);
p1.add(b26);
p1.add(b27);
p1.add(b28);
p1.add(b29);
p1.add(b30);
f.pack();
f.setBounds(280,100,500,450);
f.setResizable(false);
f.setVisible(true);
bc.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
ex();
}
});
br.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
chonglie();
}
});
b1.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(1,1,b1);
}
});
b2.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(1,2,b2);
}
});
b3.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(1,3,b3);
}
});
b4.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(1,4,b4);
}
});
b5.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(1,5,b5);
}
});
b6.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(2,1,b6);
}
});
b7.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(2,2,b7);
}
});
b8.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(2,3,b8);
}
});
b9.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(2,4,b9);
}
});
b10.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(2,5,b10);
}
});
b11.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(3,1,b11);
}
});
b12.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(3,2,b12);
}
});
b13.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(3,3,b13);
}
});
b14.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(3,4,b14);
}
});
b15.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(3,5,b15);
}
});
b16.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(4,1,b16);
}
});
b17.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(4,2,b17);
}
});
b18.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(4,3,b18);
}
});
b19.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(4,4,b19);
}
});
b20.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(4,5,b20);
}
});
b21.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(5,1,b21);
}
});
b22.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(5,2,b22);
}
});
b23.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(5,3,b23);
}
});
b24.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(5,4,b24);
}
});
b25.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(5,5,b25);
}
});
b26.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(6,1,b26);
}
});
b27.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(6,2,b27);
}
});
b28.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(6,3,b28);
}
});
b29.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(6,4,b29);
}
});
b30.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(6,5,b30);
}
});
}
public void ex() //退出界面,可用diolog來實現(xiàn)有模式的類型,更加符合
{
f1=new Frame("游戲作業(yè)");
f1.setLayout(new GridLayout(1,1));
bt1=new Button("確定退出");
bt2=new Button("再來一局");
f1.add(bt1);
f1.add(bt2);
f1.pack();
f1.setBounds(400,250,90,60);
f1.setResizable(false);
f1.show();
f1.setVisible(true);
bt1.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
System.exit(0);
}
});
bt2.addActionListener(this);
}
public void suiji() //產(chǎn)生隨機(jī)數(shù),來填充游戲界面對應(yīng)的數(shù)組的各個位置
{
int m,n,k=0,k1,k2,k3;
for(m=1;m=15;m++)
{
k1=(int)(Math.random()*25+1);
for(n=1;n=2;n++)
{
k2=(int)(Math.random()*6+1);
k3=(int)(Math.random()*5+1);
while(d[k2][k3]!=0 k!=30)
{
k2=(int)(Math.random()*6+1);
k3=(int)(Math.random()*5+1);
}
this.d[k2][k3]=k1;
k++;
}
}
}
public void guli() //隨機(jī)信息
{
int l=0;
t.setText("");
l=(int)(Math.random()*10);
System.out.println(l);
switch(l)
{
case 1:
t.setText("好!加油!");
break;
case 3:
t.setText("你真棒!");
break;
case 5:
t.setText("加快速度!");
break;
case 6:
t.setText("不錯啊!");
break;
case 8:
t.setText("加油吧!");
break;
case 9:
t.setText("夠聰明!");
break;
default:
break;
}
}
public void chonglie() //重列方法
{
int save[],i,j,n=0,k2,k3,k;
int d[][]={
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0}
};
save=new int[30];
for(n=0;n30;n++)
save[n]=0; //定義一個數(shù)組來保存當(dāng)前的每個按鈕位置上的信息
n=0;
for(i=0;i=6;i++)
for(j=0;j=5;j++)
{
if(this.d[i][j]!=0)
{
save[n]=this.d[i][j];
n++;
}
}
n=n-1;
this.d=d;
while(n=0) //產(chǎn)生隨機(jī)位置,放置按鈕
{
k2=(int)(Math.random()*6+1);
k3=(int)(Math.random()*5+1);
while(d[k2][k3]!=0)
{
k2=(int)(Math.random()*6+1);
k3=(int)(Math.random()*5+1);
}
this.d[k2][k3]=save[n];
n--;
}
f.setVisible(false);
s="no"; //這里一定要將按鈕點擊信息歸為初始
go();
ling();
}
public void ling() //將數(shù)組中為零的成員對應(yīng)的按鈕消去
{ //用按鈕類型的數(shù)組實現(xiàn)會簡化得多,
if(d[1][1]==0)
b1.setVisible(false);
if(d[1][2]==0)
b2.setVisible(false);
if(d[1][3]==0)
b3.setVisible(false);
if(d[1][4]==0)
b4.setVisible(false);
if(d[1][5]==0)
b5.setVisible(false);
if(d[2][1]==0)
b6.setVisible(false);
if(d[2][2]==0)
b7.setVisible(false);
if(d[2][3]==0)
b8.setVisible(false);
if(d[2][4]==0)
b9.setVisible(false);
if(d[2][5]==0)
b10.setVisible(false);
if(d[3][1]==0)
b11.setVisible(false);
if(d[3][2]==0)
b12.setVisible(false);
if(d[3][3]==0)
b13.setVisible(false);
if(d[3][4]==0)
b14.setVisible(false);
if(d[3][5]==0)
b15.setVisible(false);
if(d[4][1]==0)
b16.setVisible(false);
if(d[4][2]==0)
b17.setVisible(false);
if(d[4][3]==0)
b18.setVisible(false);
if(d[4][4]==0)
b19.setVisible(false);
if(d[4][5]==0)
b20.setVisible(false);
if(d[5][1]==0)
b21.setVisible(false);
if(d[5][2]==0)
b22.setVisible(false);
if(d[5][3]==0)
b23.setVisible(false);
if(d[5][4]==0)
b24.setVisible(false);
if(d[5][5]==0)
b25.setVisible(false);
if(d[6][1]==0)
b26.setVisible(false);
if(d[6][2]==0)
b27.setVisible(false);
if(d[6][3]==0)
b28.setVisible(false);
if(d[6][4]==0)
b29.setVisible(false);
if(d[6][5]==0)
b30.setVisible(false);
}
public void wei(int w1,int w2,Button bz) //判斷并紀(jì)錄每次點擊按鈕的信息
{ //當(dāng)兩次的按鈕相同才能消去
if((s.trim()).equals("no"))
{
s=b1.getLabel();
x0=w1;
y0=w2;
n1=d[x0][y0];
b=bz;
x=w1;
y=w2;
n2=d[x][y];
ba=bz;
}
else
{
x0=x;
y0=y;
n1=d[x0][y0];
b=ba;
x=w1;
y=w2;
n2=d[x][y];
ba=bz;
if(n1==n2 ba!=b)
{
xiao();
}
}
}
public void xiao() //這里是整個游戲最重要的部分,就是判斷兩個按鈕在信息
{ //相同的情況下能不能消去。仔細(xì)分析,不一條條注釋
int i=0, j=0,n=0,k=0;
if((x0==x (y0==y+1||y0==y-1)) || ((x0==x+1||x0==x-1)(y0==y))) //相鄰的情況
{
ba.setVisible(false);
b.setVisible(false);
guli();
s="no";
d[x0][y0]=0;
d[x][y]=0;
}
else
{
for (j=0;j7;j++ ) //兩個按鈕按行分析,看能否消去
{
if (d[x0][j]==0)
{
if (yj)
{
for (i=y-1;i=j;i-- )
{
if (d[x][i]!=0)
{
k=0;
break;
}
else
{
k=1;
}
}
if (k==1)
{
if (y0j)
{
for (i=y0-1;i=j ;i-- )
{
if (d[x0][i]!=0)
{
k=0;
break;
}
else
{
k=2;
}
}
}
if (y0j)
{
for (i=y0+1;i=j ;i++)
{
if (d[x0][i]!=0)
{
k=0;
break;
}
else
{
k=2;
}
}
}
}
}
if (yj)
{
for (i=y+1;i=j ;i++ )
{
if (d[x][i]!=0)
{
k=0;
break;
}
else
{
k=1;
}
}
if (k==1)
{
if (y0j)
{
for (i=y0-1;i=j ;i-- )
{
if (d[x0][i]!=0)
{
k=0;
break;
}
else
{
k=2;
}
}
}
if (y0j)
{
for (i=y0+1;i=j ;i++)
{
if (d[x0][i]!=0)
{
k=0;
break;
}
else
{
k=2;
}
}
}
}
}
if (y==j )
{
if (y0j)
{
for (i=y0-1;i=j ;i-- )
{
if (d[x0][i]!=0)
{
k=0;
break;
}
else
{
k=2;
}
}
}
if (y0j)
{
for (i=y0+1;i=j ;i++)
{
if (d[x0][i]!=0)
{
k=0;
break;
}
else
{
k=2;
}
}
}
}
}
if (k==2)
{ if (x0==x)
{
b.setVisible(false);
ba.setVisible(false);
guli();
s="no";
k=0;
d[x0][y0]=0;
d[x][y]=0;
}
if (x0x)
{
for (n=x0;n=x-1;n++ )
{
if (d[n][j]!=0)
{
k=0;
break;
}
if(d[n][j]==0 n==x-1)
{
b.setVisible(false);
ba.setVisible(false);
guli();
s="no";
k=0;
d[x0][y0]=0;
d[x][y]=0;
}
}
}
if (x0x)
{
for (n=x0;n=x+1 ;n-- )
{
if (d[n][j]!=0)
{
k=0;
break;
}
if(d[n][j]==0 n==x+1)
{
b.setVisible(false);
ba.setVisible(false);
guli();
s="no";
k=0;
d[x0][y0]=0;
d[x][y]=0;
}
}
}
}
}
for (i=0;i8;i++ ) //按列分析,看能不能消去
{
if (d[i][y0]==0)
{
if (xi)
{
for (j=x-1;j=i ;j-- )
{
if (d[j][y]!=0)
{
k=0;
break;
}
else
{
k=1;
}
}
if (k==1)
{
if (x0i)
{
for (j=x0-1;j=i ;j-- )
{
if (d[j][y0]!=0)
{
k=0;
break;
}
else
{
k=2;
}
}
}
if (x0i)
{
for (j=x0+1;j=i;j++ )
{
if (d[j][y0]!=0)
{
k=0;
break;
}
else
{
k=2;
}
}
}
}
}
if (xi)
{
for (j=x+1;j=i;j++ )
{
if (d[j][y]!=0)
{
k=0;
break;
}
else
{
k=1;
}
}
if (k==1)
{
if (x0i)
{
for (j=x0-1;j=i ;j-- )
{
if (d[j][y0]!=0)
{
k=0;
break;
}
else
{
k=2;
}
}
}
if (x0i)
{
for (j=x0+1;j=i ;j++ )
{
if (d[j][y0]!=0)
{
k=0;
break;
}
else
{
k=2;
}
}
}
}
}
if (x==i)
{
if (x0i)
{
for (j=x0-1;j=i ;j-- )
{
if (d[j][y0]!=0)
{
k=0;
break;
}
else
{
k=2;
}
}
}
if (x0i)
{
for (j=x0+1;j=i ;j++ )
{
if (d[j][y0]!=0)
{
k=0;
break;
}
else
{
k=2;
}
}
}
}
}
if (k==2)
{
if (y0==y)
{
b.setVisible(false);
ba.setVisible(false);
guli();
s="no";
k=0;
d[x0][y0]=0;
d[x][y]=0;
}
if (y0y)
{
for (n=y0;n=y-1 ;n++ )
{
if (d[i][n]!=0)
{
k=0;
break;
}
if(d[i][n]==0 n==y-1)
{
b.setVisible(false);
ba.setVisible(false);
guli();
s="no";
k=0;
d[x0][y0]=0;
d[x][y]=0;
}
}
}
if (y0y)
{
for (n=y0;n=y+1 ;n--)
{
if (d[i][n]!=0)
{
k=0;
break;
}
if(d[i][n]==0 n==y+1)
{
b.setVisible(false);
ba.setVisible(false);
guli();
s="no";
k=0;
d[x0][y0]=0;
d[x][y]=0;
}
}
}
}
}
}
}
}
剛試了。。測試通過。。
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
publicclass LianLianKan implements ActionListener {
JFrame mainFrame; // 主面板
Container thisContainer;
JPanel centerPanel, southPanel, northPanel; //子面板
JButton diamondsButton[][] = newJButton[6][5];// 游戲按鈕數(shù)組
JButton exitButton, resetButton, newlyButton;// 退出,重列,重新開始按鈕
JLabel fractionLable = newJLabel("0"); // 分?jǐn)?shù)標(biāo)簽
JButton firstButton, secondButton; // 分別記錄兩次被選中的按鈕
// 儲存游戲按鈕位置(這里其實只要6行,5列。但是我們用了8行,7列。是等于在這個面板按鈕的周圍還圍
//了一層是0的按鈕,這樣就可以實現(xiàn)靠近面板邊緣的兩個按鈕可以消去)
int grid[][] = new int[8][7];
static boolean pressInformation = false; // 判斷是否有按鈕被選中
int x0 = 0, y0 = 0, x = 0, y = 0, fristMsg =0, secondMsg = 0, validateLV; // 游戲按鈕的位置坐標(biāo)
int i, j, k, n;// 消除方法控制
public void init() {
mainFrame = new JFrame("JKJ連連看");
thisContainer = mainFrame.getContentPane();
thisContainer.setLayout(new BorderLayout());
centerPanel = new JPanel();
southPanel = new JPanel();
northPanel = new JPanel();
thisContainer.add(centerPanel,"Center");
thisContainer.add(southPanel,"South");
thisContainer.add(northPanel,"North");
centerPanel.setLayout(new GridLayout(6, 5));
for (int cols = 0; cols 6; cols++) {
for (int rows = 0; rows 5; rows++) {
diamondsButton[cols][rows] = newJButton(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個
randoms = (int) (Math.random() * 25 +1);//button上的數(shù)字
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的數(shù)字
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; i 6; i++) {
for (int j = 0; j 5; j++) {
if (grid[i + 1][j + 1] == 0)
diamondsButton[i][j].setVisible(false);
}
}
}
public void estimateEven(int placeX, intplaceY, 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 == secondMsg secondButton != firstButton) {
xiao();
}
}
}
public void xiao() { // 相同的情況下能不能消去。仔細(xì)分析,不一條條注釋
if ((x0 == x (y0 == y + 1 || y0 ==y - 1))
|| ((x0 == x + 1 || x0 == x - 1) (y0 == y))) { // 判斷是否相鄰
remove();
} else {
for (j = 0; j 7; j++) {
if (grid[x0][j] == 0) { // 判斷和第一個按鈕同行的哪個按鈕為空
//如果找到一個為空的,就按列值的三種情況比較第二個按鈕與空按鈕的位置
if (y j) {//第二個按鈕在空按鈕右邊
for (i = y - 1; i = j; i--) { //檢測從第二個按鈕橫向左邊到空格所在列為止是否全是空格
if (grid[x][i] != 0) {
k = 0;
break;//存在非空格的就退出,這一退出就不可能k==2了,所以就會到下而215行出同理的判斷列
} else {
k = 1;
} // K=1說明全是空格通過了第一次驗證,也就是從第二個按鈕橫向左邊到空格所在列為止全是空格
}
if (k == 1) {
linePassOne();//進(jìn)入第二次驗證,也就是從第一個按鈕到它同行的空格之間的空格判斷
}
}
if (y j) { // 第二個按鈕在空按鈕左邊
for (i = y + 1; i = j; i++) {//檢測從第二個按鈕橫向右邊到空格所在列為止是否全是空格
if (grid[x][i] != 0) {
k = 0;
break;
} else {
k = 1;
}
}
if (k == 1) {
linePassOne();
}
}
if (y == j) {//第二個按鈕和空按鈕同列
linePassOne();
}
}
//第三次檢測,檢測確定為空的第j列的那個按鈕豎向到第二個按鈕,看是不是有按鈕
if (k == 2) {
if (x0 == x) {//第一,二按鈕在同行
remove();
}
if (x0 x) {//第一按鈕在第二按鈕下邊
for (n = x0; n = x - 1; n++) {//從空按鈕豎向到第二個按鈕所在行是否有按鈕
if (grid[n][j] != 0) {
k= 0;
break;
}
//沒有按鈕,說明這條路經(jīng)就通了
if (grid[n][j] == 0 n == x -1) {
remove();
}
}
}
if (x0 x) {//第一按鈕在第二按鈕上邊
for (n = x0; n = x + 1; n--) {
if (grid[n][j] != 0) {
k = 0;
break;
}
if (grid[n][j] == 0 n == x +1) {
remove();
}
}
}
}
}//-------------------------------------for
//當(dāng)上面的檢測與第一個按鈕同行的空格按鈕失敗后(不能找到與第二個按鈕的相連路經(jīng)),下面就執(zhí)行
//檢測與第一個按鈕同列的空格按鈕
for (i = 0; i 8; i++) {
if (grid[i][y0] == 0) {// 判斷和第一個按鈕同列的哪個按鈕為空
if (x i) {//第二個按鈕在這個空按鈕的下面
for (j = x - 1; j = i; j--) {
if (grid[j][y] != 0) {
k = 0;
break;
} else {
k = 1;
}
}
if (k == 1) {
rowPassOne();
}
}
if (x i) {//第二個按鈕在這個空按鈕的上面
for (j = x + 1; j = i; j++) {
if (grid[j][y] != 0) {
k = 0;
break;
} else {
k = 1;
}
}
if (k == 1) {
rowPassOne();
}
}
if (x == i) {//第二個按鈕與這個空按鈕同行
rowPassOne();
}
}
if (k == 2) {
if (y0 == y) {//第二個按鈕與第一個按鈕同列
remove();
}
if (y0 y) {//第二個按鈕在第一個按鈕右邊
for (n = y0; n = y - 1; n++) {
if (grid[i][n] != 0) {
k = 0;
break;
}
if (grid[i][n] == 0 n == y -1) {
remove();
}
}
}
if (y0 y) {//第二個按鈕在第一個按鈕左邊
for (n = y0; n = y + 1; n--) {
if (grid[i][n] != 0) {
k = 0;
break;
}
if (grid[i][n] == 0 n == y +1) {
remove();
}
}
}
}
}//--------------------------------for
}//-------------else
}//------------xiao
public void linePassOne() {
if (y0 j) { // 第一按鈕同行空按鈕在左邊
for (i = y0 - 1; i = j; i--) { // 判斷第一按鈕同左側(cè)空按鈕之間有沒按鈕
if (grid[x0][i] != 0) {
k = 0;
break;
} else {
k = 2;
} // K=2說明通過了第二次驗證
}
}
if (y0 j) { // 第一按鈕同行空按鈕在右邊
for (i = y0 + 1; i = j; i++) {
if (grid[x0][i] != 0) {
k = 0;
break;
} else {
k = 2;
}
}
}
}
public void rowPassOne() {
if (x0 i) {//第一個按鈕在與它同列的那個空格按鈕下面
for (j = x0 - 1; j = i; j--) {
if (grid[j][y0] != 0) {
k = 0;
break;
} else {
k = 2;
}
}
}
if (x0 i) {//第一個按鈕在與它同列的那個空格按鈕上面
for (j = x0 + 1; j = i; j++) {
if (grid[j][y0] != 0) {
k = 0;
break;
} else {
k = 2;
}
}
}
}
public void remove() {
firstButton.setVisible(false);
secondButton.setVisible(false);
fraction();
pressInformation = false;
k = 0;
grid[x0][y0] = 0;
grid[x][y] = 0;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == newlyButton) {
int grid[][] = new int[8][7];
this.grid = grid;
randomBuild();
mainFrame.setVisible(false);
pressInformation = false;
init();
}
if (e.getSource() == exitButton)
System.exit(0);
if (e.getSource() == resetButton)
reload();
for (int cols = 0; cols 6; cols++) {
for (int rows = 0; rows 5; rows++) {
if (e.getSource() ==diamondsButton[cols][rows])
estimateEven(cols + 1, rows + 1,diamondsButton[cols][rows]);
}
}
}
public static void main(String[] args) {
LianLianKan llk = new LianLianKan();
llk.randomBuild();
llk.init();
}
}