Advertisement
starbeamrainbowlabs

Coding Conundrums 2.2: Scrabble Scores

Feb 18th, 2015
1,032
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.11 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class ScrabbleScorer
  5. {
  6.     public static void Main(string[] args)
  7.     {
  8.         if(args.Length != 1)
  9.         {
  10.             Console.WriteLine("Use it like this:");
  11.             Console.WriteLine("    ScrabbleScorer.exe <word>");
  12.             Console.WriteLine("\n<word>: The word to calculate the score for.");
  13.             return;
  14.         }
  15.  
  16.         Console.WriteLine("'{0}' scores {1} points.", args[0], CalculateWordValue(args[0]));
  17.     }
  18.  
  19.     public static Dictionary<char, int> wordValues = new Dictionary<char, int>()
  20.     {
  21.         { 'a', 1 }, { 'b', 3 }, { 'c', 3 }, { 'd', 2 }, { 'e', 1 }, { 'f', 4 }, { 'g', 2 }, { 'h', 4 }, { 'i', 1 },
  22.         { 'j', 8 }, { 'k', 5 }, { 'l', 1 }, { 'm', 3 }, { 'n', 1 }, { 'o', 1 }, { 'p', 3 }, { 'q', 10 },{ 'r', 1 },
  23.         { 's', 1 }, { 't', 1 }, { 'u', 1 }, { 'v', 4 }, { 'w', 4 }, { 'x', 8 }, { 'y', 4 }, { 'z', 10 }
  24.     };
  25.  
  26.     public static int CalculateWordValue(string word)
  27.     {
  28.         word = word.ToLower();
  29.         int totalScore = 0;
  30.  
  31.         foreach(char ch in word)
  32.         {
  33.             int chScore = 0;
  34.             if(wordValues.TryGetValue(ch, out chScore))
  35.             {
  36.                 totalScore += chScore;
  37.             }
  38.         }
  39.  
  40.         return totalScore;
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement