From c3902c6dcc4bd40bc7a15d26e5d0e42ab95e8c1a Mon Sep 17 00:00:00 2001 From: Tein Schoemaker Date: Wed, 6 Aug 2025 14:41:37 +0200 Subject: [PATCH] Completed --- csharp-scrabble-challenge.Main/Program.cs | 18 +++++++- csharp-scrabble-challenge.Main/Scrabble.cs | 54 ++++++++++++++++++++-- 2 files changed, 66 insertions(+), 6 deletions(-) diff --git a/csharp-scrabble-challenge.Main/Program.cs b/csharp-scrabble-challenge.Main/Program.cs index 3751555..d32c727 100644 --- a/csharp-scrabble-challenge.Main/Program.cs +++ b/csharp-scrabble-challenge.Main/Program.cs @@ -1,2 +1,16 @@ -// See https://aka.ms/new-console-template for more information -Console.WriteLine("Hello, World!"); +using csharp_scrabble_challenge.Main; + +class Program +{ + static void Main(string[] args) + { + Console.WriteLine("Welcome to Scrabble!\n"); + Console.WriteLine("Try putting in a word:\n"); + + string? input = Console.ReadLine(); + + Scrabble scrabble = new Scrabble(input); + scrabble.score(); + } +} + diff --git a/csharp-scrabble-challenge.Main/Scrabble.cs b/csharp-scrabble-challenge.Main/Scrabble.cs index c1ea013..7101517 100644 --- a/csharp-scrabble-challenge.Main/Scrabble.cs +++ b/csharp-scrabble-challenge.Main/Scrabble.cs @@ -3,21 +3,67 @@ using System.Linq; using System.Reflection.Metadata.Ecma335; using System.Text; +using System.Text.RegularExpressions; using System.Threading.Tasks; namespace csharp_scrabble_challenge.Main { public class Scrabble { + + private string _word; + private int modifier = 1; + + Dictionary scores = new Dictionary() + { + {'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 Scrabble(string word) - { - //TODO: do something with the word variable + { + _word = word.Trim().ToLower(); + + if (_word.StartsWith("[") && _word.EndsWith("]")) + { + _word = _word.Remove(0, 1); + _word = _word.Remove(_word.Length - 1); + modifier = 3; + } + else if (_word.StartsWith("{") && _word.EndsWith("}")) + { + _word =_word.Remove(0, 1); + _word = _word.Remove(_word.Length - 1); + modifier = 2; + } } 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; + Dictionary counts = new Dictionary(); + + foreach (char c in _word) + { + if (scores.ContainsKey(c)) + { + totalScore += scores[c]; + } + } + + foreach (KeyValuePair kvp in counts) + { + Console.WriteLine($"{kvp.Key}: {kvp.Value}"); + } + + Console.WriteLine($"\nYour total score for this word is: {totalScore}"); + return totalScore * modifier; } } }