用随机列表中的 2 个值填充二维数组(2048 游戏)
Filling 2D Array with 2 values from a random list (2048 Game)
我正在尝试重新制作 2048 游戏,但遇到了障碍,被难住了。我用二维数组制作了网格,它似乎工作正常。然后我做了一个方法在这个grid/array中存储empty/freespace的列表,这样两个起始数字就可以分配给两个随机的免费spaces。我的问题是我无法让数字显示在实际网格内。我的代码在下面,如果有人能告诉我哪里出错了,我将不胜感激。抱歉,如果我解释得很糟糕,我对 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);
}
}
除非您被要求使用打印到控制台来创建这个游戏 - 我会尽快放弃这种方法。在 space 中创建一个像 2048 这样的游戏,而不是像一个动画窗格那样设计将是非常困难和令人沮丧的。
相反,我会让每个图块成为一个对象,并在 Java 中对摇摆和图形进行一些研究。
如果你必须这样做,(顺便说一句,检查你设置网格的方式 - 它不是一致的间距)你必须打印“|”之间的值字符。
这样做的聪明方法是什么?好吧,您可以创建一个 2D int 数组,int[][]
,并将所有值存储在游戏板上。然后,您可以遍历二维数组中的每个元素,并将其打印在您的“|”之间人物。 下面的这个例子适用于任何大小的二维数组。尝试更改您传入的值的数量并注意它的行为方式.
public class Test {
private static int[][] temp = new int[][] {{0,2, 4},{4, 4,16}, {3, 0, 128}};
public static void main(String[] args) {
int rows = temp.length;
int cols = temp[0].length;
for (int i = 0; i < rows; i++) {
// This will resize our row separator for each row
for (int j = 0; j < cols; j++) {
System.out.print("+-----");
}
System.out.print("+\n");
for (int j = 0; j < cols; j++) {
System.out.print("|");
// Here you have to deal with the spacing. You can see that this is ugly.
// There's definitely better ways to do this, such as using mod
if (temp[i][j] == 0) {System.out.print(" ");}
else if (temp[i][j] < 10) {System.out.print(" " + temp[i][j] + " ");}
else if (temp[i][j] < 100) {System.out.print(" " + temp[i][j] + " ");}
else if (temp[i][j] < 1000) {System.out.print(" " + temp[i][j] + " ");}
}
System.out.print("|\n");
}
for (int j = 0; j < cols; j++) {
System.out.print("+-----");
}
System.out.print("+\n");
}
}
以上程序的输出:
+-----+-----+-----+
| | 2 | 4 |
+-----+-----+-----+
| 4 | 4 | 16 |
+-----+-----+-----+
| 3 | | 128 |
+-----+-----+-----+
但是接下来就是保持间距正确的问题。您需要考虑数字的长度,因为如果数字为空,则必须打印正确数量的 space。或者你必须使用 replace()...这听起来很混乱。
为了回馈@sleepToken 的回答,您可以使用String.format
来填充您的输出:
public class Test {
private static int[][] temp = new int[][] {{0,2, 4},{4, 4,16}, {3, 0, 128}};
public static void main(String[] args) {
int rows = temp.length;
int cols = temp[0].length;
String separator = "+---------------";
for (int i = 0; i < rows; i++) {
// This will resize our row separator for each row
for (int j = 0; j < cols; j++) {
System.out.print(separator);
}
System.out.print("+\n");
for (int j = 0; j < cols; j++) {
System.out.print(String.format("|%5s%5s%5s", "", temp[i][j], ""));
}
System.out.print("|\n");
}
for (int j = 0; j < cols; j++) {
System.out.print(separator);
}
System.out.print("+\n");
}
}
这样填充:
+---------------+---------------+---------------+
| 0 | 2 | 4 |
+---------------+---------------+---------------+
| 4 | 4 | 16 |
+---------------+---------------+---------------+
| 3 | 0 | 128 |
+---------------+---------------+---------------+
可能需要调整填充值,但我的格式最多可以播放 5 位数字。
下面是另一个示例,说明如何根据您希望最大单元格宽度(长度)的长度进行动态填充:
public class Test {
private static int[][] temp = new int[][] {{0,2, 4},{4, 4,16}, {3, 0, 8}};
public static void main(String[] args) {
int rows = temp.length;
int cols = temp[0].length;
int maxCellLength = 15; //define our cell width
int longestDigit = longestDigit(temp); //get the length of our longest diget
if (longestDigit > maxCellLength) maxCellLength = longestDigit + 10; //add extra padding if the longest digit is longer than our cell length
int leftPad = (maxCellLength - longestDigit) / 2;
int rightPad = maxCellLength - longestDigit - leftPad;
String fmt = "|%" + leftPad +"s%" + longestDigit + "s%" + rightPad + "s"; //construct our string format
String separator = "+" + repeat("-", maxCellLength); //construct the separator
for (int i = 0; i < rows; i++) {
// This will resize our row separator for each row
for (int j = 0; j < cols; j++) {
System.out.print(separator);
}
System.out.print("+\n");
for (int j = 0; j < cols; j++) {
System.out.print(String.format(fmt, "", temp[i][j], ""));
}
System.out.print("|\n");
}
for (int j = 0; j < cols; j++) {
System.out.print(separator);
}
System.out.print("+\n");
}
public static int longestDigit(int[][] arr) {
int longest = 0;
for(int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
String intStr = String.valueOf(arr[i][j]);
if (intStr.length() > longest) longest = intStr.length();
}
}
return longest;
}
private static String repeat(String n, int length) {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < length; i++) {
sb.append(n);
}
return sb.toString();
}
}
我正在尝试重新制作 2048 游戏,但遇到了障碍,被难住了。我用二维数组制作了网格,它似乎工作正常。然后我做了一个方法在这个grid/array中存储empty/freespace的列表,这样两个起始数字就可以分配给两个随机的免费spaces。我的问题是我无法让数字显示在实际网格内。我的代码在下面,如果有人能告诉我哪里出错了,我将不胜感激。抱歉,如果我解释得很糟糕,我对 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);
}
}
除非您被要求使用打印到控制台来创建这个游戏 - 我会尽快放弃这种方法。在 space 中创建一个像 2048 这样的游戏,而不是像一个动画窗格那样设计将是非常困难和令人沮丧的。
相反,我会让每个图块成为一个对象,并在 Java 中对摇摆和图形进行一些研究。
如果你必须这样做,(顺便说一句,检查你设置网格的方式 - 它不是一致的间距)你必须打印“|”之间的值字符。
这样做的聪明方法是什么?好吧,您可以创建一个 2D int 数组,int[][]
,并将所有值存储在游戏板上。然后,您可以遍历二维数组中的每个元素,并将其打印在您的“|”之间人物。 下面的这个例子适用于任何大小的二维数组。尝试更改您传入的值的数量并注意它的行为方式.
public class Test {
private static int[][] temp = new int[][] {{0,2, 4},{4, 4,16}, {3, 0, 128}};
public static void main(String[] args) {
int rows = temp.length;
int cols = temp[0].length;
for (int i = 0; i < rows; i++) {
// This will resize our row separator for each row
for (int j = 0; j < cols; j++) {
System.out.print("+-----");
}
System.out.print("+\n");
for (int j = 0; j < cols; j++) {
System.out.print("|");
// Here you have to deal with the spacing. You can see that this is ugly.
// There's definitely better ways to do this, such as using mod
if (temp[i][j] == 0) {System.out.print(" ");}
else if (temp[i][j] < 10) {System.out.print(" " + temp[i][j] + " ");}
else if (temp[i][j] < 100) {System.out.print(" " + temp[i][j] + " ");}
else if (temp[i][j] < 1000) {System.out.print(" " + temp[i][j] + " ");}
}
System.out.print("|\n");
}
for (int j = 0; j < cols; j++) {
System.out.print("+-----");
}
System.out.print("+\n");
}
}
以上程序的输出:
+-----+-----+-----+
| | 2 | 4 |
+-----+-----+-----+
| 4 | 4 | 16 |
+-----+-----+-----+
| 3 | | 128 |
+-----+-----+-----+
但是接下来就是保持间距正确的问题。您需要考虑数字的长度,因为如果数字为空,则必须打印正确数量的 space。或者你必须使用 replace()...这听起来很混乱。
为了回馈@sleepToken 的回答,您可以使用String.format
来填充您的输出:
public class Test {
private static int[][] temp = new int[][] {{0,2, 4},{4, 4,16}, {3, 0, 128}};
public static void main(String[] args) {
int rows = temp.length;
int cols = temp[0].length;
String separator = "+---------------";
for (int i = 0; i < rows; i++) {
// This will resize our row separator for each row
for (int j = 0; j < cols; j++) {
System.out.print(separator);
}
System.out.print("+\n");
for (int j = 0; j < cols; j++) {
System.out.print(String.format("|%5s%5s%5s", "", temp[i][j], ""));
}
System.out.print("|\n");
}
for (int j = 0; j < cols; j++) {
System.out.print(separator);
}
System.out.print("+\n");
}
}
这样填充:
+---------------+---------------+---------------+
| 0 | 2 | 4 |
+---------------+---------------+---------------+
| 4 | 4 | 16 |
+---------------+---------------+---------------+
| 3 | 0 | 128 |
+---------------+---------------+---------------+
可能需要调整填充值,但我的格式最多可以播放 5 位数字。
下面是另一个示例,说明如何根据您希望最大单元格宽度(长度)的长度进行动态填充:
public class Test {
private static int[][] temp = new int[][] {{0,2, 4},{4, 4,16}, {3, 0, 8}};
public static void main(String[] args) {
int rows = temp.length;
int cols = temp[0].length;
int maxCellLength = 15; //define our cell width
int longestDigit = longestDigit(temp); //get the length of our longest diget
if (longestDigit > maxCellLength) maxCellLength = longestDigit + 10; //add extra padding if the longest digit is longer than our cell length
int leftPad = (maxCellLength - longestDigit) / 2;
int rightPad = maxCellLength - longestDigit - leftPad;
String fmt = "|%" + leftPad +"s%" + longestDigit + "s%" + rightPad + "s"; //construct our string format
String separator = "+" + repeat("-", maxCellLength); //construct the separator
for (int i = 0; i < rows; i++) {
// This will resize our row separator for each row
for (int j = 0; j < cols; j++) {
System.out.print(separator);
}
System.out.print("+\n");
for (int j = 0; j < cols; j++) {
System.out.print(String.format(fmt, "", temp[i][j], ""));
}
System.out.print("|\n");
}
for (int j = 0; j < cols; j++) {
System.out.print(separator);
}
System.out.print("+\n");
}
public static int longestDigit(int[][] arr) {
int longest = 0;
for(int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
String intStr = String.valueOf(arr[i][j]);
if (intStr.length() > longest) longest = intStr.length();
}
}
return longest;
}
private static String repeat(String n, int length) {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < length; i++) {
sb.append(n);
}
return sb.toString();
}
}