سؤال

كيف تكتب هذا الرمز؟

يدور هذا السؤال بالذات حول لعبة متاهة تحتوي على قائمة من الركاب الذين هم مستكشفين (أنت) ، وحوش (لمس يقتلك) ، والكنوز. تستخدم اللعبة كتل الكائنات المربعة التي يقيم فيها هؤلاء الركاب. الشيء المعين الذي أريد القيام به هو قراءة الملفات التي يمكن أن تصدر التكوين الحالي للمتاهة أو استيرادها كملف TXT.

المواصفات: القراءة لأول مرة في الصفوف والعقيدة من المتاهة لإنشاء مربع [] من الحجم المناسب. ثم بناء وقراءة في جميع المربعات/الركاب.

بالنسبة للمربعات ، ستحدد المتاهة أولاً أن الخط يبدأ بـ "Square". سيتم قراءة ذلك في الصف و Col of the Square واستخدام هذه المعلومات لبناء كائن مربع. أخيرًا ، سوف يمرر بقية الماسح الضوئي إلى طريقة ToObject في Square حتى يتمكن من تهيئة نفسه.

بالنسبة لجميع الركاب الآخرين ، ستحدد المتاهة نوع شاغلها وبناء الكائن المناسب باستخدام المُنشئ الذي يأخذ متاهة فقط. لن يقرأ الصف أو العقيد من الماسح الضوئي ، ولكن ببساطة تمرير الماسح الضوئي إلى طريقة Toobject للكائن الذي تم إنشاؤه حديثًا.

هذا رمز لدي حتى الآن والذي قد يكون خطأ:

    public void readMazeFromFile(String fileName) throws IOException, FileNotFoundException, MazeReadException
   {
        Scanner fileSc = new Scanner(new File(fileName));
        String line = fileSc.nextLine(); //whats on the line, will be overwritten
        Scanner lineSc = new Scanner(line);
        String temp;
        lineSc.useDelimiter(",");
        int lineNum = 1; //every time you scan a line out, do lineNum++
        int r1, r2, r3, r4, c1, c2, c3, c4;

        rows = fileSc.nextInt();
        cols = fileSc.nextInt();
        Square hi = new Square(rows, cols);
        line = fileSc.nextLine();
        while ( line != null)
        {
            line = lineSc.nextLine();
            lineSc = new Scanner(line);

            if( lineSc.equals("Square"))
            {
                r1 = lineSc.nextInt();
                c1 = lineSc.nextInt(); 
                hi.toObject(lineSc);
            }
            if (lineSc.equals("Explorer"))
            {

                explorer.toObject(lineSc);
            }
            if (lineSc.equals("Treasure"))
            {
                Treasure.toObject(lineSc);
            }


            lineNum++;
        }

هنا إخراج العينة:

5,5
Square,0,0,true,false,false,true,true,true
Square,0,1,true,false,true,false,true,true
Square,0,2,true,false,true,false,false,false
Square,0,3,true,false,false,false,false,false
Square,0,4,true,true,false,false,false,false
Square,1,0,false,false,true,true,true,true
Square,1,1,true,false,true,false,false,false
Square,1,2,true,true,false,false,false,false
Square,1,3,false,true,false,true,false,false
Square,1,4,false,true,false,true,false,false
Square,2,0,true,false,false,true,false,false
Square,2,1,true,false,true,false,false,false
Square,2,2,false,true,false,false,false,false
Square,2,3,false,true,false,true,false,false
Square,2,4,false,true,false,true,false,false
Square,3,0,false,true,false,true,false,false
Square,3,1,true,false,false,true,false,false
Square,3,2,false,true,false,false,false,false
Square,3,3,false,true,true,true,false,false
Square,3,4,false,true,false,true,false,false
Square,4,0,false,true,true,true,false,false
Square,4,1,false,true,true,true,false,false
Square,4,2,false,false,true,true,false,false
Square,4,3,true,false,true,false,false,false
Square,4,4,false,true,true,false,false,false
Explorer,0,0,Scary Name
Treasure,4,4,true
Treasure,2,2,false
Monster,4,4
Monster,3,3

ماذا تكتب لهذا القسم؟

هل كانت مفيدة؟

المحلول

هذا هيكل عظمي يمكنك البدء به. أنت حقًا لا تحتاج إلى أي شيء أكثر من أ Scanner هنا؛ يمكن أن تفعل كل ما تحتاجه لمسح الإدخال والقيام بالتحويل ، وما إلى ذلك. تريد استخدامه next(), nextInt(), nextBoolean() و nextLine() أساليب.

    Scanner in = new Scanner(new File(filename));
    in.useDelimiter("\\s+|,");

    int rows = in.nextInt();
    int cols = in.nextInt();
    // construct the array

    while (in.hasNext()) {
        String type = in.next();
        int r = in.nextInt();
        int c = in.nextInt();
        // read more depending on type
    }

نصائح أخرى

استخدم قائداً في الملف ثم قم بإرفاق جهاز تدفق Treamtokenizer بذلك ، أكثر كفاءة بكثير :)

http://java.sun.com/j2se/1.4.2/docs/api/java/io/streamtokenizer.html

http://java.sun.com/j2se/1.4.2/docs/api/java/io/filereader.html

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top