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
8 changes: 8 additions & 0 deletions csharp-scrabble-challenge.Main/Program.cs
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
// See https://aka.ms/new-console-template for more information
using csharp_scrabble_challenge.Main;
using System.IO;

Console.WriteLine("Hello, World!");

Scrabble s = new Scrabble("[h}ous{e}]");
var score = s.score();

Console.WriteLine($"score = {score}");
112 changes: 108 additions & 4 deletions csharp-scrabble-challenge.Main/Scrabble.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,126 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace csharp_scrabble_challenge.Main
{
public class Scrabble
{
private Dictionary<char, int> letterToScoreMap = new()
{
{'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}
};

private string processedWord;

// [] triple points
// {} double points

private const char startDoublePoints = '{';
private const char endDoublePoints = '}';

private const char startTriplePoints = '[';
private const char endTriplePoints = ']';

bool isDoublePointsActive = false;
bool isTriplePointsActive = false;

int doublePointsMultiplier = 2;
int triplePointsMultiplier = 3;

bool endedBonusBeforeStarting = false;

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

public int score()
{
//TODO: score calculation code goes here
throw new NotImplementedException(); //TODO: Remove this line when the code has been written
int totalScore = 0;

foreach (var letter in processedWord)
{

switch (letter)
{
case startDoublePoints:
isDoublePointsActive = true;
break;

case endDoublePoints:
if(!isDoublePointsActive)
endedBonusBeforeStarting = true;

isDoublePointsActive = false;
break;

case startTriplePoints:
isTriplePointsActive = true;
break;

case endTriplePoints:
if (isTriplePointsActive == false)
endedBonusBeforeStarting = true;

isTriplePointsActive = false;
break;

default:
bool isValid = char.IsLetter(letter);
if (!isValid)
return 0;

break;
}

letterToScoreMap.TryGetValue(letter, out var score);

if (isDoublePointsActive)
score *= doublePointsMultiplier;

if (isTriplePointsActive)
score *= triplePointsMultiplier;


totalScore += score;
}

if (isDoublePointsActive || isTriplePointsActive)
return 0;

if (endedBonusBeforeStarting)
return 0;

return totalScore;
}
}
}
18 changes: 18 additions & 0 deletions csharp-scrabble-challenge.Test/ExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,28 @@ public class ExtensionTests

[TestCase("{street}", 12)] //extension double word
[TestCase("[street]", 18)] //extension triple word
[TestCase("{s}treet", 7)] //extension double word
[TestCase("[st]reet", 10)] //extension triple word
[TestCase("[st]ree{t}", 11)] //extension triple word
[TestCase("[st}ree[t}", 0)] //extension triple word
[TestCase("{quirky}", 44)] //extension double word
[TestCase("[quirky]", 66)] //extension triple word
[TestCase("{OXyPHEnBUTaZoNE}", 82)]
[TestCase("[OXyPHEnBUTaZoNE]", 123)]
[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)]
[TestCase("\n\r\t\b\f", 0)]
[TestCase("a", 1)]
[TestCase("f", 4)]
[TestCase("OXyPHEnBUTaZoNE", 41)]
[TestCase("quirky", 22)]
[TestCase("street", 6)]
[TestCase("]street[", 0)]
public void ExtendedCriteriaTests(string word, int targetScore)
{
Assert.AreEqual(this.GetWordScore(word), targetScore);
Expand Down
Loading