Return to Snippet

Revision: 67347
at September 14, 2014 13:02 by greymatters


Updated Code
package testknight;

import java.util.Scanner;

/**
 *
 * @author piguy
 */
public class TestKnight {

    public static void main(String[] args) {
        // 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);
        
        System.out.println();
        
        board.displayBoard();
        
        System.out.println();
        
        board.displayAdjMatrix();
        
        System.out.println();
        
        // Print a closed Knight's Tour
        //board.findAClosedKnightsTour();
        
        // Print all closed Knight's Tours
        //board.findAllClosedKnightsTours();
        
        ///*
        // Prepare to trap input
        Scanner input = new Scanner(System.in);
        boolean incorrectInput = true;
        int startVertex = -1;
        int vertices = board.getNumOfVertices();
        System.out.print("At which vertex would you like to start? (0 - " + (vertices - 1) + ")? ");
        
        // 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)) {
                    System.out.println("Oops! Vertex number must be between 0 and " + (vertices - 1) + ".");
                    System.out.println();
                    System.out.print("At which vertex would you like to start? (0 - " + (vertices - 1) + ")? ");
                }
                else {
                    incorrectInput = false;
                }
            }
            else {
                input.next();
                System.out.println("Oops! Answer must be a whole number between 0 and " + (vertices - 1) + ".");
                System.out.println();
                System.out.print("At which vertex would you like to start? (0 - " + (vertices - 1) + ")? ");
            }
        }
        
        // Print out selected type of Knight's Tours
        // given starting vertex
        System.out.println();
        //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);
        
        System.out.println();
        System.out.println("Highest number of date-to-move matches: " + board.getMostMatches());
        //*/
    }
}

Revision: 67346
at September 13, 2014 08:36 by greymatters


Initial Code
package testknight;

import java.util.Scanner;

/**
 *
 * @author piguy
 */
public class TestKnight {

    public static void main(String[] args) {
        // 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, 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, 0, 0, 0 }
        };
        // Take shape and turn it into custom KnightsTour object called board
        KnightsTour board = new KnightsTour(shape);
        
        System.out.println();
        
        board.displayBoard();
        
        System.out.println();
        
        board.displayAdjMatrix();
        
        System.out.println();
        
        // Print a closed Knight's Tour
        //board.findAClosedKnightsTour();
        
        // Print all closed Knight's Tours
        //board.findAllClosedKnightsTours();
        
        ///*
        // Prepare to trap input
        Scanner input = new Scanner(System.in);
        boolean incorrectInput = true;
        int startVertex = -1;
        int vertices = board.getNumOfVertices();
        System.out.print("At which vertex would you like to start? (0 - " + (vertices - 1) + ")? ");
        
        // 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)) {
                    System.out.println("Oops! Vertex number must be between 0 and " + (vertices - 1) + ".");
                    System.out.println();
                    System.out.print("At which vertex would you like to start? (0 - " + (vertices - 1) + ")? ");
                }
                else {
                    incorrectInput = false;
                }
            }
            else {
                input.next();
                System.out.println("Oops! Answer must be a whole number between 0 and " + (vertices - 1) + ".");
                System.out.println();
                System.out.print("At which vertex would you like to start? (0 - " + (vertices - 1) + ")? ");
            }
        }
        
        // Print out selected type of Knight's Tours
        // given starting vertex
        System.out.println();
        //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);
        
        System.out.println();
        System.out.println("Highest number of date-to-move matches: " + board.getMostMatches());
        //*/
    }
}

Initial URL


Initial Description
This code demonstrates the KnightsTour class I developed.

See: http://www.snipplr.com/view/77383/knightstourjava/

Initial Title
TestKnight.java

Initial Tags
path

Initial Language
Java