# Thread: Making a grid in a string

1. Member
Join Date
Apr 2011
Posts
7
Rep Power
0

## Making a grid in a string

Hi Java Forums,

Before i explain my issue, i will say i have posted something similar a few weeks back and got some help which helped me solve my problem. The current problem i have is similar but a bit more advanced (it builds on what i did before). I have also conducted another search to find help with no luck. Anyway, here goes.

I am making a console based game where the objective is to shoot an unseen target using X and Y co ordinates on a 10 by 10 grid (starting at 0 and ending at 9). Since this is console based, the grid shows up as a string. I have constructed the following code, which makes my grid perfectly for me. *Assume 'size' is set at 9*

Java Code:
```public String displayGrid() {
int gridSize = size + 1;
int gridLine = gridSize - 1;
String grid = "";

for (int i = gridLine; i >= 0; i--) {
grid = grid + i;

for (int z = 0; z < gridSize; z++) {
grid = grid + " .";
}
grid = grid + "\n";
}

grid = grid + " ";

for (int i = 0; i < gridSize; i++)
grid = grid + " " + i;
return grid;
}```
My problem now is that as a more advanced feature of the game i want an X to show at the spot where the player has chosen to fire at. I can get the coordinates from another method in terms of two separate integers (x and y), but i don't know how to use them in my code to place an X on the spot where the player has chosen.

I hope i have made my problem clear, if not, please do not hesitate to ask for clarification. Any help at all is, as always, much appreciated.

Thanks,

Josh.

2. I suggest using a 2D array to represent the grid. Then all you need to do is have a nested loop iterate over the 2D array and print the contents. Elsewhere you have code that determines/changes the contents of the 2D array.

3. Member
Join Date
Apr 2011
Posts
7
Rep Power
0

I am not too familiar with arrays yet, could you show me how i would do this or link me to a good arrays guide? I have used VB a long time ago and know what arrays are but don't have a clue on how to use them in java :(

4. Originally Posted by josh2992
could you show me how i would do this or link me to a good arrays guide?
Suggested reading : Arrays And 2D-Arrays

Goldest

5. Java Code:
```import java.util.Scanner;

class Grid {
private final int len = 10;
char[][] grid = new char[len][len];

Grid() {
init();
}

private void init() {
for(int row = 0; row < len; row++) {
for(int col = 0; col < len; col++) {
grid[row][col] = '.';
}
}
}

private void display() {
for(int row = 0; row < len; row++) {
System.out.print((len - row - 1) + " ");
for(int col = 0; col < len; col++) {

System.out.print(grid[row][col] + " ");
}
System.out.println();
}
for(int index = 0; index < len; index++) {
System.out.print((index == 0 ? "  " : " ") + index);
}
System.out.println();
}

public void run() {
display();
System.out.print("Enter the x coordinate: ");
System.out.print("Enter the y coordinate: ");
if(x >= len || y >= len) {
System.out.println("Invalid coordinates.");
} else {
y = len - 1 - y;
grid[y][x] = 'X';
display();
}
}

public static void main(String[] args) {
new Grid().run();
}
}```
I don't normally provide code as extensive as this. Since humans expect [0,0] to be bottom left corner I did some jiggery to reflect this, which may or may not be what you want.

6. Member
Join Date
Apr 2011
Posts
7
Rep Power
0
Junky, you are a legend...that does practically exactly what i require, thanks so much!
and thanks to Goldest i understand how it works too :D

Thanks for the help guys.

7. Originally Posted by Junky
humans expect [0,0] to be bottom left corner
Humans do. Programmers don't :p

I think it's better that a newbie get used to that early on.

db

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•