To lay mines for minesweeper games, the minesweeper panel for minesweeper can be represented by a two-dimensional int array. If a location is a mine, the position is represented by a number-1,
If the location is not a mine, it is temporarily represented by the number 0.
The program completes the operation of random mines in the two-dimensional array, and the program reads 3 parameters: Number of rows (r) of the Bray panel, number of columns (c), number of mines laid out (N),
And to meet the 0<n<r*c*0.75 (that is, the maximum density of the laying of mines is 75%), the program after the operation of the N mines randomly placed in a two-dimensional array of r*c, after the completion of the game of minesweeper.
ImportJava.util.*; Public classMinesweeper { Public Static voidMain (string[] args) {Scanner SCN=NewScanner (system.in); //Enter rows and columns intR =Integer.parseint (Scn.nextline ()); intc =Integer.parseint (Scn.nextline ()); if(R<2 | | c<2) {System.out.println ("The rows entered are not valid. "); return; } //define a two-dimensional array to Bray int[] A =New int[R][c]; Random Rnd=NewRandom (); //give the number of thunder randomly intn = Rnd.nextint ((int) (r*c*0.75)); System.out.println ("Number of Thunder:" +N); while(n>0) { //random mines, randomly generated mines in rows and columns intRR =Rnd.nextint (R); intCC =Rnd.nextint (c); if(A[rr][cc]!=-1) {A[RR][CC]=-1; N--; } } //Demining for(inti=0;i<r;i++) { for(intj=0;j<c;j++) { intLei=0; if(A[i][j] = = 0) { if(i-1>=0 && j-1>=0 && a[i-1][j-1]==-1) Lei++; if(I-1>=0 && A[i-1][j]==-1) Lei++; if(j<c-1) { if(I-1>=0 && A[i-1][j+1]==-1) Lei++; if(A[i][j+1]==-1) Lei++; } if(J-1>=0 && A[i][j-1]==-1) Lei++; if(i<r-1) { if(J-1>=0 && A[i+1][j-1]==-1) Lei++; if(A[i+1][j]==-1) Lei++; } if(I<r-1 && j<c-1) { if(i+1>=1 && j+1>=1 && a[i+1][j+1]==-1) Lei++; } A[i][j]=Lei; } } } //Output for(inti=0;i<r;i++) { for(intj=0;j<c;j++) {System.out.print (A[i][j]+ "\ T"); } System.out.println (); } }}
Java Practice (simulation minesweeper game)