Reputation: 105
I am new to java, so, please respond with clear and direct answers. I made a 12 by 12 matrix of 0s.
000000000000
000000000000
000000000000
000000000000
000000000000
000000000000
000000000000
000000000000
000000000000
000000000000
000000000000
000000000000
I want to surround the middle area with a "fence" of 1s.
111111111111
100000000001
100000000001
100000000001
100000000001
100000000001
100000000001
100000000001
100000000001
100000000001
100000000001
111111111111
I am really new to Java so I only know how to make the 12 by 12 and print it, but now how to make the fence. I also want to know how I could randomly spawn in 10 random 1s inside of the 10 by 10 grid of 0s using a loop. Please help!!!! Here is my code:
class Main {
public static void main(String[] args){
int[][] gameboard = new int[12][12];// make gameboard
int r_length = gameboard.length;//row length
int c_length = gameboard[0].length;//column length
for (int x = 0; x < gameboard.length; x++) {
for(int y = 0; y < gameboard.length; y++){
gameboard[x][y] = x + y;
gameboard[0][y] = gameboard[1]; //replace row 1 (not working)
gameboard[11][y] = gameboard[1]; //replace row 12 (not working)
gameboard[x][0] = gameboard[1]; //replace column 1 (not working)
gameboard[x][11] = gameboard[1]; //replace column 12 (not working)
}
}
for (int[] a : gameboard) {
for (int x :a) {
System.out.print("0");
}
System.out.println();
}//gameboard printing
}
}
Upvotes: 1
Views: 912
Reputation: 11
public static void main(String[] args) {
char border = '1';
char fillCharaters = '0';
int gridLength = 12;
int gridWidth = 12;
char[][] arr = new char[12][12];
Random random = new Random();
for (int j=0; j<gridWidth ; j++){
for (int i=0;i<gridLength;i++){
if(j==0||j==gridWidth-1||i==0||i==gridLength-1){
arr[j][i] = border;
} else {
arr[j][i] = fillCharaters;
}
}
//To print the random characters in the 10X10 grid
int r = random.nextInt(11);
arr[j][r] = border;
}
print2DMatrix(arr);
}
public static void print2DMatrix(char mat[][])
{
for (int i = 0; i < mat.length; i++)
{
for (int j = 0; j < mat[i].length; j++){
if(j==mat[i].length-1){
System.out.println(mat[i][j]);
} else{
System.out.print(mat[i][j]);
}
}
}
}
Upvotes: 1
Reputation: 140457
I have a slightly different suggestion here: don't put the border into your data.
What if you want to have "*" tomorrow instead of those 1s?! Then you are broken, because you have an array of int, not of chars?!
Meaning: you should actually separate your data and the printing format.
Sure, that makes the printing more complicated (or not, see below), but your approach does have a lot of ugly consequences. For example: what if you want to pass that matrix to some "computation" code at some point? Then that computation code will have to know where/how your "border" sits in that data, to exclude it from its computation. Or when you decide to turn your "text console" program into a GUI application? Then you will probably really draw a border line ... but you will keep carrying around your matrix that includes those "border line" chars (pun intended).
Thus, why not go:
printTopLine();
for (int[] rows : gameboard) {
printRow(row);
where
public void printTopLine() {
...println("1111...
}
and
public void printRow(int[] values) {
print("1");
for (int v : values) print(v);
print("1");
}
As you can see: this is actually very straight forward, and whenever you decide: I want to print things in a different way, you just go in and change thing were necessary, without ever worrying about the data within your 2D array!
So, long story short: you could change your code so that your 2D matrix is only about the values of your game board, and how to "display" that is completely done by other code!
Upvotes: 4
Reputation: 26046
A couple of things here:
Inner loop should iterate over inner length:
for(int y = 0; y < gameboard[0].length; y++)
instead of:
for(int y = 0; y < gameboard.length; y++)
You cannot assign array to int
, types must match.
gameboard[0][y] = gameboard[1];
This won't do what you want: gameboard[x][y] = x + y;
. It assigns a sum of indices.
Since it's a square (both lengths equal), you can iterate once:
for (int x = 0; x < gameboard.length; x++) {
gameboard[0][x] = 1;
gameboard[11][x] = 1;
gameboard[x][0] = 1;
gameboard[x][11] = 1;
}
Before that you were doing the same work gameboard.length
times.
You should also print it properly:
for (int[] a : gameboard) {
for (int x :a) {
System.out.print(x);
}
System.out.println();
}
Upvotes: 2