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

while (true)
{
Console.Write("Write a word (or 'qqq' to quit): ");
string input = Console.ReadLine();

Check warning on line 6 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 6 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 6 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 6 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 (input == "qqq")
{
break;
}

Scrabble s = 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)'.
Console.WriteLine($"Score: {s.score()}\n");
}
102 changes: 98 additions & 4 deletions csharp-scrabble-challenge.Main/Scrabble.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,113 @@
using System.Text;
using System.Threading.Tasks;


namespace csharp_scrabble_challenge.Main
{
public class Scrabble
{
private string word;

private static Dictionary<char, int> letterScores = new()
{
['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
{
this.word = word;
}

public int score()
{
//TODO: score calculation code goes here
throw new NotImplementedException(); //TODO: Remove this line when the code has been written
if (string.IsNullOrWhiteSpace(word))
return 0;

string input = word.Trim();
int wordMultiplier = 1;

// Sjekk for ytre { } rundt hele ordet (dobbel word)
if (input.Length >= 2 && input[0] == '{' && input[^1] == '}')
{
string inner = input.Substring(1, input.Length - 2);

// Hvis det ikke finnes andre { eller } inne i teksten, gjelder dobbel word
if (!inner.Contains('{') && !inner.Contains('}'))
{
wordMultiplier = 2;
input = inner;
}
}
// Sjekk for ytre [ ] rundt hele ordet (trippel word)
else if (input.Length >= 2 && input[0] == '[' && input[^1] == ']')
{
string inner = input.Substring(1, input.Length - 2);

// Hvis det ikke finnes andre [ eller ] inne i teksten, gjelder trippel word
if (!inner.Contains('[') && !inner.Contains(']'))
{
wordMultiplier = 3;
input = inner;
}
}

int total = 0;
int i = 0;

// Gå gjennom hver bokstav i ordet
while (i < input.Length)
{
char c = input[i];

// Hvis vi møter { eller [, prøver vi å finne riktig lukking (} eller ])
if (c == '{' || c == '[')
{
char expectedClose = c == '{' ? '}' : ']';
int multiplier = c == '{' ? 2 : 3;

// Finn posisjonen til tilhørende lukketegn
int closeIndex = input.IndexOf(expectedClose, i + 1);
if (closeIndex == -1)
return 0; // mangler lukketegn → ugyldig

// Innholdet mellom åpen og lukket tegn
string inside = input.Substring(i + 1, closeIndex - i - 1);

// Kun én gyldig bokstav tillatt inne i boost-par
if (inside.Length != 1 || !letterScores.ContainsKey(char.ToUpper(inside[0])))
return 0;

char letter = char.ToUpper(inside[0]);
total += letterScores[letter] * multiplier;

i = closeIndex + 1; // hopp videre
}
// Hvis vi finner en lukketegn uten åpen → ugyldig
else if (c == '}' || c == ']')
{
return 0;
}
// Vanlig bokstav
else
{
char letter = char.ToUpper(c);
if (!letterScores.ContainsKey(letter))
return 0;

total += letterScores[letter];
i++;
}
}

return total * wordMultiplier;
}


}
}
6 changes: 5 additions & 1 deletion 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 Down
1 change: 1 addition & 0 deletions csharp-scrabble-challenge.Test/ExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class ExtensionTests
[TestCase("[quirky]", 66)] //extension triple word
[TestCase("{OXyPHEnBUTaZoNE}", 82)]
[TestCase("[OXyPHEnBUTaZoNE]", 123)]

public void ExtendedCriteriaTests(string word, int targetScore)
{
Assert.AreEqual(this.GetWordScore(word), targetScore);
Expand Down
Loading