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.
I don't know how to deal with this part.
//////////////////////////////////////////
package pacman.score;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;
public class ScoreBoard {
private Map<String,Integer> allScores =
null;
private int currentScore = 0;
//constructor
//initializes the score board
public ScoreBoard() {
this.allScores = new
HashMap<>();
this.currentScore = 0;
}
//get current score
public int getScore(){
return currentScore;
}
//reset current score
public void reset(){
currentScore = 0;
}
//increase current score if addition > 0
public void increaseScore(int additional){
if(additional > 0 ){
currentScore +=
additional;
}
}
//add the name, score pair in allScores map after
doing validation check
public void setScore(String name, int score){
if(isValid(name,score)){
allScores.put(name, score);
}
}
//sets allScores from given map of scores
//after doing validation check of=n key-value
pair
public void setScores(Map<String,Integer>
scores){
if(scores!=null &&
scores.keySet()!=null){
for(String
name:scores.keySet()){
int score = scores.get(name);
if(isValid(name,score)){
allScores.put(name,
score);
}
}
}
}
//get List of entries by lexicographical order of
name
public List<String> getEntriesByName(){
List<String> entries = new
ArrayList<String>();
List<Map.Entry<String,
Integer> > list =
new LinkedList<Map.Entry<String, Integer>
>(allScores.entrySet());
//sort by ascending order of
name
Collections.sort(list, new
Comparator<Map.Entry<String,Integer>>(){
@Override
public int
compare(Entry<String, Integer> arg0, Entry<String,
Integer> arg1) {
return
arg0.getKey().compareTo(arg1.getKey());
}
});
for(Map.Entry<String,Integer>
data: list){
entries.add(data.getKey()+" : "+data.getValue());
}
return entries;
}
/**
* getList of entries by descending order of
score
* @return
*/
public List<String> getEntriesByScore(){
List<String> entries = new
ArrayList<String>();
List<Map.Entry<String,
Integer> > list =
new LinkedList<Map.Entry<String, Integer>
>(allScores.entrySet());
Collections.sort(list, new
Comparator<Map.Entry<String,Integer>>(){
@Override
public int
compare(Entry<String, Integer> arg0, Entry<String,
Integer> arg1) {
return
arg1.getValue().compareTo(arg0.getValue());
}
});
for(Map.Entry<String,Integer>
data: list){
entries.add(data.getKey()+" : "+data.getValue());
}
return entries;
}
/**
* Check if name and score is valid
* @param name
* @param score
* @return
*/
private boolean isValid(String name,int score){
boolean isValid = false;
Pattern pattern =
Pattern.compile("[A-Za-z0-9]*");
if(name!=null &&
name.length() > 0 && pattern.matcher(name).matches()){
//matches pattern for name
if(score >=
0){
isValid = true;
}else{
System.out.println("Invalid score: "+ score+ "
for name: "+name);
}
}else{
System.out.println("Invalid name: "+ name);
}
return isValid;
}
}
///////////////////////////////////////////////////
package pacman.score;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TestData {
public static void main(String[] args){
ScoreBoard sb = new
ScoreBoard();
sb.setScore("Fred", 20);
sb.setScore("Alex", 18);
sb.setScore("Bob", 11);
sb.setScore("Tom", 17);
System.out.println("Showing entries
by name:");
List<String> entries1 =
sb.getEntriesByName();
System.out.println("["+String.join(", ",
entries1.stream().toArray(String[]::new))+"]");
System.out.println();
Map<String,Integer>
otherEntries = new HashMap<String,Integer>();
otherEntries.put("Bob09$",
11);
otherEntries.put("Sam", -11);
otherEntries.put("Amy", 16);
otherEntries.put("Tom", 13);
sb.setScores(otherEntries); //set
scores from the otherEntries map
System.out.println();
System.out.println("Showing entries
by score after adding other Entries:");
List<String> entries2 =
sb.getEntriesByScore();
System.out.println("["+String.join(", ",
entries2.stream().toArray(String[]::new))+"]");
System.out.println();
System.out.println("current
score:"+sb.getScore());
sb.increaseScore(4);//increase
current score by 4
System.out.println("Current score
after increase:"+sb.getScore());
sb.increaseScore(6); //increase
current score by 6
System.out.println("Current score
after increase:"+sb.getScore());
sb.reset(); //reset current
score
System.out.println("Current score
after reset:"+sb.getScore());
}
}
=================================
OUTPUT
=================================
Showing entries by name:
[Alex : 18, Bob : 11, Fred : 20, Tom : 17]
Invalid score: -11 for name: Sam
Invalid name: Bob09$
Showing entries by score after adding other Entries:
[Fred : 20, Alex : 18, Amy : 16, Tom : 13, Bob : 11]
current score:0
Current score after increase:4
Current score after increase:10
Current score after reset:0