2048小游戏java源代码
时间: 2023-07-09 09:14:24 浏览: 124
以下是一个简单的 2048 小游戏的 Java 源代码实现:
```
import java.util.*;
public class Game2048 {
private static final int ROWS = 4;
private static final int COLS = 4;
private static final int TARGET = 2048;
private int[][] board;
private boolean isWin;
private boolean isLose;
private Random random;
public Game2048() {
board = new int[ROWS][COLS];
random = new Random();
addRandomTile();
addRandomTile();
isWin = false;
isLose = false;
}
public void move(Direction direction) {
boolean isMoved = false;
switch (direction) {
case UP:
for (int j = 0; j < COLS; j++) {
for (int i = 1; i < ROWS; i++) {
if (board[i][j] != 0) {
int k = i;
while (k > 0 && board[k - 1][j] == 0) {
k--;
}
if (k != i) {
board[k][j] = board[i][j];
board[i][j] = 0;
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 DOWN:
for (int j = 0; j < COLS; j++) {
for (int i = ROWS - 2; i >= 0; i--) {
if (board[i][j] != 0) {
int k = i;
while (k < ROWS - 1 && board[k + 1][j] == 0) {
k++;
}
if (k != i) {
board[k][j] = board[i][j];
board[i][j] = 0;
isMoved = true;
}
if (k < ROWS - 1 && board[k + 1][j] == board[k][j]) {
board[k + 1][j] *= 2;
board[k][j] = 0;
isMoved = true;
}
}
}
}
break;
case LEFT:
for (int i = 0; i < ROWS; i++) {
for (int j = 1; j < COLS; j++) {
if (board[i][j] != 0) {
int k = j;
while (k > 0 && board[i][k - 1] == 0) {
k--;
}
if (k != j) {
board[i][k] = board[i][j];
board[i][j] = 0;
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 RIGHT:
for (int i = 0; i < ROWS; i++) {
for (int j = COLS - 2; j >= 0; j--) {
if (board[i][j] != 0) {
int k = j;
while (k < COLS - 1 && board[i][k + 1] == 0) {
k++;
}
if (k != j) {
board[i][k] = board[i][j];
board[i][j] = 0;
isMoved = true;
}
if (k < COLS - 1 && board[i][k + 1] == board[i][k]) {
board[i][k + 1] *= 2;
board[i][k] = 0;
isMoved = true;
}
}
}
}
break;
}
if (isMoved) {
addRandomTile();
}
checkWin();
checkLose();
}
public boolean isWin() {
return isWin;
}
public boolean isLose() {
return isLose;
}
public int[][] getBoard() {
return board;
}
private void addRandomTile() {
List<int[]> cells = new ArrayList<>();
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (board[i][j] == 0) {
cells.add(new int[]{i, j});
}
}
}
if (!cells.isEmpty()) {
int[] cell = cells.get(random.nextInt(cells.size()));
board[cell[0]][cell[1]] = (random.nextInt(10) == 0) ? 4 : 2;
}
}
private void checkWin() {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (board[i][j] == TARGET) {
isWin = true;
return;
}
}
}
}
private void checkLose() {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (board[i][j] == 0) {
return;
}
if (i > 0 && board[i - 1][j] == board[i][j]) {
return;
}
if (i < ROWS - 1 && board[i + 1][j] == board[i][j]) {
return;
}
if (j > 0 && board[i][j - 1] == board[i][j]) {
return;
}
if (j < COLS - 1 && board[i][j + 1] == board[i][j]) {
return;
}
}
}
isLose = true;
}
public enum Direction {
UP, DOWN, LEFT, RIGHT
}
}
```
这个实现使用了一个二维数组来表示游戏面板,并提供了四个移动方向。每次移动时,会先检查是否有方块可以移动或合并,如果有,就执行移动或合并操作并添加一个随机方块。在执行操作后还会检查游戏是否胜利或失败。
阅读全文