用Java代码编写简单的开心消消乐游戏
时间: 2023-09-02 19:08:45 浏览: 148
好的,我可以为您提供一个简单的开心消消乐游戏的Java代码示例,具体如下:
```java
import java.util.Random;
import java.util.Scanner;
public class HappyEliminateGame {
// 游戏界面大小
private static final int ROWS = 8;
private static final int COLS = 8;
// 颜色种类数
private static final int COLORS = 5;
// 颜色代号
private static final char[] COLOR_CODES = {'R', 'G', 'B', 'Y', 'P'};
// 消除的最小组数
private static final int MIN_ELIMINATE = 3;
// 分数
private static int score = 0;
// 游戏界面
private static char[][] board = new char[ROWS][COLS];
// 随机数生成器
private static Random random = new Random();
// 输入读取器
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
initBoard();
printBoard();
while (true) {
System.out.println("请输入要交换的两个方块的坐标,格式为:x1 y1 x2 y2(每个数之间用空格隔开)");
int x1 = scanner.nextInt() - 1;
int y1 = scanner.nextInt() - 1;
int x2 = scanner.nextInt() - 1;
int y2 = scanner.nextInt() - 1;
if (isValidPosition(x1, y1) && isValidPosition(x2, y2)) {
if (isAdjacent(x1, y1, x2, y2)) {
swap(x1, y1, x2, y2);
int eliminateCount = eliminate();
if (eliminateCount >= MIN_ELIMINATE) {
score += eliminateCount * 10;
printBoard();
System.out.println("本次消除了 " + eliminateCount + " 组方块,得分 " + eliminateCount * 10);
} else {
swap(x1, y1, x2, y2);
System.out.println("无法消除,请重新输入");
}
} else {
System.out.println("两个方块不相邻,请重新输入");
}
} else {
System.out.println("坐标不合法,请重新输入");
}
}
}
// 初始化游戏界面
private static void initBoard() {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
board[i][j] = COLOR_CODES[random.nextInt(COLORS)];
}
}
}
// 打印游戏界面
private static void printBoard() {
System.out.println("当前得分:" + score);
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
}
// 判断位置是否合法
private static boolean isValidPosition(int x, int y) {
return x >= 0 && x < ROWS && y >= 0 && y < COLS;
}
// 判断两个位置是否相邻
private static boolean isAdjacent(int x1, int y1, int x2, int y2) {
return Math.abs(x1 - x2) + Math.abs(y1 - y2) == 1;
}
// 交换两个方块
private static void swap(int x1, int y1, int x2, int y2) {
char temp = board[x1][y1];
board[x1][y1] = board[x2][y2];
board[x2][y2] = temp;
}
// 消除相邻的同色方块,并返回消除的组数
private static int eliminate() {
boolean[][] visited = new boolean[ROWS][COLS];
int count = 0;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (!visited[i][j]) {
char color = board[i][j];
int groupSize = dfs(i, j, color, visited);
if (groupSize >= MIN_ELIMINATE) {
count++;
eliminateGroup(i, j, visited);
}
}
}
}
return count;
}
// 搜索相邻的同色方块,并返回组大小
private static int dfs(int x, int y, char color, boolean[][] visited) {
visited[x][y] = true;
int size = 1;
if (isValidPosition(x - 1, y) && !visited[x - 1][y] && board[x - 1][y] == color) {
size += dfs(x - 1, y, color, visited);
}
if (isValidPosition(x + 1, y) && !visited[x + 1][y] && board[x + 1][y] == color) {
size += dfs(x + 1, y, color, visited);
}
if (isValidPosition(x, y - 1) && !visited[x][y - 1] && board[x][y - 1] == color) {
size += dfs(x, y - 1, color, visited);
}
if (isValidPosition(x, y + 1) && !visited[x][y + 1] && board[x][y + 1] == color) {
size += dfs(x, y + 1, color, visited);
}
return size;
}
// 消除同色方块组
private static void eliminateGroup(int x, int y, boolean[][] visited) {
visited[x][y] = true;
board[x][y] = '-';
if (isValidPosition(x - 1, y) && !visited[x - 1][y] && board[x - 1][y] == board[x][y]) {
eliminateGroup(x - 1, y, visited);
}
if (isValidPosition(x + 1, y) && !visited[x + 1][y] && board[x + 1][y] == board[x][y]) {
eliminateGroup(x + 1, y, visited);
}
if (isValidPosition(x, y - 1) && !visited[x][y - 1] && board[x][y - 1] == board[x][y]) {
eliminateGroup(x, y - 1, visited);
}
if (isValidPosition(x, y + 1) && !visited[x][y + 1] && board[x][y + 1] == board[x][y]) {
eliminateGroup(x, y + 1, visited);
}
}
}
```
这个游戏界面是一个 8×8 的方格,共有 5 种颜色。玩家每次可以选择交换两个相邻的方块,如果交换后能够消除三个或以上相邻的同色方块,就会得分并消除这些方块。如果交换后无法消除任何方块,则提示玩家重新输入。游戏没有时间限制,直到玩家选择退出为止。
阅读全文