diff --git a/csharp-scrabble-challenge.Main/Program.cs b/csharp-scrabble-challenge.Main/Program.cs index 3751555..6290683 100644 --- a/csharp-scrabble-challenge.Main/Program.cs +++ b/csharp-scrabble-challenge.Main/Program.cs @@ -1,2 +1,18 @@ -// See https://aka.ms/new-console-template for more information -Console.WriteLine("Hello, World!"); + + +using csharp_scrabble_challenge.Main; +using System; + +Console.WriteLine("Welcome to Scrabble!"); + + +while (true) +{ + Console.WriteLine("Write a word, or quit with qqq"); + string input = Console.ReadLine(); + Scrabble scrabble = new Scrabble(input); + + if (input == "qqq") break; + Console.WriteLine($"{input} has score {scrabble.score()}"); + +} diff --git a/csharp-scrabble-challenge.Main/Scrabble.cs b/csharp-scrabble-challenge.Main/Scrabble.cs index c1ea013..1976c65 100644 --- a/csharp-scrabble-challenge.Main/Scrabble.cs +++ b/csharp-scrabble-challenge.Main/Scrabble.cs @@ -9,15 +9,133 @@ namespace csharp_scrabble_challenge.Main { public class Scrabble { + + private Dictionary value_of_letter; + private string _word; + private string numbers = "123456789"; + + public Scrabble(string word) { //TODO: do something with the word variable + _word = word.ToUpper(); + } public int score() { //TODO: score calculation code goes here - throw new NotImplementedException(); //TODO: Remove this line when the code has been written + + int score = 0; + + value_of_letter = new Dictionary(); + + value_of_letter.Add('A', 1); + value_of_letter.Add('E', 1); + value_of_letter.Add('I', 1); + value_of_letter.Add('O', 1); + value_of_letter.Add('U', 1); + value_of_letter.Add('L', 1); + value_of_letter.Add('N', 1); + value_of_letter.Add('R', 1); + value_of_letter.Add('S', 1); + value_of_letter.Add('T', 1); + + value_of_letter.Add('D', 2); + value_of_letter.Add('G', 2); + + value_of_letter.Add('B', 3); + value_of_letter.Add('C', 3); + value_of_letter.Add('M', 3); + value_of_letter.Add('P', 3); + + value_of_letter.Add('F', 4); + value_of_letter.Add('H', 4); + value_of_letter.Add('V', 4); + value_of_letter.Add('W', 4); + value_of_letter.Add('Y', 4); + + value_of_letter.Add('K', 5); + + value_of_letter.Add('J', 8); + value_of_letter.Add('X', 8); + + value_of_letter.Add('Q', 10); + value_of_letter.Add('Z', 10); + + bool isDobbel = false; + bool isTrippel = false; + + int multiplier = 1; + int count_start_sBrackets = 0; + int count_end_sBrackets = 0; + int count_start_cBrackets = 0; + int count_end_cBrackets = 0; + + + for (int i = 0; i < _word.Length; i++) + { + char letter = _word[i]; + + // If the word contains a number + if (numbers.Contains(letter)) + { + break; + } + + // Dobbel + if (letter == '{') + { + isDobbel = true; + multiplier = multiplier * 2; + count_start_cBrackets++; + } + if (letter == '}') + { + + isDobbel = false; + multiplier = multiplier / 2; + count_end_cBrackets++; + } + + // Trippel + if (letter == '[') + { + isDobbel = true; + multiplier = multiplier * 3; + count_start_sBrackets++; + } + + if (letter == ']') + { + + isDobbel = false; + multiplier = multiplier / 3; + count_end_sBrackets++; + } + + // Score + if (value_of_letter.TryGetValue(letter, out int value)) + { + score += value * multiplier; + } + else + { + score += 0; + } + } + + // If the start brackets and end brackets are not equal - return 0 + if (count_start_cBrackets != count_end_cBrackets) + { + return 0; + } + if (count_start_sBrackets != count_end_sBrackets) + { + return 0; + } + return score; + } } } diff --git a/csharp-scrabble-challenge.Test/CoreTests.cs b/csharp-scrabble-challenge.Test/CoreTests.cs index f42e402..6dd9dc7 100644 --- a/csharp-scrabble-challenge.Test/CoreTests.cs +++ b/csharp-scrabble-challenge.Test/CoreTests.cs @@ -5,7 +5,12 @@ 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("{{a}", 0)] [TestCase("", 0)] [TestCase(" ", 0)] [TestCase(" \t\n", 0)] @@ -17,7 +22,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();