DictatorPanda
Coder
Good day/night. I'm having an issue with creating a border. Note: It's for a school project, and I'm almost done with it. In fact, the border is the ONLY thing I have left to complete. I say this because I don't want other users to think I'm trying to get people to do my work for me. I did everything, I just need help with this.
It's supposed to look like this:
+---+
| |
+---+
And in a scratch work file, I was able to break down the border using StringBuilder. Here's the code below:
[CODE lang="java" title="Border Making code"] public static char[][] createGrid(int size) {
char[][] grid = new char[size][size]; //refers to a variable that allows the user to choose the board's size
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid.length; j++) {
grid[j] = ' ';
}
}
return grid;
}
public static String displayGrid(char[][] grid) {
StringBuilder builder = new StringBuilder(); //StringBuilder, strings that can be overriden
String section = "+-----";
//Puts everything together
//Prints diff sections of the grid
builder.append(printHeaderLine(grid));
builder.append(printDashedLine(grid, section));
builder.append(printGrid(grid, section));
return builder.toString();
}
//Prints the top with the spaced letters
public static StringBuilder printHeaderLine(char[][] grid) {
StringBuilder builder = new StringBuilder(); //creates a new Str builder
builder.append(" "); //Puts spaces into the char grid
for (int i = 0; i < grid.length; i++) {
builder.append((char) ((int) 'A' + i)); //Makes the Lettercolumns
builder.append(" ");
}
builder.append(System.lineSeparator());
return builder;
}
//Prints the grid
public static StringBuilder printGrid(char[][] grid, String section) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < grid.length; i++) {
builder.append(String.format("%3d", (i))); //Formats the row labels
builder.append(" ");
for (int j = 0; j < grid.length; j++) {
builder.append("+ ");
builder.append(grid[j]); //Puts in the empty cells
builder.append(" ");
}
builder.append("+");
builder.append(System.lineSeparator());
builder.append(printDashedLine(grid, section));
}
return builder;
}
//Prints the dashed lines.
public static StringBuilder printDashedLine(char[][] grid, String section) {
StringBuilder builder = new StringBuilder();
builder.append(" ");
for (int i = 0; i < grid.length; i++) {
builder.append(section);
}
builder.append("+");
builder.append(System.lineSeparator());
return builder;
}
[/CODE]
I did it in another file because I wanted to get the game done with (a running game is more important than a border). I finished the game and now I'm trying to integrate it with the Battleship file. I have a method that prints the board in the Battleship file:
[CODE lang="java" title="Prints game board"]public static void printBoard(char board[][]) {
System.out.print(" ");
for (int j = 0; j < size; j++) {
System.out.print((char)(j+65) + " "); //Displays letters for the columns
}
System.out.println();
for (int i = 0; i < size; i++) {
System.out.print(i); //Prints the row numbers
for (int j = 0; j < size; j++) {
System.out.print(board[j] + " ");
}
System.out.println();
}
System.out.println();
}[/CODE]
Any tips on how I can combine them? I tried for a few hours to no avail. I would either get errors or the game would run smoothly but the border would be gone. Also if you know a better way to tackle this problem, you can tell me since I'm up for suggestions. There's probably a more effective way to do this but I'm unable to figure it out! Thank you for your time, I appreciate it!
It's supposed to look like this:
+---+
| |
+---+
And in a scratch work file, I was able to break down the border using StringBuilder. Here's the code below:
[CODE lang="java" title="Border Making code"] public static char[][] createGrid(int size) {
char[][] grid = new char[size][size]; //refers to a variable that allows the user to choose the board's size
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid.length; j++) {
grid[j] = ' ';
}
}
return grid;
}
public static String displayGrid(char[][] grid) {
StringBuilder builder = new StringBuilder(); //StringBuilder, strings that can be overriden
String section = "+-----";
//Puts everything together
//Prints diff sections of the grid
builder.append(printHeaderLine(grid));
builder.append(printDashedLine(grid, section));
builder.append(printGrid(grid, section));
return builder.toString();
}
//Prints the top with the spaced letters
public static StringBuilder printHeaderLine(char[][] grid) {
StringBuilder builder = new StringBuilder(); //creates a new Str builder
builder.append(" "); //Puts spaces into the char grid
for (int i = 0; i < grid.length; i++) {
builder.append((char) ((int) 'A' + i)); //Makes the Lettercolumns
builder.append(" ");
}
builder.append(System.lineSeparator());
return builder;
}
//Prints the grid
public static StringBuilder printGrid(char[][] grid, String section) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < grid.length; i++) {
builder.append(String.format("%3d", (i))); //Formats the row labels
builder.append(" ");
for (int j = 0; j < grid.length; j++) {
builder.append("+ ");
builder.append(grid[j]); //Puts in the empty cells
builder.append(" ");
}
builder.append("+");
builder.append(System.lineSeparator());
builder.append(printDashedLine(grid, section));
}
return builder;
}
//Prints the dashed lines.
public static StringBuilder printDashedLine(char[][] grid, String section) {
StringBuilder builder = new StringBuilder();
builder.append(" ");
for (int i = 0; i < grid.length; i++) {
builder.append(section);
}
builder.append("+");
builder.append(System.lineSeparator());
return builder;
}
[/CODE]
I did it in another file because I wanted to get the game done with (a running game is more important than a border). I finished the game and now I'm trying to integrate it with the Battleship file. I have a method that prints the board in the Battleship file:
[CODE lang="java" title="Prints game board"]public static void printBoard(char board[][]) {
System.out.print(" ");
for (int j = 0; j < size; j++) {
System.out.print((char)(j+65) + " "); //Displays letters for the columns
}
System.out.println();
for (int i = 0; i < size; i++) {
System.out.print(i); //Prints the row numbers
for (int j = 0; j < size; j++) {
System.out.print(board[j] + " ");
}
System.out.println();
}
System.out.println();
}[/CODE]
Any tips on how I can combine them? I tried for a few hours to no avail. I would either get errors or the game would run smoothly but the border would be gone. Also if you know a better way to tackle this problem, you can tell me since I'm up for suggestions. There's probably a more effective way to do this but I'm unable to figure it out! Thank you for your time, I appreciate it!