定制 ryangaudet/crossword-grid-builder 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ryangaudet/crossword-grid-builder

Composer 安装命令:

composer require ryangaudet/crossword-grid-builder

包简介

A PHP backtracking algorithm that builds crossword puzzle grids from word lists.

README 文档

README

A PHP backtracking algorithm that builds crossword puzzle grids from word lists. Give it words, get back a valid crossword layout.

See it in action at CodeXword — crossword puzzles for people who code.

How It Works

The algorithm uses recursive backtracking with scoring to find optimal word placements:

  1. Words are sorted longest-first (harder to place, so they go early)
  2. The first word is placed horizontally at the center of the grid
  3. For each subsequent word, the algorithm finds all positions where it shares a letter with an already-placed word (intersection)
  4. Candidates are scored by number of intersections and proximity to center
  5. The algorithm recursively tries each candidate, backtracking if it leads to a dead end
  6. If the first ordering fails, it retries with shuffled orderings (up to 10 attempts)
  7. A configurable timeout prevents infinite loops on impossible layouts

Installation

composer require ryangaudet/crossword-grid-builder

Usage

Basic

use RyanGaudet\CrosswordGridBuilder\GridBuilder;

$builder = new GridBuilder();

$words = ['FUNCTION', 'CLASS', 'RETURN', 'WHILE', 'ARRAY', 'STRUCT', 'ENUM', 'GUARD'];
$result = $builder->build($words, 11); // 11x11 grid

if ($result) {
    // $result['grid'] — 2D array of characters ('#' = empty, 'A'-'Z' = letter)
    // $result['placements'] — array of placed words with positions

    foreach ($result['placements'] as $placement) {
        echo "{$placement['word']} at [{$placement['row']}][{$placement['col']}] {$placement['direction']}\n";
    }

    // Print the grid
    echo $builder->toString($result['grid']);
}

Output:

 . . . . . R . S . . .
 . W H I L E . W . . .
 . . . . . T . I . . .
 . . S T R U C T . . .
 . . . U . R . C . . .
 . . . P . N . H . . .
 . . . L . . . . . . .
 . . . E N U M . . . .
 . . . . . . . . . . .

Auto-sized Grid

Let the library calculate the optimal grid size based on your longest word:

$result = $builder->buildAuto($words);

echo "Grid size: {$result['gridSize']}x{$result['gridSize']}\n";
echo "Words placed: " . count($result['placements']) . "/" . count($words) . "\n";

Configuration

// Custom timeout (default 5 seconds) and minimum word count (default 6)
$builder = new GridBuilder(
    timeoutSeconds: 3.0,
    minWords: 4,
);

Grid Sizing

The GridSizer utility calculates grid dimensions based on the longest word:

use RyanGaudet\CrosswordGridBuilder\GridSizer;

$size = GridSizer::fromWords(['FUNCTION', 'CLASS', 'RETURN']);
// Returns 11 (FUNCTION is 8 chars → 11x11 grid)
Longest Word Grid Size
3-5 letters 9x9
6-8 letters 11x11
9-11 letters 13x13
12+ letters 15x15

Placement Data

Each entry in $result['placements'] contains:

[
    'word' => 'FUNCTION',       // The placed word (uppercase)
    'row' => 3,                 // Starting row (0-indexed)
    'col' => 2,                 // Starting column (0-indexed)
    'direction' => 'across',    // 'across' or 'down'
]

Algorithm Details

The placement scoring considers:

  • Intersections (50 pts each) — more shared letters = more connected grid
  • Center proximity (up to 20 pts) — words closer to center look better

Validation rules per cell:

  • A cell can only contain one letter (intersections must match)
  • No parallel adjacency (prevents unintended word formation)
  • No run-on (empty cell or edge required before and after each word)

The algorithm guarantees:

  • All placed words are fully connected (share at least one intersection)
  • No cell conflicts
  • Minimum word count met (configurable, default 6)
  • Completes within the timeout (configurable, default 5s)

Requirements

  • PHP 8.1+

Testing

composer install
composer test

License

MIT License. See LICENSE.

About

This algorithm powers CodeXword — a crossword puzzle platform where every puzzle is built around programming terminology. 9 languages, 72 themes, from Beginner to Advanced.

Built by Ryan Gaudet.

ryangaudet/crossword-grid-builder 适用场景与选型建议

ryangaudet/crossword-grid-builder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「grid」 「Algorithm」 「puzzle」 「crossword」 「backtracking」 「word-game」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 ryangaudet/crossword-grid-builder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ryangaudet/crossword-grid-builder 我们能提供哪些服务?
定制开发 / 二次开发

基于 ryangaudet/crossword-grid-builder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 37
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-04