Advertisement
starbeamrainbowlabs

Coding Conundrums 2.3: Scrabble Validator

Feb 18th, 2015
387
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class ScrabbleValidator
  5. {
  6.     // from http://scrabblewizard.com/scrabble-tile-distribution/
  7.     // the coding conundrums seems to have a few letter counts missing
  8.     public static string[] letterCounts = new string[]
  9.     {
  10.         "", // 0
  11.         "jkqxz", // 1
  12.         "bcfhmpvwy*", // 2 - * = blank tile
  13.         "g", // 3
  14.         "dlsu", // 4
  15.         "", // 5
  16.         "nrt", // 6
  17.         "", // 7
  18.         "o", // 8
  19.         "ai", // 9
  20.         "", // 10
  21.         "", // 11
  22.         "e", // 12
  23.     };
  24.  
  25.     public static int blankTileCount = 2;
  26.    
  27.     static void Main(string[] args)
  28.     {
  29.         if(args.Length != 1)
  30.         {
  31.             Console.WriteLine("This program works out whether a word is a valid scrabble word.");
  32.             Console.WriteLine("Use it like this:");
  33.             Console.WriteLine("    ScrabbleValidator.exe <word>");
  34.            
  35.             Console.WriteLine("\n<word>: The word you want to validate.");
  36.             return;
  37.         }
  38.  
  39.         string word = args[0].Trim().ToLower();
  40.  
  41.         // count the number of each letter in the word
  42.         Dictionary<char, int> letterCounts = new Dictionary<char, int>();
  43.         foreach (char ch in word)
  44.         {
  45.             if (letterCounts.ContainsKey(ch))
  46.             {
  47.                 letterCounts[ch] += 1;
  48.             }
  49.             else
  50.             {
  51.                 letterCounts.Add(ch, 1);
  52.             }
  53.         }
  54.  
  55.         // loop over the letter counts and validate the word
  56.         string invalidReasons = "";
  57.         int usedBlanks = 0;
  58.         foreach (KeyValuePair<char, int> letterCount in letterCounts)
  59.         {
  60.             char currentChar = letterCount.Key;
  61.             int currentCharCount = letterCount.Value;
  62.             int maxCharCount = getLetterCount(currentChar);
  63.  
  64.             if (currentCharCount > maxCharCount)
  65.             {
  66.                 if(usedBlanks + (currentCharCount - maxCharCount) <= blankTileCount)
  67.                 {
  68.                     usedBlanks += currentCharCount - maxCharCount;
  69.                 }
  70.                 else
  71.                 {
  72.                     invalidReasons += String.Format("The character '{0}' is used {1} times (scrabble has {2} tiles for that letter).\n", currentChar, currentCharCount, maxCharCount);
  73.                 }
  74.             }
  75.         }
  76.  
  77.         if(invalidReasons.Length > 0)
  78.         {
  79.             Console.WriteLine("{0} is not valid. Reasons: ", word);
  80.             Console.WriteLine(invalidReasons);
  81.         }
  82.         else
  83.         {
  84.             Console.WriteLine("{0} is a valid scrabble word.", word);
  85.             Console.WriteLine("It would use {0} blank tiles.", usedBlanks);
  86.         }
  87.     }
  88.    
  89.     static int getLetterCount(char ch)
  90.     {
  91.         for(int i = 0; i < letterCounts.Length; i++)
  92.         {
  93.             if(letterCounts[i].Contains(Char.ToLower(ch).ToString()))
  94.             {
  95.                 return i;
  96.             }
  97.         }
  98.         return 0; // the character wasn't recognised, so there won't be any tiles that match it.
  99.     }
  100. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement