https://github.com/sgalal/knights-tour-visualization
An online Knight's tour visualizer using divide and conquer algorithm
https://github.com/sgalal/knights-tour-visualization
algorithm divide-and-conquer emscripten html5 knight-tour knights-tour visualization visualizer
Last synced: 2 months ago
JSON representation
An online Knight's tour visualizer using divide and conquer algorithm
- Host: GitHub
- URL: https://github.com/sgalal/knights-tour-visualization
- Owner: sgalal
- License: mit
- Created: 2018-05-25T02:54:01.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2020-07-09T02:17:07.000Z (almost 5 years ago)
- Last Synced: 2025-04-14T02:26:44.373Z (2 months ago)
- Topics: algorithm, divide-and-conquer, emscripten, html5, knight-tour, knights-tour, visualization, visualizer
- Language: C
- Homepage: https://sgalal.github.io/knights-tour-visualization/
- Size: 716 KB
- Stars: 19
- Watchers: 1
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Knight's Tour Visualization
[](https://ci.appveyor.com/project/chromezh/knights-tour-visualization) [](https://codeclimate.com/github/sgalal/knights-tour-visualization/maintainability)
_An online [Knight's tour](https://en.wikipedia.org/wiki/Knight%27s_tour) visualizer using divide and conquer algorithm_
This project uses [Emscripten](http://kripken.github.io/emscripten-site/) to compile C code into JavaScript.
For an online version, see .

## Project Structure
* Source files: `src`
* For test: `test`
* For web pages: `index.html`, `index.js`, `index.css`## Build
* **Prerequisite**: [Emscripten](http://kripken.github.io/emscripten-site/)
* **Build**: `emcc -std=c11 -Weverything -Werror -Wno-unused-function -Wno-language-extension-token -O2 -s ASSERTIONS=1 -s EXPORTED_FUNCTIONS='["_getNextPointSerialize"]' -s EXTRA_EXPORTED_RUNTIME_METHODS='["ccall","cwrap"]' -o tour.js src/tour.c`
* **Test**: `clang -std=c11 -Weverything -Werror -Wno-language-extension-token -DDEBUG -o tour src/tour.c test/tour_tb.c && ./tour`## Implementation
### Algorithm
The code is an implementation of _Parberry, Ian. "An efficient algorithm for the Knight's tour problem." Discrete Applied Mathematics 73.3(1997):251-260_.
A knight’s tour is called _**closed**_ if the last square visited is also reachable from the first square by a knight’s move.
A knight’s tour is said to be _**structured**_ if it includes the knight’s moves shown in Fig. 1.

An _n_ \* _n_ chessboard has a closed knight's tour iff _n_ ≥ 6 is even.
For board size of 6 \* 6, 6 \* 8, 8 \* 8, 8 \* 10, 10 \* 10, and 10 \* 12, we have already found **structured**, **closed** knight’s tours, which are shown in Fig. 2.

This means the problem is already solved when _n_ ∈ {6, 8, 10}.
For larger _n_, divide the chess board into parts to meet the sizes above. For instance, a board with _n_ = 42 can be divided as follows:

Then connect the parts together. Since all the parts are structured, we can combine them by substitute the directions on the corners:

### Data Structure
Since every point is connected with other two points, and there are only 8 possible directions (from 0 to 7):

The 6 pre-defined tours are stored in 2-dimensional arrays.
There are 8 types of corners (as were shown in Fig. 3.), which are denoted by 0-7, is recorded in _**point attribute**_. Besides, the point attribute of ordinary points is 8.