1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > java简易版连连看_Java版连连看小游戏

java简易版连连看_Java版连连看小游戏

时间:2022-05-27 17:21:35

相关推荐

java简易版连连看_Java版连连看小游戏

2.[代码][Java]代码

package com;

import java.awt.Color;

import java.awt.EventQueue;

import java.awt.Graphics;

import java.awt.Rectangle;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.HashSet;

import java.util.LinkedList;

import java.util.Map;

import java.util.Set;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.border.EmptyBorder;

public class LianLianKan extends JFrame {

/**

*

*/

private static final long serialVersionUID = 1L;

/**

* Launch the application.

*/

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

LianLianKan frame = new LianLianKan();

frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

});

}

private int i, j;

private static JPanel contentPane;

private static JButton[][] buttonMatrix;

private static int[][] status;

private static Point[][] pointMatrix;

private static Map button2PointMap;

private static String head = "image/test";

private static String tail = ".jpg";

private static String clickTail = "-select.jpg";

private JButton first;

private JButton second;

private static final int ROW = 300 / 50;

private static final int COL = 450 / 50;

private static final int pictureNum = 11;

private static ArrayList pointList;

/**

* Create the frame.

*/

public LianLianKan() {

long start = System.currentTimeMillis();

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(100, 100, 570, 440);

contentPane = new JPanel();

contentPane.setBackground(Color.LIGHT_GRAY);

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

setContentPane(contentPane);

contentPane.setLayout(null);

buttonMatrix = new JButton[ROW + 2][COL + 2];

pointMatrix = new Point[ROW + 2][COL + 2];

status = new int[ROW + 2][COL + 2];

button2PointMap = new HashMap();

pointList = new ArrayList();

visitedPoint = new HashSet();

genPointList();

first = null;

second = null;

for (i = 0; i <= (ROW + 1); i++) {

for (j = 0; j <= (COL + 1); j++) {

pointMatrix[i][j] = new Point(i, j);

if(i==0 || j==0 || i==(ROW+1) || j==(COL+1)){

setButton(i, j, -1);

}

}

}

for (i = 1; i < ROW + 1; i++) {

for (j = 1; j < COL + 1; j++) {

if (status[i][j] != 0) {

continue;

} else {

int num = genRandomNum();

status[i][j] = num;

Point point = new Point(i, j);

pointList.remove(point);

Point pairPoint = getRandomPair(num, point);

int pairI = pairPoint.getRow();

int pairJ = pairPoint.getCol();

status[pairI][pairJ] = num;

pointList.remove(pairPoint);

setButton(i, j, num);

setButton(pairI, pairJ, num);

}

}

}

long end = System.currentTimeMillis();

System.out.println("cost time:" + (end - start));

}

private void setButton(int row, int col, int num) {

buttonMatrix[row][col] = new JButton("");

buttonMatrix[row][col].addActionListener(new MyListener());

String path = head + num + tail;

buttonMatrix[row][col].setIcon(new ImageIcon(path));

buttonMatrix[row][col].setBounds((col) * 50, (row) * 50, 50, 50);

Point point = new Point(row, col);

button2PointMap.put(buttonMatrix[row][col], point);

contentPane.add(buttonMatrix[row][col]);

if(num == -1){

buttonMatrix[row][col].setEnabled(false);

}

}

private static int genRandomNum() {

return ((int) (pictureNum * Math.random())) % pictureNum + 1;

}

private static void genPointList() {

for (int i = 1; i < ROW + 1; i++) {

for (int j = 1; j < COL + 1; j++) {

Point point = new Point(i, j);

pointList.add(point);

}

}

}

private static Point getRandomPair(int num, Point original) {

int size = pointList.size();

Point randomPoint;

int count = 0;

while (true) {

int randomIndex = ((int) (size * Math.random())) % size;

randomPoint = pointList.get(randomIndex);

int i = randomPoint.getRow();

int j = randomPoint.getCol();

int distance = randomPoint.getDistance(original);

if (status[i][j] == 0 && distance > 1 || count == 20) {

break;

} else if (size == 1) {

}

count++;

}

return randomPoint;

}

