importjavax.swing.*;\x0d\x0aimportjava.awt.*;\x0d\x0aimportjava.awt.event.*;\x0d\x0apublicclasslianliankanimplementsActionListener\x0d\x0a{\x0d\x0aJFramemainFrame;//主面板\x0d\x0aContainerthisContainer;\x0d\x0aJPanelcenterPanel,southPanel,northPanel;//子面板\x0d\x0aJButtondiamondsButton[][]=newJButton[6][5];//游戲按鈕數(shù)組\x0d\x0aJButtonexitButton,resetButton,newlyButton;//退出,重列,重新開(kāi)始按鈕\x0d\x0aJLabelfractionLable=newJLabel("0");//分?jǐn)?shù)標(biāo)簽\x0d\x0aJButtonfirstButton,secondButton;//分別記錄兩次被選中的按鈕\x0d\x0aintgrid[][]=newint[8][7];//儲(chǔ)存游戲按鈕位置\x0d\x0astaticbooleanpressInformation=false;//判斷是否有按鈕被選中\(zhòng)x0d\x0aintx0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV;//游戲按鈕的位置坐標(biāo)\x0d\x0ainti,j,k,n;//消除方法控制\x0d\x0apublicvoidinit(){\x0d\x0amainFrame=newJFrame("JKJ連連看");\x0d\x0athisContainer=mainFrame.getContentPane();\x0d\x0athisContainer.setLayout(newBorderLayout());\x0d\x0acenterPanel=newJPanel();\x0d\x0asouthPanel=newJPanel();\x0d\x0anorthPanel=newJPanel();\x0d\x0athisContainer.add(centerPanel,"Center");\x0d\x0athisContainer.add(southPanel,"South");\x0d\x0athisContainer.add(northPanel,"North");\x0d\x0acenterPanel.setLayout(newGridLayout(6,5));\x0d\x0afor(intcols=0;cols=0){\x0d\x0acols=(int)(Math.random()*6+1);\x0d\x0arows=(int)(Math.random()*5+1);\x0d\x0awhile(grid[cols][rows]!=0){\x0d\x0acols=(int)(Math.random()*6+1);\x0d\x0arows=(int)(Math.random()*5+1);\x0d\x0a}\x0d\x0athis.grid[cols][rows]=save[n];\x0d\x0an--;\x0d\x0a}\x0d\x0amainFrame.setVisible(false);\x0d\x0apressInformation=false;//這里一定要將按鈕點(diǎn)擊信息歸為初始\x0d\x0ainit();\x0d\x0afor(inti=0;ij){//如果第二個(gè)按鈕的Y坐標(biāo)大于空按鈕的Y坐標(biāo)說(shuō)明第一按鈕在第二按鈕左邊\x0d\x0afor(i=y-1;i=j;i--){//判斷第二按鈕左側(cè)直到第一按鈕中間有沒(méi)有按鈕\x0d\x0aif(grid[x][i]!=0){\x0d\x0ak=0;\x0d\x0abreak;\x0d\x0a}\x0d\x0aelse//K=1說(shuō)明通過(guò)了第一次驗(yàn)證\x0d\x0a}\x0d\x0aif(k==1){\x0d\x0alinePassOne();\x0d\x0a}\x0d\x0a}\x0d\x0aif(yx){\x0d\x0afor(n=x0;n=x+1;n--){\x0d\x0aif(grid[n][j]!=0){\x0d\x0ak=0;\x0d\x0abreak;\x0d\x0a}\x0d\x0aif(grid[n][j]==0n==x+1){\x0d\x0aremove();\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0afor(i=0;ii){\x0d\x0afor(j=x-1;j=i;j--){\x0d\x0aif(grid[j][y]!=0){\x0d\x0ak=0;\x0d\x0abreak;\x0d\x0a}\x0d\x0aelse\x0d\x0a}\x0d\x0aif(k==1){\x0d\x0arowPassOne();\x0d\x0a}\x0d\x0a}\x0d\x0aif(xy){\x0d\x0afor(n=y0;n=y+1;n--){\x0d\x0aif(grid[i][n]!=0){\x0d\x0ak=0;\x0d\x0abreak;\x0d\x0a}\x0d\x0aif(grid[i][n]==0n==y+1){\x0d\x0aremove();\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0apublicvoidlinePassOne(){\x0d\x0aif(y0j){//第一按鈕同行空按鈕在左邊\x0d\x0afor(i=y0-1;i=j;i--){//判斷第一按鈕同左側(cè)空按鈕之間有沒(méi)按鈕\x0d\x0aif(grid[x0][i]!=0){\x0d\x0ak=0;\x0d\x0abreak;\x0d\x0a}\x0d\x0aelse//K=2說(shuō)明通過(guò)了第二次驗(yàn)證\x0d\x0a}\x0d\x0a}\x0d\x0aif(y0i){\x0d\x0afor(j=x0-1;j=i;j--){\x0d\x0aif(grid[j][y0]!=0){\x0d\x0ak=0;\x0d\x0abreak;\x0d\x0a}\x0d\x0aelse\x0d\x0a}\x0d\x0a}\x0d\x0aif(x0
為曾都等地區(qū)用戶(hù)提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及曾都網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都網(wǎng)站制作、網(wǎng)站建設(shè)、曾都網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專(zhuān)業(yè)、用心的態(tài)度為用戶(hù)提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶(hù)的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!
回答于?2022-12-14
連連看java源代碼
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; //退出,重列,重新開(kāi)始按鈕
JLabel fractionLable=new JLabel("0"); //分?jǐn)?shù)標(biāo)簽
JButton firstButton,secondButton; //分別記錄兩次被選中的按鈕
int grid[][] = new int[8][7];//儲(chǔ)存游戲按鈕位置
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("再來(lái)一局");
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; //這里一定要將按鈕點(diǎn)擊信息歸為初始
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){ //判斷第一個(gè)按鈕同行哪個(gè)按鈕為空
if (yj) { //如果第二個(gè)按鈕的Y坐標(biāo)大于空按鈕的Y坐標(biāo)說(shuō)明第一按鈕在第二按鈕左邊
for (i=y-1;i=j;i-- ){ //判斷第二按鈕左側(cè)直到第一按鈕中間有沒(méi)有按鈕
if (grid[x][i]!=0) {
k=0;
break;
}
else{ k=1; } //K=1說(shuō)明通過(guò)了第一次驗(yàn)證
}
if (k==1) {
linePassOne();
}
}
if (yj){ //如果第二個(gè)按鈕的Y坐標(biāo)小于空按鈕的Y坐標(biāo)說(shuō)明第一按鈕在第二按鈕右邊
for (i=y+1;i=j ;i++ ){ //判斷第二按鈕左側(cè)直到第一按鈕中間有沒(méi)有按鈕
if (grid[x][i]!=0){
k=0;
break;
}
else { k=1; }
}
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 { k=1; }
}
if (k==1) {
rowPassOne();
}
}
if (xi) {
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 (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è)空按鈕之間有沒(méi)按鈕
if (grid[x0][i]!=0) {
k=0;
break;
}
else { k=2; } //K=2說(shuō)明通過(guò)了第二次驗(yàn)證
}
}
if (y0j){ //第一按鈕同行空按鈕在與第二按鈕之間
for (i=y0+1;i=j ;i++){
if (grid[x0][i]!=0) {
k=0;
break;
}
else{ k=2; }
}
}
}
public void rowPassOne(){
if (x0i) {
for (j=x0-1;j=i ;j-- ) {
if (grid[j][y0]!=0) {
k=0;
break;
}
else { k=2; }
}
}
if (x0i) {
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();
}
}
//old 998 lines
//new 318 lines
基于JAVA的3D坦克游戲源代碼
JAVA猜數(shù)字小游戲源代碼
/*1、編寫(xiě)一個(gè)猜數(shù)字的游戲,由電腦隨機(jī)產(chǎn)生一個(gè)100以?xún)?nèi)的整數(shù),讓用戶(hù)去猜,如果用戶(hù)猜的比電腦大,則輸出“大了,再小點(diǎn)!”,反之則輸出“小了,再大點(diǎn)!”,用戶(hù)總共只能猜十次,并根據(jù)用戶(hù)正確猜出答案所用的次數(shù)輸出相應(yīng)的信息,如:只用一次就猜對(duì),輸出“你是個(gè)天才!”,八次才猜對(duì),輸出“笨死了!”,如果十次還沒(méi)有猜對(duì),則游戲結(jié)束!*/
import java.util.*;
import java.io.*;
public class CaiShu{
public static void main(String[] args) throws IOException{
Random a=new Random();
int num=a.nextInt(100);
System.out.println("請(qǐng)輸入一個(gè)100以?xún)?nèi)的整數(shù):");
for (int i=0;i=9;i++){
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
String str=bf.readLine();
int shu=Integer.parseInt(str);
if (shunum)
System.out.println("輸入的數(shù)大了,輸小點(diǎn)的!");
else if (shunum)
System.out.println("輸入的數(shù)小了,輸大點(diǎn)的!");
else {
System.out.println("恭喜你,猜對(duì)了!");
if (i=2)
System.out.println("你真是個(gè)天才!");
else if (i=6)
System.out.println("還將就,你過(guò)關(guān)了!");
else if (i=8)
System.out.println("但是你還……真笨!");
else
System.out.println("你和豬沒(méi)有兩樣了!");
break;}
}
}
}
加上。(初始化代碼樓主清洗本身選) 對(duì)應(yīng)在這句話。別離grid[][]數(shù)組的行列即可,你只需定義25個(gè)不一樣的圖片;后面; 這句話是用來(lái)設(shè)置連連看的圖的.setIcon(icons[grid[cols + 1][rows + 1])])。 定義: diamondsButton[cols][rows] = new JButton(String .valueOf(grid[cols + 1][rows + 1])): ImageIcon icons[]= new ImageIcon[25]: diamondsButton[cols][rows].valueOf(grid[cols + 1][rows + 1]))diamondsButton[cols][rows] = new JButton(String 。它只用了數(shù)字; 然后把icons數(shù)組初始化對(duì)應(yīng)每個(gè)圖片即可
剛試了。。測(cè)試通過(guò)。。
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;// 退出,重列,重新開(kāi)始按鈕
JLabel fractionLable = newJLabel("0"); // 分?jǐn)?shù)標(biāo)簽
JButton firstButton, secondButton; // 分別記錄兩次被選中的按鈕
// 儲(chǔ)存游戲按鈕位置(這里其實(shí)只要6行,5列。但是我們用了8行,7列。是等于在這個(gè)面板按鈕的周?chē)€圍
//了一層是0的按鈕,這樣就可以實(shí)現(xiàn)靠近面板邊緣的兩個(gè)按鈕可以消去)
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("再來(lái)一局");
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對(duì)button,30個(gè)
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說(shuō)明這個(gè)空格有了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];//記下每個(gè)button的數(shù)字
n++;//有幾個(gè)沒(méi)有消去的button
}
}
}
n = n - 1;
this.grid = grid;
while (n = 0) {//把沒(méi)有消去的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; // 這里一定要將按鈕點(diǎn)擊信息歸為初始
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) { // 判斷和第一個(gè)按鈕同行的哪個(gè)按鈕為空
//如果找到一個(gè)為空的,就按列值的三種情況比較第二個(gè)按鈕與空按鈕的位置
if (y j) {//第二個(gè)按鈕在空按鈕右邊
for (i = y - 1; i = j; i--) { //檢測(cè)從第二個(gè)按鈕橫向左邊到空格所在列為止是否全是空格
if (grid[x][i] != 0) {
k = 0;
break;//存在非空格的就退出,這一退出就不可能k==2了,所以就會(huì)到下而215行出同理的判斷列
} else {
k = 1;
} // K=1說(shuō)明全是空格通過(guò)了第一次驗(yàn)證,也就是從第二個(gè)按鈕橫向左邊到空格所在列為止全是空格
}
if (k == 1) {
linePassOne();//進(jìn)入第二次驗(yàn)證,也就是從第一個(gè)按鈕到它同行的空格之間的空格判斷
}
}
if (y j) { // 第二個(gè)按鈕在空按鈕左邊
for (i = y + 1; i = j; i++) {//檢測(cè)從第二個(gè)按鈕橫向右邊到空格所在列為止是否全是空格
if (grid[x][i] != 0) {
k = 0;
break;
} else {
k = 1;
}
}
if (k == 1) {
linePassOne();
}
}
if (y == j) {//第二個(gè)按鈕和空按鈕同列
linePassOne();
}
}
//第三次檢測(cè),檢測(cè)確定為空的第j列的那個(gè)按鈕豎向到第二個(gè)按鈕,看是不是有按鈕
if (k == 2) {
if (x0 == x) {//第一,二按鈕在同行
remove();
}
if (x0 x) {//第一按鈕在第二按鈕下邊
for (n = x0; n = x - 1; n++) {//從空按鈕豎向到第二個(gè)按鈕所在行是否有按鈕
if (grid[n][j] != 0) {
k= 0;
break;
}
//沒(méi)有按鈕,說(shuō)明這條路經(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)上面的檢測(cè)與第一個(gè)按鈕同行的空格按鈕失敗后(不能找到與第二個(gè)按鈕的相連路經(jīng)),下面就執(zhí)行
//檢測(cè)與第一個(gè)按鈕同列的空格按鈕
for (i = 0; i 8; i++) {
if (grid[i][y0] == 0) {// 判斷和第一個(gè)按鈕同列的哪個(gè)按鈕為空
if (x i) {//第二個(gè)按鈕在這個(gè)空按鈕的下面
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) {//第二個(gè)按鈕在這個(gè)空按鈕的上面
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) {//第二個(gè)按鈕與這個(gè)空按鈕同行
rowPassOne();
}
}
if (k == 2) {
if (y0 == y) {//第二個(gè)按鈕與第一個(gè)按鈕同列
remove();
}
if (y0 y) {//第二個(gè)按鈕在第一個(gè)按鈕右邊
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) {//第二個(gè)按鈕在第一個(gè)按鈕左邊
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è)空按鈕之間有沒(méi)按鈕
if (grid[x0][i] != 0) {
k = 0;
break;
} else {
k = 2;
} // K=2說(shuō)明通過(guò)了第二次驗(yàn)證
}
}
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) {//第一個(gè)按鈕在與它同列的那個(gè)空格按鈕下面
for (j = x0 - 1; j = i; j--) {
if (grid[j][y0] != 0) {
k = 0;
break;
} else {
k = 2;
}
}
}
if (x0 i) {//第一個(gè)按鈕在與它同列的那個(gè)空格按鈕上面
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();
}
}
表1. CheckerDrag.java
// CheckerDrag.javaimport java.awt.*;import java.awt.event.*;public class CheckerDrag extends java.applet.Applet{ // Dimension of checkerboard square. // 棋盤(pán)上每個(gè)小方格的尺寸 final static int SQUAREDIM = 40; // Dimension of checkerboard -- includes black outline. // 棋盤(pán)的尺寸 – 包括黑色的輪廓線 final static int BOARDDIM = 8 * SQUAREDIM + 2; // Dimension of checker -- 3/4 the dimension of a square. // 棋子的尺寸 – 方格尺寸的3/4 final static int CHECKERDIM = 3 * SQUAREDIM / 4; // Square colors are dark green or white. // 方格的顏色為深綠色或者白色 final static Color darkGreen = new Color (0, 128, 0); // Dragging flag -- set to true when user presses mouse button over checker // and cleared to false when user releases mouse button. // 拖動(dòng)標(biāo)記 --當(dāng)用戶(hù)在棋子上按下鼠標(biāo)按鍵時(shí)設(shè)為true, // 釋放鼠標(biāo)按鍵時(shí)設(shè)為false boolean inDrag = false; // Left coordinate of checkerboard's upper-left corner. // 棋盤(pán)左上角的左方向坐標(biāo) int boardx; // Top coordinate of checkerboard's upper-left corner. //棋盤(pán)左上角的上方向坐標(biāo) int boardy; // Left coordinate of checker rectangle origin (upper-left corner). // 棋子矩形原點(diǎn)(左上角)的左方向坐標(biāo) int ox; // Top coordinate of checker rectangle origin (upper-left corner). // 棋子矩形原點(diǎn)(左上角)的上方向坐標(biāo) int oy; // Left displacement between mouse coordinates at time of press and checker // rectangle origin. // 在按鍵時(shí)的鼠標(biāo)坐標(biāo)與棋子矩形原點(diǎn)之間的左方向位移 int relx; // Top displacement between mouse coordinates at time of press and checker // rectangle origin. // 在按鍵時(shí)的鼠標(biāo)坐標(biāo)與棋子矩形原點(diǎn)之間的上方向位移 int rely; // Width of applet drawing area. // applet繪圖區(qū)域的寬度 int width; // Height of applet drawing area. // applet繪圖區(qū)域的高度 int height; // Image buffer. // 圖像緩沖 Image imBuffer; // Graphics context associated with image buffer. // 圖像緩沖相關(guān)聯(lián)的圖形背景 Graphics imG; public void init () { // Obtain the size of the applet's drawing area. // 獲取applet繪圖區(qū)域的尺寸 width = getSize ().width; height = getSize ().height; // Create image buffer. // 創(chuàng)建圖像緩沖 imBuffer = createImage (width, height); // Retrieve graphics context associated with image buffer. // 取出圖像緩沖相關(guān)聯(lián)的圖形背景 imG = imBuffer.getGraphics (); // Initialize checkerboard's origin, so that board is centered. // 初始化棋盤(pán)的原點(diǎn),使棋盤(pán)在屏幕上居中 boardx = (width - BOARDDIM) / 2 + 1; boardy = (height - BOARDDIM) / 2 + 1; // Initialize checker's rectangle's starting origin so that checker is // centered in the square located in the top row and second column from // the left. // 初始化棋子矩形的起始原點(diǎn),使得棋子在第一行左數(shù)第二列的方格里居中 ox = boardx + SQUAREDIM + (SQUAREDIM - CHECKERDIM) / 2 + 1; oy = boardy + (SQUAREDIM - CHECKERDIM) / 2 + 1; // Attach a mouse listener to the applet. That listener listens for // mouse-button press and mouse-button release events. // 向applet添加一個(gè)用來(lái)監(jiān)聽(tīng)鼠標(biāo)按鍵的按下和釋放事件的鼠標(biāo)監(jiān)聽(tīng)器 addMouseListener (new MouseAdapter () { public void mousePressed (MouseEvent e) { // Obtain mouse coordinates at time of press. // 獲取按鍵時(shí)的鼠標(biāo)坐標(biāo) int x = e.getX (); int y = e.getY (); // If mouse is over draggable checker at time // of press (i.e., contains (x, y) returns // true), save distance between current mouse // coordinates and draggable checker origin // (which will always be positive) and set drag // flag to true (to indicate drag in progress). // 在按鍵時(shí)如果鼠標(biāo)位于可拖動(dòng)的棋子上方 // (也就是contains (x, y)返回true),則保存當(dāng)前 // 鼠標(biāo)坐標(biāo)與棋子的原點(diǎn)之間的距離(始終為正值)并且 // 將拖動(dòng)標(biāo)志設(shè)為true(用來(lái)表明正處在拖動(dòng)過(guò)程中) if (contains (x, y)) { relx = x - ox; rely = y - oy; inDrag = true; } } boolean contains (int x, int y) { // Calculate center of draggable checker. // 計(jì)算棋子的中心位置 int cox = ox + CHECKERDIM / 2; int coy = oy + CHECKERDIM / 2; // Return true if (x, y) locates with bounds // of draggable checker. CHECKERDIM / 2 is the // radius. // 如果(x, y)仍處于棋子范圍內(nèi)則返回true // CHECKERDIM / 2為半徑 return (cox - x) * (cox - x) + (coy - y) * (coy - y) CHECKERDIM / 2 * CHECKERDIM / 2; } public void mouseReleased (MouseEvent e) { // When mouse is released, clear inDrag (to // indicate no drag in progress) if inDrag is // already set. // 當(dāng)鼠標(biāo)按鍵被釋放時(shí),如果inDrag已經(jīng)為true, // 則將其置為false(用來(lái)表明不在拖動(dòng)過(guò)程中) if (inDrag) inDrag = false; } }); // Attach a mouse motion listener to the applet. That listener listens // for mouse drag events. //向applet添加一個(gè)用來(lái)監(jiān)聽(tīng)鼠標(biāo)拖動(dòng)事件的鼠標(biāo)運(yùn)動(dòng)監(jiān)聽(tīng)器 addMouseMotionListener (new MouseMotionAdapter () { public void mouseDragged (MouseEvent e) { if (inDrag) { // Calculate draggable checker's new // origin (the upper-left corner of // the checker rectangle). // 計(jì)算棋子新的原點(diǎn)(棋子矩形的左上角) int tmpox = e.getX () - relx; int tmpoy = e.getY () - rely; // If the checker is not being moved // (at least partly) off board, // assign the previously calculated // origin (tmpox, tmpoy) as the // permanent origin (ox, oy), and // redraw the display area (with the // draggable checker at the new // coordinates). // 如果棋子(至少是棋子的一部分)沒(méi)有被 // 移出棋盤(pán),則將之前計(jì)算的原點(diǎn) // (tmpox, tmpoy)賦值給永久性的原點(diǎn)(ox, oy), // 并且刷新顯示區(qū)域(此時(shí)的棋子已經(jīng)位于新坐標(biāo)上) if (tmpox boardx tmpoy boardy tmpox + CHECKERDIM boardx + BOARDDIM tmpoy + CHECKERDIM boardy + BOARDDIM) { ox = tmpox; oy = tmpoy; repaint (); } } } }); } public void paint (Graphics g) { // Paint the checkerboard over which the checker will be dragged. // 在棋子將要被拖動(dòng)的位置上繪制棋盤(pán) paintCheckerBoard (imG, boardx, boardy); // Paint the checker that will be dragged. // 繪制即將被拖動(dòng)的棋子 paintChecker (imG, ox, oy); // Draw contents of image buffer. // 繪制圖像緩沖的內(nèi)容 g.drawImage (imBuffer, 0, 0, this); } void paintChecker (Graphics g, int x, int y) { // Set checker shadow color. // 設(shè)置棋子陰影的顏色 g.setColor (Color.black); // Paint checker shadow. // 繪制棋子的陰影 g.fillOval (x, y, CHECKERDIM, CHECKERDIM); // Set checker color. // 設(shè)置棋子顏色 g.setColor (Color.red); // Paint checker. // 繪制棋子 g.fillOval (x, y, CHECKERDIM - CHECKERDIM / 13, CHECKERDIM - CHECKERDIM / 13); } void paintCheckerBoard (Graphics g, int x, int y) { // Paint checkerboard outline. // 繪制棋盤(pán)輪廓線 g.setColor (Color.black); g.drawRect (x, y, 8 * SQUAREDIM + 1, 8 * SQUAREDIM + 1); // Paint checkerboard. // 繪制棋盤(pán) for (int row = 0; row 8; row++) { g.setColor (((row 1) != 0) ? darkGreen : Color.white); for (int col = 0; col 8; col++) { g.fillRect (x + 1 + col * SQUAREDIM, y + 1 + row * SQUAREDIM, SQUAREDIM, SQUAREDIM); g.setColor ((g.getColor () == darkGreen) ? Color.white : darkGreen); } } } // The AWT invokes the update() method in response to the repaint() method // calls that are made as a checker is dragged. The default implementation // of this method, which is inherited from the Container class, clears the // applet's drawing area to the background color prior to calling paint(). // This clearing followed by drawing causes flicker. CheckerDrag overrides // update() to prevent the background from being cleared, which eliminates // the flicker. // AWT調(diào)用了update()方法來(lái)響應(yīng)拖動(dòng)棋子時(shí)所調(diào)用的repaint()方法。該方法從 // Container類(lèi)繼承的默認(rèn)實(shí)現(xiàn)會(huì)在調(diào)用paint()之前,將applet的繪圖區(qū)域清除 // 為背景色,這種繪制之后的清除就導(dǎo)致了閃爍。CheckerDrag重寫(xiě)了update()來(lái) // 防止背景被清除,從而消除了閃爍。 public void update (Graphics g) { paint (g); }}
我以前自己寫(xiě)一個(gè)玩的。沒(méi)有把你要求的功能全部實(shí)現(xiàn),不過(guò)你看了后可以改一下就好了。參考一下吧,我給了注解:
package mybase.programe;
/*
* lianliankan總體算法思路:由兩個(gè)確定的按鈕。若這兩個(gè)按鈕的數(shù)字相等,就開(kāi)始找它們相連的路經(jīng)。這個(gè)找路經(jīng)
* 分3種情況:(從下面的這三種情況,我們可以知道,需要三個(gè)檢測(cè),這三個(gè)檢測(cè)分別檢測(cè)一條直路經(jīng)。這樣就會(huì)有
* 三條路經(jīng)。若這三條路經(jīng)上都是空按鈕,那么就剛好是三種直線(兩個(gè)轉(zhuǎn)彎點(diǎn))把兩個(gè)按鈕連接起來(lái)了)
* 1.相鄰
*
* 2. 若不相鄰的先在第一個(gè)按鈕的同行找一個(gè)空按鈕。1).找到后看第二個(gè)按鈕橫向到這個(gè)空按鈕
* 所在的列是否有按鈕。2).沒(méi)有的話再看第一個(gè)按鈕到與它同行的那個(gè)空按鈕之間是否有按鈕。3).沒(méi)有的話,再?gòu)?/p>
* 與第一個(gè)按鈕同行的那個(gè)空按鈕豎向到與第二個(gè)按鈕的同行看是否有按鈕。沒(méi)有的話路經(jīng)就通了,可以消了.
*
* 3.若2失敗后,再在第一個(gè)按鈕的同列找一個(gè)空按鈕。1).找到后看第二個(gè)按鈕豎向到這個(gè)空按鈕所在的行是否有按鈕。
* 2).沒(méi)有的話,再看第一個(gè)按鈕到與它同列的那個(gè)空按鈕之間是否有按鈕。3).沒(méi)有的話,再?gòu)呐c第一個(gè)按鈕同列的
* 那個(gè)空按鈕橫向到與第二個(gè)按鈕同列看是否有按鈕。沒(méi)有的話路經(jīng)就通了,可以消了。
*
* 若以上三步都失敗,說(shuō)明這兩個(gè)按鈕不可以消去。
*/
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; // 退出,重列,重新開(kāi)始按鈕
JLabel fractionLable = new JLabel("0"); // 分?jǐn)?shù)標(biāo)簽
JButton firstButton, secondButton; // 分別記錄兩次被選中的按鈕
// 儲(chǔ)存游戲按鈕位置(這里其實(shí)只要6行,5列。但是我們用了8行,7列。是等于在這個(gè)面板按鈕的周?chē)€圍
//了一層是0的按鈕,這樣就可以實(shí)現(xiàn)靠近面板邊緣的兩個(gè)按鈕可以消去)
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("再來(lái)一局");
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對(duì)button,30個(gè)
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說(shuō)明這個(gè)空格有了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];//記下每個(gè)button的數(shù)字
n++;//有幾個(gè)沒(méi)有消去的button
}
}
}
n = n - 1;
this.grid = grid;
while (n = 0) {//把沒(méi)有消去的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; // 這里一定要將按鈕點(diǎn)擊信息歸為初始
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) { // 判斷和第一個(gè)按鈕同行的哪個(gè)按鈕為空
//如果找到一個(gè)為空的,就按列值的三種情況比較第二個(gè)按鈕與空按鈕的位置
if (y j) {//第二個(gè)按鈕在空按鈕右邊
for (i = y - 1; i = j; i--) { //檢測(cè)從第二個(gè)按鈕橫向左邊到空格所在列為止是否全是空格
if (grid[x][i] != 0) {
k = 0;
break;//存在非空格的就退出,這一退出就不可能k==2了,所以就會(huì)到下而215行出同理的判斷列
} else {
k = 1;
} // K=1說(shuō)明全是空格通過(guò)了第一次驗(yàn)證,也就是從第二個(gè)按鈕橫向左邊到空格所在列為止全是空格
}
if (k == 1) {
linePassOne();//進(jìn)入第二次驗(yàn)證,也就是從第一個(gè)按鈕到它同行的空格之間的空格判斷
}
}
if (y j) { // 第二個(gè)按鈕在空按鈕左邊
for (i = y + 1; i = j; i++) {//檢測(cè)從第二個(gè)按鈕橫向右邊到空格所在列為止是否全是空格
if (grid[x][i] != 0) {
k = 0;
break;
} else {
k = 1;
}
}
if (k == 1) {
linePassOne();
}
}
if (y == j) {//第二個(gè)按鈕和空按鈕同列
linePassOne();
}
}
//第三次檢測(cè),檢測(cè)確定為空的第j列的那個(gè)按鈕豎向到第二個(gè)按鈕,看是不是有按鈕
if (k == 2) {
if (x0 == x) {//第一,二按鈕在同行
remove();
}
if (x0 x) {//第一按鈕在第二按鈕下邊
for (n = x0; n = x - 1; n++) {//從空按鈕豎向到第二個(gè)按鈕所在行是否有按鈕
if (grid[n][j] != 0) {
k = 0;
break;
}
//沒(méi)有按鈕,說(shuō)明這條路經(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)上面的檢測(cè)與第一個(gè)按鈕同行的空格按鈕失敗后(不能找到與第二個(gè)按鈕的相連路經(jīng)),下面就執(zhí)行
//檢測(cè)與第一個(gè)按鈕同列的空格按鈕
for (i = 0; i 8; i++) {
if (grid[i][y0] == 0) {// 判斷和第一個(gè)按鈕同列的哪個(gè)按鈕為空
if (x i) {//第二個(gè)按鈕在這個(gè)空按鈕的下面
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) {//第二個(gè)按鈕在這個(gè)空按鈕的上面
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) {//第二個(gè)按鈕與這個(gè)空按鈕同行
rowPassOne();
}
}
if (k == 2) {
if (y0 == y) {//第二個(gè)按鈕與第一個(gè)按鈕同列
remove();
}
if (y0 y) {//第二個(gè)按鈕在第一個(gè)按鈕右邊
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) {//第二個(gè)按鈕在第一個(gè)按鈕左邊
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è)空按鈕之間有沒(méi)按鈕
if (grid[x0][i] != 0) {
k = 0;
break;
} else {
k = 2;
} // K=2說(shuō)明通過(guò)了第二次驗(yàn)證
}
}
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) {//第一個(gè)按鈕在與它同列的那個(gè)空格按鈕下面
for (j = x0 - 1; j = i; j--) {
if (grid[j][y0] != 0) {
k = 0;
break;
} else {
k = 2;
}
}
}
if (x0 i) {//第一個(gè)按鈕在與它同列的那個(gè)空格按鈕上面
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();
}
}
當(dāng)前文章:java連連看源代碼大全 連連看JAVA
網(wǎng)頁(yè)地址:http://jinyejixie.com/article26/hpchjg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、微信公眾號(hào)、外貿(mào)網(wǎng)站建設(shè)、建站公司、小程序開(kāi)發(fā)、網(wǎng)站維護(hù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)