import java.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.util.Random;import javax.swing.*;public class lianliankan implementsActionListener {
JFrame mainFrame;
Container thisContainer;
JPanel centerPanel,southPanel,northPanel;
JButton diamondsButton[][]= new JButton[6][5];
JButton exitButton,resetButton,newlyButton;
JLabel fractionLabel= new JLabel("0");
JButton firstButton,secondButton;int i=0,j=0,k;int grid[][] = new int[6][5];static boolean pressInformation = false;int x0=0,y0=0,x=0,y=0,firstMsg=0,secondMsg=0,calidateLV;public voidinit(){
mainFrame= new JFrame("JKJ连连看");
thisContainer=mainFrame.getContentPane();
thisContainer.setLayout(newBorderLayout());
centerPanel= newJPanel();
southPanel= newJPanel();
northPanel= newJPanel();
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][rows]));
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);
fractionLabel.setText(String.valueOf(Integer.parseInt(fractionLabel.getText())));
northPanel.add(fractionLabel);
mainFrame.setBounds(280,100,500,450);
mainFrame.setVisible(true);
}public voidrandomBuild(){int randoms = 0,rows = 0,cols = 0;for(int twins=1; twins<=15;twins++){
Random rnd= newRandom();
randoms=rnd.nextInt(25)+1;for(int alike=1; alike<=2; alike++){
cols=rnd.nextInt(6);
rows=rnd.nextInt(5);while(grid[cols][rows]!=0){
cols=rnd.nextInt(6);
rows=rnd.nextInt(5);
}this.grid[cols][rows] =randoms;
}
}
}public voidfraction(){
fractionLabel.setText(String.valueOf(Integer.parseInt(fractionLabel.getText())+100));
}public voidreload(){int save[] = new int[30];int n=0,cols,rows;int grid[][] = new int[6][5];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){
Random rnd= newRandom();
cols=rnd.nextInt(6);
rows=rnd.nextInt(5);while(grid[cols][rows]!=0){
cols=rnd.nextInt(6);
rows=rnd.nextInt(5);
}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][j]==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;
firstMsg=secondMsg;
firstButton=secondButton;
x=placeX;
y=placeY;
secondMsg=grid[x][y];
secondButton=bz;if(firstMsg==secondMsg&&secondButton!=firstButton){
xiao();
}
}
}public void xiao(){ //内容相同的情况下能不能消去
intn;if(x0==x&&(y0==y+1||y0==y-1)||((x0==x+1||x0==x-1)&&(y0==y)))
remove();else{for(j=0; j<5; j++){if(grid[x0][j]==0){//判断第一个按钮同行哪个按钮为空
if(y>j){//吐过第二个按钮的Y坐标大于空按钮的Y坐标说明第一个按钮在第二个的左边
for(i=y-1; i>=j; i--){//判断第二按钮左侧知道第一按钮中间有没有按钮
if(grid[x][i]!=0){
k=0;break;
}elsek=1;//k=1说明通过了第一次验证
}if(k==1){
linePassOne();
}
}if(y
for(i=y+1;i<=j;i++){//判断第二按钮错侧直到第一个按钮的中间有木有按钮
if(grid[x][i]!=0){
k=0;break;
}elsek=1;
}if(k==1)
linePassOne();
}if(y==j)
linePassOne();
}if(k==2){//这里原本是k==2
if(x0==x){
remove();
}if(x0
k=0;break;
}if(grid[n][j]==0&&n==x-1){
remove();
}
}
}if(x>x0){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;i<6;i++){//列
if(grid[i][y0]==0){if(x>i){for(j=x-1; j>=i; j--){if(grid[j][y]!=0){
k=0;break;
}elsek=1;
}if(k==1){
rowPassOne();
}
}if(x
k=0;break;
}elsek=1;
}if(k==1)
rowPassOne();
}if(x==i){
rowPassOne();
}
}if(k==2){//原本是k==2
if(y0==y){
remove();
}if(y0
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();
}
}
}
}
}
}private voidlinePassOne(){if(y0>j){//第一行同行按钮在左边
for(i=y0-1; i>=j; i--){//判断第一按钮左侧空按钮之间有木有按钮
if(grid[x0][i]!=0){
k=0;break;
}elsek=2;//k=2说明通过了第二次验证
}
}if(y0
for(i=y0+1; i<=j; i++){if(grid[x0][i]!=0){
k=0;break;
}elsek=2;
}
}
}private voidrowPassOne() {//TODO Auto-generated method stub
if(x0>i){for(j=x0-1; j>=i; j--){if(grid[j][y0]!=0){
k=0;break;
}elsek=2;
}
}if(x0
k=0;break;
}elsek=2;
}
}
}public voidremove(){
firstButton.setVisible(false);
secondButton.setVisible(false);
fraction();
pressInformation=false;
k=0;
grid[x0][y0]=0;
grid[x][y]=0;
}
@Overridepublic voidactionPerformed(ActionEvent e) {//TODO Auto-generated method stub
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,rows,diamondsButton[cols][rows]);
}
}
}
}public static voidmain(String args[]){
lianliankan llk= newlianliankan();
llk.randomBuild();
llk.init();
}
}