-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathScrabble.java
More file actions
55 lines (45 loc) · 1.42 KB
/
Scrabble.java
File metadata and controls
55 lines (45 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.booleanuk;
import java.util.HashMap;
public class Scrabble {
private final String word;
public Scrabble(String word) {
this.word = word;
}
public int score() {
int score = 0;
HashMap<char[], Integer> scoreMap = getScoreMap();
score = singleScore(scoreMap);
return score;
}
private static HashMap<char[], Integer> getScoreMap() {
char[] ones = {'A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T'};
char[] twos = {'D', 'G'};
char[] threes = {'B', 'C', 'M', 'P'};
char[] fours = {'F', 'H', 'V', 'W', 'Y'};
char[] fives = {'K'};
char[] eights = {'J', 'X'};
char[] tens = {'Q', 'Z'};
HashMap<char[], Integer> scoreMap = new HashMap<>();
scoreMap.put(ones, 1);
scoreMap.put(twos, 2);
scoreMap.put(threes, 3);
scoreMap.put(fours, 4);
scoreMap.put(fives, 5);
scoreMap.put(eights, 8);
scoreMap.put(tens, 10);
return scoreMap;
}
private int singleScore(HashMap<char[], Integer> scoreMap) {
int score = 0;
for (char c : this.word.toUpperCase().toCharArray()) {
for (char[] kc : scoreMap.keySet()) {
for (char kcc : kc) {
if (c == kcc) {
score += scoreMap.get(kc);
}
}
}
}
return score;
}
}