1. Added random board gener...
Dev Ghai authored 11 years ago
|
1) #define DEVELOPING
2) #define DO_THREADED
3) #define DO_UNTHREADED
4)
5) using System;
6) using System.Collections.Generic;
7) using System.IO;
8) using System.Diagnostics;
9) using System.Text;
10)
11) namespace Boggle
12) {
13) public class Boggle
14) {
15) static void PrintWords(HashSet<WordOnBoard> words)
16) {
17) int score = 0;
18) foreach (WordOnBoard word in words)
19) {
20) Console.Write("{0} [{1}], ", word.Word, word.Score);
21) score += word.Score;
22) }
23) Console.WriteLine("\n\nTotal score for this board: {0}", score);
24) }
25) static void Main(string[] args)
26) {
27) Stopwatch st = new Stopwatch();
28) BoggleList bl = new BoggleList();
29) Console.WriteLine("Current directory: {0}", Directory.GetCurrentDirectory());
30) #if !DEVELOPING
31) string input;
32) bool isError = false;
33) #endif
34) int boardSideLength = 10;
35) int minWordLength = 4;
36)
37) char wordListInput = 'Z';
38) SupportedLists listToUseForLookup;
39)
40) #if !DEVELOPING
41) do
42) {
43) isError = false;
44) Console.WriteLine("Please enter the word list that you wish to use [Z for Zingarelli, T for TWS or standard scrabble list]: ");
45) input = Console.ReadLine();
46) if (!char.TryParse(input, out wordListInput))
47) {
48) Console.WriteLine("Invalid Input. Couldn't parse character");
49) isError = true;
50) }
51) wordListInput = char.ToUpper(wordListInput);
52) if (wordListInput != 'T' && wordListInput != 'Z')
53) {
54) Console.WriteLine("Please enter only T or Z.");
55) isError = true;
56) }
57) } while (isError);
58) #endif
59) switch (wordListInput)
60) {
61) case 'T':
62) listToUseForLookup = SupportedLists.TWS;
63) break;
64)
65) case 'Z':
66) listToUseForLookup = SupportedLists.ZINGARELLI;
67) break;
68) default:
69) throw new Exception("Unexpected!!");
70) }
71)
72) #if !DEVELOPING
73) do
74) {
75) isError = false;
76) Console.WriteLine("Please enter number of tiles that make up one side of the board: ");
77) input = Console.ReadLine();
78) if (!int.TryParse(input, out boardSideLength))
79) {
80) Console.WriteLine("Invalid Input.");
81) isError = true;
82) }
83) } while (isError);
84)
85) do
86) {
87) isError = false;
88) Console.WriteLine("Please enter number of ALPHABETS that should be there in shortest word: ");
89) input = Console.ReadLine();
90) if (!int.TryParse(input, out minWordLength))
91) {
92) Console.WriteLine("Invalid Input.");
93) isError = true;
94) }
95) } while (isError);
96) #endif
97) Console.WriteLine("Board Side Length (in Tiles): {0}, Minimum word length (in alphabets): {1}, Word list: {2}", boardSideLength, minWordLength, listToUseForLookup);
98) st.Start();
99) bl.LoadList(listToUseForLookup, boardSideLength, minWordLength, Directory.GetCurrentDirectory());
100) st.Stop();
101) Console.WriteLine("Loaded list (and serialized) in {0} ms.", st.ElapsedMilliseconds);
102)
103) //Generate a default random string for each run.
104) string boardString = string.Empty;
105) StringBuilder randomDefaultString = new StringBuilder(boardSideLength * boardSideLength);
106) Random rand = new Random(DateTime.Now.Millisecond);
107) for (int i = 0; i < boardSideLength * boardSideLength; i++)
108) {
109) randomDefaultString.Append((char)rand.Next('A', 'Z'));
110) }
111) boardString = randomDefaultString.ToString();
112)
113) #if !DEVELOPING
114) do
115) {
116) isError = false;
117) Console.WriteLine("Please enter the characters on board in row first fashion ({0} expected characters): ", boardSideLength*boardSideLength);
118) boardString = Console.ReadLine();
119) if (boardString.Length != boardSideLength*boardSideLength)
120) {
121) Console.WriteLine("Invalid Input. Q will be translated to QU internally. Please enter only Q if you want to enter the QU tile.");
122) isError = true;
123) }
124) } while (isError);
125) #endif
126) boardString = boardString.ToUpper();
127)
128) BoggleBoard board = new BoggleBoard(boardSideLength, boardString);
129) board.Print();
|
Re-enabled code to run in b...
Dev Ghai authored 11 years ago
|
133) st.Start();
134) HashSet<WordOnBoard> wordsUnThreaded = solver.GetWordsOnBoard(false);
135) st.Stop();
136) PrintWords(wordsUnThreaded);
137)
138) Console.WriteLine("Got solution in {0} ms. Number of words (UNTHREADED): {1}\n\n", st.ElapsedMilliseconds, wordsUnThreaded.Count);
|
Re-enabled code to run in b...
Dev Ghai authored 11 years ago
|
149) Console.WriteLine("Time for sanity checks... comparing solutions from threaded and non-threaded mode.");
150) if (wordsThreaded.Count == wordsUnThreaded.Count)
151) {
152) Console.WriteLine("\nALL GOOD!");
153) }
154) if (wordsUnThreaded.Count > wordsThreaded.Count)
155) {
156) wordsUnThreaded.ExceptWith(wordsThreaded);
157) Console.WriteLine("Words in unthreaded collection that are not in threaded collection:");
158) PrintWords(wordsUnThreaded);
159) }
160) if (wordsThreaded.Count > wordsUnThreaded.Count)
161) {
162) wordsThreaded.ExceptWith(wordsUnThreaded);
163) Console.WriteLine("Words in threaded collection that are not in unthreaded collection:");
164) PrintWords(wordsThreaded);
165) }
|