From 633c01adf3ec8f1fe269f96a044b726de302fddb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hans=20Jakob=20H=C3=A5land?= Date: Wed, 6 Aug 2025 14:23:29 +0200 Subject: [PATCH] Passes all tests and adds console UI --- csharp-scrabble-challenge.Main/Points.cs | 50 +++++++ csharp-scrabble-challenge.Main/Program.cs | 17 ++- csharp-scrabble-challenge.Main/Scrabble.cs | 147 +++++++++++++++++++- csharp-scrabble-challenge.Test/CoreTests.cs | 10 +- 4 files changed, 216 insertions(+), 8 deletions(-) create mode 100644 csharp-scrabble-challenge.Main/Points.cs diff --git a/csharp-scrabble-challenge.Main/Points.cs b/csharp-scrabble-challenge.Main/Points.cs new file mode 100644 index 0000000..7b773df --- /dev/null +++ b/csharp-scrabble-challenge.Main/Points.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace csharp_scrabble_challenge.Main +{ + internal class Points + { + + public Dictionary PointsDict = new Dictionary(); + + + public Points() { + PointsDict.Add("A", 1); + PointsDict.Add("E", 1); + PointsDict.Add("I", 1); + PointsDict.Add("L", 1); + PointsDict.Add("N", 1); + PointsDict.Add("O", 1); + PointsDict.Add("R", 1); + PointsDict.Add("S", 1); + PointsDict.Add("T", 1); + PointsDict.Add("U", 1); + + PointsDict.Add("D", 2); + PointsDict.Add("G", 2); + + PointsDict.Add("B", 3); + PointsDict.Add("C", 3); + PointsDict.Add("M", 3); + PointsDict.Add("P", 3); + + PointsDict.Add("F", 4); + PointsDict.Add("H", 4); + PointsDict.Add("V", 4); + PointsDict.Add("W", 4); + PointsDict.Add("Y", 4); + + PointsDict.Add("K", 5); + + PointsDict.Add("J", 8); + PointsDict.Add("X", 8); + + PointsDict.Add("Q", 10); + PointsDict.Add("Z", 10); + } + } +} diff --git a/csharp-scrabble-challenge.Main/Program.cs b/csharp-scrabble-challenge.Main/Program.cs index 3751555..36db4c3 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; +using System.Diagnostics.Tracing; + +while (true) +{ + Console.WriteLine("Enter a word"); + + string word = Console.ReadLine(); + + Scrabble scrabble = new Scrabble(word); + + int score = scrabble.score(); + + Console.WriteLine($"The score for {word} is {score}."); +} \ No newline at end of file diff --git a/csharp-scrabble-challenge.Main/Scrabble.cs b/csharp-scrabble-challenge.Main/Scrabble.cs index c1ea013..5384136 100644 --- a/csharp-scrabble-challenge.Main/Scrabble.cs +++ b/csharp-scrabble-challenge.Main/Scrabble.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Reflection.Metadata.Ecma335; +using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; @@ -9,15 +10,153 @@ namespace csharp_scrabble_challenge.Main { public class Scrabble { + string _word; + public Scrabble(string word) - { - //TODO: do something with the word variable + { + _word = word; + } + + public bool containsWhiteSpace(string word) + { + if (word.Contains("\n")) { return true; } + if (word.Contains("\r")) { return true; } + if (word.Contains("\t")) { return true; } + if (word.Contains("\b")) { return true; } + if (word.Contains("\f")) { return true; } + return false; + } + + public bool containsDigit(string word) + { + return word.Any(Char.IsDigit); + } + + public bool isInvalidBracket(string word) + { + int numOpenSquare = 0; + int numCloseSquare = 0; + int numOpenCurly = 0; + int numCloseCurly = 0; + + foreach (char c in word) + { + if (c == '[') + { + numOpenSquare++; + } + if (c == ']') + { + numCloseSquare++; + if (numCloseSquare > numOpenSquare) + { + return true; + } + } + if (c == '{') { + numOpenCurly++; + } + if (c == '}') { + numCloseCurly++; + if (numCloseCurly > numOpenCurly) + { + return true; + } + } + } + if (numOpenCurly != numCloseCurly) + { + return false; + } + if (numOpenSquare != numCloseSquare) + { + return false; + } + return false; + } + + public int calcWordMultiplier(string upperCaseWord) + { + if (upperCaseWord[0] == '{' && upperCaseWord.Last() == '}') + { + foreach (char c in upperCaseWord) + { + + } + return 2; + } + if (upperCaseWord[0] == '[' && upperCaseWord.Last() == ']') + { + return 3; + } + return 1; } 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; } + // Method above did not catch all cases + if (containsWhiteSpace(_word)) { return 0; } + if (containsDigit(_word)) { return 0; } + if (isInvalidBracket(_word)) { return 0; } + + Points points = new Points(); + + int score = 0; + + // Match upper case letters in point dictionary + string upperCaseWord = _word.ToUpper(); + + + int inSquareBracket = 0; + int inCurlyBracket = 0; + + for (int i = 0; i new Scrabble(word).score();