56106ca66eca4c2703df175117bc6ad89ab7d91b
Dev Ghai Added pre-processor directi...

Dev Ghai authored 11 years ago

1) #define ROLL_WITH_DEFAULTS
Dev Ghai 1. Added random board gener...

Dev Ghai authored 11 years ago

2) #define DO_THREADED
3) #define DO_UNTHREADED
Dev Ghai Added pre-processor directi...

Dev Ghai authored 11 years ago

4) //#define VERBOSE
Dev Ghai 1. Added random board gener...

Dev Ghai authored 11 years ago

5) 
6) using System;
7) using System.Collections.Generic;
8) using System.IO;
9) using System.Diagnostics;
10) using System.Text;
11) 
12) namespace Boggle
13) {
14)     public class Boggle
15)     {
16)         static void PrintWords(HashSet<WordOnBoard> words)
17)         {
18)             int score = 0;
19)             foreach (WordOnBoard word in words)
20)             {
21)                 Console.Write("{0} [{1}], ", word.Word, word.Score);
22)                 score += word.Score;
23)             }
24)             Console.WriteLine("\n\nTotal score for this board: {0}", score);
25)         }
26)         static void Main(string[] args)
27)         {
28)             Stopwatch st = new Stopwatch();
29)             BoggleList bl = new BoggleList();
30)             Console.WriteLine("Current directory: {0}", Directory.GetCurrentDirectory());
Dev Ghai Added pre-processor directi...

Dev Ghai authored 11 years ago

31) #if !ROLL_WITH_DEFAULTS
Dev Ghai 1. Added random board gener...

Dev Ghai authored 11 years ago

32)             string input;
33)             bool isError = false;
34) #endif
Dev Ghai Added pre-processor directi...

Dev Ghai authored 11 years ago

35)             int boardSideLength = 100;
Dev Ghai 1. Added random board gener...

Dev Ghai authored 11 years ago

36)             int minWordLength = 4;
37) 
38)             char wordListInput = 'Z';
39)             SupportedLists listToUseForLookup;
40) 
Dev Ghai Added pre-processor directi...

Dev Ghai authored 11 years ago

41) #if !ROLL_WITH_DEFAULTS
Dev Ghai 1. Added random board gener...

Dev Ghai authored 11 years ago

42)             do
43)             {
44)                 isError = false;
45)                 Console.WriteLine("Please enter the word list that you wish to use [Z for Zingarelli, T for TWS or standard scrabble list]: ");
46)                 input = Console.ReadLine();
47)                 if (!char.TryParse(input, out wordListInput))
48)                 {
49)                     Console.WriteLine("Invalid Input. Couldn't parse character");
50)                     isError = true;
51)                 }
52)                 wordListInput = char.ToUpper(wordListInput);
53)                 if (wordListInput != 'T' && wordListInput != 'Z')
54)                 {
55)                     Console.WriteLine("Please enter only T or Z.");
56)                     isError = true;
57)                 }
58)             } while (isError);
59) #endif
60)             switch (wordListInput)
61)             {
62)                 case 'T':
63)                     listToUseForLookup = SupportedLists.TWS;
64)                     break;
65) 
66)                 case 'Z':
67)                     listToUseForLookup = SupportedLists.ZINGARELLI;
68)                     break;
69)                 default:
70)                     throw new Exception("Unexpected!!");
71)             }
72) 
Dev Ghai Added pre-processor directi...

Dev Ghai authored 11 years ago

73) #if !ROLL_WITH_DEFAULTS
Dev Ghai 1. Added random board gener...

Dev Ghai authored 11 years ago

74)             do
75)             {
76)                 isError = false;
77)                 Console.WriteLine("Please enter number of tiles that make up one side of the board: ");
78)                 input = Console.ReadLine();
79)                 if (!int.TryParse(input, out boardSideLength))
80)                 {
81)                     Console.WriteLine("Invalid Input.");
82)                     isError = true;
83)                 }
84)             } while (isError);
85) 
86)             do
87)             {
88)                 isError = false;
89)                 Console.WriteLine("Please enter number of ALPHABETS that should be there in shortest word: ");
90)                 input = Console.ReadLine();
91)                 if (!int.TryParse(input, out minWordLength))
92)                 {
93)                     Console.WriteLine("Invalid Input.");
94)                     isError = true;
95)                 }
96)             } while (isError);
97) #endif
98)             Console.WriteLine("Board Side Length (in Tiles): {0}, Minimum word length (in alphabets): {1}, Word list: {2}", boardSideLength, minWordLength, listToUseForLookup);            
99)             st.Start();
100)             bl.LoadList(listToUseForLookup, boardSideLength, minWordLength, Directory.GetCurrentDirectory());
101)             st.Stop();
102)             Console.WriteLine("Loaded list (and serialized) in {0} ms.", st.ElapsedMilliseconds);
103) 
104)             //Generate a default random string for each run.
105)             string boardString = string.Empty;
106)             StringBuilder randomDefaultString = new StringBuilder(boardSideLength * boardSideLength);
107)             Random rand = new Random(DateTime.Now.Millisecond);
108)             for (int i = 0; i < boardSideLength * boardSideLength; i++)
109)             {
110)                 randomDefaultString.Append((char)rand.Next('A', 'Z'));
111)             }
112)             boardString = randomDefaultString.ToString();
113) 
Dev Ghai Added pre-processor directi...

