diff --git a/csharp-scrabble-challenge.Main/Program.cs b/csharp-scrabble-challenge.Main/Program.cs index 3751555..1bed86f 100644 --- a/csharp-scrabble-challenge.Main/Program.cs +++ b/csharp-scrabble-challenge.Main/Program.cs @@ -1,2 +1,25 @@ -// See https://aka.ms/new-console-template for more information -Console.WriteLine("Hello, World!"); + + +using csharp_scrabble_challenge.Main; + +Console.WriteLine("Welcome to the Scrabble scorer. Insert a word to be scored: "); +string? word = Console.ReadLine(); +if (word == null) +{ + word = ""; +} +Scrabble game = new Scrabble(word); + +bool run = true; +while (run) +{ + int score = game.score(); + Console.WriteLine($"The score was: {score}\nInsert a new word or x to quit: "); + word = Console.ReadLine(); + if (word == null) + { + word = ""; + } + if (word.Equals("x") || word.Equals("X")) { break; } + game.Word = word; +} diff --git a/csharp-scrabble-challenge.Main/Scrabble.cs b/csharp-scrabble-challenge.Main/Scrabble.cs index c1ea013..6f47908 100644 --- a/csharp-scrabble-challenge.Main/Scrabble.cs +++ b/csharp-scrabble-challenge.Main/Scrabble.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics.Tracing; using System.Linq; using System.Reflection.Metadata.Ecma335; using System.Text; @@ -7,17 +8,73 @@ namespace csharp_scrabble_challenge.Main { - public class Scrabble + public class Scrabble(string word) { - public Scrabble(string word) - { - //TODO: do something with the word variable + private string _word = word.ToUpper(); + public string Word { get => _word; set => _word = value.ToUpper(); } + private Dictionary _scoreDict = InitiateScores(); + public int score() + { + int score = 0; + int multiplier = 1; + int doubledActive = 0; + int tripledActive = 0; + char lastchar = 'a'; + + if (this._word.Equals("") || this._word.Equals(" \t\n")) + { + return 0; + } + + foreach (char c in this._word) + { + if (c.Equals('}') && doubledActive == 0) { return 0; } + if(c.Equals(']') && tripledActive == 0) { return 0; } + + if (c.Equals('{')) { multiplier = multiplier * 2; doubledActive++; } + if (c.Equals('[')) { multiplier = multiplier * 3; tripledActive++; } + if (c.Equals('}') && doubledActive > 0) { multiplier = multiplier / 2; doubledActive--; } + if (c.Equals(']') && tripledActive > 0) { multiplier = multiplier / 3; tripledActive--; } + + if (this._scoreDict.ContainsKey(c)) + { + score += this._scoreDict[c] * multiplier; + } + else if(!"{}[]".Contains(c)) + { + return 0; + } + if (c.Equals('}') && lastchar == '{') { return 0; } + if (c.Equals(']') && lastchar == '[') { return 0; } + lastchar = c; + } + + if (doubledActive > 0 || tripledActive > 0) { return 0; } + return score; } - public int score() + private static Dictionary InitiateScores() { - //TODO: score calculation code goes here - throw new NotImplementedException(); //TODO: Remove this line when the code has been written + List score1 = new List(new char[]{'A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T'}); + List score2 = new List(new char[] {'D', 'G'}); + List score3 = new List(new char[] {'B', 'C', 'M', 'P'}); + List score4 = new List(new char[] {'F', 'H', 'V', 'W', 'Y'}); + List score5 = new List(new char[] {'K'}); + List score8 = new List(new char[] { 'J', 'X'}); + List score10 = new List(new char[] { 'Q', 'Z'}); + + Dictionary scoreDict = new Dictionary(); + + foreach (char c in score1) { scoreDict.Add(c, 1); } + foreach (char c in score2) { scoreDict.Add(c, 2); } + foreach (char c in score3) { scoreDict.Add(c, 3); } + foreach (char c in score4) { scoreDict.Add(c, 4); } + foreach (char c in score5) { scoreDict.Add(c, 5); } + foreach (char c in score8) { scoreDict.Add(c, 8); } + foreach (char c in score10) { scoreDict.Add(c, 10); } + + return scoreDict; } } + } diff --git a/csharp-scrabble-challenge.Test/CoreTests.cs b/csharp-scrabble-challenge.Test/CoreTests.cs index f42e402..ecdc8c0 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)] @@ -17,7 +21,9 @@ public class CoreTests [TestCase("street", 6)] public void WordScoreTests(string word, int targetScore) { - Assert.AreEqual(this.GetWordScore(word), targetScore); + Scrabble scrabble = new Scrabble(word); + + Assert.That(scrabble.score(), Is.EqualTo(targetScore)); } private int GetWordScore(string word) => new Scrabble(word).score();