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
7 changes: 6 additions & 1 deletion csharp-scrabble-challenge.Main/Program.cs
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
//Console.WriteLine("Hello, World!");

using csharp_scrabble_challenge.Main;

Scrabble scrabble = new Scrabble("Test");
scrabble.score();
132 changes: 130 additions & 2 deletions csharp-scrabble-challenge.Main/Scrabble.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.Tracing;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Text;
Expand All @@ -9,15 +10,142 @@ namespace csharp_scrabble_challenge.Main
{
public class Scrabble
{
private string _word;
public Scrabble(string word)
{
{
//TODO: do something with the word variable
_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
//throw new NotImplementedException(); //TODO: Remove this line when the code has been written

ScrabbleDictionary scrabbleDictionary = new ScrabbleDictionary();

int sum = 0;
int tempValue = 0;
bool doubleLetter = false;
bool tripleLetter = false;
bool possibledoubleWord = false;
bool possibletripleWord = false;
int countIndex = 0;
int indexStartD = -1;
int indexStartT = -1;

foreach (var letter in _word)
{
Console.WriteLine($"We are on letter: {letter}");

if (letter == '{')
{
if (countIndex == 0)
{
possibledoubleWord = true;
countIndex++;
continue;
}

doubleLetter = true;
indexStartD = countIndex;
countIndex++;
continue;
}

if (letter == '[')
{
if (countIndex == 0)
{
possibletripleWord = true;
countIndex++;
continue;
}

tripleLetter = true;
indexStartT = countIndex;
countIndex++;
continue;
}

if (letter == '}')
{
doubleLetter = false;

if (indexStartD == countIndex - 2)
{
sum += tempValue;
}
else if (possibledoubleWord == true)
{
if (countIndex == 2)
{
possibledoubleWord = false;
sum *= 2;
}
}
else
{
return 0;
}

indexStartD = 0;
countIndex++;
continue;
}

if (letter == ']')
{
tripleLetter = false;

if (indexStartT == countIndex - 2)
{
sum += tempValue * 2;
}
else if (possibletripleWord == true)
{
if (countIndex == 2)
{
possibletripleWord = false;
sum *= 3;
}
}
else
{
return 0;
}

indexStartT = 0;
countIndex++;
continue;
}

int value = scrabbleDictionary.LetterValue(letter);
if (value == 0) return 0;
sum += value;

if ((doubleLetter || tripleLetter) == true)
{
tempValue = value;
}

countIndex++;
}

if (possibledoubleWord && (_word[_word.Length-1] == '}'))
{
sum *= 2;
}

if (possibletripleWord && (_word[_word.Length - 1] == ']'))
{
sum *= 3;
}

Console.WriteLine($"Sum: {sum}");

return sum;

}
}
}
62 changes: 62 additions & 0 deletions csharp-scrabble-challenge.Main/ScrabbleDictionary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace csharp_scrabble_challenge.Main
{
public class ScrabbleDictionary
{
Dictionary<char, int> scrabbledictionary;
public ScrabbleDictionary()
{
scrabbledictionary = new Dictionary<char, int>();

scrabbledictionary.Add('A', 1);
scrabbledictionary.Add('B', 3);
scrabbledictionary.Add('C', 3);
scrabbledictionary.Add('D', 2);
scrabbledictionary.Add('E', 1);
scrabbledictionary.Add('F', 4);
scrabbledictionary.Add('G', 2);
scrabbledictionary.Add('H', 4);
scrabbledictionary.Add('I', 1);
scrabbledictionary.Add('J', 8);
scrabbledictionary.Add('K', 5);
scrabbledictionary.Add('L', 1);
scrabbledictionary.Add('M', 3);
scrabbledictionary.Add('N', 1);
scrabbledictionary.Add('O', 1);
scrabbledictionary.Add('P', 3);
scrabbledictionary.Add('Q', 10);
scrabbledictionary.Add('R', 1);
scrabbledictionary.Add('S', 1);
scrabbledictionary.Add('T', 1);
scrabbledictionary.Add('U', 1);
scrabbledictionary.Add('V', 4);
scrabbledictionary.Add('W', 4);
scrabbledictionary.Add('X', 8);
scrabbledictionary.Add('Y', 4);
scrabbledictionary.Add('Z', 10);

/*foreach (KeyValuePair<char, int> kvp in scrabbledictionary)
{
Console.WriteLine($"Key: {kvp.Key} Value: {kvp.Value}");
}*/

}

public int LetterValue(char letter)
{
if (letter >= 65 && letter <= 90)
{
int value = 0;
value = scrabbledictionary[letter];
return value;
}

return 0;
}
}
}
26 changes: 24 additions & 2 deletions csharp-scrabble-challenge.Test/CoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ namespace csharp_scrabble_challenge.Test
{
[TestFixture]
public class CoreTests
{
{
[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 @@ -16,10 +20,28 @@ public class CoreTests
[TestCase("quirky", 22)]
[TestCase("street", 6)]
public void WordScoreTests(string word, int targetScore)
{
Scrabble scrabble = new Scrabble(word);

Assert.That(scrabble.score(), Is.EqualTo(targetScore));
}

// Old tests, which all passes.

/*[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);
}

private int GetWordScore(string word) => new Scrabble(word).score();
private int GetWordScore(string word) => new Scrabble(word).score();*/
}
}
Loading