File size: 5,325 Bytes
38c016b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python3

import sys
import asyncio
from pathlib import Path

# Add project root to path
project_root = Path(__file__).parent.parent  # Go up from test-integration to backend-py
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."""
    
    # Sample words that are known to cause boundary issues
    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)
    
    # Generate a crossword
    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])}")
    
    # Display the grid
    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']}")
    
    # Analyze for boundary violations
    print(f"\nπŸ” Analyzing for boundary violations...")
    
    violations = []
    
    # Check horizontal words
    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:
                    # Word ended - check if it's a valid placed 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 = ""
        
        # Check word at end of row
        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})")
    
    # Check vertical words
    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:
                    # Word ended - check if it's a valid placed 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 = ""
        
        # Check word at end of column
        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})")
    
    # Report results
    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!")