Computer Science / Game Programming
import java.io.BufferedReader; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; import comp1022p.IO; import comp1022p.assignment.ControllerLogic; import comp1022p.assignment.GameRecord; public class StudentController implements ControllerLogic { /** * Updates the highscore records and write them to a file. * * The highscore records stores a maximum of 10 records. * * This method updates the highscore records if the score of the current * game play is high enough. It first asks for the user to input his/her * name for forming the current record. If the name has more than 20 * characters, it should be trimmed down to the first 20 characters only. * * The current record should be inserted into the highscore records only if * either one of the following conditions is satisfied: * * Condition 1. There are less than 10 records in the highscore records. * * Condition 2. There are 10 records in the highscore records and the score of the * current record is strictly higher than at least one of the scores in the * highscore records. * * Condition 3. There are 10 records in the highscore records and the score of the * current record is equal to some of the scores in the highscore records and the * current level is strictly higher than at least one of those records in the highscore * records. * * For condition 2 and condition 3, after each successful insertion, the * record with the lowest score and lowest level should be removed to keep a * total of 10 records. In case there are more than one record with the * lowest score and the lowest level, any one of those can be removed. * * If the player's name already exists in the highscore records, replace the old record * with the new record if the new record is better. (i.e. either the new score is strictly * higher than the old score, or the new level is higher than the old level when the scores * are the same). * * The highscore records are then written to a file for storage. The format * of the record should be name followed by level followed by score. Each * item should be separated by a tab character '\t'. * * @param highScoreRecords The previously-saved highscore records, with a maximum of 10 records only. * @param writer The writer used to write the record to a file. You can assume that the writer is already properly initialized and that it will be properly closed by the caller of this method after its use. * @param level The level of the current game play. * @param score The score obtained by the current game play. */ public void updateHighScoreRecords(GameRecord[] records, PrintWriter writer, int level, int score) { //*** Finish this method. ***// } /** * Outputs a highscore table by reading all the previously saved game records. * * You can assume there are only 10 records in total. * * This method reads all the previously saved game records and outputs a highscore table in the terminal. * You have to ensure that the records are sorted first by the score and then by the level in the table. * The format of the highscore table is as follows: * * 1. The name column should always has a width of 20 characters. * 2. The level column should always has a width of 10 characters. * 3. The score column should always has a width of 10 characters. * 4. The names in the name column should aligned to the left. * 5. The levels in the level column should align to the right. * 6. The scores in the score column should align to the right. * 7. The second line should contains 40 "-" characters. * * Here is an example of the highscore table: * * name level score * ---------------------------------------- * abc 3 15 * hello 2 15 * testing 3 14 * def 2 8 * gg 1 8 * 123 1 4 * 667 2 3 * 567 3 2 * * @param reader The reader used to read all the records from a file. You can assume that the reader is already properly initialized and that it will be properly closed by the caller of this method after its use. * @throws IOException If an error occurs when reading a line from the file. */ public void outputHighScoreTable(BufferedReader reader) throws IOException { //*** Finish this method. ***// } }