java2048小游戏
时间: 2023-07-09 17:52:26 浏览: 40
好的,我可以和您分享一个简单的Java实现2048小游戏的代码,您可以在此基础上进行扩展和优化。
```
import java.util.Random;
import java.util.Scanner;
public class Game2048 {
private static final int ROWS = 4;
private static final int COLS = 4;
private static int[][] board = new int[ROWS][COLS];
private static boolean gameWon = false;
private static boolean gameLost = false;
private static Random random = new Random();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
initializeBoard();
while (!gameWon && !gameLost) {
printBoard();
System.out.println("Enter move (left, right, up, down): ");
String move = scanner.next();
if (move.equalsIgnoreCase("left")) {
moveLeft();
} else if (move.equalsIgnoreCase("right")) {
moveRight();
} else if (move.equalsIgnoreCase("up")) {
moveUp();
} else if (move.equalsIgnoreCase("down")) {
moveDown();
} else {
System.out.println("Invalid move");
}
if (gameWon) {
System.out.println("Congratulations! You won the game.");
}
if (gameLost) {
System.out.println("Game over. You lost.");
}
}
}
private static void initializeBoard() {
addRandomTile();
addRandomTile();
}
private static void addRandomTile() {
int row, col;
do {
row = random.nextInt(ROWS);
col = random.nextInt(COLS);
} while (board[row][col] != 0);
int value = random.nextInt(10) == 0 ? 4 : 2;
board[row][col] = value;
}
private static void printBoard() {
System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
System.out.print(board[row][col] + "\t");
}
System.out.println();
}
}
private static void moveLeft() {
boolean needToAddTile = false;
for (int row = 0; row < ROWS; row++) {
int lastMergedCol = -1;
for (int col = 1; col < COLS; col++) {
if (board[row][col] != 0) {
int mergeIndex = findMergeIndex(row, col, lastMergedCol);
if (mergeIndex == col) {
lastMergedCol = col;
}
if (mergeIndex != col) {
board[row][mergeIndex] *= 2;
board[row][col] = 0;
lastMergedCol = mergeIndex;
needToAddTile = true;
}
}
}
shiftTiles(row);
}
if (needToAddTile) {
addRandomTile();
}
checkGameOver();
}
private static void moveRight() {
reverseBoard();
moveLeft();
reverseBoard();
}
private static void moveUp() {
transposeBoard();
moveLeft();
transposeBoard();
}
private static void moveDown() {
transposeBoard();
reverseBoard();
moveLeft();
reverseBoard();
transposeBoard();
}
private static int findMergeIndex(int row, int col, int lastMergedCol) {
for (int i = lastMergedCol + 1; i < col; i++) {
if (board[row][i] == board[row][col]) {
return i;
}
}
return col;
}
private static void shiftTiles(int row) {
int[] newRow = new int[COLS];
int index = 0;
for (int col = 0; col < COLS; col++) {
if (board[row][col] != 0) {
newRow[index] = board[row][col];
index++;
}
}
board[row] = newRow;
}
private static void reverseBoard() {
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS / 2; col++) {
int temp = board[row][col];
board[row][col] = board[row][COLS - 1 - col];
board[row][COLS - 1 - col] = temp;
}
}
}
private static void transposeBoard() {
int[][] newBoard = new int[ROWS][COLS];
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
newBoard[col][row] = board[row][col];
}
}
board = newBoard;
}
private static void checkGameOver() {
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
if (board[row][col] == 2048) {
gameWon = true;
}
if (board[row][col] == 0) {
return;
}
if (col < COLS - 1 && board[row][col] == board[row][col + 1]) {
return;
}
if (row < ROWS - 1 && board[row][col] == board[row + 1][col]) {
return;
}
}
}
gameLost = true;
}
}
```
这个代码实现了一个基本的2048游戏,包括打印游戏板、添加随机块、移动块、检测游戏结束等功能。您可以运行这个代码,尝试一下游戏的基本玩法。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)