用java 写一个 2048 小游戏
时间: 2023-12-07 07:08:00 浏览: 129
基于JAVA开发的2048小游戏
好的,以下是 Java 实现的 2048 小游戏代码:
```java
import java.util.*;
public class Game2048 {
private final int size = 4;
private int[][] grid;
private Random rand;
public Game2048() {
grid = new int[size][size];
rand = new Random();
addRandomTile();
addRandomTile();
}
private void addRandomTile() {
List<int[]> emptyCells = new ArrayList<>();
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (grid[i][j] == 0) {
emptyCells.add(new int[]{i, j});
}
}
}
if (!emptyCells.isEmpty()) {
int[] chosenCell = emptyCells.get(rand.nextInt(emptyCells.size()));
grid[chosenCell[0]][chosenCell[1]] = (rand.nextDouble() < 0.9) ? 2 : 4;
}
}
public void move(Direction direction) {
boolean hasMoved = false;
switch (direction) {
case UP:
for (int j = 0; j < size; j++) {
int[] col = getColumn(j);
int[] newCol = merge(col);
setColumn(j, newCol);
if (!hasMoved && !Arrays.equals(col, newCol)) {
hasMoved = true;
}
}
break;
case DOWN:
for (int j = 0; j < size; j++) {
int[] col = getColumn(j);
int[] newCol = merge(reverse(col));
setColumn(j, reverse(newCol));
if (!hasMoved && !Arrays.equals(col, newCol)) {
hasMoved = true;
}
}
break;
case LEFT:
for (int i = 0; i < size; i++) {
int[] row = getRow(i);
int[] newRow = merge(row);
setRow(i, newRow);
if (!hasMoved && !Arrays.equals(row, newRow)) {
hasMoved = true;
}
}
break;
case RIGHT:
for (int i = 0; i < size; i++) {
int[] row = getRow(i);
int[] newRow = merge(reverse(row));
setRow(i, reverse(newRow));
if (!hasMoved && !Arrays.equals(row, newRow)) {
hasMoved = true;
}
}
break;
}
if (hasMoved) {
addRandomTile();
}
}
private int[] getColumn(int j) {
int[] col = new int[size];
for (int i = 0; i < size; i++) {
col[i] = grid[i][j];
}
return col;
}
private void setColumn(int j, int[] col) {
for (int i = 0; i < size; i++) {
grid[i][j] = col[i];
}
}
private int[] getRow(int i) {
return grid[i];
}
private void setRow(int i, int[] row) {
grid[i] = row;
}
private int[] merge(int[] cells) {
int[] newCells = new int[size];
int writePos = 0;
for (int i = 0; i < size; i++) {
if (cells[i] != 0) {
if (writePos > 0 && newCells[writePos - 1] == cells[i]) {
newCells[writePos - 1] *= 2;
} else {
newCells[writePos++] = cells[i];
}
}
}
return newCells;
}
private int[] reverse(int[] cells) {
int[] newCells = new int[size];
for (int i = 0; i < size; i++) {
newCells[i] = cells[size - i - 1];
}
return newCells;
}
public boolean isGameOver() {
if (getEmptyCells().size() > 0) {
return false;
}
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
int cellValue = grid[i][j];
if (i > 0 && grid[i - 1][j] == cellValue) {
return false;
}
if (i < size - 1 && grid[i + 1][j] == cellValue) {
return false;
}
if (j > 0 && grid[i][j - 1] == cellValue) {
return false;
}
if (j < size - 1 && grid[i][j + 1] == cellValue) {
return false;
}
}
}
return true;
}
private List<int[]> getEmptyCells() {
List<int[]> emptyCells = new ArrayList<>();
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (grid[i][j] == 0) {
emptyCells.add(new int[]{i, j});
}
}
}
return emptyCells;
}
public void print() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
System.out.print(grid[i][j] + "\t");
}
System.out.println();
}
}
public static void main(String[] args) {
Game2048 game = new Game2048();
Scanner scanner = new Scanner(System.in);
while (!game.isGameOver()) {
game.print();
System.out.print("Enter direction (up/down/left/right): ");
String input = scanner.nextLine();
switch (input) {
case "up":
game.move(Direction.UP);
break;
case "down":
game.move(Direction.DOWN);
break;
case "left":
game.move(Direction.LEFT);
break;
case "right":
game.move(Direction.RIGHT);
break;
default:
System.out.println("Invalid input");
break;
}
}
System.out.println("Game over!");
game.print();
}
private enum Direction {
UP, DOWN, LEFT, RIGHT
}
}
```
以上代码实现了 2048 小游戏的核心逻辑,包括添加随机方块、合并方块、移动方块、判断是否结束等功能,并提供了一个简单的命令行界面供玩家操作。
阅读全文