我正在尝试重新创建 2048 游戏,但我撞到了一堵砖墙并被难住了。我用二维数组制作了我的网格,它似乎工作正常。然后我做了一个方法来在这个网格/数组中存储一个空白/可用空间列表,以便可以将两个起始数字分配给两个随机可用空间。我的问题是我无法让数字显示在实际网格中。我在下面有我的代码,如果有人能告诉我我哪里出错了,我将不胜感激。抱歉,如果我对此进行了可怕的解释,我对 Java 还是很陌生。
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class Main {
//Game Board Size Method - Getting User Number to use for dimension of game board
public static int gameBoardSize() {
int number = 0;
int row = 0;
Scanner in = new Scanner(System.in);
System.out.println("Welcome to 1024");
System.out.print("Please select a number between 4 & 8 to determine the size of your game board: "); //Prompt user to select number
number = in.nextInt(); //Storing number in variable
if (number >= 4 && number <= 8) { //If number is >= 4 and <= 8
row = number; //Assign number to row variable
} else {
System.out.print("Error, please select a number between 4 & 8"); //Error print message
}
return row; //Return
}
//Display Game board method - Array for game board grid and to display the grid.
public static void displayGameBoard(int[][] gameBoard, int gameBoardSize) {
String divider;
switch (gameBoardSize) {
case 5:
divider = "\n+-----+-----+-----+-----+-----+"; //User Number 5
break;
case 6:
divider = "\n+-----+-----+-----+-----+-----+-----+"; //User Number 6
break;
case 7:
divider = "\n+-----+-----+-----+-----+-----+-----+-----+"; //User Number 7
break;
case 8:
divider = "\n+-----+-----+-----+-----+-----+-----+-----+-----+"; //User Number 8
break;
default:
divider = "\n+----+-----+-----+----+"; //User Number 4
}
System.out.println(divider); //Printing Divider at top
for (int i = 0; i < gameBoard.length; i++) {
for (int j = 0; j < gameBoard[i].length; j++) {
if (gameBoard[i][j] == 0) { //If both i & j is == 0
System.out.print("| "); //Print this left border
}
if (j == gameBoard[j].length - 1) { //If 2nd array length -1 (end)
System.out.print("|"); //Print end border
}
}
System.out.println(divider); //Printing Divider at bottom
}
}
public static int[][] createGameBoard(int userRows) {
return new int[userRows][userRows]; //Returning rows
}
//Method to loop through array to find empty space
public static int[][] findEmptyCells(int[][] gameBoard) {
int freeCellCount = 0;
int[][] emptyList = new int[gameBoard.length * gameBoard.length][2];
for (int i = 0; i < gameBoard.length; i++) {
for (int j = 0; j < gameBoard[i].length; j++) {
if (gameBoard[i][j] == 0) {
emptyList[freeCellCount][0] = i;
emptyList[freeCellCount][1] = j;
freeCellCount++;
}
Random rnd = new Random();
int rndPair = rnd.nextInt(freeCellCount);
emptyList[rndPair][0] = i;
emptyList[rndPair][1] = j;
}
}
return emptyList;
}
//Use WASD: W for up, S for Down, A for Left and D for Right
public static void instructions() {
System.out.println("How to Play");
System.out.println("Press W to move up");
System.out.println("Press S to move down");
System.out.println("Press A to move left");
System.out.println("Press D to move right");
}
public static void main(String[] args) {
int rows = gameBoardSize();
int[][] gameBoard = createGameBoard(rows);
displayGameBoard(gameBoard, rows);
instructions();
int[][] findEmptyCells = findEmptyCells(gameBoard);
}
}