简体   繁体   English

使用String的JAVA 2D地图? 即“ +”和“ C”

[英]JAVA 2D Map using String? i.e. “+” and “C”

so I've been looking but I simply just don't know how to state my problem. 所以我一直在寻找,但是我只是不知道如何陈述我的问题。

So I'm just going to break an egg, and if you can link to the correct answer anyhow then please don't be afraid to, this is a long shot and I know this exists many places, I am just unable to find it. 因此,我将打破一个鸡蛋,如果您可以以任何方式链接到正确的答案,那么请不要害怕,这是一个漫长的过程,而且我知道这里存在很多地方,我只是找不到它。

I am looking at making a 2D map, based off on PLUS signs (+) and ONE (C), the C is the characters current location. 我正在寻找一个基于加号(+)和一个(C)的2D地图,C是字符的当前位置。

It would look like this 看起来像这样

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

When printed. 打印时。 Notice C is based off of integers, namely currentX and currentY (1 & 1). 注意C基于整数,即currentX和currentY(1&1)。

This is my current code in bp_Map.class 这是我在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++;
    }
}

I could use an array, instead of mapP and mapC and just do 我可以使用一个数组,而不是mapP和mapC,只是做

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

But I don't feel the need to do this atm. 但是我觉得不需要这样做。

My current problem is I don't want 20 if statements (or 1 if and 19 if else statements) to check the location of X, and then print correspondingly Y. 我当前的问题是我不希望20个if语句(或1个if语句和19个if else语句)检查X的位置,然后相应地打印Y。

I am new to java and still learning, I have used while, but should I use for? 我是Java的新手,仍然在学习,我已经使用了一段时间,但是我应该用于吗? I'm a bit lost, hope you guys can help me. 我有点迷茫,希望你们能帮助我。 This is for a text-based rpg, and I'm working on it alongside my studies. 这是针对基于文本的rpg,我正在与研究一起进行研究。

An approach I would use in this case is the following in pseudo-code: 在这种情况下,我将使用以下伪代码中的一种方法:

  1. Create a character matrix with the dimensions of sizeX by sizeY 创建一个尺寸为sizeX x sizeY的字符矩阵
  2. Use the java.util.Arrays.fill builtin to fill the entire matrix with the character '+' 使用内置的java.util.Arrays.fill ,用字符“ +”填充整个矩阵
  3. Replace the character at position {currX, currY} (1-indexed) with character 'C' 用字符“ C”替换位置{currX, currY} (1索引)处的字符
  4. Pretty-print the matrix 漂亮地打印矩阵

Here a possible implementation of what I described above: 这是我上面描述的可能的实现:

/*
 * 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();
  }
}

This could then be called with: 然后可以这样调用:

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

Which will output: 将输出:

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

Try it online. 在线尝试。

Some things to note: 注意事项:

  1. I've added the size and characters as parameter to the method. 我已经将大小和字符添加为方法的参数。 In my TIO-link above you can see a call with different sizes or characters also works (ie m.drawMap(5, 5, 'a', 'B', 5, 5); ). 在上面的我的TIO链接中,您可以看到具有不同大小或字符的调用也有效( m.drawMap(5, 5, 'a', 'B', 5, 5); )。
  2. I've added a TODO for validation checks. 我添加了一个待办事项以进行验证检查。 If the given posX or poxY are larger than the sizeX or sizeY respectively, it will of course give an ArrayOutOfBoundsException. 如果给定的posXpoxY分别大于sizeXsizeY ,则当然会给出ArrayOutOfBoundsException。 So perhaps a check at the top of the method to see if the given pos -parameters are valid is in order depending on how you want to use it. 因此,也许在方法顶部检查一下给定的pos -parameters是否有效,这取决于您要如何使用它。

You don't need if-else cases - this is a perfect usage example for loops . 您不需要if-else情况-这是loops的完美用法示例。

First of all, define things which will never change as final fields in your class: 首先,定义永远不会更改的内容作为您班级的最终领域:

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;

For this example, I'll be using fields for the current X and Y coordinates too, but you may want to change this since I assume they come from elsewhere in your program: 对于此示例,我也将使用当前X和Y坐标的字段,但是您可能想要更改此设置,因为我认为它们来自程序的其他位置:

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

Now, think of how a TV draws pixels on a screen: from the top to the bottom and from left to right, pixel by pixel, at least 30 times a second. 现在,考虑一下电视如何在屏幕上绘制像素:从上到下,从左到右,逐像素,至少每秒30次。 Let's try and do the same, and draw one row at a time: 让我们尝试做同样的事情,一次绘制一行:

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);
    }
}

What would the drawRow() function look like? drawRow()函数是什么样的? One possible implementation is below: 下面是一种可能的实现:

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());
}

This produces: 这将产生:

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

Try playing around with the coordinates and run main again. 尝试使用坐标,然后再次运行main This is just one of many possible solutions - the neat thing about the above code is that no Map or even array is needed, but it may be that you do need them eventually and the code would have to change to accommodate this (eg keep a bit matrix, make a nested for loop over it and draw the set bit as the character ). 这只是许多可能的解决方案之一-上面代码的整洁之处在于,不需要Map甚至数组,但最终您可能确实需要它们,并且代码必须进行更改以适应此问题(例如, 保留一个位矩阵,在其上进行嵌套的for循环,然后将设置的位绘制为字符 )。 Let us know if you would like an example of this. 让我们知道您是否想举个例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM