用Java写一个连连看游戏的代码
时间: 2024-06-08 13:11:04 浏览: 17
以下是用Java实现连连看游戏的简单代码示例:
```
import java.util.ArrayList;
import java.util.Scanner;
public class LianLianKan {
private static int ROWS = 6; // 游戏区域行数
private static int COLS = 6; // 游戏区域列数
private static int[][] map = new int[ROWS][COLS]; // 游戏区域地图
private static ArrayList<Integer> list = new ArrayList<>(); // 存储可行路径
public static void main(String[] args) {
initMap(); // 初始化游戏区域地图
printMap(); // 打印游戏区域地图
playGame(); // 开始游戏
}
// 初始化游戏区域地图
private static void initMap() {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
map[i][j] = (int) (Math.random() * 6) + 1; // 随机生成数字(1~6)
}
}
}
// 打印游戏区域地图
private static void printMap() {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
System.out.print(map[i][j] + " ");
}
System.out.println();
}
}
// 开始游戏
private static void playGame() {
while (true) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入选中的两个方块的坐标(x1,y1,x2,y2),如:1,2,3,4");
String[] input = scanner.nextLine().split(",");
int x1 = Integer.parseInt(input[0]) - 1; // 转换为数组下标
int y1 = Integer.parseInt(input[1]) - 1;
int x2 = Integer.parseInt(input[2]) - 1;
int y2 = Integer.parseInt(input[3]) - 1;
if (map[x1][y1] != map[x2][y2]) { // 两个方块数字不相同
System.out.println("不可消除,请重新选择!");
continue;
}
if (!isLink(x1, y1, x2, y2)) { // 两个方块无法连通
System.out.println("不可消除,请重新选择!");
continue;
}
map[x1][y1] = 0; // 消除选中的两个方块
map[x2][y2] = 0;
printMap(); // 打印游戏区域地图
if (isGameOver()) { // 游戏结束
System.out.println("恭喜你,游戏胜利!");
break;
}
}
}
// 判断两个方块是否可以连通
private static boolean isLink(int x1, int y1, int x2, int y2) {
if (x1 == x2 && y1 == y2) { // 同一方块
return false;
}
if (map[x1][y1] != map[x2][y2]) { // 数字不相同
return false;
}
if (isDirectLink(x1, y1, x2, y2)) { // 直接相连
return true;
}
if (isOneCornerLink(x1, y1, x2, y2)) { // 一拐角连接
return true;
}
if (isTwoCornerLink(x1, y1, x2, y2)) { // 二拐角连接
return true;
}
return false;
}
// 判断两个方块是否直接相连
private static boolean isDirectLink(int x1, int y1, int x2, int y2) {
if (x1 == x2) { // 在同一行
int min = Math.min(y1, y2);
int max = Math.max(y1, y2);
for (int i = min + 1; i < max; i++) {
if (map[x1][i] != 0) { // 中间有障碍物
return false;
}
}
return true;
}
if (y1 == y2) { // 在同一列
int min = Math.min(x1, x2);
int max = Math.max(x1, x2);
for (int i = min + 1; i < max; i++) {
if (map[i][y1] != 0) { // 中间有障碍物
return false;
}
}
return true;
}
return false;
}
// 判断两个方块是否一拐角连接
private static boolean isOneCornerLink(int x1, int y1, int x2, int y2) {
if (map[x1][y2] == 0 && isDirectLink(x1, y1, x1, y2) && isDirectLink(x1, y2, x2, y2)) { // 上下连接
return true;
}
if (map[x2][y1] == 0 && isDirectLink(x1, y1, x2, y1) && isDirectLink(x2, y1, x2, y2)) { // 左右连接
return true;
}
return false;
}
// 判断两个方块是否二拐角连接
private static boolean isTwoCornerLink(int x1, int y1, int x2, int y2) {
for (int i = 0; i < ROWS; i++) {
if (i == x1 || i == x2) { // 两个方块在同一行
continue;
}
if (map[i][y1] == 0 && isDirectLink(x1, y1, i, y1) && isOneCornerLink(i, y1, x2, y2)) { // 上下左右连接
return true;
}
}
for (int j = 0; j < COLS; j++) {
if (j == y1 || j == y2) { // 两个方块在同一列
continue;
}
if (map[x1][j] == 0 && isDirectLink(x1, y1, x1, j) && isOneCornerLink(x1, j, x2, y2)) { // 上下左右连接
return true;
}
}
return false;
}
// 判断游戏是否结束
private static boolean isGameOver() {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (map[i][j] != 0) { // 还有未消除的方块
return false;
}
}
}
return true;
}
}
```
说明:
1. 初始化游戏区域地图:使用一个二维数组表示游戏区域,每个元素存储一个数字(1~6)。
2. 打印游戏区域地图:按行列顺序遍历二维数组,输出每个元素。
3. 开始游戏:使用一个死循环,每次从控制台读取用户输入的两个方块的坐标,进行判断是否可以消除。如果可以消除,则将两个方块的数字设置为0,并重新打印游戏区域地图。如果游戏结束,则跳出循环。
4. 判断两个方块是否可以连通:使用DFS算法,从第一个方块出发,搜索到第二个方块的所有可行路径,并存储在一个ArrayList中。如果存在可行路径,则两个方块可以消除。
5. 判断两个方块是否直接相连:判断两个方块是否在同一行或同一列,并在它们之间没有障碍物。
6. 判断两个方块是否一拐角连接:在两个方块的上下或左右方向上,存在一块空地,使得两个方块分别与这块空地直接相连。
7. 判断两个方块是否二拐角连接:在两个方块的上下或左右方向上,存在一块空地,使得两个方块分别与这块空地一拐角相连,且这块空地与第二个方块的上下或左右方向上也存在一块空地,使得这块空地与第二个方块一拐角相连。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)