15-puzzle 中文文档教程

发布于 3年前 浏览 30 项目主页 更新于 3年前

15-puzzle ????

15-puzzle 是一个简单的 15 拼图库,具有许多有用的功能。 ???

Usage

Installation

使用 npm 安装(或 code>yarn 如果你愿意的话)。

npm install 15-puzzle

然后从脚本中的 '15-puzzle' 导入类 RandomPuzzle

import { RandomPuzzle } from '15-puzzle'      // ES Module
const { RandomPuzzle } = require('15-puzzle') // CommonJS

Generating

您可以使用名为 generate() 的静态方法生成随机有效拼图 ?????✨。 要指定尺寸,请为宽度和高度提供一个数字,分别为宽度和高度提供两个数字,或者为 4x4 不提供任何内容。

import { RandomPuzzle } from '15-puzzle'

const foo = RandomPuzzle.generate()      // 4x4
const bar = RandomPuzzle.generate(5)     // 5x5
const baz = RandomPuzzle.generate(6, 7)  // 6x7

请参阅类型定义以了解您可以做什么。 ???

现在(实际上自从 v2.0.2 ????),作为我们使用random-seed来生成拼图,这个功能居然支持! ??? 只需将种子作为第一个参数。 种子必须是字符串,这就是原因

import { RandomPuzzle } from '15-puzzle'

const foo = RandomPuzzle.generate('kazukazu123123')        // 4x4
const bar = RandomPuzzle.generate('kazukazu123123', 5)     // 5x5
const baz = RandomPuzzle.generate('kazukazu123123', 6, 7)  // 6x7

如果您有一个棋子数组,请使用 Puzzle,它允许您创建一个没有 seed 的实例。 请注意,0 表示空方块。

仅供参考,RandomPuzzle 实际上扩展了 Puzzle

import { Puzzle } from '15-puzzle'

const pieces = [ [  1 ,  2 ,  3 ,  4 ]
                 [  5 ,  6 ,  7 ,  8 ]
                 [  9 , 10 , 11 , 12 ]
                 [ 13 , 14 , 15 ,  0 ] ]

const puzzle = new Puzzle(pieces)

Checking

您可以检查拼图是否可以解决、正在解决或完全解决。 ???

import { Puzzle } from '15-puzzle'

const foo = new Puzzle([ [  1 ,  2 ,  3 ,  4 ]
                         [  5 ,  6 ,  7 ,  8 ]
                         [  9 , 10 , 11 , 12 ]
                         [ 13 , 14 , 15 ,  0 ] ])
foo.isSolvable() // true
foo.isSolving() // false
foo.isSolved() // true


const bar = new Puzzle([ [  1 ,  2 ,  3 ,  4 ]
                         [  5 ,  6 ,  7 ,  8 ]
                         [  9 , 10 , 11 , 12 ]
                         [ 13 ,  0 , 14 , 15 ] ])
bar.isSolvable() // true
bar.isSolving() // false
bar.isSolved() // false

bar.tap(2, 3)
bar.isSolving() // true
bar.isSolved() // false

bar.tap(3, 3)
bar.isSolving() // false
bar.isSolved() // true


const baz = new Puzzle([ [  1 ,  2 ,  3 ,  4 ]
                         [  5 ,  6 ,  7 ,  8 ]
                         [  9 , 10 , 11 , 12 ]
                         [ 13 , 15 , 14 ,  0 ] ])
baz.isSolvable() // false

Controlling

使用 Puzzle.prototype.tap(x, y)

import { RandomPuzzle } from '15-puzzle'

const puzzle = RandomPuzzle.generate('kazukazu123123')
console.log(puzzle.toString())
// +--+--+--+--+
// | 5|  | 1|12|
// +--+--+--+--+
// |13|10| 8| 4|
// +--+--+--+--+
// | 3|14| 6| 7|
// +--+--+--+--+
// |11|15| 9| 2|
// +--+--+--+--+

puzzle.tap(0, 0)
console.log(puzzle.toString())
// +--+--+--+--+
// |  | 5| 1|12|
// +--+--+--+--+
// |13|10| 8| 4|
// +--+--+--+--+
// | 3|14| 6| 7|
// +--+--+--+--+
// |11|15| 9| 2|
// +--+--+--+--+

