由八个方向,控制计数,
我们通过,二维数组的坐标变化来模拟八个方向的变化
下面处理越界的时候,如果超边界肯定就不执行就ok
随机雷阵就是注释掉的那一块,然后随机需要把n也改为控制台输入
下面的n变为n-1
在自定义函数的时候添加输入数组
package 小玩意;
import java.util.Random;
import java.util.Scanner;
import javax.swing.text.StyledEditorKit.ForegroundAction;
public class 扫雷 {
static int[] inx = {0,-1,-1,-1,0,1,1,1};
static int[] iny = {1,1,0,-1,-1,-1,0,1};
static int count = 0;
static int n = 6;
public static int[][] arr= {
{0,1,0,0,0,1,0},
{1,0,1,1,0,0,0},
{0,1,0,0,0,0,0},
{0,1,0,0,1,0,0},
{0,0,0,1,0,0,0},
{1,0,0,0,0,0,1},
{0,0,0,0,0,1,0},
};
public static void main(String[] args) {
// Random ra = new Random();
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < n; j++) {
// int num = 0;
// int temp = ra.nextInt(10);
// if ((temp==4||temp==8)&&num<=10) {
// num++;
// arr[i][j]==ra.nextInt(1);
// }
// }
// }
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);;
while (true) {
int x = sc.nextInt();int y = sc.nextInt();
if (arr[x][y]==1) {
System.out.println("游戏结束");
break;
}else {
game(x,y);
}
System.out.println(count);
}
}
private static void game(int x, int y) {
// TODO Auto-generated method stub
count = 0;
for (int k = 0; k < 8; k++) {
int xx = x + inx[k];
int yy = y + iny[k];
if (xx >= 0 && yy >= 0&&xx<=n&&yy<=n) {
if (arr[xx][yy] == 1) {
count = count + 1;
}
}
}
}
}