diff --git a/High Scores/Part A/Player.java b/High Scores/Part A/Player.java new file mode 100644 index 0000000..64382a6 --- /dev/null +++ b/High Scores/Part A/Player.java @@ -0,0 +1,37 @@ +/* COMPLETE WORKING CLASS PROVIDED */ +/* DO NOT MODIFY THE IMPLEMENTATION OF THIS CLASS*/ + +public class Player +{ + private String playerName; + private int highScore; + + + public Player (String name, int score) + { + playerName = name; + highScore = score; + } + + + /** @param score an int value representing the player's most recent score + * updates the value of highScore to score if this is higher than the current value + */ + public void updateScore(int score) + { + if(score > highScore) + highScore = score; + } + + + /** @return the player's name + */ + public String getName() + { return playerName; } + + + /** @return the high score of the player + */ + public int getHighScore() + { return highScore; } +} diff --git a/High Scores/Part A/ScoreList.java b/High Scores/Part A/ScoreList.java new file mode 100644 index 0000000..41fc73a --- /dev/null +++ b/High Scores/Part A/ScoreList.java @@ -0,0 +1,29 @@ +import java.util.*; +public class ScoreList +{ + private ArrayList scoreboard; + // listed in decreasing order of highScore value + // no two Player objects have the same playerName + + + /** Moves the Player object at index pos upwards in scoreboard to the correct + * position in the list according to its highScore value + * @param pos a valid index for the scoreboard list + */ + public void moveUp(int pos) + { + int i, value = scoreboard.get(pos).getHighScore(); + for(i = 0; i < pos && scoreboard.get(i).getHighScore() >= value; i++); + scoreboard.add(i, scoreboard.remove(pos)); + } + + /* CLASS MEMBERS FOR TESTING */ + /* DO NOT MODIFY ANY OF THE CONSTRUCTORS OR METHODS BELOW*/ + + public ScoreList(ArrayList sb) + { scoreboard = sb; } + + public ArrayList getScoreBoard() + { return scoreboard; } + +} diff --git a/High Scores/Part B/Player.java b/High Scores/Part B/Player.java new file mode 100644 index 0000000..64382a6 --- /dev/null +++ b/High Scores/Part B/Player.java @@ -0,0 +1,37 @@ +/* COMPLETE WORKING CLASS PROVIDED */ +/* DO NOT MODIFY THE IMPLEMENTATION OF THIS CLASS*/ + +public class Player +{ + private String playerName; + private int highScore; + + + public Player (String name, int score) + { + playerName = name; + highScore = score; + } + + + /** @param score an int value representing the player's most recent score + * updates the value of highScore to score if this is higher than the current value + */ + public void updateScore(int score) + { + if(score > highScore) + highScore = score; + } + + + /** @return the player's name + */ + public String getName() + { return playerName; } + + + /** @return the high score of the player + */ + public int getHighScore() + { return highScore; } +} diff --git a/High Scores/Part B/ScoreList.java b/High Scores/Part B/ScoreList.java new file mode 100644 index 0000000..b5d1dd3 --- /dev/null +++ b/High Scores/Part B/ScoreList.java @@ -0,0 +1,53 @@ +import java.util.*; +public class ScoreList +{ + private ArrayList scoreboard; + // listed in decreasing order of highScore value + // no two Player objects have the same playerName + + + /** Searches for a Player in scoreboard with a matching name and updates the high + * score if there is one. Otherwise a new player is created with the name and high score + * passed at the end of scoreboard. The new/updated Player object is moved up to + * the correct place on the list using the method moveUp. + * @param name the name of the player who recorded the score + * @param score the score recorded + * @return true if a new Player was created; false otherwise + */ + public boolean newScore(String name, int score) { + for (int i = 0; i < scoreboard.size(); i++) { + Player currentPlayer = scoreboard.get(i); + + if (currentPlayer.getName().equals(name)) { + currentPlayer.updateScore(score); + moveUp(i); + } + } + + Player newPlayer = new Player(name, score); + scoreboard.add(newPlayer); + moveUp(scoreboard.size() - 1); + return true; + } + + /** Moves the Player object at index pos upwards in scoreboard to the correct + * position in the list according to its highScore value + * @param pos a valid index for the scoreboard list + */ + public void moveUp(int pos) + { + /* COMPLETE WORKING METHOD PROVIDED */ + /* DO NOT MODIFY THE IMPLEMENTATION OF THIS METHOD*/ + testing.FRQFunctions.moveUp(pos, scoreboard, scoreboard.stream().mapToInt(s -> s.getHighScore()).toArray()); + } + + /* CLASS MEMBERS FOR TESTING */ + /* DO NOT MODIFY ANY OF THE CONSTRUCTORS OR METHODS BELOW*/ + + public ScoreList(ArrayList sb) + { scoreboard = sb; } + + public ArrayList getScoreBoard() + { return scoreboard; } + +}