GithubHelp home page GithubHelp logo

tcyang-md / knight-board Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 12 KB

Knight's tour implemented in java using stacks and backtracking to find a single solution for 3x3 - 8x8 sized boards

Java 100.00%
backtracking dfs java knight-tour stacks

knight-board's Introduction

Knight's Tour โ™Ÿ๏ธ

The knight's tour is a sequence of moves of a knight on a chessboard such that the knight visits every square once. If the knight is able to visit every square and ends up on the beginning square as its last move, this is a complete knight tour.

My program written in Java and implements Stacks and Backtracking in order to find a solution for boards between 3x3 and 8x8.

Instructions

Download both KnightTour.java and KnightBoard.java. The knight tour can be run by calling $ java KnightTour n where n is an n x n size board.

$ java KnightTour 5
Possible Knight Tour with max #squares visited in this (5x5) board:
+--+--+--+--+--+
| 1|10| 5|16|25|
+--+--+--+--+--+
| 4|17| 2|11| 6|
+--+--+--+--+--+
| 9|20|13|24|15|
+--+--+--+--+--+
|18| 3|22| 7|12|
+--+--+--+--+--+
|21| 8|19|14|23|
+--+--+--+--+--+

(Time to find this solution = 70.3757 milliseconds)

Breakdown

  • KnightBoard: Chessboard class is a 2D array that has n size, keeps track of the number of moves in moveCount, and current position of the knight in currentX and currentY. There are also some helper methods such as printChessBoard, copyBoard, and move (checks if a knights move to a specific position is valid). There are also setters and getters to modify moveCount, currentX, and currentY positions.
  • KnightTour: Implements and prints a valid knight tour. This does not print all valid knight tours as the valid number of tours for a 6x6 board is 6,637,920 and the it only gets exponentially larger. The program essentially has all 8 moves of a knight listed out in two separate arrays possibleRow and possibleCol. It only adds valid moves from the current position to the stack. This for every single space that the knight can occupy until a board is found where currentBoard.getMoveCount() == n * n or if n == 3 || n == 4 there is no solution where every space is visited so a solution with all but one space visited is valid.

Solution Times

Size Time in ms
3x3 0.771277
4x4 10.917376
5x5 55.889333
6x6 236.791035
7x7 90.314049
8x8 94947.098818

Visualization of the Stack

$ java KnightTour 3
Stack: Round 1
+--+--+--+
| 1|  |  |
+--+--+--+
|  |  |  |
+--+--+--+
|  |  |  |
+--+--+--+
Stack: Round 2 (two more possible moves are added to the stack)
+--+--+--+      +--+--+--+
| 1|  |  |      | 1|  |  |
+--+--+--+      +--+--+--+
|  |  | 2|      |  |  |  |
+--+--+--+      +--+--+--+
|  |  |  |      |  | 2|  |
+--+--+--+      +--+--+--+
Stack: Round 3 (explores all possible moves from the last board added to the stack)
+--+--+--+
| 1|  |  |
+--+--+--+
|  |  | 2|
+--+--+--+
| 3|  |  |
+--+--+--+
Stack: Round 4
+--+--+--+
| 1| 4|  |
+--+--+--+
|  |  | 2|
+--+--+--+
| 3|  |  |
+--+--+--+
Stack: Round 5
...
Stack: Round 6
...
Stack: Round 7
...
Stack: Round 8
+--+--+--+
| 1| 4| 7|
+--+--+--+
| 6|  | 2|
+--+--+--+
| 3| 8| 5|
+--+--+--+

knight-board's People

Contributors

tcyang-md avatar

Watchers

 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.