-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
139 lines (130 loc) · 5.12 KB
/
Copy pathProgram.cs
File metadata and controls
139 lines (130 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Diagnostics;
namespace WordSearch
{
class Program
{
static void Main(string[] args)
{
var field = GenerateField(5, 5);
DisplayField(field);
var fileToRead = "words.txt";
Console.WriteLine("Reading from file {0}", fileToRead);
var words = File.ReadAllLines(fileToRead);
Console.WriteLine("Loading dictionary {0}", fileToRead);
var dictionary = WordSearchFunctions.LoadWords(words, UniqueCharsIn(field));
Console.WriteLine("Loaded {0} words from file {1}", words.Length, fileToRead);
Console.WriteLine("Press enter to list the words found...");
Console.ReadLine();
var wordsInField = WordSearchFunctions.FindWords(field, dictionary).ToArray();
Console.WriteLine("Number of words found {0} : ", wordsInField.Length);
foreach (var foundword in wordsInField.Select(w => w.Word).OrderBy(w => w))
Console.WriteLine(foundword);
string word;
do
{
word = Console.ReadLine();
var wordToDisplay = wordsInField.FirstOrDefault(w => w.Word == word);
if (wordToDisplay == null)
{
// either its not a word or its not in field.
Console.WriteLine("Sorry!, '{0}' is not a word in the field.", word);
}
else
{
DisplayWord(wordToDisplay, field);
}
} while (!String.IsNullOrEmpty(word));
}
static void DisplayField(char[,] chars)
{
for (int y = 0; y < chars.GetLength(1); y++)
{
for (int x = 0; x < chars.GetLength(0); x++)
{
Console.Write(chars[x, y]);
Console.Write(' ');
}
Console.WriteLine();
}
}
private static char[,] GenerateField(int xmax, int ymax)
{
// English scrabble distribution for generation of field.
// This is used for the frequency of letter usage in the language
var chars =
new string('a', 9) +
new string('b', 2) +
new string('c', 2) +
new string('d', 4) +
new string('e', 12) +
new string('f', 2) +
new string('g', 3) +
new string('h', 2) +
new string('i', 9) +
new string('j', 1) +
new string('k', 1) +
new string('l', 4) +
new string('m', 2) +
new string('n', 6) +
new string('o', 8) +
new string('p', 2) +
new string('q', 1) +
new string('r', 6) +
new string('s', 4) +
new string('t', 6) +
new string('u', 4) +
new string('v', 2) +
new string('w', 2) +
new string('x', 1) +
new string('y', 2) +
new string('z', 1);
var field = new char[xmax, ymax];
var random = new Random();
for (int x = 0; x < xmax; x++)
for (int y = 0; y < ymax; y++)
{
field[x, y] = chars[random.Next(chars.Length)];
}
return field;
}
private static HashSet<char> UniqueCharsIn(char[,] chars)
{
return new HashSet<char>(chars.Cast<char>().Distinct());
}
private static void DisplayWord(WordSearchSol word, char[,] field)
{
for (int y = 0; y < field.GetLength(1); y++)
{
for (int x = 0; x < field.GetLength(0); x++)
{
var thispoint = new Point { X = x, Y = y };
if (word.Path.Contains(thispoint))
Console.BackgroundColor = ConsoleColor.White;
Console.Write(field[x, y].ToString().ToUpper());
Console.BackgroundColor = ConsoleColor.White;
if (word.PathContains(thispoint, new Point { X = x + 1, Y = y }))
Console.BackgroundColor = ConsoleColor.DarkGreen;
Console.Write(" ");
Console.BackgroundColor = ConsoleColor.Black;
}
Console.WriteLine();
for (int x = 0; x < field.GetLength(0); x++)
{
var thispoint = new Point { X = x, Y = y };
if (word.PathContains(thispoint, new Point { X = x, Y = y + 1 }))
Console.BackgroundColor = ConsoleColor.DarkGreen;
Console.Write(" ");
Console.BackgroundColor = ConsoleColor.Black;
Console.Write(" ");
}
Console.WriteLine();
}
}
}
}