diff --git a/.gitignore b/.gitignore index a6576fc..8b05f5a 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,9 @@ ## ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore +# Jetbrains IDEs local files +.idea/ + # User-specific files *.rsuser *.suo diff --git a/csharp-scrabble-challenge.Main/CharacterMultiplier.cs b/csharp-scrabble-challenge.Main/CharacterMultiplier.cs new file mode 100644 index 0000000..2abec02 --- /dev/null +++ b/csharp-scrabble-challenge.Main/CharacterMultiplier.cs @@ -0,0 +1,8 @@ +namespace csharp_scrabble_challenge.Main; + +public struct CharacterMultiplier +{ + public char OpenIndicator; + public char CloseIndicator; + public int Multiplier; +} \ No newline at end of file diff --git a/csharp-scrabble-challenge.Main/Program.cs b/csharp-scrabble-challenge.Main/Program.cs index 3751555..736c0a0 100644 --- a/csharp-scrabble-challenge.Main/Program.cs +++ b/csharp-scrabble-challenge.Main/Program.cs @@ -1,2 +1,20 @@ -// See https://aka.ms/new-console-template for more information -Console.WriteLine("Hello, World!"); +namespace csharp_scrabble_challenge.Main; + +internal static class Program +{ + public static void Main(string[] args) + { + bool exit = false; + while (!exit) + { + Console.WriteLine("Please type a word:"); + string? word = Console.ReadLine(); + + if (word != null) + { + if (word == "exit") exit = true; + else Console.WriteLine($"The score is: {new Scrabble(word).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..0da39ad 100644 --- a/csharp-scrabble-challenge.Main/Scrabble.cs +++ b/csharp-scrabble-challenge.Main/Scrabble.cs @@ -1,23 +1,67 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection.Metadata.Ecma335; -using System.Text; -using System.Threading.Tasks; - -namespace csharp_scrabble_challenge.Main +namespace csharp_scrabble_challenge.Main; + +public class Scrabble { - public class Scrabble + private static readonly Dictionary _characterScores = new() { - public Scrabble(string word) - { - //TODO: do something with the word variable - } + { '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 int score() + private static readonly CharacterMultiplier[] _multipliers = + [ + new() { CloseIndicator = '}', OpenIndicator = '{', Multiplier = 2, }, + new() { CloseIndicator = ']', OpenIndicator = '[', Multiplier = 3, }, + ]; + + private readonly string _inputWord; + private const int DEFAULT_MULTIPLIER = 1; + + public Scrabble(string word) + { + _inputWord = word; + } + + public int score() + { + int score = 0; + string lowerWord = _inputWord.ToLower(); + char[] chars = lowerWord.ToCharArray(); + + int currentMultiplier = DEFAULT_MULTIPLIER; + + foreach (char c in chars) { - //TODO: score calculation code goes here - throw new NotImplementedException(); //TODO: Remove this line when the code has been written + foreach (var characterMultiplier in _multipliers) + { + if (c == characterMultiplier.OpenIndicator) + { + currentMultiplier = characterMultiplier.Multiplier; + } + else if (c == characterMultiplier.CloseIndicator) + { + currentMultiplier = DEFAULT_MULTIPLIER; + } + } + + if (_characterScores.TryGetValue(c, out int cScore)) + { + score += cScore * currentMultiplier; + } } + + return score; } -} +} \ No newline at end of file