|
|
|
|
|
|
|
|
import sys |
|
|
import asyncio |
|
|
from pathlib import Path |
|
|
|
|
|
|
|
|
project_root = Path(__file__).parent.parent |
|
|
sys.path.insert(0, str(project_root)) |
|
|
|
|
|
from src.services.crossword_generator import CrosswordGenerator |
|
|
|
|
|
async def test_boundary_fix(): |
|
|
"""Test that the boundary fix works correctly.""" |
|
|
|
|
|
|
|
|
test_words = [ |
|
|
{"word": "COMPUTER", "clue": "Electronic device"}, |
|
|
{"word": "MACHINE", "clue": "Device with moving parts"}, |
|
|
{"word": "SCIENCE", "clue": "Systematic study"}, |
|
|
{"word": "EXPERT", "clue": "Specialist"}, |
|
|
{"word": "CODE", "clue": "Programming text"}, |
|
|
{"word": "DATA", "clue": "Information"} |
|
|
] |
|
|
|
|
|
generator = CrosswordGenerator() |
|
|
|
|
|
print("π§ͺ Testing Boundary Fix") |
|
|
print("=" * 50) |
|
|
|
|
|
|
|
|
result = generator._create_grid(test_words) |
|
|
|
|
|
if not result: |
|
|
print("β Grid generation failed") |
|
|
return False |
|
|
|
|
|
grid = result["grid"] |
|
|
placed_words = result["placed_words"] |
|
|
|
|
|
print(f"β
Generated grid with {len(placed_words)} words") |
|
|
print(f"Grid size: {len(grid)}x{len(grid[0])}") |
|
|
|
|
|
|
|
|
print("\nGenerated Grid:") |
|
|
for i, row in enumerate(grid): |
|
|
row_str = " ".join(cell if cell != "." else " " for cell in row) |
|
|
print(f"{i:2d} | {row_str}") |
|
|
|
|
|
print(f"\nPlaced Words:") |
|
|
for word in placed_words: |
|
|
print(f" {word['word']} at ({word['row']},{word['col']}) {word['direction']}") |
|
|
|
|
|
|
|
|
print(f"\nπ Analyzing for boundary violations...") |
|
|
|
|
|
violations = [] |
|
|
|
|
|
|
|
|
for r in range(len(grid)): |
|
|
current_word = "" |
|
|
word_start = -1 |
|
|
|
|
|
for c in range(len(grid[r])): |
|
|
if grid[r][c] != ".": |
|
|
if current_word == "": |
|
|
word_start = c |
|
|
current_word += grid[r][c] |
|
|
else: |
|
|
if current_word: |
|
|
|
|
|
is_valid_word = any( |
|
|
placed['word'] == current_word and |
|
|
placed['row'] == r and |
|
|
placed['col'] == word_start and |
|
|
placed['direction'] == 'horizontal' |
|
|
for placed in placed_words |
|
|
) |
|
|
if not is_valid_word and len(current_word) > 1: |
|
|
violations.append(f"Invalid horizontal word '{current_word}' at ({r},{word_start})") |
|
|
current_word = "" |
|
|
|
|
|
|
|
|
if current_word: |
|
|
is_valid_word = any( |
|
|
placed['word'] == current_word and |
|
|
placed['row'] == r and |
|
|
placed['col'] == word_start and |
|
|
placed['direction'] == 'horizontal' |
|
|
for placed in placed_words |
|
|
) |
|
|
if not is_valid_word and len(current_word) > 1: |
|
|
violations.append(f"Invalid horizontal word '{current_word}' at ({r},{word_start})") |
|
|
|
|
|
|
|
|
for c in range(len(grid[0])): |
|
|
current_word = "" |
|
|
word_start = -1 |
|
|
|
|
|
for r in range(len(grid)): |
|
|
if grid[r][c] != ".": |
|
|
if current_word == "": |
|
|
word_start = r |
|
|
current_word += grid[r][c] |
|
|
else: |
|
|
if current_word: |
|
|
|
|
|
is_valid_word = any( |
|
|
placed['word'] == current_word and |
|
|
placed['row'] == word_start and |
|
|
placed['col'] == c and |
|
|
placed['direction'] == 'vertical' |
|
|
for placed in placed_words |
|
|
) |
|
|
if not is_valid_word and len(current_word) > 1: |
|
|
violations.append(f"Invalid vertical word '{current_word}' at ({word_start},{c})") |
|
|
current_word = "" |
|
|
|
|
|
|
|
|
if current_word: |
|
|
is_valid_word = any( |
|
|
placed['word'] == current_word and |
|
|
placed['row'] == word_start and |
|
|
placed['col'] == c and |
|
|
placed['direction'] == 'vertical' |
|
|
for placed in placed_words |
|
|
) |
|
|
if not is_valid_word and len(current_word) > 1: |
|
|
violations.append(f"Invalid vertical word '{current_word}' at ({word_start},{c})") |
|
|
|
|
|
|
|
|
if violations: |
|
|
print(f"β Found {len(violations)} boundary violations:") |
|
|
for violation in violations: |
|
|
print(f" - {violation}") |
|
|
return False |
|
|
else: |
|
|
print(f"β
No boundary violations found!") |
|
|
print(f"β
All words in grid are properly placed and bounded") |
|
|
return True |
|
|
|
|
|
if __name__ == "__main__": |
|
|
success = asyncio.run(test_boundary_fix()) |
|
|
if success: |
|
|
print(f"\nπ Boundary fix is working correctly!") |
|
|
else: |
|
|
print(f"\nπ₯ Boundary fix needs more work!") |