private SearchNode findPath;

private static Set visitedPoint;

private static LinkedList pathPoint = new LinkedList();

class MyListener implements ActionListener {

private static final int CORNERNUM = 3;

@Override

public void actionPerformed(ActionEvent e) {

JButton clickButton = (JButton) e.getSource();

if (first == null) {

first = clickButton;

setButtonClick(first);

} else if (second == null) {

second = clickButton;

setButtonClick(second);

}

if (first != null && second != null) {

if (first.equals(second)) {

setButtonOriginal(first);

} else {

setButtonOriginal(first);

setButtonOriginal(second);

if (checkSameType(first, second) == true

&& checkLessThanTwoCorner(first, second) == true) {

// first.setVisible(false);

// second.setVisible(false);

first.setEnabled(false);

second.setEnabled(false);

setPairStatusZero(first,second);

//findPath.printPath();

//System.out.println();

getPathPoint(findPath);

for(int index =0;index < pathPoint.size()-1;index++){

Point start = pathPoint.get(index);

Point end = pathPoint.get(index+1);

try {

drawLineByTwoPoint(start,end);

} catch (Exception e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

}

//System.out.println();

//setIcon2Null();

setIconPass2Null(pathPoint);

//first.setIcon(null);

//second.setIcon(null);

pathPoint.clear();

}

}

first = null;

second = null;

}

}

private void setIconPass2Null(LinkedList pathPoint) {

// TODO Auto-generated method stub

for(int index =0;index < pathPoint.size()-1;index++){

Point start = pathPoint.get(index);

Point end = pathPoint.get(index+1);

int startX = start.getRow();

int startY = start.getCol();

int endX = end.getRow();

int endY = end.getCol();

if(startX == endX){

int min = Math.min(endY, startY);

int max = Math.max(endY, startY);

for(int temp = min;temp<=max;temp++){

buttonMatrix[startX][temp].setIcon(new ImageIcon(head+1+tail));

buttonMatrix[startX][temp].setIcon(null);

}

}

if(startY == endY){

int min = Math.min(endX, startX);

int max = Math.max(endX, startX);

for(int temp = min;temp<=max;temp++){

buttonMatrix[temp][startY].setIcon(new ImageIcon(head+1+tail));

buttonMatrix[temp][startY].setIcon(null);

}

}

}

}

//private void setIcon2Null(){

//for (i = 0; i <= ROW + 1; i++) {

//for (j = 0; j <= COL + 1; j++) {

//if(status[i][j] == 0){

//buttonMatrix[i][j].setIcon(new ImageIcon(head+1+tail));

//buttonMatrix[i][j].setIcon(null);

//}

//}

//}

//}

private void drawLineByTwoPoint(Point start, Point end) throws Exception {

// TODO Auto-generated method stub

Graphics g = contentPane.getGraphics();

JButton startButton = buttonMatrix[start.getRow()][start.getCol()];

JButton endButton = buttonMatrix[end.getRow()][end.getCol()];

Rectangle startBounds = startButton.getBounds();

Rectangle endBounds = endButton.getBounds();

int startX = (int) startBounds.getCenterX();

int startY = (int) startBounds.getCenterY();

int endX = (int) endBounds.getCenterX();

int endY = (int) endBounds.getCenterY();

g.setColor(Color.RED);

//g.fillRect(startX, startY, (endX-startX), 2);

if(Math.abs(startX - endX) <= 2){

if(startY < endY){

//down

int delta = endY - startY;

int step = delta/ 50;

for(int index = 1;index<=50;index++){

g.fillRect(startX, startY+(index-1)*step, 2, step);

Thread.sleep(5);

}

}

else{

//up

int delta = startY - endY;

int step = delta/ 50;

for(int index = 1;index<=50;index++){

g.fillRect(startX, startY-index*step, 2, step);

Thread.sleep(5);

}

}

}

if(Math.abs(startY-endY) <= 2){

if(startX < endX){

//right

int delta = endX - startX;

int step = delta/ 50;

for(int index = 1;index<=50;index++){

g.fillRect(startX +(index-1)*step, startY, step, 2);

Thread.sleep(5);

}

}

else{

//left

int delta = startX - endX;

int step = delta/ 50;

for(int index = 1;index<=50;index++){

g.fillRect(startX -index*step, startY, step, 2);

Thread.sleep(5);

}

}

}

}

private void getPathPoint(SearchNode findPath) {

while(findPath != null){

pathPoint.addFirst(findPath.getCur());

findPath = findPath.getForward();

}

}

private void setPairStatusZero(JButton first, JButton second) {

Point point1 = button2PointMap.get(first);

Point point2 = button2PointMap.get(second);

status[point1.getRow()][point1.getCol()] = 0;

status[point2.getRow()][point2.getCol()] = 0;

}

private boolean checkLessThanTwoCorner(JButton first, JButton second) {

boolean flag = false;

Point point1 = button2PointMap.get(first);

Point point2 = button2PointMap.get(second);

//if (point1.getDistance(point2) <= 1) {

//return true;

//}

//int firstType = getPointStatus(point1);

SearchNode sn = new SearchNode(null, point1, 0);

LinkedList searchList = new LinkedList();

searchList.add(sn);

visitedPoint.clear();

visitedPoint.add(point1);

while (searchList.size() > 0) {

SearchNode curNode = searchList.removeFirst();

int curDeep = curNode.getDeep();

Point cur = curNode.getCur();

int rowIndex = cur.getRow();

int colIndex = cur.getCol();

int pointStatus = getPointStatus(cur);

if (curDeep > CORNERNUM) {

break;

}

if (point2.equals(cur)) {

flag = true;

findPath = curNode;

break;

}

if (curDeep == 0 || pointStatus == 0) {

// check four direction

int temp;

for (temp = rowIndex + 1; temp <= (ROW + 1); temp++) {

Point tempPoint = pointMatrix[temp][colIndex];

int tempPointStatus = getPointStatus(tempPoint);

if(tempPointStatus != 0){

if(point2.equals(tempPoint)){

SearchNode tempSn = new SearchNode(curNode, tempPoint, curDeep+1);

searchList.add(tempSn);

visitedPoint.add(tempPoint);

}

break;

}

if (visitedPoint.contains(tempPoint) == false) {

SearchNode tempSn = new SearchNode(curNode, tempPoint, curDeep+1);

searchList.add(tempSn);

visitedPoint.add(tempPoint);

}

}

for (temp = rowIndex - 1; temp >= 0; temp--) {

Point tempPoint = pointMatrix[temp][colIndex];

int tempPointStatus = getPointStatus(tempPoint);

if(tempPointStatus != 0){

if(point2.equals(tempPoint)){

SearchNode tempSn = new SearchNode(curNode, tempPoint, curDeep+1);

searchList.add(tempSn);

visitedPoint.add(tempPoint);

}

break;

}

if (visitedPoint.contains(tempPoint) == false) {

SearchNode tempSn = new SearchNode(curNode, tempPoint, curDeep+1);

searchList.add(tempSn);

visitedPoint.add(tempPoint);

}

}

for (temp = colIndex + 1; temp <= (COL + 1); temp++) {

Point tempPoint = pointMatrix[rowIndex][temp];

int tempPointStatus = getPointStatus(tempPoint);

if(tempPointStatus != 0){

if(point2.equals(tempPoint)){

SearchNode tempSn = new SearchNode(curNode, tempPoint, curDeep+1);

searchList.add(tempSn);

visitedPoint.add(tempPoint);

}

break;

}

if (visitedPoint.contains(tempPoint) == false) {

SearchNode tempSn = new SearchNode(curNode, tempPoint, curDeep+1);

searchList.add(tempSn);

visitedPoint.add(tempPoint);

}

}

for (temp = colIndex - 1; temp >= 0; temp--) {

Point tempPoint = pointMatrix[rowIndex][temp];

int tempPointStatus = getPointStatus(tempPoint);

if(tempPointStatus != 0){

if(point2.equals(tempPoint)){

SearchNode tempSn = new SearchNode(curNode, tempPoint, curDeep+1);

searchList.add(tempSn);

visitedPoint.add(tempPoint);

}

break;

}

if (visitedPoint.contains(tempPoint) == false) {

SearchNode tempSn = new SearchNode(curNode, tempPoint, curDeep+1);

searchList.add(tempSn);

visitedPoint.add(tempPoint);

}

}

}

}

return flag;

}

private int getPointStatus(Point point) {

return status[point.getRow()][point.getCol()];

}

private boolean checkSameType(JButton first, JButton second) {

Point point1 = button2PointMap.get(first);

int clickRow = point1.getRow();

int clickCol = point1.getCol();

int num1 = status[clickRow][clickCol];

Point point2 = button2PointMap.get(second);

clickRow = point2.getRow();

clickCol = point2.getCol();

int num2 = status[clickRow][clickCol];

return (num1 == num2);

}

private void setButtonClick(JButton button) {

Point clickPoint = button2PointMap.get(button);

int clickRow = clickPoint.getRow();

int clickCol = clickPoint.getCol();

int num = status[clickRow][clickCol];

String path = head + num + clickTail;

button.setIcon(new ImageIcon(path));

}

private void setButtonOriginal(JButton button) {

Point clickPoint = button2PointMap.get(button);

int clickRow = clickPoint.getRow();

int clickCol = clickPoint.getCol();

int num = status[clickRow][clickCol];

String path = head + num + tail;

button.setIcon(new ImageIcon(path));

}

}

}

