#define DEVELOPING #define DO_THREADED #define DO_UNTHREADED using System; using System.Collections.Generic; using System.IO; using System.Diagnostics; using System.Text; namespace Boggle { public class Boggle { static void PrintWords(HashSet words) { int score = 0; foreach (WordOnBoard word in words) { Console.Write("{0} [{1}], ", word.Word, word.Score); score += word.Score; } Console.WriteLine("\n\nTotal score for this board: {0}", score); } static void Main(string[] args) { Stopwatch st = new Stopwatch(); BoggleList bl = new BoggleList(); Console.WriteLine("Current directory: {0}", Directory.GetCurrentDirectory()); #if !DEVELOPING string input; bool isError = false; #endif int boardSideLength = 10; int minWordLength = 4; char wordListInput = 'Z'; SupportedLists listToUseForLookup; #if !DEVELOPING do { isError = false; Console.WriteLine("Please enter the word list that you wish to use [Z for Zingarelli, T for TWS or standard scrabble list]: "); input = Console.ReadLine(); if (!char.TryParse(input, out wordListInput)) { Console.WriteLine("Invalid Input. Couldn't parse character"); isError = true; } wordListInput = char.ToUpper(wordListInput); if (wordListInput != 'T' && wordListInput != 'Z') { Console.WriteLine("Please enter only T or Z."); isError = true; } } while (isError); #endif switch (wordListInput) { case 'T': listToUseForLookup = SupportedLists.TWS; break; case 'Z': listToUseForLookup = SupportedLists.ZINGARELLI; break; default: throw new Exception("Unexpected!!"); } #if !DEVELOPING do { isError = false; Console.WriteLine("Please enter number of tiles that make up one side of the board: "); input = Console.ReadLine(); if (!int.TryParse(input, out boardSideLength)) { Console.WriteLine("Invalid Input."); isError = true; } } while (isError); do { isError = false; Console.WriteLine("Please enter number of ALPHABETS that should be there in shortest word: "); input = Console.ReadLine(); if (!int.TryParse(input, out minWordLength)) { Console.WriteLine("Invalid Input."); isError = true; } } while (isError); #endif Console.WriteLine("Board Side Length (in Tiles): {0}, Minimum word length (in alphabets): {1}, Word list: {2}", boardSideLength, minWordLength, listToUseForLookup); st.Start(); bl.LoadList(listToUseForLookup, boardSideLength, minWordLength, Directory.GetCurrentDirectory()); st.Stop(); Console.WriteLine("Loaded list (and serialized) in {0} ms.", st.ElapsedMilliseconds); //Generate a default random string for each run. string boardString = string.Empty; StringBuilder randomDefaultString = new StringBuilder(boardSideLength * boardSideLength); Random rand = new Random(DateTime.Now.Millisecond); for (int i = 0; i < boardSideLength * boardSideLength; i++) { randomDefaultString.Append((char)rand.Next('A', 'Z')); } boardString = randomDefaultString.ToString(); #if !DEVELOPING do { isError = false; Console.WriteLine("Please enter the characters on board in row first fashion ({0} expected characters): ", boardSideLength*boardSideLength); boardString = Console.ReadLine(); if (boardString.Length != boardSideLength*boardSideLength) { Console.WriteLine("Invalid Input. Q will be translated to QU internally. Please enter only Q if you want to enter the QU tile."); isError = true; } } while (isError); #endif boardString = boardString.ToUpper(); BoggleBoard board = new BoggleBoard(boardSideLength, boardString); board.Print(); BoggleSolver solver = new BoggleSolver(board, bl, minWordLength); st.Reset(); #if DO_UNTHREADED st.Start(); HashSet wordsUnThreaded = solver.GetWordsOnBoard(false); st.Stop(); PrintWords(wordsUnThreaded); Console.WriteLine("Got solution in {0} ms. Number of words (UNTHREADED): {1}\n\n", st.ElapsedMilliseconds, wordsUnThreaded.Count); #endif st.Reset(); #if DO_THREADED st.Start(); HashSet wordsThreaded = solver.GetWordsOnBoard(true); st.Stop(); PrintWords(wordsThreaded); Console.WriteLine("\nGot solution in {0} ms. Number of words (THREADED): {1}.", st.ElapsedMilliseconds, wordsThreaded.Count); #endif #if DO_THREADED && DO_UNTHREADED Console.WriteLine("Time for sanity checks... comparing solutions from threaded and non-threaded mode."); if (wordsThreaded.Count == wordsUnThreaded.Count) { Console.WriteLine("\nALL GOOD!"); } if (wordsUnThreaded.Count > wordsThreaded.Count) { wordsUnThreaded.ExceptWith(wordsThreaded); Console.WriteLine("Words in unthreaded collection that are not in threaded collection:"); PrintWords(wordsUnThreaded); } if (wordsThreaded.Count > wordsUnThreaded.Count) { wordsThreaded.ExceptWith(wordsUnThreaded); Console.WriteLine("Words in threaded collection that are not in unthreaded collection:"); PrintWords(wordsThreaded); } #endif Console.WriteLine("\nPress any key to exit."); Console.ReadKey(); } } }