diff --git a/.gitignore b/.gitignore index a6576fc..de3d636 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,8 @@ ## ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore +.DS_Store + # User-specific files *.rsuser *.suo diff --git a/csharp-scrabble-challenge.Main/Scrabble.cs b/csharp-scrabble-challenge.Main/Scrabble.cs index c1ea013..66e3819 100644 --- a/csharp-scrabble-challenge.Main/Scrabble.cs +++ b/csharp-scrabble-challenge.Main/Scrabble.cs @@ -1,7 +1,9 @@ using System; using System.Collections.Generic; +using System.IO.Pipelines; using System.Linq; using System.Reflection.Metadata.Ecma335; +using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; @@ -9,15 +11,85 @@ namespace csharp_scrabble_challenge.Main { public class Scrabble { + private string _word; + private Dictionary _scores = new Dictionary() + { + {'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 Scrabble(string word) - { - //TODO: do something with the word variable + { + _word = word.ToLower(); } public int score() { - //TODO: score calculation code goes here - throw new NotImplementedException(); //TODO: Remove this line when the code has been written + if (_word.Count(f => f == '[') != _word.Count(f => f == '[')) return 0; + if (_word.Count(f => f == '{') != _word.Count(f => f == '}')) return 0; + + int result = 0; + bool isDouble = false; + bool isTripple = false; + + foreach (char c in _word) + { + if (Char.IsLetter(c)) + { + int points = _scores[c]; + + if (isDouble) points *= 2; + if (isTripple) points *= 3; + + result += points; + } + else if (c == '[') + { + isTripple = true; + } + else if (c == '{') + { + isDouble = true; + } + else if (c == ']') + { + isTripple = false; + } + else if (c == '}') + { + isDouble = false; + } + else + { + return 0; + } + } + + return result; } } }