import java.util.Scanner;java
public class MinesweepTest {dom
int[][] mine ;
int COLS = 10;
int ROWS = 10;
int mineNum = 20;
public MinesweepTest(){
mine = new int[ROWS][COLS];
for(int i=0;i<ROWS;i++)
for(int j=0;j<COLS;j++)
mine[i][j] = 0;
productRandomMines();
for(int i=0;i<ROWS;i++){
for(int j=0;j<COLS;j++){
if(mine[i][j] == 0){
mine[i][j] = checkMineNum(i,j);
}
System.out.print(" | "+mine[i][j]);
}
System.out.println();
}
}
public int checkMineNum(int co_X,int co_Y){
int beginX = co_X - 1;
int beginY = co_Y - 1;
int endX = co_X + 1;
int endY = co_Y + 1;
if(beginX < 0) beginX = 0;
if(beginY < 0) beginY = 0;
if(endX >= COLS) endX = COLS - 1;
if(endY >= ROWS) endY = ROWS - 1;
int num = 0;
for(int i=beginX;i<=endX;i++)
for(int j=beginY;j<=endY;j++)
if(mine[i][j] == 9)
num++;
return num;
}
public void productRandomMines(){
int co_X = 0;
int co_Y = 0;
for(int i=0;i<mineNum;i++){
co_X = (int)(Math.random()*COLS);
co_Y = (int)(Math.random()*ROWS);
mine[co_X][co_Y] = 9;
}
}
public void showResult(int[][] array,int beginX,int beginY,int endX,int endY){
if(beginX < 0) beginX = 0;
if(beginY < 0) beginY = 0;
if(endX >= COLS) endX = COLS - 1;
if(endY >= ROWS) endY = ROWS - 1;
for(int i=beginX;i<=endX;i++){
for(int j=beginY;j<=endY;j++)
System.out.print(" | "+array[i][j]);
System.out.print(" |\n");
}
}
public static void main(String[] args) {
MinesweepTest mt = new MinesweepTest();
while(true){
System.out.println("Please inter the coordinate value , example x,y .");
String coordinate = new Scanner(System.in).nextLine().trim();
if(coordinate.contains(",")){
int co_X = Integer.parseInt((coordinate.split(","))[0]);
int co_Y = Integer.parseInt((coordinate.split(","))[1]);
if(mt.mine[co_X][co_Y] == 9){
System.out.println("game over!");
break;
}else
mt.showResult(mt.mine, co_X-1,co_Y-1,co_X+1,co_Y+1);
}else if(coordinate.equals("q")){
break;
}else{
System.out.println("The input format is error!");
}
}
}ide
}
orm