In: Computer Science
Package pacman.score
Class ScoreBoard
public class ScoreBoard extends ObjectScoreBoard contains previous scores and the current score of the PacmanGame. A score is a name and value that a valid name only contains the following characters:
Implement this for Assignment 1
Constructor Summary
Constructor | Description |
---|---|
ScoreBoard() |
Creates a score board that has no entries and a current score of 0. |
Method Summary
Modifier and Type | Method | Description |
---|---|---|
List<String> | getEntriesByName() |
Gets the stored entries ordered by Name in lexicographic order. |
List<String> | getEntriesByScore() |
Gets the stored entries ordered by the score in descending order ( 9999 first then 9998 and so on ...) then in lexicographic order of the name if the scores match. |
int | getScore() |
Get the current score. |
void | increaseScore(int additional) |
Increases the score if the given additional is greater than 0. |
void | reset() |
Set the current score to 0. |
void | setScore(String name, int score) |
Sets the score for the given name if: name is not null name is a valid score name score is equal to or greater than zero. This should override any score stored for the given name if name and score are valid. |
void | setScores(Map<String,Integer> scores) |
Sets a collection of scores if "scores" is not null, otherwise no scores are modified. |
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitConstructor Detail
ScoreBoard
public ScoreBoard()
Creates a score board that has no entries and a current score of 0.
Implement this for Assignment 1
Method Detail
getEntriesByName
public List<String> getEntriesByName()Gets the stored entries ordered by Name in lexicographic order. The format of the list should be:
ScoreBoard board = new ScoreBoard(); board.setScore("Fred", 100); board.setScore("fred", 20); board.setScore("Fred", 24); List<String> scores = board.getEntriesByName(); System.out.println(scores); // this outputs: // [Fred : 24, fred : 20]
Returns:
List of scores formatted as "NAME : VALUE" in the order described above or an empty list if no entries are stored.
Implement this for Assignment 1
getEntriesByScore
public List<String> getEntriesByScore()Gets the stored entries ordered by the score in descending order ( 9999 first then 9998 and so on ...) then in lexicographic order of the name if the scores match. The format of the list should be:
ScoreBoard board = new ScoreBoard(); board.setScore("Alfie", 100); board.setScore("richard", 20); board.setScore("Alfie", 24); board.setScore("ben", 20); List<String> scores = board.getEntriesByScore(); System.out.println(scores); // this outputs // [Alfie : 24, ben : 20, richard : 20]
Returns:
List of scores formatted as "NAME : VALUE" in the order described above or an empty list if no entries are stored.
Implement this for Assignment 1
setScore
public void setScore(String name, int score)Sets the score for the given name if:
Parameters:
name - of scorer.
score - to set to the given name.
Implement this for Assignment 1
setScores
public void setScores(Map<String,Integer> scores)Sets a collection of scores if "scores" is not null, otherwise no scores are modified. For each score contained in the scores if:
Parameters:
scores - to add.
Implement this for Assignment 1
increaseScore
public void increaseScore(int additional)
Increases the score if the given additional is greater than 0. No change to the current score if additional is less than or equal to 0.
Parameters:
additional - score to add.
Implement this for Assignment 1
getScore
public int getScore()
Get the current score.
Returns:
the current score.
Implement this for Assignment 1
reset
public void reset()
Set the current score to 0.
How to use treemap to solve this problem?
package packman.score;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Map.Entry.*;
import java.util.stream.Collectors;
public class ScoreBoard extends Object {
HashMap<String, Integer> scoreboard = new HashMap<String, Integer>();
int currentScore;
public ScoreBoard() {
this.currentScore = 0;
}
public List<String> getEntriesByName() {
List<String> names = new
ArrayList<String>(scoreboard.keySet());
Collections.sort(names);
List<String> result = new ArrayList<>();
for (String str : names) {
result.add(str +
" : " + scoreboard.get(str));
}
return result;
}
public List<String> getEntriesByScore()
{
//Using java8 feature
HashMap<String, Integer>
sortedMap =scoreboard
.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue())
.collect(
Collectors.toMap(e ->
e.getKey(), e -> e.getValue(), (e1, e2) ->
e2,LinkedHashMap::new));
Iterator<Entry<String,
Integer>> iterator = sortedMap.entrySet().iterator();
List<String> result = new
ArrayList<>();
// Iterate over
the HashMap
while
(iterator.hasNext()) {
// Get the entry at this iteration
Entry<String, Integer> entry =
iterator.next();
result.add(entry.getKey()+" :
"+entry.getValue());
}
return result;
}
public int getScore() {
return this.currentScore;
}
public void increaseScore(int additional) {
if (additional > 0)
this.currentScore = this.currentScore + additional;
}
public void reset() {
this.currentScore = 0;
}
public void setScores(HashMap<String, Integer> scores) {
Iterator<Entry<String, Integer>> iterator = scores.entrySet().iterator();
// Iterate over the
HashMap
while (iterator.hasNext()) {
// Get the
entry at this iteration
Entry<String,
Integer> entry = iterator.next();
boolean flag = entry.getKey().matches("[A-Za-z0-9]{1,}");
if (flag) {
if (entry.getValue() >= 0) {
scoreboard.put(entry.getKey(), entry.getValue());
}
else
System.out.println("Invalid
score");
}
else
System.out.println("Invalid Name");
}
}
public void setScore(String name, int score) {
boolean flag = name.matches("[A-Za-z0-9]{1,}");
if (flag) {
if (score >= 0) {
scoreboard.put(name, score);
}
else
System.out.println("Invalid score");
}
else
System.out.println("Invalid Name");
}
}
Please Give an upvote if you find the answer helpful.