java版連連看遊戲源碼,java連連看小遊戲代碼詳解

本文目錄一覽:

用java語言編寫連連看遊戲

我以前自己寫一個玩的。沒有把你要求的功能全部實現,不過你看了後可以改一下就好了。參考一下吧,我給了註解:

package mybase.programe;

/*

* lianliankan總體算法思路:由兩個確定的按鈕。若這兩個按鈕的數字相等,就開始找它們相連的路經。這個找路經

* 分3種情況:(從下面的這三種情況,我們可以知道,需要三個檢測,這三個檢測分別檢測一條直路經。這樣就會有

* 三條路經。若這三條路經上都是空按鈕,那麼就剛好是三種直線(兩個轉彎點)把兩個按鈕連接起來了)

* 1.相鄰

*

* 2. 若不相鄰的先在第一個按鈕的同行找一個空按鈕。1).找到後看第二個按鈕橫向到這個空按鈕

* 所在的列是否有按鈕。2).沒有的話再看第一個按鈕到與它同行的那個空按鈕之間是否有按鈕。3).沒有的話,再從

* 與第一個按鈕同行的那個空按鈕豎向到與第二個按鈕的同行看是否有按鈕。沒有的話路經就通了,可以消了.

*

* 3.若2失敗後,再在第一個按鈕的同列找一個空按鈕。1).找到後看第二個按鈕豎向到這個空按鈕所在的行是否有按鈕。

* 2).沒有的話,再看第一個按鈕到與它同列的那個空按鈕之間是否有按鈕。3).沒有的話,再從與第一個按鈕同列的

* 那個空按鈕橫向到與第二個按鈕同列看是否有按鈕。沒有的話路經就通了,可以消了。

*

* 若以上三步都失敗,說明這兩個按鈕不可以消去。

*/

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];// 遊戲按鈕數組

JButton exitButton, resetButton, newlyButton; // 退出,重列,重新開始按鈕

JLabel fractionLable = new JLabel(“0”); // 分數標籤

JButton firstButton, secondButton; // 分別記錄兩次被選中的按鈕

// 儲存遊戲按鈕位置(這裡其實只要6行,5列。但是我們用了8行,7列。是等於在這個面板按鈕的周圍還圍

//了一層是0的按鈕,這樣就可以實現靠近面板邊緣的兩個按鈕可以消去)

int grid[][] = new int[8][7];

static boolean pressInformation = false; // 判斷是否有按鈕被選中

int x0 = 0, y0 = 0, x = 0, y = 0, fristMsg = 0, secondMsg = 0, validateLV; // 遊戲按鈕的位置坐標

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上的數字

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; 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() { // 相同的情況下能不能消去。仔細分析,不一條條注釋

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();//進入第二次驗證,也就是從第一個按鈕到它同行的空格之間的空格判斷

}

}

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;

}

//沒有按鈕,說明這條路經就通了

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

//當上面的檢測與第一個按鈕同行的空格按鈕失敗後(不能找到與第二個按鈕的相連路經),下面就執行

//檢測與第一個按鈕同列的空格按鈕

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–) { // 判斷第一按鈕同左側空按鈕之間有沒按鈕

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();

}

}

連連看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];//遊戲按鈕數組

JButton exitButton,resetButton,newlyButton; //退出,重列,重新開始按鈕

JLabel fractionLable=new JLabel(“0”); //分數標籤

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; //遊戲按鈕的位置坐標

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() { //相同的情況下能不能消去。仔細分析,不一條條注釋

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坐標大於空按鈕的Y坐標說明第一按鈕在第二按鈕左邊

for (i=y-1;i=j;i– ){ //判斷第二按鈕左側直到第一按鈕中間有沒有按鈕

if (grid[x][i]!=0) {

k=0;

break;

}

else //K=1說明通過了第一次驗證

}

if (k==1) {

linePassOne();

}

}

