/ Published in: Java
This code demonstrates the KnightsTour class I developed.
See: http://www.snipplr.com/view/77383/knightstourjava/
See: http://www.snipplr.com/view/77383/knightstourjava/
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
package testknight; import java.util.Scanner; /** * * @author piguy */ public class TestKnight { // Create array called "shape" // Must be rectangular integer array (each row must be same length) to pass to KnightsTourOrig // 1 denotes square onto which knight can be moved // 0 denotes square onto which knight cannot be moved int[][] shape = new int[][]{ { 0, 0, 0, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1, 1, 0 } }; // Take shape and turn it into custom KnightsTour object called board KnightsTour board = new KnightsTour(shape); board.displayBoard(); board.displayAdjMatrix(); // Print a closed Knight's Tour //board.findAClosedKnightsTour(); // Print all closed Knight's Tours //board.findAllClosedKnightsTours(); ///* // Prepare to trap input boolean incorrectInput = true; int startVertex = -1; int vertices = board.getNumOfVertices(); // Trap processing while input is either not an integer or an integer out of range while (incorrectInput) { if (input.hasNextInt()) { startVertex = input.nextInt(); if ((startVertex < 0) || (startVertex >= vertices)) { } else { incorrectInput = false; } } else { input.next(); } } // Print out selected type of Knight's Tours // given starting vertex //board.findAnOpenKnightsTour(startVertex); //board.findAllOpenKnightsTours(startVertex); //board.findAClosedKnightsTour(startVertex); //board.findAllClosedKnightsTours(startVertex); //board.findAllKnightsTours(startVertex); // Find tours with matches to their dates // Inspired by: // http://forums.xkcd.com/viewtopic.php?f=3&t=62580 board.findAllKnightsToursCalendars(startVertex); //*/ } }