File size: 7,150 Bytes
88452f7
 
 
 
 
 
 
 
 
 
d9a16d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88452f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d9a16d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88452f7
 
 
d9a16d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
---
title: Crossword Puzzle Generator
emoji: 🧩
colorFrom: blue
colorTo: purple
sdk: docker
pinned: false
license: mit
---

# Crossword Puzzle Generator

A full-stack web application that generates custom crossword puzzles based on selected topics using React frontend and Node.js backend.

## Features

- **Topic Selection**: Choose from various categories (Animals, Science, Geography, Technology)
- **Dynamic Grid Generation**: Automatically creates crossword grids with word intersections
- **Interactive Interface**: Click-to-fill crossword grid with real-time validation
- **Difficulty Levels**: Easy, Medium, and Hard puzzle generation
- **Responsive Design**: Works on desktop and mobile devices

## Tech Stack

### Frontend
- **React 18** with hooks and functional components
- **Vite** for fast development and building
- **CSS Grid** for puzzle layout
- **Vanilla CSS** for styling (no external UI libraries)

### Backend
- **Node.js** with Express.js framework
- **File-based word storage** (JSON files by topic)
- **CORS** enabled for cross-origin requests
- **Rate limiting** for API protection
- **Helmet** for security headers

### Database (Optional)
- **PostgreSQL** schema included for advanced features
- **Migration and seed scripts** provided
- Currently uses JSON files for simplicity

## Project Structure

```
crossword-app/
β”œβ”€β”€ frontend/                 # React frontend application
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ components/      # React components
β”‚   β”‚   β”‚   β”œβ”€β”€ TopicSelector.jsx
β”‚   β”‚   β”‚   β”œβ”€β”€ PuzzleGrid.jsx
β”‚   β”‚   β”‚   β”œβ”€β”€ ClueList.jsx
β”‚   β”‚   β”‚   └── LoadingSpinner.jsx
β”‚   β”‚   β”œβ”€β”€ hooks/           # Custom React hooks
β”‚   β”‚   β”‚   └── useCrossword.js
β”‚   β”‚   β”œβ”€β”€ utils/           # Utility functions
β”‚   β”‚   β”‚   └── gridHelpers.js
β”‚   β”‚   β”œβ”€β”€ styles/          # CSS styling
β”‚   β”‚   β”‚   └── puzzle.css
β”‚   β”‚   β”œβ”€β”€ App.jsx          # Main app component
β”‚   β”‚   └── main.jsx         # React entry point
β”‚   β”œβ”€β”€ public/              # Static assets
β”‚   β”œβ”€β”€ package.json
β”‚   └── vite.config.js
β”œβ”€β”€ backend/                 # Node.js backend API
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ controllers/     # Request handlers
β”‚   β”‚   β”‚   └── puzzleController.js
β”‚   β”‚   β”œβ”€β”€ services/        # Business logic
β”‚   β”‚   β”‚   β”œβ”€β”€ crosswordGenerator.js
β”‚   β”‚   β”‚   └── wordService.js
β”‚   β”‚   β”œβ”€β”€ models/          # Data models
β”‚   β”‚   β”‚   β”œβ”€β”€ Word.js
β”‚   β”‚   β”‚   └── Topic.js
β”‚   β”‚   β”œβ”€β”€ routes/          # API routes
β”‚   β”‚   β”‚   └── api.js
β”‚   β”‚   └── app.js           # Express application
β”‚   β”œβ”€β”€ data/
β”‚   β”‚   └── word-lists/      # JSON word files by topic
β”‚   β”œβ”€β”€ package.json
β”‚   └── .env                 # Environment variables
└── database/                # Database schema (optional)
    β”œβ”€β”€ migrations/
    └── seeds/
```

## Getting Started

### Prerequisites
- Node.js 18+ and npm 9+
- PostgreSQL (optional, for advanced features)

### Installation

1. **Clone the repository**
   ```bash
   git clone <repository-url>
   cd crossword-app
   ```

2. **Install backend dependencies**
   ```bash
   cd backend
   npm install
   ```

3. **Install frontend dependencies**
   ```bash
   cd ../frontend
   npm install
   ```

4. **Configure environment variables**
   ```bash
   cd ../backend
   cp .env.example .env
   # Edit .env with your configuration
   ```

### Development

1. **Start the backend server**
   ```bash
   cd backend
   npm run dev
   ```
   Backend runs on http://localhost:3000

2. **Start the frontend development server**
   ```bash
   cd frontend
   npm run dev
   ```
   Frontend runs on http://localhost:5173

3. **Access the application**
   Open http://localhost:5173 in your browser

### API Endpoints

- `GET /api/topics` - Get available topics
- `POST /api/generate` - Generate a crossword puzzle
- `POST /api/validate` - Validate user answers
- `GET /api/words/:topic` - Get words for a topic (admin)
- `GET /api/health` - Health check

### Example API Usage

**Generate a puzzle:**
```bash
curl -X POST http://localhost:3000/api/generate \
  -H "Content-Type: application/json" \
  -d '{
    "topics": ["animals", "science"],
    "difficulty": "medium"
  }'
```

## Algorithm Details

### Crossword Generation Process

1. **Word Selection**: Picks 6-12 words from selected topics based on difficulty
2. **Grid Sizing**: Automatically calculates optimal grid size
3. **Placement Algorithm**: Uses backtracking to place words with intersections
4. **Validation**: Ensures valid crossword structure with proper letter matching

### Core Algorithm Features

- **Backtracking**: Tries all possible word placements and backtracks on conflicts
- **Intersection Logic**: Words can only cross where letters match
- **Grid Optimization**: Minimizes grid size while maximizing word density
- **Difficulty Scaling**: Adjusts word length and complexity based on difficulty

## Deployment

### Docker (Hugging Face Spaces/Local)

**Build and run locally:**
```bash
# Build the Docker image
docker build -t crossword-app .

# Run the container (production mode)
docker run -p 7860:7860 -e NODE_ENV=production crossword-app

# Or run in background
docker run -d -p 7860:7860 -e NODE_ENV=production --name crossword-app crossword-app
```

**Test the deployment:**
```bash
# Test API endpoint
curl http://localhost:7860/api/topics

# Test frontend (should return HTML)
curl http://localhost:7860/

# View container logs
docker logs crossword-app

# Stop and cleanup
docker stop crossword-app && docker rm crossword-app
```

The app will be available at http://localhost:7860

**For Hugging Face Spaces:**
1. Create a new Space with "Docker" SDK
2. Upload the entire `crossword-app/` directory
3. The container will build automatically and deploy on port 7860

### Frontend (Vercel/Netlify)
```bash
cd frontend
npm run build
# Deploy dist/ folder to your hosting service
```

### Backend (Railway/Heroku)
```bash
cd backend
# Set environment variables in your hosting service
# Deploy with git or Docker
```

### Environment Variables for Production
```bash
NODE_ENV=production
PORT=7860
DATABASE_URL=postgresql://user:pass@host:port/db (optional)
CORS_ORIGIN=https://your-frontend-domain.com (for separate deployments)
```

## Contributing

1. Fork the repository
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request

## Future Enhancements

- [ ] User accounts and saved puzzles
- [ ] Puzzle sharing and social features
- [ ] Advanced word filtering and custom topics
- [ ] Print-friendly puzzle format
- [ ] Mobile app versions
- [ ] Multiplayer crossword solving
- [ ] AI-generated clues

## License

This project is licensed under the MIT License - see the LICENSE file for details.