-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboggle.py
106 lines (93 loc) · 3.93 KB
/
boggle.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
# author : Sai Shanmukha Narumanchi
import random
dim=4
scoreDict = {3: 1, 4: 1, 5: 2, 6: 3, 7: 5}
def populateWordCollection():
wordCollection = []
with open("words.txt") as f:
return [line.strip().upper() for line in f]
def isAdjacent(pos,word,boggleTray,visited):
if(len(word)==0):
return True
x,y=pos
if((x not in range(dim) or y not in range(dim))):
return False
for i in range(x-1,x+2):
for j in range(y-1,y+2):
if((x==i and y==j) or (i not in range(dim) or j not in range(dim))):
continue
if(word[0]==boggleTray[i][j] or word[0:2] == boggleTray[i][j] and not visited[i][j]):
visited[i][j]=True
# print(boggleTray[i][j],i,j)
nextWord = word[1:] if boggleTray[i][j]!='QU' else word[2:]
if(isAdjacent((i,j), nextWord,boggleTray, visited)):
return True
else:
visited[i][j]=False
# print(boggleTray[i][j],i,j,' X')
return False
def isInGrid(word,boggleTray):
visited=[[False for i in range(dim)] for j in range(dim)]
for i in range(dim):
for j in range(dim):
if(boggleTray[i][j]==word[0] or word[0:2] == boggleTray[i][j]):
# print(boggleTray[i][j],i,j)
visited[i][j]=True
nextWord = word[1:] if boggleTray[i][j]!='QU' else word[2:]
if(isAdjacent((i,j), nextWord,boggleTray, visited)):
return True
else:
visited[i][j]=False
# print(boggleTray[i][j],i,j,' X')
return False
def computeScore(word,boggleTray):
if(word not in wordCollection):
return -1
if(len(word)<3):
return 0
if(isInGrid(word,boggleTray)):
if(len(word)>7):
return 11
return scoreDict[len(word)]
def printOutput(word,score):
if score is None:
print('The word '+word+' is not present in the grid.')
elif (score<0):
print('The word '+word+' is not a word.')
elif(score<1):
print('The word '+word+' is too short.')
elif(score<2):
print('The word '+word+' is worth 1 point.')
else:
print('The word '+word+' is worth '+str(score)+' points.')
def getRandomBoggleTray(diceCombination):
return [[diceCombination[i+j][random.randint(0,len(diceCombination[0]))] for i in range(dim)] for j in range(dim)]
def printBoggleTray(boggleTray):
for i in range(dim):
for j in range(dim):
print('['+boggleTray[i][j] + '] ',end='')
print()
def readInput():
wordList=[]
while True:
word = input().upper()
if(word=='X'):
break
wordList.append(word)
return wordList
if __name__ == "__main__":
wordCollection = populateWordCollection()
diceCombination = [['A', 'E', 'A', 'N', 'E', 'G'],['A', 'H', 'S', 'P', 'C', 'O'],['A', 'S', 'P', 'F', 'F', 'K'], ['O', 'B', 'J', 'O', 'A', 'B'], ['I', 'O', 'T', 'M', 'U', 'C'], ['R', 'Y', 'V', 'D', 'E', 'L'], ['L', 'R', 'E', 'I', 'X', 'D'], ['E', 'I', 'U', 'N', 'E', 'S'],['W', 'N', 'G', 'E', 'E', 'H'],['L', 'N', 'H', 'N', 'R', 'Z'], ['T', 'S', 'T', 'I', 'Y', 'D'],['O', 'W', 'T', 'O', 'A', 'T'],['E', 'R', 'T', 'T', 'Y', 'L'],['T', 'O', 'E', 'S', 'S', 'I'],['T', 'E', 'R', 'W', 'H', 'V'],['N', 'U', 'I', 'H', 'M', 'QU']]
boggleTray = getRandomBoggleTray(diceCombination)
printBoggleTray(boggleTray)
print("Start typing your words! (press enter after each word and enter 'X' when done):")
wordList = readInput()
totalScore=0
for i,word in enumerate(wordList):
if(word in wordList[:i]):
print('The word '+word+' has already been used.')
continue
score = computeScore(word,boggleTray)
totalScore+=score if score is not None and score > 0 else 0
printOutput(word,score)
print('Your total score is '+str(totalScore)+' points!')