-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cell.py
133 lines (117 loc) · 3.61 KB
/
Cell.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
from termcolor import colored
class Path(object):
def __init__(self):
self.path = []
self.wordMult = 1
self.points = 0
def getPoints(self):
return self.wordMult * self.points + len(self.path)
class MatchResult(object):
def __init__(self):
self.isMatching = False
self.paths = []
def badResult():
result = MatchResult()
result.isMatching = False
return result
def goodResult():
result = MatchResult()
result.isMatching = True
return result
def createPath(cellId, points, wMult):
result = Path()
updatePath(result, cellId, points, wMult)
return result
def updatePath(path, cellId, points, wMult):
path.path.insert(0, cellId)
path.wordMult = path.wordMult * wMult
path.points = path.points + points
return path
class Cell:
def __init__(self, id):
self.m_value = ''
self.m_paths = []
self.id = id
self.wordMult = 1
self.letterMult = 1
self.isRoot = False
def getPoints(self, l):
points = 0
if self.isRoot:
return 0
if l in ['A', 'E', 'I', 'N', 'R', 'S', 'T']:
points = 1
if l in ['C', 'L', 'M', 'O', 'U']:
points = 2
if l in ['D', 'P']:
points = 3
if l in ['B', 'F', 'G', 'H', 'V', 'Z']:
points = 4
if l in ['Q', 'Y']:
points = 6
if l in ['J', 'X']:
points = 8
if l in ['K', 'W']:
points = 10
if points == 0:
raise Exception("Bad string")
return points * self.letterMult
def setValue(self, value):
self.m_value = value
def setBonus(self, bonus):
if bonus == 'dl':
self.letterMult = 2
if bonus == 'tl':
self.letterMult = 3
if bonus == 'dw':
self.wordMult = 2
if bonus == 'tw':
self.wordMult = 3
def getValue(self):
return self.m_value
def addPath(self, cell):
self.m_paths.append(cell)
def match(self, params):
idx = params['idx']
word = params['word']
l = ''
if idx > -1:
l = word[idx]
# print('Word = ' + word + ' idx =' + str(idx) + " Is Root : " + str(self.isRoot) + " . l = " + l + " . v= " + self.getValue())
if self.isRoot == False and l != self.getValue():
return badResult()
result = goodResult()
if idx == len(word) - 1:
path = createPath(self.id, self.getPoints(l), self.wordMult)
result.paths.append(path)
return result
paths = []
for cell in self.m_paths:
params = {"word": word, "idx": idx+1 }
res = cell.match(params)
if res.isMatching == False:
continue
for path in res.paths:
if self.id in path.path:
continue
if self.isRoot == False:
updatePath(path, self.id, self.getPoints(l), self.wordMult)
paths.append(path)
if len(paths) == 0:
return badResult()
result.paths = paths
return result
def getDisplay(self):
color = 'white'
fmting = []
if self.letterMult == 2:
color = 'light_blue'
if self.letterMult == 3:
color = 'magenta'
if self.wordMult == 2:
color = 'red'
fmting.append("bold")
if self.wordMult == 3:
color = 'yellow'
fmting.append("bold")
return colored(self.getValue(), color, attrs=fmting)