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.
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...##",
(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.
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.
You can create a 2D array in Java by doing
For example,
would create an array that is 2 rows tall and 3 columns wide. Therefore, you could have your character move up and down the grid by changing the row, and have it move left and right by changing the column.
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?
Map x,y "coordinates" to index.
Output:
The answer for this question is exactly what you want but with integers.
But I really recommend you use some solution using Objects instead. Since you are using Java.
I don't know your project but I imagine that is a tile based 2D game. You could have a Tile class that have the position information and all properties of a tile.
Another approaches can be used thinking in scalability and performance.