From a9e1043335fb8d3e64ac92b8251923764fba2b77 Mon Sep 17 00:00:00 2001 From: Magnus Hissingby Date: Wed, 6 Aug 2025 16:02:20 +0200 Subject: [PATCH] Task scrabble --- src/main/java/com/booleanuk/Scrabble.java | 80 ++++++++++++++++++++++- 1 file changed, 78 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/booleanuk/Scrabble.java b/src/main/java/com/booleanuk/Scrabble.java index 88108a8..dd07bf8 100644 --- a/src/main/java/com/booleanuk/Scrabble.java +++ b/src/main/java/com/booleanuk/Scrabble.java @@ -1,12 +1,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 -1; + return totalPoints; } - }