有一个方法叫做 swap它实际上是交换两块,但不推荐使用这种方法,因为手动调用它可能会使拼图变得无法解决。

15-puzzle ????

15-puzzle is a simple 15 puzzle library with many useful features. ????

Usage

Installation

Use npm to install (or yarn if you'd like).

npm install 15-puzzle

Then import the class RandomPuzzle from '15-puzzle' in your script.

import { RandomPuzzle } from '15-puzzle'      // ES Module
const { RandomPuzzle } = require('15-puzzle') // CommonJS

Generating

You can generate a random valid puzzle using a static method called generate() ????✨. To specify the size, give a number for both width and height, two numbers for width and height respectively, or nothing for 4x4.

import { RandomPuzzle } from '15-puzzle'

const foo = RandomPuzzle.generate()      // 4x4
const bar = RandomPuzzle.generate(5)     // 5x5
const baz = RandomPuzzle.generate(6, 7)  // 6x7

See the type definition to learn what you can do. ????

Now (actually since v2.0.2 ????), as we use random-seed to generate a puzzle, this feature is actually supported! ???? Just give a seed as the first argument. A seed must be a string, and here's why.

import { RandomPuzzle } from '15-puzzle'

const foo = RandomPuzzle.generate('kazukazu123123')        // 4x4
const bar = RandomPuzzle.generate('kazukazu123123', 5)     // 5x5
const baz = RandomPuzzle.generate('kazukazu123123', 6, 7)  // 6x7

If you have an array of pieces, instead, use Puzzle which allows you to create an instance without seed. Note that 0 means an empty square.

FYI, RandomPuzzle actually extends Puzzle.

import { Puzzle } from '15-puzzle'

const pieces = [ [  1 ,  2 ,  3 ,  4 ]
                 [  5 ,  6 ,  7 ,  8 ]
                 [  9 , 10 , 11 , 12 ]
                 [ 13 , 14 , 15 ,  0 ] ]

const puzzle = new Puzzle(pieces)

Checking

You can check if the puzzle is solvable, being solved, or completely solved. ????

import { Puzzle } from '15-puzzle'

const foo = new Puzzle([ [  1 ,  2 ,  3 ,  4 ]
                         [  5 ,  6 ,  7 ,  8 ]
                         [  9 , 10 , 11 , 12 ]
                         [ 13 , 14 , 15 ,  0 ] ])
foo.isSolvable() // true
foo.isSolving() // false
foo.isSolved() // true


const bar = new Puzzle([ [  1 ,  2 ,  3 ,  4 ]
                         [  5 ,  6 ,  7 ,  8 ]
                         [  9 , 10 , 11 , 12 ]
                         [ 13 ,  0 , 14 , 15 ] ])
bar.isSolvable() // true
bar.isSolving() // false
bar.isSolved() // false

bar.tap(2, 3)
bar.isSolving() // true
bar.isSolved() // false

bar.tap(3, 3)
bar.isSolving() // false
bar.isSolved() // true


const baz = new Puzzle([ [  1 ,  2 ,  3 ,  4 ]
                         [  5 ,  6 ,  7 ,  8 ]
                         [  9 , 10 , 11 , 12 ]
                         [ 13 , 15 , 14 ,  0 ] ])
baz.isSolvable() // false

Controlling

Use Puzzle.prototype.tap(x, y).

import { RandomPuzzle } from '15-puzzle'

const puzzle = RandomPuzzle.generate('kazukazu123123')
console.log(puzzle.toString())
// +--+--+--+--+
// | 5|  | 1|12|
// +--+--+--+--+
// |13|10| 8| 4|
// +--+--+--+--+
// | 3|14| 6| 7|
// +--+--+--+--+
// |11|15| 9| 2|
// +--+--+--+--+

puzzle.tap(0, 0)
console.log(puzzle.toString())
// +--+--+--+--+
// |  | 5| 1|12|
// +--+--+--+--+
// |13|10| 8| 4|
// +--+--+--+--+
// | 3|14| 6| 7|
// +--+--+--+--+
// |11|15| 9| 2|
// +--+--+--+--+

There's a method called swap which literally swaps two pieces, but using this method is not recommended, because calling this manually might make the puzzle become unsolvable.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文