|
|
|
|
|
""" |
|
|
Test to identify the word extension bug where words appear to extend beyond their boundaries. |
|
|
""" |
|
|
|
|
|
import sys |
|
|
from pathlib import Path |
|
|
|
|
|
|
|
|
project_root = Path(__file__).parent.parent |
|
|
sys.path.insert(0, str(project_root)) |
|
|
|
|
|
from src.services.crossword_generator_fixed import CrosswordGeneratorFixed |
|
|
|
|
|
def debug_word_extension_issue(): |
|
|
"""Debug the specific word extension issue.""" |
|
|
|
|
|
print("🔍 Debugging Word Extension Issue\n") |
|
|
|
|
|
generator = CrosswordGeneratorFixed(vector_service=None) |
|
|
|
|
|
|
|
|
grid = [["." for _ in range(10)] for _ in range(10)] |
|
|
placed_words = [] |
|
|
|
|
|
print("Step-by-step placement test:") |
|
|
print("=" * 50) |
|
|
|
|
|
|
|
|
print("1. Placing COMPUTER at (4, 0) horizontal") |
|
|
if generator._can_place_word(grid, "COMPUTER", 4, 0, "horizontal"): |
|
|
generator._place_word(grid, "COMPUTER", 4, 0, "horizontal") |
|
|
placed_words.append({ |
|
|
"word": "COMPUTER", "row": 4, "col": 0, "direction": "horizontal", "number": 1 |
|
|
}) |
|
|
print("✅ COMPUTER placed successfully") |
|
|
print_grid_section(grid, 2, 7, 0, 10) |
|
|
|
|
|
|
|
|
print("\n2. Placing MACHINE at (2, 0) vertical") |
|
|
if generator._can_place_word(grid, "MACHINE", 2, 0, "vertical"): |
|
|
generator._place_word(grid, "MACHINE", 2, 0, "vertical") |
|
|
placed_words.append({ |
|
|
"word": "MACHINE", "row": 2, "col": 0, "direction": "vertical", "number": 2 |
|
|
}) |
|
|
print("✅ MACHINE placed successfully") |
|
|
print_grid_section(grid, 2, 9, 0, 10) |
|
|
|
|
|
|
|
|
print("\n3. Testing EXPERT placement at various positions:") |
|
|
|
|
|
test_positions = [ |
|
|
(7, 3, "horizontal"), |
|
|
(7, 2, "horizontal"), |
|
|
(6, 3, "horizontal"), |
|
|
] |
|
|
|
|
|
for row, col, direction in test_positions: |
|
|
print(f" Testing EXPERT at ({row}, {col}) {direction}") |
|
|
|
|
|
can_place = generator._can_place_word(grid, "EXPERT", row, col, direction) |
|
|
print(f" Can place: {can_place}") |
|
|
|
|
|
if can_place: |
|
|
|
|
|
test_grid = [r[:] for r in grid] |
|
|
original_state = generator._place_word(test_grid, "EXPERT", row, col, direction) |
|
|
|
|
|
print(" Grid after placement:") |
|
|
print_grid_section(test_grid, 5, 10, 0, 10) |
|
|
|
|
|
|
|
|
sequences = find_all_sequences(test_grid) |
|
|
print(f" All sequences found: {sequences}") |
|
|
|
|
|
|
|
|
generator._remove_word(test_grid, original_state) |
|
|
print(" " + "-" * 30) |
|
|
|
|
|
|
|
|
print("\n4. Testing validation functions:") |
|
|
print("=" * 50) |
|
|
|
|
|
|
|
|
test_grid = [["." for _ in range(10)] for _ in range(10)] |
|
|
|
|
|
|
|
|
for i, letter in enumerate("COMPUTER"): |
|
|
test_grid[4][i] = letter |
|
|
|
|
|
|
|
|
for i, letter in enumerate("MACHINE"): |
|
|
test_grid[2 + i][0] = letter |
|
|
|
|
|
|
|
|
expert_letters = "AEXPERT" |
|
|
for i, letter in enumerate(expert_letters): |
|
|
test_grid[7][2 + i] = letter |
|
|
|
|
|
print("Test grid with extension issue:") |
|
|
print_grid_section(test_grid, 2, 9, 0, 10) |
|
|
|
|
|
|
|
|
mock_placed_words = [ |
|
|
{"word": "COMPUTER", "row": 4, "col": 0, "direction": "horizontal"}, |
|
|
{"word": "MACHINE", "row": 2, "col": 0, "direction": "vertical"}, |
|
|
{"word": "EXPERT", "row": 7, "col": 3, "direction": "horizontal"}, |
|
|
] |
|
|
|
|
|
print("\nValidation results:") |
|
|
is_valid = generator._validate_final_grid(test_grid, mock_placed_words) |
|
|
print(f"Grid validation result: {is_valid}") |
|
|
|
|
|
def print_grid_section(grid, start_row, end_row, start_col, end_col): |
|
|
"""Print a section of the grid.""" |
|
|
print("Grid:") |
|
|
for r in range(start_row, min(end_row, len(grid))): |
|
|
row_str = f"R{r}: " |
|
|
for c in range(start_col, min(end_col, len(grid[0]))): |
|
|
if grid[r][c] == ".": |
|
|
row_str += ". " |
|
|
else: |
|
|
row_str += f"{grid[r][c]} " |
|
|
print(row_str) |
|
|
|
|
|
def find_all_sequences(grid): |
|
|
"""Find all letter sequences in the grid.""" |
|
|
sequences = [] |
|
|
|
|
|
|
|
|
for r in range(len(grid)): |
|
|
current_seq = "" |
|
|
start_col = None |
|
|
|
|
|
for c in range(len(grid[0])): |
|
|
if grid[r][c] != ".": |
|
|
if start_col is None: |
|
|
start_col = c |
|
|
current_seq += grid[r][c] |
|
|
else: |
|
|
if current_seq and len(current_seq) > 1: |
|
|
sequences.append(f"H({r},{start_col}): {current_seq}") |
|
|
current_seq = "" |
|
|
start_col = None |
|
|
|
|
|
|
|
|
if current_seq and len(current_seq) > 1: |
|
|
sequences.append(f"H({r},{start_col}): {current_seq}") |
|
|
|
|
|
|
|
|
for c in range(len(grid[0])): |
|
|
current_seq = "" |
|
|
start_row = None |
|
|
|
|
|
for r in range(len(grid)): |
|
|
if grid[r][c] != ".": |
|
|
if start_row is None: |
|
|
start_row = r |
|
|
current_seq += grid[r][c] |
|
|
else: |
|
|
if current_seq and len(current_seq) > 1: |
|
|
sequences.append(f"V({start_row},{c}): {current_seq}") |
|
|
current_seq = "" |
|
|
start_row = None |
|
|
|
|
|
|
|
|
if current_seq and len(current_seq) > 1: |
|
|
sequences.append(f"V({start_row},{c}): {current_seq}") |
|
|
|
|
|
return sequences |
|
|
|
|
|
if __name__ == "__main__": |
|
|
debug_word_extension_issue() |