/
TheBoogle.cs
233 lines (209 loc) · 7.36 KB
/
TheBoogle.cs
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Game.Dictionary;
using System.Collections;
using Game.Board;
namespace InterfaceTestDictonary
{
public partial class TheBoogle : Form
{
GameDictionary Game;
private int validPaths = 0;
private Board bBoard;
Label[,] labels;
GameDictionary rs;
public TheBoogle(GameDictionary _Game)
{
Game = _Game;
rs = new GameDictionary();
InitializeComponent();
}
private void Bai4_Load(object sender, EventArgs e)
{
bBoard = new Board(genBroad (Int32.Parse (textBox1.Text))); // creating boggle board
labels = new Label[Int32.Parse(textBox1.Text),Int32.Parse(textBox1.Text)];
//MessageBox.Show(bBoard.toString ());
String s = "";
for (int i = 0; i < bBoard.dimensions (); i++)
{
for (int j = 0; j < bBoard.dimensions(); j++)
{
Label l = new Label ();
l.Location = new Point (590 + i*20, 50 + j*20);
l.Text = bBoard.getItem(i,j);
l.Size = new Size(15, 15);
l.BorderStyle = BorderStyle.FixedSingle;
s += l.Text;
labels[i, j] = l;
this.Controls.Add(l);
}
}
//MessageBox.Show(s);
genALlWord();
//richTextBox1.Text = bBoard.toString();
}
private void genALlWord()
{
for (int i = 0; i < bBoard.dimensions(); i++)
{
for (int j = 0; j < bBoard.dimensions(); j++)
{
Word wordSoFar = new Word();
BoardLocation curLoc = new BoardLocation(i, j);
wordSoFar.addLetter(bBoard.letterAtLocation(curLoc));
Word newWord = wordSoFar.makeCopy();
Board newBoard = bBoard.makeCopy();
newBoard.markAsTaken(curLoc);
recurseWords(newWord, newBoard, curLoc);
}
}
}
GameDictionary boogles = new GameDictionary();
/**
*The recursive method takes in three parameters,
**/
private void recurseWords(Word wordSoFar, Board bBoard, BoardLocation loc){
String str = wordSoFar.toString();
bBoard.markAsTaken(loc);
BoardLocation[] adjLoc = bBoard.getAdjacentUntakens(loc);
if(wordSoFar.getLength() > 2 && Game.ContainsKey (str)){
if (!boogles.ContainsKey (str))
boogles.Add(str, null);
validPaths++;
}
if(!this.wordsExistThaGameartWith (str)){
return;
}else{
for(int i =0; i< adjLoc.Length; i++){
Word newWord = wordSoFar.makeCopy();
newWord.addLetter(bBoard.letterAtLocation(adjLoc[i]));
recurseWords(newWord, bBoard.makeCopy(), adjLoc[i]);
}
}
}
private bool wordsExistThaGameartWith(String word)
{
int maxLength = 35;
string longest = word;
for (int i = 0; i < maxLength; i++)
{
longest += "*";
if ( Game.PartialMatch(longest).Count > 0)
{
return true;
}
}
return false;
}
private String genBroad(int t)
{
Random randon = new Random();
int n = t*t;
Byte[] buffer = new Byte[n];
for (int i = 0; i < n; i++)
{
buffer[i] = (Byte)randon.Next(65, 90);
}
return Encoding.UTF8.GetString(buffer, 0, buffer.Length);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
int point = 0;
private void button2_Click(object sender, EventArgs e)
{
if (boogles.ContainsKey(textBox2.Text) == true)
{
if (rs.ContainsKey(textBox2.Text) == false)
{
rs.Add(textBox2.Text, null);
point++;
label2.Text = point.ToString();
if (rs.Count == boogles.Count)
{
MessageBox.Show("You win! Point = " + point);
rs.Clear();
}
}
else
{
MessageBox.Show("You have found this word already!");
}
}
else
{
MessageBox.Show("This word doesn't exist");
}
}
private void label2_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("Your Points = " + point);
foreach (DictionaryEntry de in boogles)
{
string[] row = { de.Key.ToString() };
listView1.Items.Add(new ListViewItem(row));
}
point = 0;
rs.Clear();
button2.Visible = false;
}
private void button1_Click(object sender, EventArgs e)
{
try
{
if (Int32.Parse(textBox1.Text) < 2 || Int32.Parse(textBox1.Text) >= 10)
{
MessageBox.Show("You have to enter the number from 2-9");
textBox1.Text = "4";
}
genBroad(Int32.Parse(textBox1.Text));
}
catch (FormatException)
{
textBox1.Clear();
textBox1.Text = "4";
}
button2.Visible = true;
rs.Clear();
listView1.Items.Clear();
for (int i = 0; i < bBoard.dimensions(); i++)
{
for (int j = 0; j < bBoard.dimensions(); j++)
{
if (labels is Object)
{
labels[i,j].Visible = false;
}
}
}
bBoard = new Board(genBroad(Int32.Parse(textBox1.Text))); // creating boggle board
labels = new Label[Int32.Parse(textBox1.Text), Int32.Parse(textBox1.Text)];
String s = "";
for (int i = 0; i < bBoard.dimensions(); i++)
{
for (int j = 0; j < bBoard.dimensions(); j++)
{
Label l = new Label();
l.Location = new Point(590 + i * 20, 50 + j * 20);
l.Text = bBoard.getItem(i, j);
l.Size = new Size(15, 15);
l.BorderStyle = BorderStyle.FixedSingle;
s += l.Text;
labels[i, j] = l;
this.Controls.Add(l);
}
}
boogles.Clear();
genALlWord();
}
}
}