-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph_construction.py
327 lines (287 loc) · 15.3 KB
/
graph_construction.py
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import copy as copy
import util
class Dictionary1:
"""
Dictionary object for game
"""
def __init__(self, filename):
with open(filename, "r") as file:
words = file.readlines()
words = [word.rstrip() for word in words]
self.dictionary = util.Trie(words)
def check_word(self, word):
"""
Used to check if word is in dictionary. Example, d.check_word("ADMIRE"). Return True if it is, False if not
"""
if self.dictionary.search(word) == True:
return True
else:
return False
wordChecker=Dictionary1("dictionary.txt")
def GADDAGAdder(stringname,array):
for i in range(1,len(stringname),1):
array.append(stringname[0:i][::-1]+'+'+stringname[i:len(stringname)])
array.append(stringname[::-1])
array=[]
wordCheckerReversed=Dictionary1("revdictionary.txt")
#Row/Column Checker
# Flow:
# We start by putting a play container so that its extendable to the right.
# When the node is extendable to left,insert. Then check if node extendable to right, then extend. If node not extendable to right, then append to words, if the word is not extendable anymore.
def ExtendRight(Square,Board,hand,play,Direction,allsuffixes,hands,words,x,y,lefttrail,initial,positions,temppositions):
#insert contains all letters
print("play="+play)
if Square.get_letterTile()!=None and Direction=="Horizontal":
if y<14:
print("Down not empty")
ExtendRight(Board[x][y+1],Board,hand,play+Board[x][y].get_letterTile().get_letter(),Direction,allsuffixes,hands,words,x,y+1,lefttrail,initial,positions,temppositions)
if wordCheckerReversed.dictionary.t.has_subtrie((play+Board[x][y].get_letterTile().get_letter())[::-1])==True and Board[x][y+1].get_letterTile()==None :
allsuffixes.append((play+Board[x][y].get_letterTile().get_letter())[::-1])
hands.append(copy.deepcopy(hand))
temppositions.append([initial,y])
if y==14 and wordCheckerReversed.dictionary.t.has_subtrie((play+Board[x][y].get_letterTile().get_letter())[::-1])==True:
allsuffixes.append((play+Board[x][y].get_letterTile().get_letter())[::-1])
hands.append(copy.deepcopy(hand))
temppositions.append([initial,y])
return
if Square.get_letterTile()!=None and Direction=="Vertical":
if x<14:
print("Right not empty")
ExtendRight(Board[x+1][y],Board,hand,play+Board[x][y].get_letterTile().get_letter(),Direction,allsuffixes,hands,words,x+1,y,lefttrail,initial,positions,temppositions)
if wordCheckerReversed.dictionary.t.has_subtrie((play+Board[x][y].get_letterTile().get_letter())[::-1])==True and Board[x+1][y].get_letterTile()==None:
allsuffixes.append((play+Board[x][y].get_letterTile().get_letter())[::-1])
hands.append(copy.deepcopy(hand))
temppositions.append([initial,x])
return
if x==14 and wordCheckerReversed.dictionary.t.has_subtrie((play+Board[x][y].get_letterTile().get_letter())[::-1])==True:
allsuffixes.append((play+Board[x][y].get_letterTile().get_letter())[::-1])
hands.append(copy.deepcopy(hand))
temppositions.append([initial,y])
return
if (Square.get_letterTile()==None and not hand) :
return
if Direction=="Horizontal" and (Square.get_letterTile()==None and hand): #If extendable from position
if y<14:
for i in range(0,len(hand)):
print(hand)
print("i="+hand[i])
if wordChecker.check_word(play+hand[i]) and Board[x][y+1].get_letterTile()==None:
if lefttrail==None:
words.append(play+hand[i])
positions.append([initial,y])
if wordCheckerReversed.dictionary.t.has_subtrie((play+hand[i])[::-1])==True and Board[x][y+1].get_letterTile()==None and cross_check(hand[i],"Horizontal",Board,x,y):
print("Check Reversed")
temp=hand[i]https://github.com/nicholas-10/Scrabifier/blob/master/graph_construction.py
hand.pop(i)
allsuffixes.append((play+temp)[::-1])
hands.append(copy.deepcopy(hand))
hand.insert(i,temp)
temppositions.append([initial,y])
if (wordChecker.dictionary.t.has_subtrie(play+hand[i])==True or wordCheckerReversed.dictionary.t.has_subtrie((play+hand[i])[::-1])) and cross_check(hand[i],"Horizontal",Board,x,y):
print("Extension entered")
temp=hand[i]
hand.pop(i)
ExtendRight(Board[x][y+1],Board,hand,play+temp,Direction,allsuffixes,hands,words,x,y+1,lefttrail,initial,positions,temppositions)
hand.insert(i,temp)
print("Appended")
if y==14:
for i in range(0,len(hand)):
if wordChecker.check_word(play+hand[i]):
if lefttrail==None:
words.append(play+hand[i])
positions.append([initial,y])
if wordCheckerReversed.dictionary.t.has_subtrie((play+hand[i])[::-1])==True and cross_check(hand[i],"Horizontal",Board,x,y):
print("Check Reversed")
temp=hand[i]
hand.pop(i)
allsuffixes.append((play+temp)[::-1])
hands.append(copy.deepcopy(hand))
hand.insert(i,temp)
temppositions.append([initial,y])
if Direction=="Vertical" and x<15 and (Square.get_letterTile()==None and hand):
if x<14:
for i in range(0,len(hand)):
if wordChecker.check_word(play+hand[i]) and (Board[x+1][y].get_letterTile()==None):
if lefttrail==None:
words.append(play+hand[i])
positions.append([initial,x])
if wordCheckerReversed.dictionary.t.has_subtrie((play+hand[i])[::-1])==True and (Board[x+1][y].get_letterTile()==None or x==14) and cross_check(hand[i],"Vertical",Board,x,y):
print("Check Reversed")
temp=hand[i]
hand.pop(i)
allsuffixes.append((play+temp)[::-1])
hands.append(copy.deepcopy(hand))
hand.insert(i,temp)
temppositions.append([initial,x])
if (wordChecker.dictionary.t.has_subtrie(play+hand[i])==True or wordCheckerReversed.dictionary.t.has_subtrie((play+hand[i])[::-1])) and cross_check(hand[i],"Vertical",Board,x,y):
print("Extension entered")
temp=hand[i]
hand.pop(i)
ExtendRight(Board[x+1][y],Board,hand,play+temp,Direction,allsuffixes,hands,words,x+1,y,lefttrail,initial,positions,temppositions)
hand.insert(i,temp)
print("Appended")
if x==14:
for i in range(0,len(hand)):
if wordChecker.check_word(play+hand[i]):
if lefttrail==None:
words.append(play+hand[i])
positions.append([initial,x])
if wordCheckerReversed.dictionary.t.has_subtrie((play+hand[i])[::-1])==True and cross_check(hand[i],"Vertical",Board,x,y):
print("Check Reversed")
temp=hand[i]
hand.pop(i)
allsuffixes.append((play+temp)[::-1])
hands.append(copy.deepcopy(hand))
hand.insert(i,temp)
temppositions.append([initial,x])
def ExtendLeft(Square,Board,hand,play,Direction,words,x,y,positions,temppositions):
if Direction == "Vertical" and Square.get_letterTile()!=None:
if x>0:
ExtendLeft(Board[x-1][y],Board,hand,play+Board[x][y].get_letterTile().get_letter(),"Vertical",words,x-1,y,positions,temppositions)
print("Left Appended:"+play+Board[x][y].get_letterTile().get_letter())
if wordCheckerReversed.check_word(play+Board[x][y].get_letterTile().get_letter()) and Board[x-1][y].get_letterTile()==None:
words.append((play+Board[x][y].get_letterTile().get_letter())[::-1])
positions.append([x,temppositions[0][1]])
temppositions.pop(0)
print("Reversed True")
if x==0 and wordCheckerReversed.check_word(play+Board[x][y].get_letterTile().get_letter()) :
words.append((play+Board[x][y].get_letterTile().get_letter())[::-1])
positions.append([x,temppositions[0][1]])
temppositions.pop(0)
print("Reversed True")
if Direction == "Horizontal" and Square.get_letterTile()!=None:
if y>0:
ExtendLeft(Board[x][y-1],Board,hand,play+Board[x][y].get_letterTile().get_letter(),"Horizontal",words,x,y-1,positions,temppositions)
if wordCheckerReversed.check_word(play+Board[x][y].get_letterTile().get_letter()) and Board[x][y-1].get_letterTile()==None:
words.append((play+Board[x][y].get_letterTile().get_letter())[::-1])
positions.append([y,temppositions[0][1]])
temppositions.pop(0)
print("Reversed True")
if y==0 and wordCheckerReversed.check_word(play+Board[x][y].get_letterTile().get_letter()):
words.append((play+Board[x][y].get_letterTile().get_letter())[::-1])
positions.append([y,temppositions[0][1]])
temppositions.pop(0)
print("Reversed True")
for m in range(0,len(hand)):
if Direction =="Vertical" and x>0 and Square.get_letterTile()==None and hand:
if x>0:
if wordCheckerReversed.check_word(play+hand[m]) and Board[x-1][y].get_letterTile()==None and cross_check(hand[m],"Vertical",Board,x,y):
words.append((play+hand[m])[::-1])
positions.append([x,temppositions[0][1]])
temppositions.pop(0)
print("Reversed True")
if wordCheckerReversed.dictionary.t.has_subtrie(play+hand[m])==True and cross_check(hand[m],"Vertical",Board,x,y):
temp=hand[m]
hand.pop(m)
ExtendLeft(Board[x-1][y],Board,hand,play+temp,Direction,words,x-1,y,positions,temppositions)
hand.insert(m,temp)
if x==0 and wordCheckerReversed.check_word(play+hand[m]) and cross_check(hand[m],"Vertical",Board,x,y):
words.append((play+hand[m])[::-1])
positions.append([x,temppositions[0][1]])
temppositions.pop(0)
print("Reversed True")
if Direction=="Horizontal" and y>0 and Square.get_letterTile()==None and hand:
print("left play="+play)
print(hand)
print("i="+hand[m])
if y>0:
if wordCheckerReversed.check_word(play+hand[m]) and Board[x][y-1].get_letterTile()==None and wordCheckerReversed.check_word(play+hand[m]):
words.append((play+hand[m])[::-1])
positions.append( [y,temppositions[0][1]])
temppositions.pop(0)
print("Reversed True")
if wordCheckerReversed.dictionary.t.has_subtrie(play+hand[m])==True and cross_check(hand[m],"Horizontal",Board,x,y):
temp=hand[m]
hand.pop(m)
ExtendLeft(Board[x][y-1],Board,hand,play+temp,Direction,words,x,y-1,positions,temppositions)
hand.insert(m,temp)
print("Appended")
if y==0 and wordCheckerReversed.check_word(play+hand[m]) and cross_check(hand[m],"Horizontal",Board,x,y):
words.append((play+hand[m])[::-1])
positions.append([y,temppositions[0][1]])
temppositions.pop(0)
print("Reversed True")
def wordSearch(Square,Board,hand,play,Direction,words,x,y):
lefttrail=None
temppositions=[]
positions=[]
allsuffixes=[]
allhands=[]
for i in range(0,len(hand)):
hand[i]=hand[i].upper()
if Direction=="Vertical":
if Board[x-1][y].get_letterTile()!=None:
lefttrail=1
ExtendRight(Square,Board,hand,play,Direction,allsuffixes,allhands,words,x,y,lefttrail,x,positions,temppositions)
if Direction=="Horizontal":
if Board[x][y-1].get_letterTile()!=None:
lefttrail=1
ExtendRight(Square,Board,hand,play,Direction,allsuffixes,allhands,words,x,y,lefttrail,y,positions,temppositions)
if Direction=="Vertical" and x>0:
for i in range(0,len(allsuffixes)):
play=allsuffixes[i]
hand=allhands[i]
ExtendLeft(Board[x-1][y],Board,hand,play,Direction,words,x-1,y,positions,temppositions)
if Direction=='Horizontal' and y>0:
for i in range(0,len(allsuffixes)):
play=allsuffixes[i]
hand=allhands[i]
ExtendLeft(Board[x][y-1],Board,hand,play,Direction,words,x,y-1,positions,temppositions)
print(words)
print(positions)
def DictionaryGADDAG(filename):
with open (filename,"r") as file:
words=file.readlines()
words=[word.rstrip() for word in words]
file = open("revdictionary.txt","w")
array=[]
for k in words:
GADDAGAdder(k,array)
for i in array:
file.write(i+"\n")
file.close()
def cross_check(Letter,Direction,Board,x,y):
pass
word=Letter
print("Test")
if Direction=="Vertical":
j=1
while(y<14):
if Board[x][y+j].get_letterTile()==None:
break
else:
word=word+Board[x][y+j].get_letterTile().get_letter()
j=j+1
j=1
while(y>0):
if Board[x][y-j].get_letterTile()==None:
break
else:
word=Board[x][y-j].get_letterTile().get_letter()+word
j=j+1
if len(word)<=1:
return True
else:
return wordChecker.check_word(word)
if Direction=="Horizontal":
j=1
while(x<14):
if Board[x+j][y].get_letterTile()==None:
break
else:
word=word+Board[x+j][y].get_letterTile().get_letter()
j=j+1
j=1
while(x>0):
if Board[x-j][y].get_letterTile()==None:
break
else:
word=Board[x-j][y].get_letterTile().get_letter()+word
j=j+1
if len(word)<=1:
return True
else:
return wordChecker.check_word(word)
DictionaryGADDAG("dictionary.txt")
#DictionaryReversal("dictionary.txt")
print((wordChecker.dictionary.t.has_subtrie("AD")))