Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 89 additions & 1 deletion csharp-scrabble-challenge.Main/Scrabble.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,103 @@ namespace csharp_scrabble_challenge.Main
{
public class Scrabble
{
public string _word;
public Dictionary<char, int> _scores = new Dictionary<char, int> {
{'a', 1 },
{'b', 3 },
{'c', 3 },
{'d', 2 },
{'e', 1 },
{'f', 4 },
{'g', 2 },
{'h', 4 },
{'i', 1 },
{'j', 8 },
{'k', 5 },
{'l', 1 },
{'m', 3 },
{'n', 1 },
{'o', 1 },
{'p', 3 },
{'q', 10 },
{'r', 1 },
{'s', 1 },
{'t', 1 },
{'u', 1 },
{'v', 4 },
{'w', 4 },
{'x', 8 },
{'y', 4 },
{'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 == "" || _word == " ")
{
return 0;
}
else if (_word.Any(char.IsDigit))
{
return 0;
}
//throw new NotImplementedException(); //TODO: Remove this line when the code has been written
//else if (_word.Length == 1)
//{
// return _scores[char.Parse(_word)];
//}

int sum = 0;
int multiplier = 1;
int curly = 0;
int square = 0;
char[] characters = _word.ToCharArray();
foreach (char item in characters)
{
if (Char.IsWhiteSpace(item) || curly < 0 || square < 0)
{
return 0;
}
else if (item == '{')
{
multiplier *= 2;
curly += 1;
}
else if (item == '[')
{
multiplier *= 3;
square += 1;
}
else if (item == '}' && curly >= 0)
{
multiplier /= 2;
curly -= 1;
}
else if (item == ']' && square >= 0)
{
multiplier /= 3;
square -= 1;
}
else
{
sum += _scores[item] * multiplier;
}
}

return sum;

//else
//{
// return 0;
//}
}
}
}
25 changes: 23 additions & 2 deletions csharp-scrabble-challenge.Test/CoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,25 @@ namespace csharp_scrabble_challenge.Test
{
[TestFixture]
public class CoreTests
{
{
//[TestCase("", 0)]
//[TestCase(" ", 0)]
//[TestCase(" \t", 0)]
//[TestCase("\n\r\t\b\f", 0)]
//[TestCase("a", 1)]
//[TestCase("f", 4)]
//[TestCase("OXyPHEnBUTaZoNE", 41)]
//[TestCase("quirky", 22)]
//[TestCase("street", 6)]
//public void WordScoreTests(string word, int targetScore)
//{
// Assert.AreEqual(this.GetWordScore(word), targetScore);
//}

[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)]
Expand All @@ -15,9 +33,12 @@ public class CoreTests
[TestCase("OXyPHEnBUTaZoNE", 41)]
[TestCase("quirky", 22)]
[TestCase("street", 6)]
[TestCase("]",0)]
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();
Expand Down
1 change: 1 addition & 0 deletions csharp-scrabble-challenge.Test/ExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class ExtensionTests
[TestCase("[quirky]", 66)] //extension triple word
[TestCase("{OXyPHEnBUTaZoNE}", 82)]
[TestCase("[OXyPHEnBUTaZoNE]", 123)]
[TestCase("A[Q]A", 32)]
public void ExtendedCriteriaTests(string word, int targetScore)
{
Assert.AreEqual(this.GetWordScore(word), targetScore);
Expand Down
Loading