请你用java编写一个2048小游戏
时间: 2023-09-13 10:03:08 浏览: 82
好的,以下是使用Java编写的2048小游戏:
```java
import java.util.Scanner;
public class Game2048 {
private int[][] board;
private int score;
private boolean gameOver;
public Game2048() {
board = new int[4][4];
score = 0;
gameOver = false;
addRandomTile();
addRandomTile();
}
public void addRandomTile() {
int row = (int) (Math.random() * 4);
int col = (int) (Math.random() * 4);
while (board[row][col] != 0) {
row = (int) (Math.random() * 4);
col = (int) (Math.random() * 4);
}
board[row][col] = Math.random() < 0.9 ? 2 : 4;
}
public void moveLeft() {
for (int i = 0; i < 4; i++) {
int[] row = board[i];
int[] newRow = merge(row);
board[i] = newRow;
}
}
public void moveRight() {
for (int i = 0; i < 4; i++) {
int[] row = board[i];
int[] newRow = reverse(merge(reverse(row)));
board[i] = newRow;
}
}
public void moveUp() {
for (int j = 0; j < 4; j++) {
int[] col = new int[]{board[0][j], board[1][j], board[2][j], board[3][j]};
int[] newCol = merge(col);
board[0][j] = newCol[0];
board[1][j] = newCol[1];
board[2][j] = newCol[2];
board[3][j] = newCol[3];
}
}
public void moveDown() {
for (int j = 0; j < 4; j++) {
int[] col = new int[]{board[0][j], board[1][j], board[2][j], board[3][j]};
int[] newCol = reverse(merge(reverse(col)));
board[0][j] = newCol[0];
board[1][j] = newCol[1];
board[2][j] = newCol[2];
board[3][j] = newCol[3];
}
}
public int[] merge(int[] rowOrCol) {
int[] merged = new int[4];
int index = 0;
for (int i = 0; i < 4; i++) {
if (rowOrCol[i] != 0) {
if (merged[index] == 0) {
merged[index] = rowOrCol[i];
} else if (merged[index] == rowOrCol[i]) {
merged[index] *= 2;
score += merged[index];
index++;
} else {
index++;
merged[index] = rowOrCol[i];
}
}
}
return merged;
}
public int[] reverse(int[] rowOrCol) {
int[] reversed = new int[4];
for (int i = 0; i < 4; i++) {
reversed[i] = rowOrCol[3 - i];
}
return reversed;
}
public boolean isGameOver() {
if (!isFull()) {
return false;
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (i < 3 && board[i][j] == board[i + 1][j]) {
return false;
}
if (j < 3 && board[i][j] == board[i][j + 1]) {
return false;
}
}
}
return true;
}
public boolean isFull() {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (board[i][j] == 0) {
return false;
}
}
}
return true;
}
public void printBoard() {
System.out.println("Score: " + score);
System.out.println("+------+------+------+------+\n" +
"| " + board[0][0] + " | " + board[0][1] + " | " + board[0][2] + " | " + board[0][3] + " |\n" +
"+------+------+------+------+\n" +
"| " + board[1][0] + " | " + board[1][1] + " | " + board[1][2] + " | " + board[1][3] + " |\n" +
"+------+------+------+------+\n" +
"| " + board[2][0] + " | " + board[2][1] + " | " + board[2][2] + " | " + board[2][3] + " |\n" +
"+------+------+------+------+\n" +
"| " + board[3][0] + " | " + board[3][1] + " | " + board[3][2] + " | " + board[3][3] + " |\n" +
"+------+------+------+------+\n");
}
public void play() {
Scanner scanner = new Scanner(System.in);
while (!gameOver) {
printBoard();
System.out.print("Enter direction (left, right, up, down): ");
String direction = scanner.nextLine();
switch (direction) {
case "left":
moveLeft();
break;
case "right":
moveRight();
break;
case "up":
moveUp();
break;
case "down":
moveDown();
break;
default:
System.out.println("Invalid direction!");
continue;
}
if (!isFull()) {
addRandomTile();
}
gameOver = isGameOver();
}
printBoard();
System.out.println("Game over! Score: " + score);
}
public static void main(String[] args) {
Game2048 game = new Game2048();
game.play();
}
}
```
这个游戏会随机在空白的方格中生成数字 2 或 4,玩家通过按方向键移动方块,将相同数字的方块合并,直到出现数字 2048 或无法继续移动为止。
阅读全文