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

using csharp_scrabble_challenge.Main;

Console.WriteLine("Welcome to Scrabble");
Console.WriteLine("Enter a word and get the score! Add a {} or [] around a letter or multiple letters to multiply score by 2 or 3 respectively");
Console.WriteLine("To exit press q and Enter.");
while (true)
{
string word = Console.ReadLine();

Check warning on line 10 in csharp-scrabble-challenge.Main/Program.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.

Check warning on line 10 in csharp-scrabble-challenge.Main/Program.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.

Check warning on line 10 in csharp-scrabble-challenge.Main/Program.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.

Check warning on line 10 in csharp-scrabble-challenge.Main/Program.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.
if (word == "q") break;
Scrabble scrabble = new Scrabble(word);

Check warning on line 12 in csharp-scrabble-challenge.Main/Program.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'word' in 'Scrabble.Scrabble(string word)'.

Check warning on line 12 in csharp-scrabble-challenge.Main/Program.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'word' in 'Scrabble.Scrabble(string word)'.

Check warning on line 12 in csharp-scrabble-challenge.Main/Program.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'word' in 'Scrabble.Scrabble(string word)'.

Check warning on line 12 in csharp-scrabble-challenge.Main/Program.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'word' in 'Scrabble.Scrabble(string word)'.
Console.WriteLine($"{scrabble.score()}");
}

Console.WriteLine("Exiting... Thanks for playing!");
76 changes: 72 additions & 4 deletions csharp-scrabble-challenge.Main/Scrabble.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,83 @@ namespace csharp_scrabble_challenge.Main
{
public class Scrabble
{
public string _word;
private Dictionary<char, int> _dictionary = new Dictionary<char, int>
{
{ '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;
}

public int score()
{
//TODO: score calculation code goes here
throw new NotImplementedException(); //TODO: Remove this line when the code has been written
int sum = 0;
int openCurly = 0;
int openHard = 0;
int BonusMultiplier = 1; // 1 -> no bonus, 2 -> double, 3 -> triple
foreach (char item in _word)
{
if (char.IsWhiteSpace(item)) return 0;
if (char.IsNumber(item)) return 0;
if (item == '{')
{
openCurly++;
BonusMultiplier = BonusMultiplier * 2;
}
else if (item == '[')
{
openHard++;
BonusMultiplier = BonusMultiplier * 3;
}
else if (item == '}')
{
if (openCurly == 0) return 0;
openCurly--;
BonusMultiplier = BonusMultiplier / 2;
}
else if (item == ']')
{
if (openHard == 0) return 0;
openHard--;
BonusMultiplier = BonusMultiplier / 3;
}
else
{
char letter = char.Parse(item.ToString().ToUpper());
sum = sum + _dictionary[letter] * BonusMultiplier;
}


}
if (openCurly > 0 || openHard > 0) return 0;
return sum;
}
}
}
10 changes: 8 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 @@ -17,7 +21,9 @@ public class CoreTests
[TestCase("street", 6)]
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
2 changes: 2 additions & 0 deletions csharp-scrabble-challenge.Test/ExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class ExtensionTests
[TestCase("[quirky]", 66)] //extension triple word
[TestCase("{OXyPHEnBUTaZoNE}", 82)]
[TestCase("[OXyPHEnBUTaZoNE]", 123)]
[TestCase("A[Q]A", 32)]
[TestCase("{{A}}", 4)]
public void ExtendedCriteriaTests(string word, int targetScore)
{
Assert.AreEqual(this.GetWordScore(word), targetScore);
Expand Down
Loading