A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© yu244934256 中级黑马   /  2016-9-21 01:42  /  446 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

public class SweepMine {
        public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                Random sj = new Random();
                int[][] arr = new int[9][9];
                for (int i = 0; i < arr.length; i++) {
                        for (int j = 0; j < arr[i].length; j++) {
                                arr[i][j] = sj.nextInt(2) + 9;
                        }
                }
                for (int i = 0; i < arr.length; i++) {
                        for (int j = 0; j < arr[i].length; j++) {
                                while (arr[i][j] == 9) {
                                        int count = 0;
                                        if (i < arr.length - 1 && j < arr[i].length - 1) {
                                                if (arr[i][j + 1] == 10) {// →
                                                        count++;
                                                }
                                                if (arr[i + 1][j + 1] == 10) {// ↘
                                                        count++;
                                                }
                                                if (arr[i + 1][j] == 10) {// ↓
                                                        count++;
                                                }
                                        }
                                        if (i < arr.length - 1 && j > 0) {
                                                if (arr[i + 1][j - 1] == 10) {// ↙
                                                        count++;
                                                }
                                        }
                                        if (i > 0 && j > 0) {
                                                if (arr[i][j - 1] == 10) {// ←
                                                        count++;
                                                }
                                                if (arr[i - 1][j - 1] == 10) {// ↖
                                                        count++;
                                                }
                                                if (arr[i - 1][j] == 10) {// ↑
                                                        count++;
                                                }
                                        }
                                        if (i > 0 && j < arr[i].length - 1) {
                                                if (arr[i - 1][j + 1] == 10) {// ↗
                                                        count++;
                                                }
                                        }
                                        arr[i][j] = count;
                                        break;
                                }
                        }
                }
                int[][] arr2 = new int[arr.length][arr[0].length];
                for (int i = 0; i < arr.length; i++) {
                        for (int j = 0; j < arr[0].length; j++) {
                                arr2[i][j] = 11;
                        }
                }
                while (true) {
                        showMap(arr2);
                        System.out.println("输入x");
                        int x = sc.nextInt();
                        System.out.println("输入y");
                        int y = sc.nextInt();
                        if (arr[x][y] != 10) {
                                arr2[x][y] = arr[x][y];
                        } else {
                                break;
                        }
                }
                System.out.println("游戏结束");
                showMap(arr);
        }

        public static void showMap(int arr[][]) {
                System.out.print("  ");
                for (int i = 0; i < arr[0].length; i++) {
                        System.out.print(i);
                }
                System.out.println("");
                for (int i = 0; i < arr.length; i++) {
                        System.out.print(i + " ");
                        for (int j = 0; j < arr[i].length; j++) {
                                if (arr[i][j] == 10) {
                                        System.out.print("*");
                                } else if (arr[i][j] == 0) {
                                        System.out.print(" ");
                                } else if (arr[i][j] == 11) {
                                        System.out.print("?");
                                } else {
                                        System.out.print(arr[i][j]);
                                }
                        }
                        System.out.println("");
                }
        }
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马