Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- class ScrabbleValidator
- {
- // from http://scrabblewizard.com/scrabble-tile-distribution/
- // the coding conundrums seems to have a few letter counts missing
- public static string[] letterCounts = new string[]
- {
- "", // 0
- "jkqxz", // 1
- "bcfhmpvwy*", // 2 - * = blank tile
- "g", // 3
- "dlsu", // 4
- "", // 5
- "nrt", // 6
- "", // 7
- "o", // 8
- "ai", // 9
- "", // 10
- "", // 11
- "e", // 12
- };
- public static int blankTileCount = 2;
- static void Main(string[] args)
- {
- if(args.Length != 1)
- {
- Console.WriteLine("This program works out whether a word is a valid scrabble word.");
- Console.WriteLine("Use it like this:");
- Console.WriteLine(" ScrabbleValidator.exe <word>");
- Console.WriteLine("\n<word>: The word you want to validate.");
- return;
- }
- string word = args[0].Trim().ToLower();
- // count the number of each letter in the word
- Dictionary<char, int> letterCounts = new Dictionary<char, int>();
- foreach (char ch in word)
- {
- if (letterCounts.ContainsKey(ch))
- {
- letterCounts[ch] += 1;
- }
- else
- {
- letterCounts.Add(ch, 1);
- }
- }
- // loop over the letter counts and validate the word
- string invalidReasons = "";
- int usedBlanks = 0;
- foreach (KeyValuePair<char, int> letterCount in letterCounts)
- {
- char currentChar = letterCount.Key;
- int currentCharCount = letterCount.Value;
- int maxCharCount = getLetterCount(currentChar);
- if (currentCharCount > maxCharCount)
- {
- if(usedBlanks + (currentCharCount - maxCharCount) <= blankTileCount)
- {
- usedBlanks += currentCharCount - maxCharCount;
- }
- else
- {
- invalidReasons += String.Format("The character '{0}' is used {1} times (scrabble has {2} tiles for that letter).\n", currentChar, currentCharCount, maxCharCount);
- }
- }
- }
- if(invalidReasons.Length > 0)
- {
- Console.WriteLine("{0} is not valid. Reasons: ", word);
- Console.WriteLine(invalidReasons);
- }
- else
- {
- Console.WriteLine("{0} is a valid scrabble word.", word);
- Console.WriteLine("It would use {0} blank tiles.", usedBlanks);
- }
- }
- static int getLetterCount(char ch)
- {
- for(int i = 0; i < letterCounts.Length; i++)
- {
- if(letterCounts[i].Contains(Char.ToLower(ch).ToString()))
- {
- return i;
- }
- }
- return 0; // the character wasn't recognised, so there won't be any tiles that match it.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement