Guest User

Untitled

a guest
Oct 17th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. // Ex 7.22 Knight's Tour
  2. // To see if a knight can move to each space
  3. // on a chess board hitting each space only once
  4.  
  5. import java.util.Arrays;
  6.  
  7. public class KnightsTour2 {
  8.  
  9. //Initialize an 8x8 array to represent the chessboard
  10. public static int[][] chessBoard = new int[8][8];
  11.  
  12.  
  13. public static void main(String[] args) {
  14.  
  15.  
  16. int[][] accessibility = { {2,3,4,4,4,4,3,2},
  17. {3,4,6,6,6,6,4,3},
  18. {4,6,8,8,8,8,6,4},
  19. {4,6,8,8,8,8,6,4},
  20. {4,6,8,8,8,8,6,4},
  21. {4,6,8,8,8,8,6,4},
  22. {3,4,6,6,6,6,4,3},
  23. {2,3,4,4,4,4,3,2} };
  24.  
  25. int[] moveValues = {10,10,10,10,10,10,10,10};
  26.  
  27.  
  28. int currentRow = 0; //Represents the row our knight is on
  29. int currentCol = 0; // column
  30.  
  31. int[] horizontal = { 2,1,-1,-2,-2,-1,1,2 }; //Moves list that operates on the columns
  32. int[] vertical = { -1,-2,-2,-1,1,2,2,1 }; // rows
  33.  
  34. chessBoard[currentRow][currentCol] = 1; // Set starting position to 1
  35.  
  36.  
  37. //Iterates as far as 64 to cover every possible square
  38. for( int counter = 2; counter <= 64; counter++) {
  39.  
  40.  
  41. //Iterates through the 8 possible moves a knight can make
  42. for ( int i = 0; i < 8; i++ ) {
  43.  
  44. currentCol += horizontal[i];
  45. currentRow += vertical[i];
  46.  
  47.  
  48.  
  49. //Checks if move is valid
  50. //If it is valid it finds the value of the target location and stores it in
  51. //moveValues array
  52.  
  53. try{
  54.  
  55. if ( chessBoard[currentRow][currentCol] == 0) {
  56.  
  57.  
  58. moveValues[i] = accessibility[currentRow][currentCol];
  59.  
  60.  
  61.  
  62. }else {
  63. moveValues[i] = 10;
  64.  
  65. //lower the number of possible jumps from the current location
  66. accessibility[currentRow][currentCol] -= 1;
  67. }
  68.  
  69. }
  70. catch ( ArrayIndexOutOfBoundsException e ) {
  71.  
  72. moveValues[i] = 10;
  73. }
  74.  
  75.  
  76. currentCol -= horizontal[i];
  77. currentRow -= vertical[i];
  78.  
  79.  
  80. }//end of i controlled for statement
  81.  
  82.  
  83. //Uses the lowest valued move as our movenumber,then applies that move to the knight
  84. int moveNumber = getIndexOfLowest( moveValues );
  85.  
  86. //System.out.printf("%d", moveNumber);
  87. try {
  88. currentCol += horizontal[ moveNumber ];
  89. currentRow += vertical[ moveNumber ];
  90.  
  91. chessBoard[currentRow][currentCol] = counter;
  92. }
  93. catch ( ArrayIndexOutOfBoundsException e) {
  94. System.out.println("No moves remaining!");
  95. }
  96.  
  97.  
  98.  
  99.  
  100. //fill the moveValues array with 10's before next iteration
  101. Arrays.fill( moveValues, 10);
  102.  
  103.  
  104. //puts the value of counter on the knight's current position
  105. chessBoard[currentRow][currentCol] = counter;
  106.  
  107.  
  108.  
  109. }//end of counter controlled for statement
  110.  
  111.  
  112. System.out.printf("Covered %d squares using this tour!\n",getNumberOfMoves());
  113.  
  114.  
  115. //Displays chessboard with move taken on each square
  116. for ( int[] rows : chessBoard ) {
  117. for ( int column : rows) {
  118. System.out.printf("%2d ",column);
  119. }
  120. System.out.println();
  121. }
  122. }
  123.  
  124.  
  125.  
  126. //Method returns the number of spaces that have been filled by the knight
  127. //by finding the highest value in the array
  128. public static int getNumberOfMoves() {
  129.  
  130. int moves = chessBoard[0][0];
  131.  
  132. for ( int[] rows : chessBoard ) {
  133. for( int testsquare : rows ) {
  134.  
  135. if ( testsquare > moves ) {
  136. moves = testsquare;
  137. }
  138. }
  139. }
  140.  
  141. return moves;
  142. }
  143.  
  144.  
  145. public static int getIndexOfLowest ( int[] arrayValues ) {
  146.  
  147. int[] arrayValuesCopy = new int[ arrayValues.length ];
  148.  
  149. System.arraycopy( arrayValues, 0, arrayValuesCopy, 0, arrayValues.length);
  150.  
  151.  
  152.  
  153. Arrays.sort( arrayValuesCopy );
  154.  
  155. int lowest = arrayValuesCopy[0];
  156.  
  157. System.out.printf("the value of lowest is %d\n", lowest);
  158.  
  159.  
  160. int location = Arrays.binarySearch( arrayValues, lowest );
  161.  
  162. System.out.printf("the value of location is %d\n", location);
  163.  
  164. return location;
  165.  
  166. }
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174. }
Add Comment
Please, Sign In to add comment