-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinder.cs
More file actions
121 lines (102 loc) · 3.56 KB
/
Copy pathFinder.cs
File metadata and controls
121 lines (102 loc) · 3.56 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Woooordle
{
// Finder Class: Finding 5-letter words that can be answers
public partial class Finder : Form
{
private string[] wordsList;
private Regex regexLeft, regexRight, regexBoth;
// Constructor
public Finder()
{
InitializeComponent();
string filePath = "./wordsList.txt";
// if word list file exists
if (File.Exists(filePath))
{
// read world list file and put in list array
using (StreamReader reader = new StreamReader(filePath))
{
wordsList = reader.ReadLine().Split(' ');
wordsList = Array.ConvertAll(wordsList, str => str.ToUpper());
Array.Sort(wordsList);
}
}
else
{
// this can't be executed with word list file
MessageBox.Show("No Word List File Exists!");
this.Close();
}
InsertAll();
}
/// <summary>
/// Putting all items in list
/// </summary>
private void InsertAll()
{
resultList.Items.Clear();
resultList.Items.AddRange(wordsList);
countlbl.Text = $"{resultList.Items.Count} word(s) found in match.";
}
/// <summary>
/// Called when text is changed. Checks the input and updates the list
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Input_TextChanged(object sender, EventArgs e)
{
// Automatically make the letters uppercase
input.Text = input.Text.ToUpper();
// if input is blank, display all words available.
if (input.Text.Length == 0)
{
InsertAll();
return;
}
// input can't be longer than 5 letters.
if (input.Text.Length > 5)
{
input.Text = input.Text.Substring(0, 5);
}
// Enforce the selection position
input.Select(input.Text.Length, 0);
// if input is matched with format, renew the list
if (Regex.IsMatch(input.Text, @"^[A-Z*]+$"))
{
ValidateInput();
}
}
/// <summary>
/// Renew the list with text as query.
/// </summary>
private void ValidateInput()
{
// clearing all items in list
resultList.Items.Clear();
// enabling both-side wildcard letter
regexLeft = new Regex(("^*" + input.Text + "$").Replace("*", ".*"));
regexRight = new Regex(("^" + input.Text + "*$").Replace("*", ".*"));
regexBoth = new Regex(("^*" + input.Text + "*$").Replace("*", ".*"));
// if each word matches the input query, add to list
foreach (string str in wordsList)
{
if (regexLeft.IsMatch(str) || regexRight.IsMatch(str) || regexBoth.IsMatch(str))
{
resultList.Items.Add(str);
}
}
countlbl.Text = $"{resultList.Items.Count} word(s) found in match.";
}
}
}