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
89 changes: 87 additions & 2 deletions csharp-scrabble-challenge.Main/Scrabble.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,100 @@ namespace csharp_scrabble_challenge.Main
{
public class Scrabble
{
String word;
public Scrabble(string word)
{
{
//TODO: do something with the word variable
this.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

//set score to be zero
int score = 0;
//initialise a dictionary with all characters and ther points
Dictionary <char, int> map = 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},{ 'X', 8},{ 'Y', 4},
{ 'Z', 10},};



for(int i = 0; i < word.Length; i++)
{

if (map.ContainsKey(word[i]))
{
score += map[word[i]];
}
else if (!(map.ContainsKey(word[i])))
{
if (word[i] == '{' || word[i] == '[')
{
int outerMulti = word[i] == '{' ? 2 : 3;
//skrive expected end bracket
char expectedOuterBracket= word[i] == '{' ? '}' : ']';
i++;


while (word[i] != '}' && word[i] != ']')
{

if (word[i] == '{' || word[i] == '[')
{
int innerMulti = word[i] == '{' ? 2 : 3;
char expectedInnerBracket = word[i] == '{' ? '}' : ']';
i++;
while (word[i] != '}' && word[i] != ']')
{
if (!(map.ContainsKey(word[i]))){
return 0;
}
int mdl = map[word[i]];
//hva skal multiplieren være??
score += mdl * innerMulti * outerMulti;
i++;

}
//her kan man sjekke bracket
if (expectedInnerBracket != word[i])
{
return 0;
}
i++;

}
else {
if (!(map.ContainsKey(word[i])))
{
return 0;
}
int newscore = map[word[i]];
score += newscore * outerMulti;
i++;
}

}
//her kan man sjekke bracket
if (expectedOuterBracket != word[i])
{
return 0;
}

}

}


}
return score;


}

}
}
15 changes: 15 additions & 0 deletions csharp-scrabble-challenge.Test/ExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,28 @@ namespace csharp_scrabble_challenge.Test
[TestFixture]
public class ExtensionTests
{
[TestCase("d{o}g", 6)] //extension double letter
[TestCase("d[o]g", 7)] //extension triple letter
[TestCase("[d[1]g]", 0)]
[TestCase("[[zz]z]", 210)]

[TestCase("j[o]h{n}", 17)] // "j[o]h{n}": o blir trippel, n blir dobbel
[TestCase("k[e]y", 12)] // "k[e]y": e blir trippel
[TestCase("{x}yz", 30)] // "{x}yz": x blir dobbel
[TestCase("k[e][y]", 20)] // "k[e]y": e blir trippel
[TestCase("{x}[y]z", 38)] // "{x}yz": x blir dobbel

[TestCase("{street}", 12)] //extension double word
[TestCase("[street]", 18)] //extension triple word
[TestCase("{quirky}", 44)] //extension double word
[TestCase("[quirky]", 66)] //extension triple word
[TestCase("{OXyPHEnBUTaZoNE}", 82)]
[TestCase("[OXyPHEnBUTaZoNE]", 123)]

[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)
public void ExtendedCriteriaTests(string word, int targetScore)
{
Assert.AreEqual(this.GetWordScore(word), targetScore);
Expand Down