簡體   English   中英

JAVA:從2D數組制作對象

[英]JAVA: Make objects from a 2d array

我正在制作一款名為“尖峰時刻”的游戲(您可以在其中移動紅色汽車)。

到目前為止,我制作了一個2D數組,該數組從文本文件讀取級別。 現在下一步,我想將值從文本文件轉換為對象,以便可以開始使用坐標移動它們。

下面稍微解釋一下我的代碼:首先,我要制作一個6x6的2D數組,用戶可以選擇一個難度級別,並根據他們選擇的內容來加載4個級別之一。txt(初學者,中級,高級或專家)。

選擇關卡后,它將把關卡加載到2D數組中(我將在代碼下方提供一個關卡,以便您有所了解)。 加載級別時,它將為所有內容添加“ 0”(在我眼中,它看起來更加用戶友好),並使用|分隔值。 標志。

所以現在我想我需要根據我在2D數組中讀取的值來制作對象才能移動它們(如果我輸入錯了,請更正我),我需要檢查數字周圍的值是否匹配或不匹配如果它們匹配,我需要將它們結合起來。

我該如何進行呢? 提前致謝

到目前為止,我的代碼:

public class Bord {
private static String[][] bordArray = new String[6][6];

public static void kiesLevel() {
    System.out.println("\nKies moeilijkheidsgraad\n1) Beginner\n2) Intermediate\n3) Advanced\n4) Expert");
    Scanner keyboardScanner = new Scanner(System.in);
    int keuze = keyboardScanner.nextInt();
    switch (keuze) {
        case 1:
            beginner();
            break;
        case 2:
            intermediate();
            break;
        case 3:
            advanced();
            break;
        case 4:
            expert();
            break;
        default:
            throw new NumberFormatException("Geef een geldige invoer");
    }
}

public static void beginner() {
    leesBordIn("c:/users/Glenn/desktop/beginner.txt");

}

public static void intermediate() {
    leesBordIn("d:/intermediate.txt");

}

public static void advanced() {
    leesBordIn("d:/advanced.txt");

}

public static void expert() {
    leesBordIn("d:/expert.txt");

}

private static void leesBordIn(String filename) {
    try {
        BufferedReader br = new BufferedReader(new FileReader(filename));
        String line = null;
        int r = 0;
        while ((line = br.readLine()) != null) {
            for (int c = 0; c < bordArray[r].length; c++) {
                if (line.toCharArray()[c] == '0') {
                    bordArray[r][c] = "  ";
                } else {
                    String characterstring = Character.toString(line.toCharArray()[c]);
                    if (characterstring.length() != 2) {
                        characterstring = "0" + characterstring;
                    }
                    bordArray[r][c] = characterstring;
                }
                System.out.print("|" + bordArray[r][c]);
            }
            if (r == 2) {
                System.out.println("=");
            } else {
                System.out.println("|");
            }
            r++;
        }


    } catch (FileNotFoundException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    } catch (IOException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }
}

}

一個級別的范例:

220007
300807
311807
300800
400066
405550

輸出示例:

|02|02|  |  |  |07|
|03|  |  |08|  |07|
|03|01|01|08|  |07=
|03|  |  |08|  |  |
|04|  |  |  |06|06|
|04|  |05|05|05|  |

我不了解創建bordArray對象后要做什么,但是我看到,如果願意的話,可以用更好的方式編寫leesBordin方法(盡量不要將所有方法都寫為靜態方法):

private void leesBordIn(String filename)
{
    try
    {
        File file = new File(filename);
        Scanner scanner = new Scanner(file);
        int r = 0;
        while (scanner.hasNextLine())
        {
            String line = scanner.nextLine();
            String[] fields = line.split("");
            for (int c = 0; c < bordArray[r].length; c++)
            {
                if (fields[c+1].equals("0"))
                {
                    bordArray[r][c] = "  ";
                } 
                else 
                {
                    bordArray[r][c] = ("0" + fields[c+1]);
                }
                System.out.print("|" + bordArray[r][c]);
            }
            if (r == 2) 
            {
               System.out.println("=");
            } 
            else
            {
                System.out.println("|");
            }
            r++;
        }
    catch (FileNotFoundException e) 
    {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
}

一個問題,為什么在第三行中加上“ =”?

然后,如果您更好地向我解釋了您想要做什么,那么我將嘗試為此編寫一些代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM