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
50 changes: 50 additions & 0 deletions csharp-scrabble-challenge.Main/Points.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace csharp_scrabble_challenge.Main
{
internal class Points
{

public Dictionary<string, int> PointsDict = new Dictionary<string, int>();


public Points() {
PointsDict.Add("A", 1);
PointsDict.Add("E", 1);
PointsDict.Add("I", 1);
PointsDict.Add("L", 1);
PointsDict.Add("N", 1);
PointsDict.Add("O", 1);
PointsDict.Add("R", 1);
PointsDict.Add("S", 1);
PointsDict.Add("T", 1);
PointsDict.Add("U", 1);

PointsDict.Add("D", 2);
PointsDict.Add("G", 2);

PointsDict.Add("B", 3);
PointsDict.Add("C", 3);
PointsDict.Add("M", 3);
PointsDict.Add("P", 3);

PointsDict.Add("F", 4);
PointsDict.Add("H", 4);
PointsDict.Add("V", 4);
PointsDict.Add("W", 4);
PointsDict.Add("Y", 4);

PointsDict.Add("K", 5);

PointsDict.Add("J", 8);
PointsDict.Add("X", 8);

PointsDict.Add("Q", 10);
PointsDict.Add("Z", 10);
}
}
}
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;
using System.Diagnostics.Tracing;

while (true)
{
Console.WriteLine("Enter a word");

string word = Console.ReadLine();

Check warning on line 8 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 8 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 8 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 8 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(word);

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

int score = scrabble.score();

Console.WriteLine($"The score for {word} is {score}.");
}
147 changes: 143 additions & 4 deletions csharp-scrabble-challenge.Main/Scrabble.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,161 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace csharp_scrabble_challenge.Main
{
public class Scrabble
{
string _word;

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

public bool containsWhiteSpace(string word)
{
if (word.Contains("\n")) { return true; }
if (word.Contains("\r")) { return true; }
if (word.Contains("\t")) { return true; }
if (word.Contains("\b")) { return true; }
if (word.Contains("\f")) { return true; }
return false;
}

public bool containsDigit(string word)
{
return word.Any(Char.IsDigit);
}

public bool isInvalidBracket(string word)
{
int numOpenSquare = 0;
int numCloseSquare = 0;
int numOpenCurly = 0;
int numCloseCurly = 0;

foreach (char c in word)
{
if (c == '[')
{
numOpenSquare++;
}
if (c == ']')
{
numCloseSquare++;
if (numCloseSquare > numOpenSquare)
{
return true;
}
}
if (c == '{') {
numOpenCurly++;
}
if (c == '}') {
numCloseCurly++;
if (numCloseCurly > numOpenCurly)
{
return true;
}
}
}
if (numOpenCurly != numCloseCurly)
{
return false;
}
if (numOpenSquare != numCloseSquare)
{
return false;
}
return false;
}

public int calcWordMultiplier(string upperCaseWord)
{
if (upperCaseWord[0] == '{' && upperCaseWord.Last() == '}')
{
foreach (char c in upperCaseWord)
{

}
return 2;
}
if (upperCaseWord[0] == '[' && upperCaseWord.Last() == ']')
{
return 3;
}
return 1;
}

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; }
// Method above did not catch all cases
if (containsWhiteSpace(_word)) { return 0; }
if (containsDigit(_word)) { return 0; }
if (isInvalidBracket(_word)) { return 0; }

Points points = new Points();

int score = 0;

// Match upper case letters in point dictionary
string upperCaseWord = _word.ToUpper();


int inSquareBracket = 0;
int inCurlyBracket = 0;

for (int i = 0; i<upperCaseWord.Length; i++)
{

if (upperCaseWord[i] == '[')
{
inSquareBracket = 1;
continue;
}
if (upperCaseWord[i] == '{')
{
inCurlyBracket = 1;
continue;
}
if (upperCaseWord[i] == ']')
{
inSquareBracket = 0;
continue;
}
if (upperCaseWord[i] == '}')
{
inCurlyBracket = 0;
continue;
}

char currentLetter = upperCaseWord[i];

int multiplier = 1;
if (inCurlyBracket == 1 && inSquareBracket == 1)
{
multiplier = 6;
}
else if (inCurlyBracket == 1)
{
multiplier = 2;
}
else if (inSquareBracket == 1)
{
multiplier = 3;
}

score += points.PointsDict[currentLetter.ToString()] * multiplier;

}

return score;
}
}
}
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
Loading