if (yj){ //如果第二個按鈕的Y坐標小於空按鈕的Y坐標說明第一按鈕在第二按鈕右邊

for (i=y+1;i=j ;i++ ){ //判斷第二按鈕左側直到第一按鈕中間有沒有按鈕

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– ){ //判斷第一按鈕同左側空按鈕之間有沒按鈕

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

如何用JAVA 編寫一個連連看遊戲全程設計

剛試了。。測試通過。。

importjavax.swing.*;

importjava.awt.*;

importjava.awt.event.*;

publicclass LianLianKan implements ActionListener {

JFrame mainFrame; // 主面板

Container thisContainer;

JPanel centerPanel, southPanel, northPanel; //子面板

JButton diamondsButton[][] = newJButton[6][5];// 遊戲按鈕數組

JButton exitButton, resetButton, newlyButton;// 退出,重列,重新開始按鈕

JLabel fractionLable = newJLabel(“0”); // 分數標籤

JButton firstButton, secondButton; // 分別記錄兩次被選中的按鈕

// 儲存遊戲按鈕位置(這裡其實只要6行,5列。但是我們用了8行,7列。是等於在這個面板按鈕的周圍還圍

//了一層是0的按鈕,這樣就可以實現靠近面板邊緣的兩個按鈕可以消去)

int grid[][] = new int[8][7];

static boolean pressInformation = false; // 判斷是否有按鈕被選中

int x0 = 0, y0 = 0, x = 0, y = 0, fristMsg =0, secondMsg = 0, validateLV; // 遊戲按鈕的位置坐標

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上的數字

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; 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() { // 相同的情況下能不能消去。仔細分析,不一條條注釋

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();//進入第二次驗證,也就是從第一個按鈕到它同行的空格之間的空格判斷

}

}

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;

}

//沒有按鈕,說明這條路經就通了

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

//當上面的檢測與第一個按鈕同行的空格按鈕失敗後(不能找到與第二個按鈕的相連路經),下面就執行

//檢測與第一個按鈕同列的空格按鈕

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–) { // 判斷第一按鈕同左側空按鈕之間有沒按鈕

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();

}

}

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/237725.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-12 12:06
下一篇 2024-12-12 12:07

相關推薦

  • 雲智直聘 源碼分析

    本文將會對雲智直聘的源碼進行分析,包括前端頁面和後端代碼,幫助讀者了解其架構、技術實現以及對一些常見的問題進行解決。通過本文的閱讀,讀者將會了解到雲智直聘的特點、優勢以及不足之處,…

    編程 2025-04-29
  • Python小遊戲變現攻略

    想要通過編寫小遊戲來變現嗎?Python是一個非常受歡迎的編程語言,特別是在遊戲開發領域。在本文中,我們將介紹如何使用Python編寫遊戲並從中獲利。 一、選擇適合的遊戲類型 首先…

    編程 2025-04-28
  • Python網站源碼解析

    本文將從多個方面對Python網站源碼進行詳細解析,包括搭建網站、數據處理、安全性等內容。 一、搭建網站 Python是一種高級編程語言,適用於多種領域。它也可以用於搭建網站。最常…

    編程 2025-04-28
  • 源碼是什麼

    源碼是一段計算機程序的原始代碼,它是程序員所編寫的可讀性高、理解性強的文本。在計算機中,源碼是指編寫的程序代碼,這些代碼按照一定規則排列,被計算機識別並執行。 一、源碼的組成 源碼…

    編程 2025-04-27
  • Go源碼閱讀

    Go語言是Google推出的一門靜態類型、編譯型、並髮型、語法簡單的編程語言。它因具有簡潔高效,內置GC等優秀特性,被越來越多的開發者所鍾愛。在這篇文章中,我們將介紹如何從多個方面…

    編程 2025-04-27
  • Python怎麼看源碼

    本文將從以下幾個方面詳細介紹Python如何看源碼,幫助讀者更好地了解Python。 一、查看Python版本 在查看Python源碼之前,首先需要確認Python版本。可以在命令…

    編程 2025-04-27
  • 源碼審計面試題用法介紹

    在進行源碼審計面試時,可能會遇到各種類型的問題,本文將以實例為基礎,從多個方面對源碼審計面試題進行詳細闡述。 一、SQL注入 SQL注入是常見的一種攻擊方式,攻擊者通過在輸入的參數…

    編程 2025-04-27
  • 小遊戲編程代碼可粘貼

    小遊戲編程是一種非常流行的編程方式,可以幫助人們輕鬆創建自己的小型遊戲。而小遊戲編程代碼可粘貼,則是指將代碼以文本的形式複製到剪貼板上,然後再將其粘貼到編程軟件開發環境中。這種方法…

    編程 2025-04-27
  • 神經網絡代碼詳解

    神經網絡作為一種人工智能技術,被廣泛應用於語音識別、圖像識別、自然語言處理等領域。而神經網絡的模型編寫,離不開代碼。本文將從多個方面詳細闡述神經網絡模型編寫的代碼技術。 一、神經網…

    編程 2025-04-25
  • Linux sync詳解

    一、sync概述 sync是Linux中一個非常重要的命令,它可以將文件系統緩存中的內容,強制寫入磁盤中。在執行sync之前,所有的文件系統更新將不會立即寫入磁盤,而是先緩存在內存…

    編程 2025-04-25

發表回復

登錄後才能評論