-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathScrabble.java
More file actions
88 lines (80 loc) · 2.95 KB
/
Scrabble.java
File metadata and controls
88 lines (80 loc) · 2.95 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
package com.booleanuk;
public class Scrabble {
int totalPoints;
public Scrabble(String word) {
totalPoints = 0;
boolean bothAtStart = false, foundCurly = false, invalid = false, foundSquare = false, atStart = false;
char startSymbol = '#';
int ind = 0;
int counter = -1;
for (char c: word.toUpperCase().toCharArray()) {
if (ind == 0){
if (c == '['){
atStart = true;
startSymbol = '[';
} else if (c == '{'){
atStart = true;
startSymbol = '{';
}
}
if (ind == 1 && (c == '[' || c == '{') && atStart){
bothAtStart = true;
}
if (c == ']' && ind == word.length()-1 && atStart && startSymbol != '[' && bothAtStart){
invalid = true;
}
if (c == '}' && ind == word.length()-1 && atStart && startSymbol != '{' && bothAtStart){
invalid = true;
}
if (c == ']' && ind == word.length()-1 && atStart && startSymbol != '[' && !foundSquare){
invalid = true;
} else if (c == '}' && ind == word.length()-1 && atStart && startSymbol != '{' && !foundCurly){
invalid = true;
}
int points = 0;
switch (c) {
case '[' -> foundSquare = true;
case '{' -> foundCurly = true;
case ']' -> {
if (!foundSquare || counter > 1 && !atStart) {
invalid = true;
}
else { foundSquare = false; }
}
case '}' -> {
if (!foundCurly || counter > 1 && !atStart){
invalid = true;
} else { foundCurly = false; }
}
case 'A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T' -> points = 1;
case 'D', 'G' -> points = 2;
case 'B', 'C', 'M', 'P' -> points = 3;
case 'F', 'H', 'V', 'W', 'Y' -> points = 4;
case 'K' -> points = 5;
case 'J', 'X' -> points = 8;
case 'Q', 'Z' -> points = 10;
case '|', '!', '"' -> invalid = true;
}
if (foundCurly && foundSquare) {
totalPoints += points * 2 * 3;
counter ++;
} else if (foundSquare) {
totalPoints += points * 3;
counter ++;
} else if (foundCurly) {
totalPoints += points * 2;
counter ++;
} else {
totalPoints += points;
counter = -1;
}
ind++;
}
if (foundCurly || foundSquare || invalid){
totalPoints = 0;
}
}
public int score() {
return totalPoints;
}
}