class SearchNode {

private SearchNode former;

private Point cur;

private int deep;

public SearchNode(SearchNode former, Point cur, int deep) {

this.former = former;

this.cur = cur;

this.deep = deep;

}

public void printPath(){

if(former != null){

former.printPath();

System.out.print("->");

}

if(cur != null){

System.out.print("("+cur.getRow()+","+cur.getCol()+")");

}

}

/**

* @return the cur

*/

public Point getCur() {

return cur;

}

/**

* @param cur

* the cur to set

*/

public void setCur(Point cur) {

this.cur = cur;

}

/**

* @return the forward

*/

public SearchNode getForward() {

return former;

}

/**

* @param forward

* the forward to set

*/

public void setForward(SearchNode forward) {

this.former = forward;

}

/**

* @return the deep

*/

public int getDeep() {

return deep;

}

/**

* @param deep

* the deep to set

*/

public void setDeep(int deep) {

this.deep = deep;

}

}

class Point {

private int row;

private int col;

public Point(int row, int col) {

this.row = row;

this.col = col;

}

/**

* @return the row

*/

public int getRow() {

return row;

}

/**

* @return the col

*/

public int getCol() {

return col;

}

@Override

public String toString() {

return "row:" + row + ",col:" + col;

}

/*

* (non-Javadoc)

*

* @see java.lang.Object#hashCode()

*/

@Override

public int hashCode() {

final int prime = 31;

int result = 1;

result = prime * result + col;

result = prime * result + row;

return result;

}

/*

* (non-Javadoc)

*

* @see java.lang.Object#equals(java.lang.Object)

*/

@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

return false;

Point other = (Point) obj;

if (col != other.col)

return false;

if (row != other.row)

return false;

return true;

}

public int getDistance(Point other) {

int distance = 0;

distance = Math.abs(this.row - other.row)

+ Math.abs(this.col - other.col);

return distance;

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。