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


while (true)
{
Console.WriteLine("Write a Scrabble-word to get it's score(q to quit):");
string input = Console.ReadLine();

Check warning on line 9 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 9 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 9 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 9 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 == "q")
{
break;
}

Scrabble scrabble = new Scrabble(input);

Check warning on line 15 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 15 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 15 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 15 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());
}
93 changes: 91 additions & 2 deletions csharp-scrabble-challenge.Main/Scrabble.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,104 @@ namespace csharp_scrabble_challenge.Main
{
public class Scrabble
{
private Dictionary<char, int> _scores = 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},
{'w', 4},
{'x', 8},
{'y', 4},
{'z', 10},
{'{', 0},
{'}', 0},
{'[', 0},
{']', 0}
};
private string _word = "";
private bool OnlyHasValidChars(string str)
{
string lowerStr = str.ToLower();
foreach (char c in lowerStr)
{
if (!_scores.ContainsKey(c))
{
return false;
}
}
return true;
}

public Scrabble(string word)
{
{
_word = word;
//TODO: do something with the word variable
}

public int score()
{
int _score = 0;
int _multiplier = 1;
int _balanced_curly = 0;
int _balanced_square = 0;
char _opening_bracket = ' ';


if (!OnlyHasValidChars(_word.ToLower())) { return 0; }
foreach(char c in _word.ToLower())
{
if (c == '{')
{
_balanced_curly++;
_multiplier *= 2;
_opening_bracket = '{';
}
else if (c == '[')
{
_balanced_square++;
_multiplier *= 3;
_opening_bracket = '[';
}
else if(c == ']')
{
if (_multiplier%3 != 0 || _opening_bracket == '{') { return 0;}
_balanced_square--;
_multiplier /= 3;
_opening_bracket = ' ';

}
else if (c == '}')
{
if (_multiplier % 2 != 0 || _opening_bracket == '[') { return 0; }
_balanced_curly--;
_multiplier /= 2;
_opening_bracket = ' ';

}
_score += _scores[c] * _multiplier;
}
if (_balanced_square != 0 || _balanced_curly != 0 || _multiplier != 1) { return 0; }
return _score;
//TODO: score calculation code goes here
throw new NotImplementedException(); //TODO: Remove this line when the code has been written
}
}
}
13 changes: 13 additions & 0 deletions csharp-scrabble-challenge.Test/CoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ namespace csharp_scrabble_challenge.Test
[TestFixture]
public class CoreTests
{
[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)]
[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
Loading