From c2c79989b60bbbc5ae46c10ec04babb63d703815 Mon Sep 17 00:00:00 2001 From: Robin Kaga Date: Wed, 8 Jan 2025 15:46:07 +0100 Subject: [PATCH 1/2] Scrabble core finished --- src/main/java/com/booleanuk/Scrabble.java | 47 ++++++++++++++++++++++- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/booleanuk/Scrabble.java b/src/main/java/com/booleanuk/Scrabble.java index 88108a8..8642b50 100644 --- a/src/main/java/com/booleanuk/Scrabble.java +++ b/src/main/java/com/booleanuk/Scrabble.java @@ -1,12 +1,55 @@ package com.booleanuk; +import java.util.ArrayList; +import java.util.Arrays; + public class Scrabble { + + public String word; + public int wordScore; + ArrayList onePoint = new ArrayList<>(Arrays.asList('a','e','i','o','u','l','n','r','s','t')); + ArrayList twoPoint = new ArrayList<>(Arrays.asList('d', 'g')); + ArrayList threePoint = new ArrayList<>(Arrays.asList('b','c','m','p')); + ArrayList fourPoint = new ArrayList<>(Arrays.asList('f','h','v','w','y')); + ArrayList fivePoint = new ArrayList<>(Arrays.asList('k')); + ArrayList eightPoint = new ArrayList<>(Arrays.asList('j', 'x')); + ArrayList tenPoint = new ArrayList<>(Arrays.asList('q', 'z')); + public Scrabble(String word) { + this.word = word.toLowerCase(); + this.wordScore = 0; + } + public void addPoints(){ + for (int i = 0; i < word.length(); i++){ + if (onePoint.contains(word.charAt(i))){ + wordScore += 1; + } else if (twoPoint.contains(word.charAt(i))){ + wordScore += 2; + } else if (threePoint.contains(word.charAt(i))){ + wordScore += 3; + } else if (fourPoint.contains(word.charAt(i))){ + wordScore += 4; + } else if (fivePoint.contains(word.charAt(i))){ + wordScore += 5; + } else if (eightPoint.contains(word.charAt(i))){ + wordScore += 8; + } else if (tenPoint.contains(word.charAt(i))){ + wordScore += 10; + } + } } - public int score() { - return -1; + public int score(){ + if (word.isEmpty() || word.contains("\\")){ + return 0; + } else { + addPoints(); + return wordScore; + } + } + + } From c7490f1ced7e28480c4639ae139e0a1c270be4a5 Mon Sep 17 00:00:00 2001 From: Robin Kaga Date: Thu, 9 Jan 2025 20:00:42 +0100 Subject: [PATCH 2/2] finished scrabble extension --- src/main/java/com/booleanuk/Scrabble.java | 136 +++++++++++++++++----- 1 file changed, 106 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/booleanuk/Scrabble.java b/src/main/java/com/booleanuk/Scrabble.java index 8642b50..6a2b9f0 100644 --- a/src/main/java/com/booleanuk/Scrabble.java +++ b/src/main/java/com/booleanuk/Scrabble.java @@ -1,55 +1,131 @@ package com.booleanuk; -import java.util.ArrayList; -import java.util.Arrays; +import java.util.HashMap; public class Scrabble { public String word; public int wordScore; - ArrayList onePoint = new ArrayList<>(Arrays.asList('a','e','i','o','u','l','n','r','s','t')); - ArrayList twoPoint = new ArrayList<>(Arrays.asList('d', 'g')); - ArrayList threePoint = new ArrayList<>(Arrays.asList('b','c','m','p')); - ArrayList fourPoint = new ArrayList<>(Arrays.asList('f','h','v','w','y')); - ArrayList fivePoint = new ArrayList<>(Arrays.asList('k')); - ArrayList eightPoint = new ArrayList<>(Arrays.asList('j', 'x')); - ArrayList tenPoint = new ArrayList<>(Arrays.asList('q', 'z')); + HashMap pointMap = new HashMap<>(); + public int multiplier = 1; public Scrabble(String word) { this.word = word.toLowerCase(); this.wordScore = 0; } + public void fillPointMap(HashMap 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 (onePoint.contains(word.charAt(i))){ - wordScore += 1; - } else if (twoPoint.contains(word.charAt(i))){ - wordScore += 2; - } else if (threePoint.contains(word.charAt(i))){ - wordScore += 3; - } else if (fourPoint.contains(word.charAt(i))){ - wordScore += 4; - } else if (fivePoint.contains(word.charAt(i))){ - wordScore += 5; - } else if (eightPoint.contains(word.charAt(i))){ - wordScore += 8; - } else if (tenPoint.contains(word.charAt(i))){ - wordScore += 10; + 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("\\")){ + if (word.isEmpty() || word.contains("!") || word.contains("|")){ + return 0; + } else if (invalidWord(word)){ return 0; - } else { - addPoints(); - return wordScore; } - + addPoints(); + wordMultiplier(word); + wordScore = wordScore * multiplier; + return wordScore; } - - }