Hi guys,
I have just started learning java in Eclipse and was wondering about this:
I have tried to make a minesweeper application (just threw myself into the deep end there) and have created a class that fills a [field_size][field_size][2] multidimensional array with:
1st. 0's and 1's for no bombs and bombs, respectively,
2nd. the number of mines surrounding each coördinate (1 to 8, 9 if its a mine itself) in that first part.
All in one class file. I have never worked with projects that involved multiple class files yet. Now the thing is, I want to know how I can make this class that I have into a method that can provide another class with this randomly filled matrix. I suppose that if I am going to write the rest of the minesweeper app in the same class, and I have to run it again and again after clicking a box (no idea how to do that yet btw) it would create a new minefield every time, which would pretty much ruin the whole idea of the game.
Code so far, which I am quite proud of:
package part1; public class msw { public static void main(String[] args) { int field_size=10; int numberofmines=10; int[][][]field=new int[field_size][field_size][3]; //fill field with 0's for(int i=0;i<field_size;i++) { for(int j=0;j<field_size;j++) { field[i][j][1]=0; } } // pick mines for (int i=0;i<numberofmines;i++) { int rx =(int)(Math.random()*field_size); int ry =(int)(Math.random()*field_size); if (field[rx][ry][1]==1) i=i-1; else field[rx][ry][1]=1; } // print field[x][x][1] (for debugging purposes) for (int i=0;i<field_size;i++) { for(int j=0;j<field_size;j++) { System.out.print(field[i][j][1]+" "); } System.out.println(); } // fill field[x][x][2] with amount of mines surrounding a coordinate in field[x][x][1] for(int i=0;i<field_size;i++) { for(int j=0;j<field_size;j++) { for(int k=-1;k<2;k++) { for(int l=-1;l<2;l++) { if (field[i][j][1]==1) field[i][j][2]=9; else { if (i+k<0) k=k+1; if (j+l<0) l=l+1; if (i+k>field_size) k=2; if (j+l>field_size) l=2; if (i+k<field_size&&j+l<field_size&&k<2&&l<2) { if (field[i+k][j+l][1]==1) field[i][j][2]=field[i][j][2]+1; } } } } } } // printing 2nd part of matrix System.out.println(); for (int i=0;i<field_size;i++) { for(int j=0;j<field_size;j++) { System.out.print(field[i][j][2]+" "); } System.out.println(); } } }
Any help is most welcome.