From ef4fbac33a49af54b9637468d8b0a09989f87e92 Mon Sep 17 00:00:00 2001 From: Linda Do Date: Wed, 6 Aug 2025 14:39:13 +0200 Subject: [PATCH 1/2] completed --- src/main/java/com/booleanuk/Scrabble.java | 38 +++++++++++++++++++++-- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/booleanuk/Scrabble.java b/src/main/java/com/booleanuk/Scrabble.java index 88108a8..099f9e3 100644 --- a/src/main/java/com/booleanuk/Scrabble.java +++ b/src/main/java/com/booleanuk/Scrabble.java @@ -1,12 +1,44 @@ package com.booleanuk; +import java.util.*; + public class Scrabble { - public Scrabble(String word) { + private final String word; + public Scrabble(String word) { + this.word = word; + } public int score() { - return -1; - } + List value_1 = new ArrayList<>(List.of('A','E','I','O','U','L','N','R','S','T')); + List value_2 = new ArrayList<>(List.of('D', 'G')); + List value_3 = new ArrayList<>(List.of('B','C','M','P')); + List value_4 = new ArrayList<>(List.of('F', 'H', 'V', 'W', 'Y')); + List value_5 = new ArrayList<>(List.of('K')); + List value_8 = new ArrayList<>(List.of('J','X')); + List value_10 = new ArrayList<>(List.of('Q','Z')); + + HashMap scores = new HashMap<>(); + value_1.forEach(x -> scores.put(x,1)); + value_2.forEach(x -> scores.put(x,2)); + value_3.forEach(x -> scores.put(x,3)); + value_4.forEach(x -> scores.put(x,4)); + value_5.forEach(x -> scores.put(x,5)); + value_8.forEach(x -> scores.put(x,8)); + value_10.forEach(x -> scores.put(x,10)); + String wordToUpper = word.toUpperCase(); + char[] characters = wordToUpper.toCharArray(); + + int sum = 0; + + for (char character : characters) { + if (scores.containsKey(character)){ + sum+=scores.get(character); + } + } + + return sum; + } } From ae0a648bca8806ef40455fd474768f0ef01acb9e Mon Sep 17 00:00:00 2001 From: Linda Do Date: Thu, 7 Aug 2025 14:21:39 +0200 Subject: [PATCH 2/2] completed --- src/main/java/com/booleanuk/Scrabble.java | 129 ++++++++++++++++++---- 1 file changed, 107 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/booleanuk/Scrabble.java b/src/main/java/com/booleanuk/Scrabble.java index 099f9e3..bdbffdb 100644 --- a/src/main/java/com/booleanuk/Scrabble.java +++ b/src/main/java/com/booleanuk/Scrabble.java @@ -7,38 +7,123 @@ public class Scrabble { public Scrabble(String word) { this.word = word; - } public int score() { - List value_1 = new ArrayList<>(List.of('A','E','I','O','U','L','N','R','S','T')); - List value_2 = new ArrayList<>(List.of('D', 'G')); - List value_3 = new ArrayList<>(List.of('B','C','M','P')); - List value_4 = new ArrayList<>(List.of('F', 'H', 'V', 'W', 'Y')); - List value_5 = new ArrayList<>(List.of('K')); - List value_8 = new ArrayList<>(List.of('J','X')); - List value_10 = new ArrayList<>(List.of('Q','Z')); - - HashMap scores = new HashMap<>(); - value_1.forEach(x -> scores.put(x,1)); - value_2.forEach(x -> scores.put(x,2)); - value_3.forEach(x -> scores.put(x,3)); - value_4.forEach(x -> scores.put(x,4)); - value_5.forEach(x -> scores.put(x,5)); - value_8.forEach(x -> scores.put(x,8)); - value_10.forEach(x -> scores.put(x,10)); + List value_1 = new ArrayList<>(List.of('A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T')); //immutable to mutable list + List value_2 = List.of('D', 'G'); //immutable list + List value_3 = List.of('B', 'C', 'M', 'P'); + List value_4 = List.of('F', 'H', 'V', 'W', 'Y'); + List value_5 = List.of('K'); + List value_8 = List.of('J', 'X'); + List value_10 =List.of('Q', 'Z'); + + HashMap scores = new HashMap<>(); + value_1.forEach(x -> scores.put(x, 1)); + value_2.forEach(x -> scores.put(x, 2)); + value_3.forEach(x -> scores.put(x, 3)); + value_4.forEach(x -> scores.put(x, 4)); + value_5.forEach(x -> scores.put(x, 5)); + value_8.forEach(x -> scores.put(x, 8)); + value_10.forEach(x -> scores.put(x, 10)); String wordToUpper = word.toUpperCase(); - char[] characters = wordToUpper.toCharArray(); + int wordMultiplier = 1; + + if (wordToUpper.startsWith("[") && wordToUpper.endsWith("]")) { + String temp = wordToUpper.substring(1, wordToUpper.length() - 1); + if (temp.startsWith("{") && temp.endsWith("}")) { + wordMultiplier = 3 * 2; + wordToUpper = temp.substring(1, temp.length() - 1); + } else if (isSimpleWord(temp)) { + wordMultiplier = 3; + wordToUpper = temp; + } + } else if (wordToUpper.startsWith("{") && wordToUpper.endsWith("}")) { + String temp = wordToUpper.substring(1, wordToUpper.length() - 1); + if (temp.startsWith("[") && temp.endsWith("]")) { + wordMultiplier = 2 * 3; + wordToUpper = temp.substring(1, temp.length() - 1); + } else if (isSimpleWord(temp)) { + wordMultiplier = 2; + wordToUpper = temp; + } else if(!hasLetterMultiplyPattern(temp)) { + wordMultiplier = 2; + wordToUpper = temp; + } + } int sum = 0; - for (char character : characters) { - if (scores.containsKey(character)){ - sum+=scores.get(character); + for (int i = 0; i < wordToUpper.length(); i++) { + char current = wordToUpper.charAt(i); + + int value; + int letterMultiplier = 1; + + if (current == '{') { + if (i + 2 < wordToUpper.length() && wordToUpper.charAt(i + 2) == '}') { + char letter = wordToUpper.charAt(i + 1); + + if (scores.containsKey(letter)) { + value = scores.get(letter); + letterMultiplier = 2; + i += 2; + } else { + return 0; + } + } else { + return 0; + } + } else if (current == '[') { + if (i + 2 < wordToUpper.length() && wordToUpper.charAt(i + 2) == ']') { + char letter = wordToUpper.charAt(i + 1); + if (scores.get(letter) != null) { + value = scores.get(letter); + letterMultiplier = 3; + i += 2; + } else { + return 0; + } + } else { + return 0; + } + } else if (current == '}' || current == ']') { + return 0; + } else { + if (scores.containsKey(current)) { + value = scores.get(current); + } else { + return 0; + } } + sum += value * letterMultiplier; } + return sum * wordMultiplier; + } - return sum; + private boolean hasLetterMultiplyPattern (String word) { + boolean hasLetter = false; + boolean hasBracketsAfterLetter = false; + for (int i = 0; i < word.length(); i++) { + char c = word.charAt(i); + if (Character.isLetter(c)){ + hasLetter = true; + } else if ((c == '}' || c == '{') && hasLetter && i < word.length() - 1) { + hasBracketsAfterLetter = true; + } + } + return hasLetter && hasBracketsAfterLetter; + } + + private boolean isSimpleWord(String s) { + for (char c : s.toCharArray()) { + if (c == '{' || c == '}' || c == '[' || c == ']') { + return false; + } + } + return true; } } + +