-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathScrabble.java
More file actions
100 lines (78 loc) · 2.25 KB
/
Scrabble.java
File metadata and controls
100 lines (78 loc) · 2.25 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
package com.booleanuk;
import java.util.HashMap;
import java.util.Map;
public class Scrabble {
private String word;
public Scrabble(String word) {
this.word = word;
}
public int score() {
this.word = this.word.toLowerCase();
//Map<Character, Integer> map = new HashMap<>();
Map<Character, Integer> score = new HashMap<>();
score.put('a', 1);
score.put('e', 1);
score.put('i', 1);
score.put('o', 1);
score.put('u', 1);
score.put('l', 1);
score.put('n', 1);
score.put('r', 1);
score.put('s', 1);
score.put('t', 1);
score.put('d', 2);
score.put('g', 2);
score.put('b', 3);
score.put('m', 3);
score.put('p', 3);
score.put('c', 3);
score.put('f', 4);
score.put('h', 4);
score.put('v', 4);
score.put('w', 4);
score.put('y', 4);
score.put('k', 5);
score.put('j', 8);
score.put('x', 8);
score.put('q', 10);
score.put('z', 10);
int total = 0;
char oldC = ' ';
char oldC2 = ' ';
for (char c : this.word.toCharArray()){
if (c== '{' || c== '[' || oldC== '{' || oldC== '['){
oldC2 = oldC;
oldC = c;
}
else if ((c== ']' && oldC2 != '[') || (c== '}' && oldC2 != '{')){
return 0;
}
else if (oldC2 == '{' || oldC2 == '[') {
if (c == '}') {
total += score.get(oldC)*2;
oldC2 = oldC;
oldC = c;
}
else if (c==']') {
total += score.get(oldC)*3;
oldC2 = oldC;
oldC = c;
}
else {
return 0;
}
}
// else if (oldC2 == '[' && c == ']'){
// total += score.get(oldC)*2;
// oldC2 = oldC;
// oldC = c;
// }
else if (score.containsKey(c)) {
total += score.get(c);
oldC2 = oldC;
oldC = c;
}
}
return total;
}
}