我必须使用扫描仪在数组中移动字符。 (java)

时间:2019-05-17 15:10:59

标签: java

我有这个数组游戏场,它包含一些“ R”,“ O”,“ X”和一个“ +”。我必须将X移到+,当X处于“ +”时,游戏结束。我必须使用java.util.Scanner来做,但是我不知道怎么做。有人能帮我吗?

如您所见,我已经尝试过了,但是没有用。 <-//玩家移动后的一切

代码:

public class test {

public static void main(String[] args) {

    String moveSelect;
    boolean validInput = true;

    int WIDTH = 18;
    int HEIGHT = 11;
    final int CHAR_COUNT = 15;
    final char[] CHARS = {'R','O'};
    Random random = new Random();

//field
    char[][] field = new char[HEIGHT][WIDTH];

    for (int i = 0; i < HEIGHT; i++) {
        Arrays.fill(field[i], '.');

    }
    for (char ch: CHARS) {

        for (int j = 0,x,y; j < CHAR_COUNT ; j++) {

            do{
                x = random.nextInt(WIDTH);

                y = random.nextInt(HEIGHT);
            } while (field[y][x] != '.');
            field[5][0] = '+';
            field[5][17] = 'X';
            field[y][x] = ch;
        }
    }

    //print field
    for (char[]row : field) {
        System.out.println(row);

    }
// player movements
    System.out.println("Enter a movement choice W A S or D");
    do {

        Scanner keyboard = new Scanner(System.in);
        moveSelect = keyboard.nextLine();

        if (moveSelect.equals("s")) {
           WIDTH -= 1;
        } else if (moveSelect.equals("d")) {
            HEIGHT -= 1;
            validInput = true;
        } else if (moveSelect.equals("w")) {
            WIDTH += 1;
            validInput = true;
        } else if (moveSelect.equals("a")) {
            HEIGHT += 1;
            validInput = true;
        } else {
            System.out.println("Invalid Entry. Try again.");
            validInput = false;
        }

    } while (validInput == false);

    //print field
    for (char[]row : field) {
        System.out.println(row);

    }

}

}

0 个答案:

没有答案