-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathScrabble.java
More file actions
131 lines (121 loc) · 4.3 KB
/
Scrabble.java
File metadata and controls
131 lines (121 loc) · 4.3 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package com.booleanuk;
import java.util.HashMap;
public class Scrabble {
public String word;
public int wordScore;
HashMap<Character, Integer> pointMap = new HashMap<>();
public int multiplier = 1;
public Scrabble(String word) {
this.word = word.toLowerCase();
this.wordScore = 0;
}
public void fillPointMap(HashMap<Character, Integer> pointMap){
pointMap.put('a', 1);
pointMap.put('e', 1);
pointMap.put('i', 1);
pointMap.put('o', 1);
pointMap.put('u', 1);
pointMap.put('l', 1);
pointMap.put('n', 1);
pointMap.put('r', 1);
pointMap.put('s', 1);
pointMap.put('t', 1);
pointMap.put('d', 2);
pointMap.put('g', 2);
pointMap.put('b', 3);
pointMap.put('c', 3);
pointMap.put('m', 3);
pointMap.put('p', 3);
pointMap.put('f', 4);
pointMap.put('h', 4);
pointMap.put('v', 4);
pointMap.put('w', 4);
pointMap.put('y', 4);
pointMap.put('k', 5);
pointMap.put('j', 8);
pointMap.put('x', 8);
pointMap.put('q', 10);
pointMap.put('z', 10);
pointMap.put('{', 0);
pointMap.put('}', 0);
pointMap.put('[', 0);
pointMap.put(']', 0);
}
public boolean invalidWord(String word){
int brackets = 0;
int curlyBrackets = 0;
for (int i = 0; i < word.length(); i++){
if (word.charAt(i) == '{'){
curlyBrackets += 1;
int closingIndex = word.indexOf('}', i);
if (closingIndex == -1) {
return true;
}
String enclosed = word.substring(i + 1, closingIndex);
if (enclosed.length() > 1) {
if (i != 0 || closingIndex != word.length() - 1) {
return true;
}
}
}else if (word.charAt(i) == '}'){
curlyBrackets -= 1;
if (curlyBrackets < 0){return true;}
}else if (word.charAt(i) == '['){
brackets += 1;
}else if (word.charAt(i) == ']'){
brackets -= 1;
if (brackets < 0) {return true;}
}
}
return curlyBrackets != 0 || brackets != 0;
}
public void addPoints(){
fillPointMap(pointMap);
for (int i = 0; i < word.length(); i++){
if (pointMap.containsKey(word.charAt(i))){
wordScore += pointMap.get(word.charAt(i));
if (word.charAt(i) == '{' && (i+2 < word.length() && word.charAt(i+2) == '}')){
wordScore += pointMap.get(word.charAt(i+1));
}else if (word.charAt(i) == '[' && (i+2 < word.length() && word.charAt(i+2) == ']')) {
wordScore += pointMap.get(word.charAt(i + 1)) * 2;
}
}
}
}
public void wordMultiplier(String word){
if (word.charAt(0) == '{'){
if (word.charAt(2) != '}' && word.charAt(word.length()-1) == '}') {
multiplier = multiplier * 2;
if (word.charAt(1) == '[') {
if (word.charAt(3) != ']' && word.charAt(word.length() - 2) == ']') {
multiplier = multiplier * 3;
}
}
}else if (word.charAt(2) != '}' && word.charAt(word.length()-1) == ']'){
multiplier = 0;
}
} else if (word.charAt(0) == '['){
if (word.charAt(2) != ']' && word.charAt(word.length()-1) == ']') {
multiplier = multiplier * 3;
if (word.charAt(1) == '{') {
if (word.charAt(3) != '}' && word.charAt(word.length() - 2) == '}') {
multiplier = multiplier * 2;
}
}
} else if (word.charAt(2) != ']' && word.charAt(word.length()-1) == '}'){
multiplier = 0;
}
}
}
public int score(){
if (word.isEmpty() || word.contains("!") || word.contains("|")){
return 0;
} else if (invalidWord(word)){
return 0;
}
addPoints();
wordMultiplier(word);
wordScore = wordScore * multiplier;
return wordScore;
}
}