首页 > 解决方案 > 如何将 BestScores 保存在文件中 JAVA

问题描述

我开发了一个游戏,我想在一个文件中记录前 3 名的最佳分数,这样我们就不会在关闭游戏后丢失分数。它不能正常工作,我不知道为什么。- 最好的分数是整数,越低越好。最佳分数是最低的。- 它没有按升序保存分数它确实创建了一个文件,并且它似乎可以与主要测试一起使用,但是在添加了一些分数后,它没有按升序对数组进行排序,因此文件的方式错误.

    package pt.iscte.dcti.poo.sokoban.starter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Scanner;

import pt.iul.ista.poo.utils.Point2D;

public class BestScores {
    public int[] bestScore = new int[3];
    public final int level;

    public BestScores(int level) {
        this.level = level;
    }

    public int[] getBestScore() {
        return bestScore;
    }

    //Register BestScores
    public void setBestScore(int score) {
        for(int i = 0; i < bestScore.length; i++) {
            if(bestScore[i] == 0) {
                bestScore[i] = score;
                return;
            }
        }
        Arrays.sort(bestScore);
        for (int i = 0; i < bestScore.length-1; i++) {
            if(bestScore[i] == 0) {
                int box1 = bestScore[i];
                bestScore[i] = bestScore[i+1];
                bestScore[i+1] = box1;
            }
        }
        addBestScore(score);
    }
    public void addBestScore(int score) {
        if(score < bestScore[bestScore.length - 1])
            bestScore[bestScore.length - 1] = score;
        Arrays.sort(bestScore);
        for (int i = 0; i < bestScore.length-1; i++) {
            if(bestScore[i] == 0) {
                int box1 = bestScore[i];
                bestScore[i] = bestScore[i+1];
                bestScore[i+1] = box1;
            }
        }
    }

    public int getTopOne() {
        return bestScore[0];
    }

    public int getLevel() {
        return level;
    }

    //Check if the file exists, else create it
    public void searchFile() {
        File tmpDir = new File("bestScores/BestScore_" + level + ".txt");
        if (!tmpDir.exists())
            createOrAddScore();
        else {
            checkScores(tmpDir);
            createOrAddScore();
        }
    }

    //Create file with Scores if they exist.
    public void createOrAddScore() {
        try {
            PrintWriter writer = new PrintWriter(new File("bestScores/BestScore_" + level + ".txt"));
            writer.println("BestScores: ************Level:" + level + "************");
            for(int i = 0; i < bestScore.length; i++) {
                if(bestScore[i] != 0)
                    writer.println((i+1) + "º " + bestScore[i] + " " + "moves.");
            }
             writer.close();
        }
        catch (FileNotFoundException e) {
        System.err.println("problema a escrever o ficheiro");
        }
    }

    //Read File and return Best Scores, so that we don't lose the bestScores even after closing the game :D.
    public void checkScores(File file) {
        int[] array = new int[3];
        try {
            Scanner scanner = new Scanner(file);
            String title_line = scanner.nextLine();
            int i = 0;
            while(scanner.hasNextLine()) {
                String[] line = scanner.nextLine().split(" ");
                array[i] = Integer.parseInt(line[1]);
                i++;
            }
        }
        catch (FileNotFoundException e) {
            System.err.println("problema a escrever o ficheiro");
        }
        for (int i = 0; i < array.length; i++)
            if(array[i] != 0)
                setBestScore(array[i]);
    }

    public static void main(String[] args) {
//      BestScores bS = new BestScores(4);
        //test1 No BEstScores
//      bS.searchFile();
        //test2 WithBestScores
//      bS.setBestScore(40);
//      bS.setBestScore(15);
        bS.setBestScore(50);
//      bS.setBestScore(30);
//      bS.setBestScore(10);
//      bS.searchFile();
//      int[] test = bS.getBestScore();
//      for(int i = 0; i < test.length; i++)
//          System.out.println(test[i]);
        //test3 With file with Scores
//      bS.searchFile();
//      int[] test = bS.getBestScore();
//      for(int i = 0; i < test.length; i++)
//          System.out.println(test[i]);
    }

}

标签: javafile

解决方案


推荐阅读