跨字生成器 0.3.5

跨字生成器 0.3.5

测试已测试
语言语言 SwiftSwift
许可 MIT
发布时间最后发布时间2018年9月
SPM支持SPM

Maxim Bilan 维护。



  • 作者
  • Maxim Bilan

跨字生成器

Version License Platform CocoaPods CocoaPods

使用Swift生成的简单横拼谜题的算法。
基于 Python Crossword Puzzle Generator.

安装

CocoaPods

pod 'CrosswordsGenerator'

手动

Copy CrosswordsGenerator.swift, Array2D.swift, ArrayShuffle.swift files to your project.

使用

初始化和横拼谜题生成

let generator = CrosswordsGenerator(columns: 10, rows: 10, maxLoops: 2000, words: ["saffron", "pumpernickel", "leaven", "coda", "paladin", "syncopation", "albatross", "harp", "piston", "caramel", "coral", "dawn", "pitch", "fjord", "lip", "lime", "mist", "plague", "yarn", "snicker"])
generator.generate()

得到结果

let result = crosswordsGenerator.result

结果是一系列结构体

public struct Word {
	public var word = ""
	public var column = 0
	public var row = 0
	public var direction: WordDirection = .Vertical
}

工作示例

--- Words ---
["pumpernickel", "syncopation", "albatross", "saffron", "paladin",
"caramel", "snicker", "leaven", "piston", "plague", "coral", "pitch",
"fjord", "coda", "harp", "dawn", "lime", "mist", "yarn", "lip"]

--- Result ---
pumpernickel-
---a-------e-
---l-------a-
caramel-f--v-
o--d----j--e-
r--i----o--n-
a-snicker----
l----o--dawn-
-----d-------
----harp-----
-------------
-------------
-------------

为了在10次尝试中生成最佳的横拼谜题

let crosswordsGenerator = CrosswordsGenerator()
crosswordsGenerator.words = ["saffron", "pumpernickel", "leaven", "coda", "paladin", "syncopation", "albatross", "harp", "piston", "caramel", "coral", "dawn", "pitch", "fjord", "lip", "lime", "mist", "plague", "yarn", "snicker"]
crosswordsGenerator.columns = 10
crosswordsGenerator.rows = 10
	
var bestResult: Array = Array()
let attempts = 10
		
for var i: Int = 0; i < attempts; ++i {
	crosswordsGenerator.generate()
	let result = crosswordsGenerator.result
			
	if result.count > bestResult.count {
		bestResult.removeAll()
		for word in result {
			bestResult.append(word)
		}
	}
}

为了在60秒内生成最佳的横拼谜题

let crosswordsGenerator = CrosswordsGenerator()
crosswordsGenerator.words = ["saffron", "pumpernickel", "leaven", "coda", "paladin", "syncopation", "albatross", "harp", "piston", "caramel", "coral", "dawn", "pitch", "fjord", "lip", "lime", "mist", "plague", "yarn", "snicker"]
crosswordsGenerator.columns = 10
crosswordsGenerator.rows = 10
		
var bestResult: Array = Array()
let startTime = NSDate()
let timeInterval: NSTimeInterval = 10
		
while (fabs(startTime.timeIntervalSinceNow) < timeInterval) {
	crosswordsGenerator.generate()
	let result = crosswordsGenerator.result
			
	if result.count > bestResult.count {
		bestResult.removeAll()
		for word in result {
			bestResult.append(word)
		}
	}
}

我们还提供了fillAllWords选项。生成横拼谜题后,你可以在没有交叉的地方随机填充单词。例如

let crosswordsGenerator = CrosswordsGenerator(columns: 15, rows: 15, words: ["beijing", "havana", "rome", "paris", "amsterdam"])
crosswordsGenerator.fillAllWords = true
crosswordsGenerator.generate()

amsterdam-b-h--
-----o----e-a--
-----m----i-v--
-----e----j-a--
paris-----i-n--
----------n-a--
----------g----
---------------
---------------

在这个存储库中,你可以找到这个算法的工作示例。随意查看。编程愉快!