使用字符串数组在Java中创建一个2D(图)阵列 [英] Using an array of Strings to create a 2D (map) array in Java
问题描述
如何将有可能创造的字符在Java中的二维数组?我研究这个适用于我的地图的目标,你可以围绕上移动人物,但没有发现任何有用的。我这样做(有一点帮助)在C ++之前,虽然不知道如何在Java中。
How would it be possible to create a 2D array of characters in Java? I've researching this as applicable to my goal of a map you can move a character around on, though have not found anything helpful. I have done this (with a little help) in C++ before, though don't know how to in Java.
对于C ++版本,我开始与一维数组的字符串:
For the C++ version, I started with a 1D array of strings:
"#######.#.~~~####.##.......~~~..##.~~....H....######..#######",
"#####..T.#~~~..##.H......~~~...#..T.~~.........######.###....",
"######..~~~~#######...#..~~~.........~~~.##.###..#####.###...",
"...###..~~~.######...##H.~~~.@........~~.#.####...##.H.###...",
".#####..~~.~~###C.....#..~~~.....#...~~~..###..#....##....#..",
"######....~~~.~.##....#..~~~.....#....~~~.#######C...........",
"#######.H..~.~~~~#....#..~~~.....###...~~~.##########........",
"########.H...~~~~~.....~~~......H..##....~~~.H######...T...##",
(你是@符号)
然后能够向上突破每个字符串成独立的字符与嵌套对于打破了它成什么基本上是一个二维数组的循环,你可以围绕移动你的性格2。
(You are the @ Symbol) And then was able to break each string up into separate characters with 2 nested for loops that broke it into what was basically a 2D array that you could move your character around in.
这是做的一个很好的方式,如果是的话怎么样? (我花了10小时了试图获得一个基本版工作)。是否有这样做的更好的办法?我想以后创造一个非常复杂的游戏地图这样,但需要帮助想出如何。
Is this a good way of doing it, if so how? (I've spent 10 hours now attempting to get a basic version to work). Is there a better way of doing this? I would like to create a very complex game later on with a map like this, but need help coming up with how.
推荐答案
做好你需要为你的字符串二维数组?为什么不储存像ABCDEFGHI的字符串,并访问它,如果它是3x3的二维数组?
Well do you need 2D array for your String? Why not store a String like "ABCDEFGHI" and access it as if it was 3x3 2D array?
地图的的x,y 的坐标来的首页的。
Map x,y "coordinates" to index.
public class Char2DDemo
{
public static int ROWS = 3;
public static int COLS = 3;
public static class Char2D
{
private StringBuilder sb = null;
private int ROWS;
private int COLS;
public Char2D(String str, int rows, int cols)
{
this.sb = new StringBuilder(str);
this.ROWS = rows;
this.COLS = cols;
}
public StringBuilder getSb()
{
return sb;
}
public int getRowCount()
{
return ROWS;
}
public int getColCount()
{
return COLS;
}
public int xy2idx(int x, int y)
{
int idx = y * getRowCount() + x;
return idx;
}
public int idx2x(int idx)
{
int x = idx % getRowCount();
return x;
}
public int idx2y(int idx)
{
int y = (idx - idx2x(idx)) / getRowCount();
return y;
}
public Character getCh(int x, int y)
{
return getSb().charAt(xy2idx(x, y));
}
public void setCh(Character ch, int x, int y)
{
getSb().setCharAt(xy2idx(x, y), ch);
}
}
public static void main(String[] args) throws java.lang.Exception
{
String test = "ABC"
+ "DEF"
+ "GHI";
Char2D c2d = new Char2D(test, 3, 3);
System.out.println("Before " + c2d.getSb());
System.out.println("at [2][2] " + c2d.getCh(2, 2));
c2d.setCh('J', 0, 0);
c2d.setCh('K', 2, 2);
c2d.setCh('M', 1, 1);
System.out.println("After " + c2d.getSb());
System.out.println("at [1][0] " + c2d.getCh(1, 0));
}
}
输出:
Before ABCDEFGHI
at [2][2] I
After JBCDMFGHK
at [1][0] B
这篇关于使用字符串数组在Java中创建一个2D(图)阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!