如何使用Minesweeper for if-else语句在我的游戏代码中的列表中使用不同类型的变量来解决问题,而在while循环中工作

问题描述

private static final int NR_OF_FIELDS = 81;
private static final char EMPTY_SPACE = '.';
    
static List<Point> points = new ArrayList<>(); // hold coordinates x,y of mines in game.
static List<Integer> guess = new ArrayList<>(); // hold coordinates x,y which user introduce
static List<Character> fields = init(); // a list with length 81 (dimenssion 2d array)
static List<Point> userCoordinates = new ArrayList<>(); // to be able to use the coordinates

在while循环中,用户引入了2个协调int n = sc.nextInt();int m = sc.nextInt();。该坐标转到List<Point> userCoordinates。 其他if语句检查是否为(fields.get(userCoordinates) == '.')开头。如果为true,则在列表字段中找到此坐标并设置'*' fields.set(userCoordinates,'*');。同时在列表猜测guess.add(userCoordinates);添加userCoordinates。在finished = checkIfFinished();行中,如果来自finished的坐标(来自矿山的坐标)等于来自{的坐标,则我根据truefalse的值分配List<Point> pointsList<Integer> guess {1}}。如果finishedtrue,则在最后打印cpncratlations。 问题是我不知道如何使用此列出的创建的内容进行操作,因为其中保存的值类型不同,而且我也不知道如何使用点进行操作。 我很困惑!!!

public void game() {
        while (!finished)
        {
            System.out.print("Set/delete mines marks (x and y coordinates): ");
            int n = sc.nextInt();
            int m = sc.nextInt();
            int x = n - 1;
            int y = m - 1;
            Point location = new Point(x,y);
            userCoordinates.add(location);
            if (x < 0 || x > 8 || m < 0 || m > 8) {
                System.out.println("Coordinates should be from 1 to 9!");
            }

               else if (fields.get(userCoordinates) == '.') {
                    fields.set(userCoordinates,'*');
                    guess.add(userCoordinates);
                    finished = checkIfFinished();
                    printminesweeper();
                } else if (fields.get(userCoordinates) == '*') {
                    fields.set(userCoordinates,'.');
                    guess.remove(Integer.valueOf(x));
                    finished = checkIfFinished();
                    printminesweeper();
                } else {
                    System.out.println("There is a number here!");
                }
            }
            System.out.println("Congratulations! You found all mines!");
        }
    }
private static boolean checkIfFinished() {
            return guess.equals(points);
        }


private static List<Character> init() {
    List<Character> fields = new ArrayList<>(NR_OF_FIELDS);
    for (int i = 0; i < NR_OF_FIELDS; i++) {
        fields.add(EMPTY_SPACE);
    }
    return fields;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)