请你用Java写一个2048小游戏
时间: 2024-03-06 15:48:43 浏览: 51
一个用Java写的2048小游戏.zip
好的,以下是一个简单的Java实现2048小游戏的代码:
```
import java.util.Scanner;
public class Game2048 {
private static int[][] board = new int[4][4];
private static boolean isGameOver = false;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
initBoard();
printBoard();
while (!isGameOver) {
System.out.print("请输入方向(w:上, s:下, a:左, d:右):");
String direction = scanner.nextLine();
move(direction);
if (!isGameOver) {
generateNumber();
printBoard();
}
}
System.out.println("游戏结束!");
}
private static void initBoard() {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
board[i][j] = 0;
}
}
generateNumber();
generateNumber();
}
private static void generateNumber() {
int x, y;
do {
x = (int) (Math.random() * 4);
y = (int) (Math.random() * 4);
} while (board[x][y] != 0);
board[x][y] = Math.random() < 0.9 ? 2 : 4;
}
private static void printBoard() {
System.out.println("┌───┬───┬───┬───┐");
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
System.out.printf("│%4d", board[i][j]);
}
System.out.println("│\n├───┼───┼───┼───┤");
}
System.out.println("└───┴───┴───┴───┘");
}
private static void move(String direction) {
boolean isMoved = false;
switch (direction) {
case "w": // 上移
for (int j = 0; j < board[0].length; j++) {
for (int i = 1; i < board.length; i++) {
if (board[i][j] != 0) {
int k = i;
while (k > 0 && board[k - 1][j] == 0) {
board[k - 1][j] = board[k][j];
board[k][j] = 0;
k--;
isMoved = true;
}
if (k > 0 && board[k - 1][j] == board[k][j]) {
board[k - 1][j] *= 2;
board[k][j] = 0;
isMoved = true;
}
}
}
}
break;
case "s": // 下移
for (int j = 0; j < board[0].length; j++) {
for (int i = board.length - 2; i >= 0; i--) {
if (board[i][j] != 0) {
int k = i;
while (k < board.length - 1 && board[k + 1][j] == 0) {
board[k + 1][j] = board[k][j];
board[k][j] = 0;
k++;
isMoved = true;
}
if (k < board.length - 1 && board[k + 1][j] == board[k][j]) {
board[k + 1][j] *= 2;
board[k][j] = 0;
isMoved = true;
}
}
}
}
break;
case "a": // 左移
for (int i = 0; i < board.length; i++) {
for (int j = 1; j < board[i].length; j++) {
if (board[i][j] != 0) {
int k = j;
while (k > 0 && board[i][k - 1] == 0) {
board[i][k - 1] = board[i][k];
board[i][k] = 0;
k--;
isMoved = true;
}
if (k > 0 && board[i][k - 1] == board[i][k]) {
board[i][k - 1] *= 2;
board[i][k] = 0;
isMoved = true;
}
}
}
}
break;
case "d": // 右移
for (int i = 0; i < board.length; i++) {
for (int j = board[i].length - 2; j >= 0; j--) {
if (board[i][j] != 0) {
int k = j;
while (k < board[i].length - 1 && board[i][k + 1] == 0) {
board[i][k + 1] = board[i][k];
board[i][k] = 0;
k++;
isMoved = true;
}
if (k < board[i].length - 1 && board[i][k + 1] == board[i][k]) {
board[i][k + 1] *= 2;
board[i][k] = 0;
isMoved = true;
}
}
}
}
break;
default:
System.out.println("无效的方向!");
}
if (isMoved) {
checkGameOver();
}
}
private static void checkGameOver() {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (board[i][j] == 0) {
return;
}
if (i < board.length - 1 && board[i][j] == board[i + 1][j]) {
return;
}
if (j < board[i].length - 1 && board[i][j] == board[i][j + 1]) {
return;
}
}
}
isGameOver = true;
}
}
```
玩家每次输入方向键后,程序会将数字方块向该方向移动,并在空白处随机生成一个新的数字方块(2或4)。如果相邻的两个数字方块的数字相同,则它们会合并成一个新的数字方块,数字为原来两个数字的和。当所有方块都被填满,且相邻的数字方块没有相同的数字时,游戏结束。
阅读全文