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


using csharp_scrabble_challenge.Main;
using System;

Console.WriteLine("Welcome to Scrabble!");


while (true)
{
Console.WriteLine("Write a word, or quit with qqq");
string input = Console.ReadLine();

Check warning on line 12 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 12 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 12 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 12 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.
Scrabble scrabble = new Scrabble(input);

Check warning on line 13 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 13 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 13 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 13 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)'.

if (input == "qqq") break;
Console.WriteLine($"{input} has score {scrabble.score()}");

}
120 changes: 119 additions & 1 deletion csharp-scrabble-challenge.Main/Scrabble.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,133 @@
{
public class Scrabble
{

private Dictionary<char, int> value_of_letter;
private string _word;
private string numbers = "123456789";


public Scrabble(string word)

Check warning on line 18 in csharp-scrabble-challenge.Main/Scrabble.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'value_of_letter' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 18 in csharp-scrabble-challenge.Main/Scrabble.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'value_of_letter' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 18 in csharp-scrabble-challenge.Main/Scrabble.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'value_of_letter' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 18 in csharp-scrabble-challenge.Main/Scrabble.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'value_of_letter' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.
{
//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

int score = 0;

value_of_letter = new Dictionary<char, int>();

value_of_letter.Add('A', 1);
value_of_letter.Add('E', 1);
value_of_letter.Add('I', 1);
value_of_letter.Add('O', 1);
value_of_letter.Add('U', 1);
value_of_letter.Add('L', 1);
value_of_letter.Add('N', 1);
value_of_letter.Add('R', 1);
value_of_letter.Add('S', 1);
value_of_letter.Add('T', 1);

value_of_letter.Add('D', 2);
value_of_letter.Add('G', 2);

value_of_letter.Add('B', 3);
value_of_letter.Add('C', 3);
value_of_letter.Add('M', 3);
value_of_letter.Add('P', 3);

value_of_letter.Add('F', 4);
value_of_letter.Add('H', 4);
value_of_letter.Add('V', 4);
value_of_letter.Add('W', 4);
value_of_letter.Add('Y', 4);

value_of_letter.Add('K', 5);

value_of_letter.Add('J', 8);
value_of_letter.Add('X', 8);

value_of_letter.Add('Q', 10);
value_of_letter.Add('Z', 10);

bool isDobbel = false;

Check warning on line 66 in csharp-scrabble-challenge.Main/Scrabble.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'isDobbel' is assigned but its value is never used

Check warning on line 66 in csharp-scrabble-challenge.Main/Scrabble.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'isDobbel' is assigned but its value is never used

Check warning on line 66 in csharp-scrabble-challenge.Main/Scrabble.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'isDobbel' is assigned but its value is never used

Check warning on line 66 in csharp-scrabble-challenge.Main/Scrabble.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'isDobbel' is assigned but its value is never used
bool isTrippel = false;

Check warning on line 67 in csharp-scrabble-challenge.Main/Scrabble.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'isTrippel' is assigned but its value is never used

Check warning on line 67 in csharp-scrabble-challenge.Main/Scrabble.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'isTrippel' is assigned but its value is never used

Check warning on line 67 in csharp-scrabble-challenge.Main/Scrabble.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'isTrippel' is assigned but its value is never used

Check warning on line 67 in csharp-scrabble-challenge.Main/Scrabble.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'isTrippel' is assigned but its value is never used

int multiplier = 1;
int count_start_sBrackets = 0;
int count_end_sBrackets = 0;
int count_start_cBrackets = 0;
int count_end_cBrackets = 0;


for (int i = 0; i < _word.Length; i++)
{
char letter = _word[i];

// If the word contains a number
if (numbers.Contains(letter))
{
break;
}

// Dobbel
if (letter == '{')
{
isDobbel = true;
multiplier = multiplier * 2;
count_start_cBrackets++;
}
if (letter == '}')
{

isDobbel = false;
multiplier = multiplier / 2;
count_end_cBrackets++;
}

// Trippel
if (letter == '[')
{
isDobbel = true;
multiplier = multiplier * 3;
count_start_sBrackets++;
}

if (letter == ']')
{

isDobbel = false;
multiplier = multiplier / 3;
count_end_sBrackets++;
}

// Score
if (value_of_letter.TryGetValue(letter, out int value))
{
score += value * multiplier;
}
else
{
score += 0;
}
}

// If the start brackets and end brackets are not equal - return 0
if (count_start_cBrackets != count_end_cBrackets)
{
return 0;
}
if (count_start_sBrackets != count_end_sBrackets)
{
return 0;
}
return score;

}
}
}
11 changes: 9 additions & 2 deletions csharp-scrabble-challenge.Test/CoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ 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("{{a}", 0)]
[TestCase("", 0)]
[TestCase(" ", 0)]
[TestCase(" \t\n", 0)]
Expand All @@ -17,7 +22,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
Loading