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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
##
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore

# Jetbrains IDEs local files
.idea/

# User-specific files
*.rsuser
*.suo
Expand Down
8 changes: 8 additions & 0 deletions csharp-scrabble-challenge.Main/CharacterMultiplier.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace csharp_scrabble_challenge.Main;

public struct CharacterMultiplier
{
public char OpenIndicator;
public char CloseIndicator;
public int Multiplier;
}
22 changes: 20 additions & 2 deletions csharp-scrabble-challenge.Main/Program.cs
Original file line number Diff line number Diff line change
@@ -1,2 +1,20 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
namespace csharp_scrabble_challenge.Main;

internal static class Program
{
public static void Main(string[] args)
{
bool exit = false;
while (!exit)
{
Console.WriteLine("Please type a word:");
string? word = Console.ReadLine();

if (word != null)
{
if (word == "exit") exit = true;
else Console.WriteLine($"The score is: {new Scrabble(word).score()}");
}
}
}
}
78 changes: 61 additions & 17 deletions csharp-scrabble-challenge.Main/Scrabble.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,67 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Threading.Tasks;

namespace csharp_scrabble_challenge.Main
namespace csharp_scrabble_challenge.Main;

public class Scrabble
{
public class Scrabble
private static readonly Dictionary<char, int> _characterScores = new()
{
public Scrabble(string word)
{
//TODO: do something with the word variable
}
{ 'a', 1 }, { 'e', 1 }, { 'i', 1 }, { 'o', 1 }, { 'u', 1 },
{ 'l', 1 }, { 'n', 1 }, { 'r', 1 }, { 's', 1 }, { 't', 1 },

{ 'd', 2 }, { 'g', 2 },

{ 'b', 3 }, { 'c', 3 }, { 'm', 3 }, { 'p', 3 },

{ 'f', 4 }, { 'h', 4 }, { 'v', 4 }, { 'w', 4 }, { 'y', 4 },

{ 'k', 5 },

{ 'j', 8 }, { 'x', 8 },

{ 'q', 10 }, { 'z', 10 },
};

public int score()
private static readonly CharacterMultiplier[] _multipliers =
[
new() { CloseIndicator = '}', OpenIndicator = '{', Multiplier = 2, },
new() { CloseIndicator = ']', OpenIndicator = '[', Multiplier = 3, },
];

private readonly string _inputWord;
private const int DEFAULT_MULTIPLIER = 1;

public Scrabble(string word)
{
_inputWord = word;
}

public int score()
{
int score = 0;
string lowerWord = _inputWord.ToLower();
char[] chars = lowerWord.ToCharArray();

int currentMultiplier = DEFAULT_MULTIPLIER;

foreach (char c in chars)
{
//TODO: score calculation code goes here
throw new NotImplementedException(); //TODO: Remove this line when the code has been written
foreach (var characterMultiplier in _multipliers)
{
if (c == characterMultiplier.OpenIndicator)
{
currentMultiplier = characterMultiplier.Multiplier;
}
else if (c == characterMultiplier.CloseIndicator)
{
currentMultiplier = DEFAULT_MULTIPLIER;
}
}

if (_characterScores.TryGetValue(c, out int cScore))
{
score += cScore * currentMultiplier;
}
}

return score;
}
}
}
Loading