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


using csharp_scrabble_challenge.Main;

Console.WriteLine("Welcome to the Scrabble scorer. Insert a word to be scored: ");
string? word = Console.ReadLine();
if (word == null)
{
word = "";
}
Scrabble game = new Scrabble(word);

bool run = true;
while (run)
{
int score = game.score();
Console.WriteLine($"The score was: {score}\nInsert a new word or x to quit: ");
word = Console.ReadLine();
if (word == null)
{
word = "";
}
if (word.Equals("x") || word.Equals("X")) { break; }
game.Word = word;
}
71 changes: 64 additions & 7 deletions csharp-scrabble-challenge.Main/Scrabble.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,80 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.Tracing;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Threading.Tasks;

namespace csharp_scrabble_challenge.Main
{
public class Scrabble
public class Scrabble(string word)
{
public Scrabble(string word)
{
//TODO: do something with the word variable
private string _word = word.ToUpper();
public string Word { get => _word; set => _word = value.ToUpper(); }
private Dictionary<char, int> _scoreDict = InitiateScores();
public int score()
{
int score = 0;
int multiplier = 1;
int doubledActive = 0;
int tripledActive = 0;
char lastchar = 'a';

if (this._word.Equals("") || this._word.Equals(" \t\n"))
{
return 0;
}

foreach (char c in this._word)
{
if (c.Equals('}') && doubledActive == 0) { return 0; }
if(c.Equals(']') && tripledActive == 0) { return 0; }

if (c.Equals('{')) { multiplier = multiplier * 2; doubledActive++; }
if (c.Equals('[')) { multiplier = multiplier * 3; tripledActive++; }
if (c.Equals('}') && doubledActive > 0) { multiplier = multiplier / 2; doubledActive--; }
if (c.Equals(']') && tripledActive > 0) { multiplier = multiplier / 3; tripledActive--; }

if (this._scoreDict.ContainsKey(c))
{
score += this._scoreDict[c] * multiplier;
}
else if(!"{}[]".Contains(c))
{
return 0;
}
if (c.Equals('}') && lastchar == '{') { return 0; }
if (c.Equals(']') && lastchar == '[') { return 0; }
lastchar = c;
}

if (doubledActive > 0 || tripledActive > 0) { return 0; }
return score;
}

public int score()
private static Dictionary<char, int> InitiateScores()
{
//TODO: score calculation code goes here
throw new NotImplementedException(); //TODO: Remove this line when the code has been written
List<char> score1 = new List<char>(new char[]{'A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T'});
List<char> score2 = new List<char>(new char[] {'D', 'G'});
List<char> score3 = new List<char>(new char[] {'B', 'C', 'M', 'P'});
List<char> score4 = new List<char>(new char[] {'F', 'H', 'V', 'W', 'Y'});
List<char> score5 = new List<char>(new char[] {'K'});
List<char> score8 = new List<char>(new char[] { 'J', 'X'});
List<char> score10 = new List<char>(new char[] { 'Q', 'Z'});

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

foreach (char c in score1) { scoreDict.Add(c, 1); }
foreach (char c in score2) { scoreDict.Add(c, 2); }
foreach (char c in score3) { scoreDict.Add(c, 3); }
foreach (char c in score4) { scoreDict.Add(c, 4); }
foreach (char c in score5) { scoreDict.Add(c, 5); }
foreach (char c in score8) { scoreDict.Add(c, 8); }
foreach (char c in score10) { scoreDict.Add(c, 10); }

return scoreDict;
}
}

}
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