Posted By


greymatters on 09/13/14

Tagged


Statistics


Viewed 998 times
Favorited by 0 user(s)

TestKnight.java


/ Published in: Java
Save to your folder(s)

This code demonstrates the KnightsTour class I developed.

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


Copy this code and paste it in your HTML
  1. package testknight;
  2.  
  3. import java.util.Scanner;
  4.  
  5. /**
  6.  *
  7.  * @author piguy
  8.  */
  9. public class TestKnight {
  10.  
  11. public static void main(String[] args) {
  12. // Create array called "shape"
  13. // Must be rectangular integer array (each row must be same length) to pass to KnightsTourOrig
  14. // 1 denotes square onto which knight can be moved
  15. // 0 denotes square onto which knight cannot be moved
  16. int[][] shape = new int[][]{
  17. { 0, 0, 0, 1, 1, 1, 1 },
  18. { 1, 1, 1, 1, 1, 1, 1 },
  19. { 1, 1, 1, 1, 1, 1, 1 },
  20. { 1, 1, 1, 1, 1, 1, 1 },
  21. { 1, 1, 1, 1, 1, 1, 0 }
  22. };
  23. // Take shape and turn it into custom KnightsTour object called board
  24. KnightsTour board = new KnightsTour(shape);
  25.  
  26. System.out.println();
  27.  
  28. board.displayBoard();
  29.  
  30. System.out.println();
  31.  
  32. board.displayAdjMatrix();
  33.  
  34. System.out.println();
  35.  
  36. // Print a closed Knight's Tour
  37. //board.findAClosedKnightsTour();
  38.  
  39. // Print all closed Knight's Tours
  40. //board.findAllClosedKnightsTours();
  41.  
  42. ///*
  43. // Prepare to trap input
  44. Scanner input = new Scanner(System.in);
  45. boolean incorrectInput = true;
  46. int startVertex = -1;
  47. int vertices = board.getNumOfVertices();
  48. System.out.print("At which vertex would you like to start? (0 - " + (vertices - 1) + ")? ");
  49.  
  50. // Trap processing while input is either not an integer or an integer out of range
  51. while (incorrectInput) {
  52. if (input.hasNextInt()) {
  53. startVertex = input.nextInt();
  54. if ((startVertex < 0) || (startVertex >= vertices)) {
  55. System.out.println("Oops! Vertex number must be between 0 and " + (vertices - 1) + ".");
  56. System.out.println();
  57. System.out.print("At which vertex would you like to start? (0 - " + (vertices - 1) + ")? ");
  58. }
  59. else {
  60. incorrectInput = false;
  61. }
  62. }
  63. else {
  64. input.next();
  65. System.out.println("Oops! Answer must be a whole number between 0 and " + (vertices - 1) + ".");
  66. System.out.println();
  67. System.out.print("At which vertex would you like to start? (0 - " + (vertices - 1) + ")? ");
  68. }
  69. }
  70.  
  71. // Print out selected type of Knight's Tours
  72. // given starting vertex
  73. System.out.println();
  74. //board.findAnOpenKnightsTour(startVertex);
  75. //board.findAllOpenKnightsTours(startVertex);
  76. //board.findAClosedKnightsTour(startVertex);
  77. //board.findAllClosedKnightsTours(startVertex);
  78. //board.findAllKnightsTours(startVertex);
  79.  
  80. // Find tours with matches to their dates
  81. // Inspired by:
  82. // http://forums.xkcd.com/viewtopic.php?f=3&t=62580
  83. board.findAllKnightsToursCalendars(startVertex);
  84.  
  85. System.out.println();
  86. System.out.println("Highest number of date-to-move matches: " + board.getMostMatches());
  87. //*/
  88. }
  89. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.