在显示界面中,如果用户可以把两个同样的图用线(连线的弯不能多于两个,或者说两个图之间的线段不能超过3条)连在一起,那么这两个图象就会消掉,当所有的头象全部消掉后游戏成功结束。
package com.detail.algorithm;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class LianLianKan2 implements ActionListener{
JFrame mainFrame; //主面板
Container thisContainer;
JPanel centerPanel,southPanel,northPanel; //子面板
JButton buttons[][] = new JButton[6][5];//游戏按钮数组
JButton exitButton,resetButton,newlyButton; //退出,重列,重新开始按钮
JLabel scoreLable=new JLabel("0"); //分数标签
JButton firstButton,secondButton; //分别记录两次被选中的按钮
int buttonLocation[][] = new int[8][7];//储存游戏按钮位置
static boolean isAnyButtonPressed=false; //判断是否有按钮被选中
int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戏按钮的位置坐标
int blanki,blankj,n;//消除方法控制
static Random random = new Random();
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 rows = 0;rows < 6;rows++){
for(int cols = 0;cols < 5;cols++ ){
buttons[rows][cols]=new JButton(String.valueOf(buttonLocation[rows+1][cols+1]));
buttons[rows][cols].addActionListener(this);
centerPanel.add(buttons[rows][cols]);
}
}
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);
scoreLable.setText(String.valueOf(Integer.parseInt(scoreLable.getText())));
northPanel.add(scoreLable);
mainFrame.setBounds(280,100,500,450);
mainFrame.setVisible(true);
}
public void assignNumber(){
int rows,cols,randNum;
for(int j=0;j<15;j++){
randNum = (int)(Math.random() * 30 + 1);
for(int m=0;m<2;m++){
rows = (int)(Math.random()*6+1);
cols = (int)(Math.random()*5+1);
while(buttonLocation[rows][cols]!=0){
rows = (int)(Math.random()*6+1);
cols = (int)(Math.random()*5+1);
}
buttonLocation[rows][cols] = randNum;
}
}
}
public void rebuild(){
int[] Num = new int[30];
int index=0;
for(int rows = 1;rows < 7;rows++){
for(int cols = 1;cols < 6;cols++ ){
Num[index] = buttonLocation[rows][cols];
System.out.print(Num[index]+" ");
index++;
}
}
System.out.println();
Num = reOrder(Num);
int mm=0;
for(int rows = 1;rows < 7;rows++){
for(int cols = 1;cols < 6;cols++ ){
buttonLocation[rows][cols] = Num[mm];
mm++;
}
}
mainFrame.setVisible(false);
this.isAnyButtonPressed=false;
init();
for(int row = 0;row < 6;row++){
for(int col = 0;col < 5;col++ ){
if(buttonLocation[row+1][col+1]==0){
buttons[row][col].setVisible(false);
}
}
}
}
public void ButtonPressed(int rows,int cols,JButton currButton){
if(!isAnyButtonPressed){
x = rows;
y = cols;
secondButton = currButton;
secondMsg = buttonLocation[x][y];
//System.out.println("x="+x+" y="+y+" secondMsg="+secondMsg);
isAnyButtonPressed = true;
}else{
x0 = x;
y0 = y;
x = rows;
y = cols;
firstButton = secondButton;
secondButton = currButton;
fristMsg = secondMsg;
secondMsg = buttonLocation[x][y];
if(fristMsg == secondMsg && firstButton != secondButton)
DiXiao();
}
}
public void DiXiao(){
if(edgeMatch(x0,y0,x,y))
remove();
else if(horizonMatch(x0,y0,x,y) || VerticalMatch(x0,y0,x,y)){
remove();
}
else if(twoStepConnect(x0,y0,x,y)){
remove();
}
else if(threeStepConnect(x0,y0,x,y)){
remove();
}
}
public boolean edgeMatch(int x0,int y0,int x,int y){
//System.out.println("x0="+x0+" y0="+y0+" x="+x+" y="+y);
if((y0==1 && y==1) || (y0==5 && y==5) || (x0==1 && x==1) || (x0==6 && x==6))
return true;
else
return false;
}
public boolean VerticalMatch(int x0,int y0,int x,int y){
System.out.println("Vertical x0="+x0+" y0="+y0+" x="+x+" y="+y);
boolean isMatch = false;
if(y0==y && (x0==x+1 || x0==x-1)){
isMatch = true;
}
if(x0+1<x && y0==y){
for(int i=x0+1;i<x;i++){
if(buttonLocation[i][y0]!=0){
isMatch = false;
break;
}
else if(buttonLocation[i][y0]==0 && i==x-1)
isMatch = true;
}
}
if(x+1<x0 && y0==y){
for(int i=x+1;i<x0;i++){
if(buttonLocation[i][y0]!=0){
isMatch = false;
break;
}
else if(buttonLocation[i][y0]==0 && i==x0-1){
isMatch = true;
}
}
}
//System.out.println("isMatch="+isMatch);
return isMatch;
}
public boolean horizonMatch(int x0,int y0,int x,int y){
System.out.println("horizon x0="+x0+" y0="+y0+" x="+x+" y="+y);
boolean isMatch = false;
if(x0==x && (y0==y+1 || y0==y-1)){
isMatch = true;
}
if(y0+1<y && x0==x){
for(int i=y0+1;i<y;i++){
if(buttonLocation[x0][i]!=0){
isMatch = false;
break;
}
else if(buttonLocation[x0][i]==0 && i==y-1)
isMatch = true;
}
}
if(y+1<y0 && x0==x){
for(int i=y+1;i<y0;i++){
if(buttonLocation[x0][i]!=0){
isMatch = false;
break;
}
else if(buttonLocation[x0][i]==0 && i==y0-1){
isMatch = true;
}
}
}
//System.out.println("isMatch="+isMatch);
return isMatch;
}
public boolean twoStepConnect(int x0,int y0,int x,int y){
boolean isMatch = false;
int xc = x;
int yc = y0;
int xd = x0;
int yd = y;
System.out.println("twoStepConnect x0="+x0+" y0="+y0+" x="+x+" y="+y);
if((VerticalMatch(x0,y0,xc,yc) && horizonMatch(xc,yc,x,y)) || (horizonMatch(x0,y0,xd,yd) && VerticalMatch(xd,yd,x,y)))
isMatch = true;
return isMatch;
}
public boolean threeStepConnect(int x0,int y0,int x,int y){
boolean isMatch = false;
int colindex=1;
//while(colindex<7){//先列后行
for(int i=1;i<6;i++){
if(buttonLocation[x0][i]==0){
System.out.println("空格坐标为:x0="+x0+" y0="+i+" x="+x+" y="+y);
if(twoStepConnect(x0,i,x,y))
isMatch = true;
}
}
//}
int rowindex=1;
if(!isMatch){
//while(rowindex<8){
for(int j=1;j<7;j++){
if(buttonLocation[j][y0]==0){
System.out.println("空格坐标为:x0="+j+" y0="+y0+" x="+x+" y="+y);
if(twoStepConnect(j,y0,x,y))
isMatch = true;
}
}
//}
}
return isMatch;
}
public void remove(){
firstButton.setVisible(false);
secondButton.setVisible(false);
isAnyButtonPressed=false;
addScore();
buttonLocation[x0][y0]=0;
buttonLocation[x][y]=0;
}
public void addScore(){
scoreLable.setText(Integer.parseInt(scoreLable.getText())+100+"");
}
public int[] reOrder(int[] arr){
int rad;
int temp;
for(int i=0;i<arr.length;i++){
rad = (int)(Math.random()*arr.length);
temp = arr[rad];
arr[rad] = arr[i];
arr[i] = temp;
}
return arr;
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==resetButton){//点击重列按钮
rebuild();
}
if(e.getSource()==exitButton){
System.exit(0);
}
if(e.getSource()==newlyButton){//点击再来一局
buttonLocation = new int[8][7];
assignNumber();
mainFrame.setVisible(false);
isAnyButtonPressed=false;
init();
}
for(int rows = 0;rows < 6;rows++){
for(int cols = 0;cols < 5;cols++ ){
if(e.getSource()==buttons[rows][cols])
ButtonPressed(rows+1,cols+1,buttons[rows][cols]);
}
}
}
public static void main(String[] args){
LianLianKan2 game = new LianLianKan2();
game.assignNumber();
game.init();
}
}
程序中设了“重列”按钮用来打破“死锁”状态。当局面中已经不存在两个相同的图片相连的路径转弯数目小于3时不是终止游戏而是随机打乱局面,打破“死锁”状态。
程序中讨论了四种情况:
1、两个可消块处于边界
2、两个可消块水平或垂直可消
3、经过两条连线可消
4、经过三条连线可消