GithubHelp home page GithubHelp logo

huntit / ios_crosswords_generator Goto Github PK

View Code? Open in Web Editor NEW

This project forked from maximbilan/ios-crosswords-generator

0.0 2.0 0.0 57 KB

Simple algorithm for generating crosswords written on Swift. Based on Python Crossword Puzzle Generator

License: MIT License

Ruby 3.01% Swift 96.99%

ios_crosswords_generator's Introduction

Crosswords Generator

Version License Platform CocoaPods CocoaPods

Simple algorithm for generating crosswords with Swift.
Based on Python Crossword Puzzle Generator.

Installation

CocoaPods:

Swift 2.2:
pod 'CrosswordsGenerator', '~> 0.1.3'

Swift 2.3:
pod 'CrosswordsGenerator', '~> 0.2.3'

Swift 3.0:
pod 'CrosswordsGenerator'

Manual:

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

Using

Initialization and crosswords generation:

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()

To get result:

let result = crosswordsGenerator.result

The result is an array of structures:

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

Sample of working:

--- 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-----
-------------
-------------
-------------

To generate the best crosswords in 10 attempts:

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)
		}
	}
}

To generate the best crosswords in 60 seconds:

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)
		}
	}
}

Also we have option fillAllWords. After generation of crosswords you can fill words to grid in random places that have not intersections. For example:

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----
---------------
---------------

In this repository you can find sample of working of this algorithm. Feel free. Happy coding!

ios_crosswords_generator's People

Contributors

maximbilan avatar readmecritic avatar

Watchers

James Cloos avatar Peter Hunt avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.