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
120 changes: 118 additions & 2 deletions csharp-scrabble-challenge.Main/Scrabble.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,139 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Diagnostics.Tracing;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Runtime.ConstrainedExecution;
using System.Text;
using System.Threading.Tasks;

namespace csharp_scrabble_challenge.Main
{
public class Scrabble
{

private Dictionary<char, int> charScore = new Dictionary<char, int>();
private string _word;
private int _mulitplier = 1;

public Scrabble(string word)
{
{
//TODO: do something with the word variable
addAlphabetToDict();
word = word.ToUpper();
_word = word;
}

public int score()
{
//TODO: score calculation code goes here
throw new NotImplementedException(); //TODO: Remove this line when the code has been written
int finalScore = 0;
string subString = _word;
int _wordMultiplier = 1;

char currentOpenBracket = ' ';

if (_word.Length == 0) { return finalScore; }
if (checkLegalWord(_word))
{
if (_word[0] == '{' && _word[_word.Length - 1] == '}')
{
subString = _word.Substring(1, _word.Length - 2);

if (subString[1] == '}')
{
_wordMultiplier = 1;
_mulitplier = 2;
currentOpenBracket = '{';
}
else { _wordMultiplier = 2; }
}
else if (_word[0] == '[' && _word[_word.Length - 1] == ']')
{
subString = _word.Substring(1, _word.Length - 2);
if (subString[1] == ']')
{
_wordMultiplier = 1;
_mulitplier = 3;
currentOpenBracket = '[';
}
else { _wordMultiplier = 3; }
}

foreach (char ch in subString)
{
if (ch == '{')
{
if (currentOpenBracket == ' ') { _mulitplier += 1; currentOpenBracket = '{'; }
else { return 0; }
}
else if (ch == '}')
{
if (currentOpenBracket == '{') { _mulitplier -= 1; currentOpenBracket = ' '; }
else { return 0; }
}
else if (ch == '[')
{
if (currentOpenBracket == ' ') { _mulitplier += 2; currentOpenBracket = '['; }
else { return 0; }
}
else if (ch == ']')
{
if (currentOpenBracket == ']') { _mulitplier -= 2; currentOpenBracket = ' '; }
else { return 0; }

}
finalScore += charScore[ch] * _mulitplier;
}
}

return finalScore * _wordMultiplier;

}
private bool checkLegalWord(string word)
{
foreach (char ch in word)
{
if (!charScore.ContainsKey(ch))
{
return false;
}
}
return true;
}
private void addAlphabetToDict()
{
charScore.Add('A', 1);
charScore.Add('E', 1);
charScore.Add('I', 1);
charScore.Add('O', 1);
charScore.Add('U', 1);
charScore.Add('L', 1);
charScore.Add('N', 1);
charScore.Add('R', 1);
charScore.Add('S', 1);
charScore.Add('T', 1);
charScore.Add('D', 2);
charScore.Add('G', 2);
charScore.Add('B', 3);
charScore.Add('C', 3);
charScore.Add('M', 3);
charScore.Add('P', 3);
charScore.Add('F', 4);
charScore.Add('H', 4);
charScore.Add('V', 4);
charScore.Add('W', 4);
charScore.Add('Y', 4);
charScore.Add('K', 5);
charScore.Add('J', 8);
charScore.Add('X', 8);
charScore.Add('Q', 10);
charScore.Add('Z', 10);
charScore.Add('{', 0);
charScore.Add('}', 0);
charScore.Add('[', 0);
charScore.Add(']', 0);
}
}
}
18 changes: 18 additions & 0 deletions csharp-scrabble-challenge.Test/CoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,24 @@ public class CoreTests
[TestCase("OXyPHEnBUTaZoNE", 41)]
[TestCase("quirky", 22)]
[TestCase("street", 6)]


[TestCase("[{h}o1s{e}]", 0)] // error case (zero for errors)
[TestCase("{h}ous{e}", 13)]
[TestCase("{h}{{ous{e}", 0)]
[TestCase("{h}}ous{e}", 0)]
[TestCase("{h}{o]us{e}", 0)]
[TestCase("[{h}ous{e}]", 39)]
[TestCase("[h}ous{e}]", 0)] //Error case (zero for errors)
[TestCase("", 0)]
[TestCase(" ", 0)]
[TestCase(" \t\n", 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);
Expand Down
Loading