public void writeHighScores(LinkedList<Score> scores, int playerScore, String playerName) throws IOException {
	try (BufferedWriter out = new BufferedWriter(new FileWriter("highscore.txt"))) {

	    if (!scores.isEmpty()) {
		for (int i = 0; i < scores.size(); i++) {
		    if (playerScore > scores.get(i).score) {
			int playerRank = i + 1;
			if (scores.size() < 10) { // there is free space in the list for a new highscore
			    scores.add(i, new Score(playerRank, playerName, playerScore));
			    break;
			} else { // if there already are 10 highscores, replace the last one with the current player's
			    scores.removeLast();
			    scores.add(i, new Score(playerRank, playerName, playerScore));
			    break;
			}
		    }
		}
		// this is to make sure that there is always a new score added to the list if there is room for it
		if (scores.size() < 10 && score < scores.peekLast().score) {
		    scores.addLast(new Score((scores.peekLast().position + 1), playerName, playerScore));
		}
		// Update the position of the scores
		for (int i = 0; i < scores.size(); i++) {
		    Score temp = scores.get(i);
		    temp.position = i + 1;
		    scores.set(i, temp);
		}

		for (int i = 0; i < scores.size() - 1; i++) {
		    out.write(scores.get(i).position + "," + scores.get(i).name + "," + scores.get(i).score);
		    out.newLine();
		}
	    } else { // add the new score if the file is empty
		scores.add(new Score(1, playerName, playerScore));
	    }

	    out.write(scores.get(scores.size() - 1).position + "," + scores.get(scores.size() - 1).name + "," +
		      scores.get(scores.size() - 1).score);

	    out.close();
	}
    }