From dce4f0a240b1d3ea62f9fa41bdc0d85e514075ae Mon Sep 17 00:00:00 2001 From: Miadog7Extra Date: Mon, 13 Jan 2025 18:56:10 +0100 Subject: [PATCH] core and extension --- csharp-scrabble-challenge.Main/Scrabble.cs | 56 ++++++++++++++++++++-- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/csharp-scrabble-challenge.Main/Scrabble.cs b/csharp-scrabble-challenge.Main/Scrabble.cs index c1ea013..6fed3a9 100644 --- a/csharp-scrabble-challenge.Main/Scrabble.cs +++ b/csharp-scrabble-challenge.Main/Scrabble.cs @@ -9,15 +9,65 @@ namespace csharp_scrabble_challenge.Main { public class Scrabble { + private string _word; public Scrabble(string word) { - //TODO: do something with the word variable + _word = word.ToUpper(); + + } + + public int LetterValue(char letter) + { + int value = letter switch + { + 'A' or 'E' or 'I' or 'O' or 'U' or 'L' or 'N' or 'R' or 'S' or 'T' => 1, + 'D' or 'G' => 2, + 'B' or 'C' or 'M' or 'P' => 3, + 'F' or 'H' or 'V' or 'W' or 'Y' => 4, + 'K' => 5, + 'J' or 'X' => 8, + 'Q' or 'Z' => 10, + '{' or '}' or '[' or ']' => 0, + _ => 0 + }; + return value; } public int score() { - //TODO: score calculation code goes here - throw new NotImplementedException(); //TODO: Remove this line when the code has been written + + bool doublePoint = false; + bool triplePoint = false; + + int score = 0; + if (_word.Length == 0 || !IsValid(_word)) + { + return score; + } + + foreach (char c in _word) + { + + if (c == '{') + { + doublePoint = true; + } + else if (c == '[') + { + triplePoint = true; + } + + score += LetterValue(c); + } + if (doublePoint) { score = score * 2; } + else if (triplePoint) { score = score * 3; } + + return score; + } + + public bool IsValid(string word) + { + return System.Text.RegularExpressions.Regex.IsMatch(word, @"^[a-zA-Z\[\]{}]+$"); } } }