Module 17: Write Words to the Grid (Python code)
This commit is contained in:
parent
e944bd3cbb
commit
8f3a5fa69a
60
a.py
60
a.py
@ -102,7 +102,7 @@ class Library:
|
||||
for line in f:
|
||||
line = ToUpper(line.rstrip())
|
||||
len_w = len(line)
|
||||
if len_w < max_size:
|
||||
if len_w <= max_size:
|
||||
self.words_.word.append(line)
|
||||
self.CreatePatternHash(line)
|
||||
print(f"Read {len(self.words_.word)} words from file '{filename}'")
|
||||
@ -161,6 +161,10 @@ class Grid:
|
||||
assert self.in_bounds(p), p
|
||||
return self.lines[p.row][p.col]
|
||||
|
||||
def write_box(self, p, c):
|
||||
assert self.in_bounds(p), p
|
||||
self.lines[p.row][p.col] = c
|
||||
|
||||
# Returns True if the point p is a '.' "block" in the grid
|
||||
# 'p' must be in bounds
|
||||
def is_block(self, p):
|
||||
@ -190,6 +194,14 @@ class Grid:
|
||||
temp.append(c)
|
||||
return ''.join(temp)
|
||||
|
||||
def WriteString(self, sp, s):
|
||||
len_ = sp.len
|
||||
assert(len(s) == len_)
|
||||
new_str = []
|
||||
for i in range(len_):
|
||||
p = sp.GetPoint(i)
|
||||
self.write_box(p, s[i])
|
||||
|
||||
# Next increments the point across the grid, one box at a time
|
||||
# Returns True if point is still in bounds
|
||||
def Next(self, p, vert):
|
||||
@ -293,9 +305,14 @@ class Solver:
|
||||
def Solve(self, grid):
|
||||
print(f"Solving this grid")
|
||||
grid.Print()
|
||||
self.Loop(grid)
|
||||
self.Loop(grid, 0)
|
||||
|
||||
def Loop(self, grid, depth):
|
||||
depth += 1
|
||||
#if depth > 3:
|
||||
#print(f"Aborting loop because depth={depth}")
|
||||
#return
|
||||
|
||||
def Loop(self, grid):
|
||||
empty_slots = []
|
||||
partial_slots = [] # these are the ones we want to work on
|
||||
full_slots = []
|
||||
@ -313,28 +330,41 @@ class Solver:
|
||||
num_empty = len(empty_slots)
|
||||
num_partial = len(partial_slots)
|
||||
num_full = len(full_slots)
|
||||
print(f"empty = {num_empty}")
|
||||
print(f"partial = {num_partial}")
|
||||
print(f"full = {num_full}")
|
||||
# print(f"empty = {num_empty}")
|
||||
# print(f"partial = {num_partial}")
|
||||
# print(f"full = {num_full}")
|
||||
|
||||
# need to check that all words so far are valid!
|
||||
for slot in full_slots:
|
||||
#print(f"CHECKING {slot.pattern} if it is a word")
|
||||
if not lib.IsWord(slot.pattern):
|
||||
#print(" --> NOT! ABORT")
|
||||
return
|
||||
|
||||
if (num_partial == 0 and num_empty == 0):
|
||||
print("SOLUTION!!")
|
||||
# FIX what do we do here?
|
||||
grid.Print()
|
||||
return
|
||||
|
||||
assert(num_partial > 0)
|
||||
self.CommitSlot(grid, partial_slots[0])
|
||||
self.CommitSlot(grid, partial_slots[0], depth)
|
||||
|
||||
def CommitSlot(self, grid, slot):
|
||||
def CommitSlot(self, grid, slot, depth):
|
||||
grid = deepcopy(grid)
|
||||
print(f"COMMIT slot {slot}")
|
||||
print(f"Possible word choices for this slot are:")
|
||||
# print(f"COMMIT slot {slot}")
|
||||
# print(f"Possible word choices for this slot are:")
|
||||
|
||||
words = lib.FindWord(slot.pattern)
|
||||
if words:
|
||||
print(f"{' '.join(words)}")
|
||||
else:
|
||||
print("NO MATCHES to pattern")
|
||||
|
||||
for w in words:
|
||||
#print(f"{' '.join(words)}")
|
||||
#print(f" Committing '{w}'!")
|
||||
grid.WriteString(slot.span, w)
|
||||
#print("New grid is:")
|
||||
#grid.Print()
|
||||
self.Loop(grid, depth)
|
||||
#else:
|
||||
#print("NO MATCHES to pattern")
|
||||
## ----------------------------------------------------------------------------
|
||||
if __name__ == "__main__":
|
||||
grid = Grid("MY GRID")
|
||||
|
Loading…
x
Reference in New Issue
Block a user