使用字符串的 JAVA 2D 地图?即 "+"和"C"



所以我一直在寻找,但我只是不知道如何陈述我的问题。

所以我只是要打破一个鸡蛋,如果你可以链接到正确的答案,那么请不要害怕,这是一个很长的机会,我知道这存在于很多地方,我只是找不到它。

我正在考虑制作基于加号 (+( 和 ONE (C( 的 2D 地图,C 是字符当前位置。

它看起来像这样

C+++++++++++++++++++
++++++++++++++++++++
++++++++++++++++++++
++++++++++++++++++++
++++++++++++++++++++

打印时。 注意C基于整数,即currentX和currentY(1和1(。

这是我当前的代码bp_Map.class

public class bp_Map {
// Map
public static String mapP = "+";
public static String mapC = "C";
public static int sizeY = 19;
public static int sizeX = 19;

public static void drawMap(int currX, int currY) {
int currentY = 0;
while (currentY <= sizeY) {
drawX();
System.out.print("n");
currentY ++;
}
}
public static void drawX() {
int currentX = 0;
while (currentX <= sizeX) {
System.out.print(mapP);
currentX++;
}
}

我可以使用数组,而不是mapP和mapC,然后做

public static final String mapChar[] = {"+", "C"}

但我不觉得有必要做这个自动取款机。

我目前的问题是我不希望 20 if 语句(或 1 if 和 19 if else 语句(检查 X 的位置,然后相应地打印 Y。

我是Java的新手,还在学习,我已经用过了,但是我应该用吗?我有点迷茫,希望你们能帮到我。这是一个基于文本的角色扮演游戏,我正在研究它。

你不需要 if-else 案例 - 这是循环的完美使用示例。

首先,将永远不会更改的内容定义为类中的最终字段:

private static final String EMPTY = "+";
private static final String CHARACTER = "C";
private static final int SIZE_X = 20;
private static final int SIZE_Y = 5;

对于此示例,我也将使用当前 X 和 Y 坐标的字段,但您可能希望更改此设置,因为我假设它们来自程序的其他地方:

private static int currentX = 7;
private static int currentY = 3;

现在,想想电视如何在屏幕上绘制像素:从上到下,从左到右,逐个像素,每秒至少 30 次。让我们尝试做同样的事情,一次绘制一行:

public static void main(String[] args) {
if(currentX > SIZE_X - 1 || currentY > SIZE_Y - 1) {
throw new IllegalStateException("Out of bounds");
}
for (int y = 0; y < SIZE_Y; y++) {
drawRow(y);
}
}

drawRow()函数会是什么样子?一种可能的实现如下:

private static void drawRow(int i) {
// Use a StringBuilder, ~30 times faster than String concatenation!
StringBuilder row = new StringBuilder();
if(i == currentY) {
// Create this row differently, as it contains the character.
for (int x = 0; x < SIZE_X; x++) {
if(x == currentX) {
row.append(CHARACTER);
} else {
row.append(EMPTY);
}
}
} else {
// Create an empty row.
for (int x = 0; x < SIZE_X; x++) {
row.append(EMPTY);
}
}
// "Draw" the row by printing it to the console.
System.out.println(row.toString());
}

这会产生:

++++++++++++++++++++
++++++++++++++++++++
++++++++++++++++++++
+++++++C++++++++++++
++++++++++++++++++++

尝试使用坐标并再次运行main。这只是许多可能的解决方案之一 - 上面代码的巧妙之处在于不需要Map甚至数组,但最终可能确实需要它们,并且代码必须更改以适应这一点(例如保留位矩阵,在其上创建一个嵌套的 for 循环并将设置位绘制为字符(。如果您想要一个例子,请告诉我们。

在这种情况下,我将在伪代码中使用以下方法:

  1. 创建维度为sizeXbysizeY的字符矩阵
  2. 使用内置的java.util.Arrays.fill用字符"+"填充整个矩阵
  3. 将位置{currX, currY}(1 索引(处的字符替换为字符"C">
  4. 漂亮打印矩阵

以下是我上面描述的可能实现:

/*
* Prints a block of sizeX by sizeY of the filler character,
* where the character at position {posX, posY} (1-indexed) is replaced with the replacement character
* 
* TODO: Validation checks. Currently assumes posX and posY are always within range of the matrix
*/
public void drawMap(int sizeX, int sizeY, char fillerChar, char replacementChar, int posX, int posY){
// Create a char-matrix of dimensions sizeX by sizeY
char[][] matrix = new char[sizeX][sizeY];
// Fill this matrix initially with the filler-character
for(char[] row : matrix)
java.util.Arrays.fill(row, fillerChar);
// Replace the character at position {currX, currY} (1-indexed) with the replacement-character
matrix[posX-1][posY-1] = replacementChar;
// Print the matrix
prettyPrintMatrix(matrix);
}
private void prettyPrintMatrix(char[][] matrix){
for(char[] row : matrix){
for(char ch : row)
System.out.print(ch);
System.out.println();
}
}

然后可以调用:

drawMap(10, 10, '+', 'C', 4, 2);

这将输出:

++++++++++
++++++++++
++++++++++
+C++++++++
++++++++++
++++++++++
++++++++++
++++++++++
++++++++++
++++++++++

在线试用。

需要注意的一些事项:

  1. 我已将大小和字符作为参数添加到方法中。在上面的TIO链接中,您可以看到具有不同大小或字符的呼叫也可以工作(即m.drawMap(5, 5, 'a', 'B', 5, 5);(。
  2. 我添加了一个用于验证检查的待办事项。如果给定的posXpoxY分别大于sizeXsizeY,它当然会给出一个ArrayOutOfBoundsException。因此,也许在方法顶部检查给定的pos参数是否有效,具体取决于您希望如何使用它。

相关内容

最新更新