Dev Ghai authored 11 years ago

114) #if !ROLL_WITH_DEFAULTS
Dev Ghai 1. Added random board gener...

Dev Ghai authored 11 years ago

115)             do
116)             {
117)                 isError = false;
118)                 Console.WriteLine("Please enter the characters on board in row first fashion ({0} expected characters): ", boardSideLength*boardSideLength);
119)                 boardString = Console.ReadLine();
120)                 if (boardString.Length != boardSideLength*boardSideLength)
121)                 {
122)                     Console.WriteLine("Invalid Input. Q will be translated to QU internally. Please enter only Q if you want to enter the QU tile.");
123)                     isError = true;
124)                 }
125)             } while (isError);
126) #endif
127)             boardString = boardString.ToUpper();
128) 
129)             BoggleBoard board = new BoggleBoard(boardSideLength, boardString);
Dev Ghai Added pre-processor directi...

Dev Ghai authored 11 years ago

130) #if VERBOSE
Dev Ghai 1. Added random board gener...

Dev Ghai authored 11 years ago

131)             board.Print();
Dev Ghai Added pre-processor directi...

Dev Ghai authored 11 years ago

132) #endif
Dev Ghai Re-enabled code to run in b...

Dev Ghai authored 11 years ago

133)             BoggleSolver solver = new BoggleSolver(board, bl, minWordLength);
Dev Ghai 1. Added random board gener...

Dev Ghai authored 11 years ago

134)             st.Reset();
135) #if DO_UNTHREADED
Dev Ghai Re-enabled code to run in b...

Dev Ghai authored 11 years ago

136)             st.Start();
137)             HashSet<WordOnBoard> wordsUnThreaded = solver.GetWordsOnBoard(false);
138)             st.Stop();
Dev Ghai Added pre-processor directi...

Dev Ghai authored 11 years ago

139) #if VERBOSE
Dev Ghai Re-enabled code to run in b...

Dev Ghai authored 11 years ago

140)             PrintWords(wordsUnThreaded);
Dev Ghai Added pre-processor directi...

Dev Ghai authored 11 years ago

141) #endif
Dev Ghai Re-enabled code to run in b...

Dev Ghai authored 11 years ago

142) 
143)             Console.WriteLine("Got solution in {0} ms. Number of words (UNTHREADED): {1}\n\n", st.ElapsedMilliseconds, wordsUnThreaded.Count);
Dev Ghai 1. Added random board gener...

Dev Ghai authored 11 years ago

144) #endif
Dev Ghai Re-enabled code to run in b...

Dev Ghai authored 11 years ago

145)             st.Reset();
Dev Ghai 1. Added random board gener...

Dev Ghai authored 11 years ago

146) #if DO_THREADED
147)             st.Start();
148)             HashSet<WordOnBoard> wordsThreaded = solver.GetWordsOnBoard(true);
149)             st.Stop();
Dev Ghai Added pre-processor directi...

Dev Ghai authored 11 years ago

150) #if VERBOSE
Dev Ghai 1. Added random board gener...

Dev Ghai authored 11 years ago

151)             PrintWords(wordsThreaded);
Dev Ghai Added pre-processor directi...

Dev Ghai authored 11 years ago

152) #endif
Dev Ghai Re-enabled code to run in b...

Dev Ghai authored 11 years ago

153)             Console.WriteLine("\nGot solution in {0} ms. Number of words (THREADED): {1}.", st.ElapsedMilliseconds, wordsThreaded.Count);
Dev Ghai 1. Added random board gener...

Dev Ghai authored 11 years ago

154) #endif
155) #if DO_THREADED && DO_UNTHREADED
Dev Ghai Re-enabled code to run in b...

Dev Ghai authored 11 years ago

156)             Console.WriteLine("Time for sanity checks... comparing solutions from threaded and non-threaded mode.");
157)             if (wordsThreaded.Count == wordsUnThreaded.Count)
158)             {
159)                 Console.WriteLine("\nALL GOOD!");
160)             }
161)             if (wordsUnThreaded.Count > wordsThreaded.Count)
162)             {
163)                 wordsUnThreaded.ExceptWith(wordsThreaded);
164)                 Console.WriteLine("Words in unthreaded collection that are not in threaded collection:");
165)                 PrintWords(wordsUnThreaded);
166)             }
167)             if (wordsThreaded.Count > wordsUnThreaded.Count)
168)             {
169)                 wordsThreaded.ExceptWith(wordsUnThreaded);
170)                 Console.WriteLine("Words in threaded collection that are not in unthreaded collection:");
171)                 PrintWords(wordsThreaded);
172)             }