From 763c97ad505402b59cc59809e902c46df05058c3 Mon Sep 17 00:00:00 2001 From: Mona Eikli Andresen Date: Wed, 6 Aug 2025 14:44:34 +0200 Subject: [PATCH] Mona --- csharp-scrabble-challenge.Main/Program.cs | 17 ++- csharp-scrabble-challenge.Main/Scrabble.cs | 102 +++++++++++++++++- csharp-scrabble-challenge.Test/CoreTests.cs | 6 +- .../ExtensionTests.cs | 1 + 4 files changed, 119 insertions(+), 7 deletions(-) diff --git a/csharp-scrabble-challenge.Main/Program.cs b/csharp-scrabble-challenge.Main/Program.cs index 3751555..ce60a26 100644 --- a/csharp-scrabble-challenge.Main/Program.cs +++ b/csharp-scrabble-challenge.Main/Program.cs @@ -1,2 +1,15 @@ -// See https://aka.ms/new-console-template for more information -Console.WriteLine("Hello, World!"); +using csharp_scrabble_challenge.Main; + +while (true) +{ + Console.Write("Write a word (or 'qqq' to quit): "); + string input = Console.ReadLine(); + + if (input == "qqq") + { + break; + } + + Scrabble s = new Scrabble(input); + Console.WriteLine($"Score: {s.score()}\n"); +} diff --git a/csharp-scrabble-challenge.Main/Scrabble.cs b/csharp-scrabble-challenge.Main/Scrabble.cs index c1ea013..f6f154e 100644 --- a/csharp-scrabble-challenge.Main/Scrabble.cs +++ b/csharp-scrabble-challenge.Main/Scrabble.cs @@ -5,19 +5,113 @@ using System.Text; using System.Threading.Tasks; + namespace csharp_scrabble_challenge.Main { public class Scrabble { + private string word; + + private static Dictionary letterScores = new() + { + ['A'] = 1, ['E'] = 1, ['I'] = 1, ['O'] = 1, ['U'] = 1, ['L'] = 1, ['N'] = 1, ['R'] = 1, ['S'] = 1, ['T'] = 1, + ['D'] = 2, ['G'] = 2, + ['B'] = 3, ['C'] = 3, ['M'] = 3, ['P'] = 3, + ['F'] = 4, ['H'] = 4, ['V'] = 4, ['W'] = 4, ['Y'] = 4, + ['K'] = 5, + ['J'] = 8, ['X'] = 8, + ['Q'] = 10, ['Z'] = 10 + }; + public Scrabble(string word) - { - //TODO: do something with the word variable + { + this.word = word; } public int score() { - //TODO: score calculation code goes here - throw new NotImplementedException(); //TODO: Remove this line when the code has been written + if (string.IsNullOrWhiteSpace(word)) + return 0; + + string input = word.Trim(); + int wordMultiplier = 1; + + // Sjekk for ytre { } rundt hele ordet (dobbel word) + if (input.Length >= 2 && input[0] == '{' && input[^1] == '}') + { + string inner = input.Substring(1, input.Length - 2); + + // Hvis det ikke finnes andre { eller } inne i teksten, gjelder dobbel word + if (!inner.Contains('{') && !inner.Contains('}')) + { + wordMultiplier = 2; + input = inner; + } + } + // Sjekk for ytre [ ] rundt hele ordet (trippel word) + else if (input.Length >= 2 && input[0] == '[' && input[^1] == ']') + { + string inner = input.Substring(1, input.Length - 2); + + // Hvis det ikke finnes andre [ eller ] inne i teksten, gjelder trippel word + if (!inner.Contains('[') && !inner.Contains(']')) + { + wordMultiplier = 3; + input = inner; + } + } + + int total = 0; + int i = 0; + + // Gå gjennom hver bokstav i ordet + while (i < input.Length) + { + char c = input[i]; + + // Hvis vi møter { eller [, prøver vi å finne riktig lukking (} eller ]) + if (c == '{' || c == '[') + { + char expectedClose = c == '{' ? '}' : ']'; + int multiplier = c == '{' ? 2 : 3; + + // Finn posisjonen til tilhørende lukketegn + int closeIndex = input.IndexOf(expectedClose, i + 1); + if (closeIndex == -1) + return 0; // mangler lukketegn → ugyldig + + // Innholdet mellom åpen og lukket tegn + string inside = input.Substring(i + 1, closeIndex - i - 1); + + // Kun én gyldig bokstav tillatt inne i boost-par + if (inside.Length != 1 || !letterScores.ContainsKey(char.ToUpper(inside[0]))) + return 0; + + char letter = char.ToUpper(inside[0]); + total += letterScores[letter] * multiplier; + + i = closeIndex + 1; // hopp videre + } + // Hvis vi finner en lukketegn uten åpen → ugyldig + else if (c == '}' || c == ']') + { + return 0; + } + // Vanlig bokstav + else + { + char letter = char.ToUpper(c); + if (!letterScores.ContainsKey(letter)) + return 0; + + total += letterScores[letter]; + i++; + } + } + + return total * wordMultiplier; } + + } } diff --git a/csharp-scrabble-challenge.Test/CoreTests.cs b/csharp-scrabble-challenge.Test/CoreTests.cs index f42e402..57dff72 100644 --- a/csharp-scrabble-challenge.Test/CoreTests.cs +++ b/csharp-scrabble-challenge.Test/CoreTests.cs @@ -5,7 +5,11 @@ namespace csharp_scrabble_challenge.Test { [TestFixture] public class CoreTests - { + { + [TestCase("[{h}o1s{e}]", 0)] // error case (zero for errors) + [TestCase("{h}ous{e}", 13)] + [TestCase("[{h}ous{e}]", 39)] + [TestCase("[h}ous{e}]", 0)] //Error case (zero for errors) [TestCase("", 0)] [TestCase(" ", 0)] [TestCase(" \t\n", 0)] diff --git a/csharp-scrabble-challenge.Test/ExtensionTests.cs b/csharp-scrabble-challenge.Test/ExtensionTests.cs index 8eb37dc..453d231 100644 --- a/csharp-scrabble-challenge.Test/ExtensionTests.cs +++ b/csharp-scrabble-challenge.Test/ExtensionTests.cs @@ -18,6 +18,7 @@ public class ExtensionTests [TestCase("[quirky]", 66)] //extension triple word [TestCase("{OXyPHEnBUTaZoNE}", 82)] [TestCase("[OXyPHEnBUTaZoNE]", 123)] + public void ExtendedCriteriaTests(string word, int targetScore) { Assert.AreEqual(this.GetWordScore(word), targetScore);