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

namespace csharp_scrabble_challenge.Main
{
public class MyDictionary
{

Dictionary<char, int> dictionary;

public MyDictionary() {
dictionary = new Dictionary<char, int>();
dictionary.Add('a', 1);
dictionary.Add('b', 3);
dictionary.Add('c', 3);
dictionary.Add('d', 2);
dictionary.Add('e', 1);
dictionary.Add('f', 4);
dictionary.Add('g', 2);
dictionary.Add('h', 4);
dictionary.Add('i', 1);
dictionary.Add('j', 8);
dictionary.Add('k', 5);
dictionary.Add('l', 1);
dictionary.Add('m', 3);
dictionary.Add('n', 1);
dictionary.Add('o', 1);
dictionary.Add('p', 3);
dictionary.Add('q', 10);
dictionary.Add('r', 1);
dictionary.Add('s', 1);
dictionary.Add('t', 1);
dictionary.Add('u', 1);
dictionary.Add('v', 4);
dictionary.Add('w', 4);
dictionary.Add('x', 8);
dictionary.Add('y', 4);
dictionary.Add('z', 10);
dictionary.Add(' ', 0);
}

public int GetValue(char c)
{
if (!dictionary.ContainsKey(c))
{
return 0;
}
return dictionary[char.ToLower(c)];
}

public bool KeyIsContained(char c)
{
return dictionary.ContainsKey(c);
}
}
}
11 changes: 10 additions & 1 deletion csharp-scrabble-challenge.Main/Program.cs
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

using csharp_scrabble_challenge.Main;

Console.WriteLine("enter a word: ");
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.
Scrabble scr = new Scrabble(input);

Check warning on line 7 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 7 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 7 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 7 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 = scr.score();

Console.WriteLine($"{score}");
126 changes: 122 additions & 4 deletions csharp-scrabble-challenge.Main/Scrabble.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,135 @@

namespace csharp_scrabble_challenge.Main
{


public class Scrabble
{
public MyDictionary myDictionary = new MyDictionary();

private string _word;
private int multiplier = 1;

Check warning on line 17 in csharp-scrabble-challenge.Main/Scrabble.cs

View workflow job for this annotation

GitHub Actions / build

The field 'Scrabble.multiplier' is assigned but its value is never used

Check warning on line 17 in csharp-scrabble-challenge.Main/Scrabble.cs

View workflow job for this annotation

GitHub Actions / build

The field 'Scrabble.multiplier' is assigned but its value is never used

Check warning on line 17 in csharp-scrabble-challenge.Main/Scrabble.cs

View workflow job for this annotation

GitHub Actions / build

The field 'Scrabble.multiplier' is assigned but its value is never used

Check warning on line 17 in csharp-scrabble-challenge.Main/Scrabble.cs

View workflow job for this annotation

GitHub Actions / build

The field 'Scrabble.multiplier' is assigned but its value is never used




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


public int score()
{
int _score = 0;
int _multiplier = 1;

int open3x = 0;
int open2x = 0;
//TODO: score calculation code goes here
//throw new NotImplementedException(); //TODO: Remove this line when the code has been written
foreach (var c in _word)
{

int tempScore = 0;


if (c == '{')
{
open2x++;
_multiplier *= 2;
}
else if (c == '}')
{
if (open2x == 0)
{
return 0;
}
open2x--;
_multiplier /= 2;
}
else if (c == '[')
{
open3x++;
_multiplier *= 3;
}
else if (c == ']')
{
if (open3x == 0)
{
return 0;
}
open3x--;
_multiplier /= 3;
}
else if (myDictionary.KeyIsContained(char.ToLower(c)))
{
tempScore = myDictionary.GetValue(char.ToLower(c)) * _multiplier;
}
else
{
return 0;
}
_score += tempScore;
}





return _score;
}



/*
public int score()
{

//TODO: score calculation code goes here
throw new NotImplementedException(); //TODO: Remove this line when the code has been written
//throw new NotImplementedException(); //TODO: Remove this line when the code has been written
return ScoreRecursive(_word);
}
}
public int ScoreRecursive(string subString)
{
if (subString == null) return 0;
else if (subString.Length <= 1)
{
return myDictionary.GetValue(subString[0]);
}
else if (subString[0] == '{')
{
multiplier *= 2;
return ScoreRecursive(subString.Substring(1));
}
else if (subString[0] == '[')
{
multiplier *= 3;
return ScoreRecursive(subString.Substring(1));
}
else if (subString[0] == '}')
{
multiplier /= 2;
return ScoreRecursive(subString.Substring(1));
}
else if (subString[0] == ']')
{
multiplier /= 3;
return ScoreRecursive(subString.Substring(1));
}

else
{
return multiplier * myDictionary.GetValue(subString[0]) + ScoreRecursive(subString.Substring(1));
}
}



*/



}
}

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
Loading