Java 用随机列表中的2个值填充2D数组(2048游戏)
我试图重现2048年的比赛,但我撞到了砖墙,被难倒了。我已经用2d阵列制作了网格,看起来工作正常。然后我制作了一个方法,在这个网格/数组中存储一个空/空闲空间列表,这样两个起始数字就可以分配给两个随机的空闲空间。我的问题是,我无法在实际网格中显示数字。我下面有我的代码,如果有人能告诉我哪里出了问题,我将不胜感激。抱歉,如果我解释得很糟糕,我对Java还是相当陌生的Java 用随机列表中的2个值填充2D数组(2048游戏),java,arrays,2048,Java,Arrays,2048,我试图重现2048年的比赛,但我撞到了砖墙,被难倒了。我已经用2d阵列制作了网格,看起来工作正常。然后我制作了一个方法,在这个网格/数组中存储一个空/空闲空间列表,这样两个起始数字就可以分配给两个随机的空闲空间。我的问题是,我无法在实际网格中显示数字。我下面有我的代码,如果有人能告诉我哪里出了问题,我将不胜感激。抱歉,如果我解释得很糟糕,我对Java还是相当陌生的 import java.util.Arrays; import java.util.Random; import java.util
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);
}
}
导入java.util.array;
导入java.util.Random;
导入java.util.Scanner;
公共班机{
//游戏板尺寸方法-获取用于游戏板尺寸的用户编号
公共静态int gameBoardSize(){
整数=0;
int行=0;
扫描仪输入=新扫描仪(系统输入);
System.out.println(“欢迎使用1024”);
System.out.print(“请选择一个介于4和8之间的数字,以确定游戏板的大小:”;//提示用户选择数字
number=in.nextInt();//将数字存储在变量中
如果(number>=4&&number=4和,除非要求您使用打印到控制台来创建此游戏-我会尽快放弃此方法。在一个没有设计成动画窗格的空间中创建类似2048的游戏将是非常困难和令人沮丧的
相反,我希望每个tile都是一个对象,并在Java中对swing和图形进行一些研究
如果必须这样做,(顺便说一下,请检查设置网格的方式-间距不一致),则必须打印“|”字符之间的值
什么是聪明的方法?好吧,你可以创建一个2D int数组,int[][]
,并将所有值存储在游戏板上。然后,你可以循环2D数组中的每个元素,并在“|”之间打印它字符。下面的示例适用于任何大小的二维数组。请尝试更改传入的值的数量,并注意其行为
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");
}
}
但还有一个问题是如何保持正确的间距。您需要考虑数字的长度,因为如果数字为空,则必须打印正确的空格数。否则,您必须使用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");
}
}
可能需要使用填充值,但我使用的格式可以很好地播放5位数字
下面是另一个示例,说明如何根据最大单元格宽度(长度)的长度进行动态填充:
公共类测试{
私有静态int[][]temp=newint[][{{0,2,4},{4,4,16},{3,0,8};
公共静态void main(字符串[]args){
int行=临时长度;
int cols=temp[0]。长度;
int maxCellLength=15;//定义单元格宽度
int longestDigit=longestDigit(temp);//获取最长数字的长度
if(longestDigit>maxCellLength)maxCellLength=longestDigit+10;//如果最长的数字比我们的单元格长度长,请添加额外的填充
int leftPad=(maxCellLength-longestDigit)/2;
int rightPad=maxCellLength-LongestDigital-leftPad;
String fmt=“|%”+leftPad+“s%”+longestDigit+“s%”+rightPad+“s”;//构造我们的字符串格式
字符串分隔符=“+”+重复(“-”,maxCellLength);//构造分隔符
对于(int i=0;ilongest)longest=intStr.length();
}
}
返回时间最长;
}
私有静态字符串重复(字符串n,整数长度){
StringBuilder sb=新的StringBuilder();
for(int i=0;i
Token,感谢您的回复。不幸的是,我需要使用打印控制台创建此项。我知道我的间距仅适用于空白单元格的大小。我目前无法确定如何在网格/单元格中打印数字,因此我还无法解决大小问题。感谢您的回复,我将测试您的代码,以获取su我知道必须有一种方法来智能化我天真的if语句,lol+1更新了我的答案,添加了更多单元格宽度的动态计算和填充
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 |
+---------------+---------------+---------------+
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();
}
}