Advertisement
nubenugget

Untitled

Feb 9th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Random;
  3.  
  4. /**
  5. * Pedram Namiranian Jan 31, 2016 Period 4 ~1 hours 0 minutes This lab was
  6. * pretty enjoyable except for the fact that for the longest time my code was
  7. * not able to have the night move two to the left and one down, it would
  8. * usually get stuck at 16 steps in the top right corner. I spent about 30
  9. * minutes trying to debug it, I ran through my code multiple times with a sheet
  10. * of paper and a pencil and nothing seemed to work. Eventually I was tired so I
  11. * deleted my "moves" array and I retyped it, and my code works now and I have
  12. * no idea what was going on.
  13. */
  14.  
  15. public class P4_Namiranian_Pedram_KnightsTour2 {
  16. static int row = 0;
  17. static int colum = 0;
  18. static int[][] access = { { 2, 3, 4, 4, 4, 4, 3, 2 }, { 3, 4, 6, 6, 6, 6, 4, 3 }, { 4, 6, 8, 8, 8, 8, 6, 4 },
  19. { 4, 6, 8, 8, 8, 8, 6, 4 }, { 4, 6, 8, 8, 8, 8, 6, 4 }, { 4, 6, 8, 8, 8, 8, 6, 4 },
  20. { 3, 4, 6, 6, 6, 6, 4, 3 }, { 2, 3, 4, 4, 4, 4, 3, 2 } };
  21. static int[][] board = new int[8][8];
  22. static int[][] moves = { { 2, 1 }, { -2, 1 }, { 2, -1 }, { -2, -1 }, { 1, 2 }, { -1, 2 }, { 1, -2 }, { -1, -2 } };
  23. static ArrayList<Integer> possible = new ArrayList<Integer>();
  24.  
  25. public static void main(String[] args) {
  26. int numSteps = 0;
  27. numSteps++;
  28. board[0][0] = numSteps;
  29. while (canMove()) {
  30. move();
  31. numSteps++;
  32. board[row][colum] = numSteps;
  33. }
  34. System.out.println(" 1 2 3 4 5 6 7 8");
  35. for (int i = 0; i < 8; i++) {
  36. System.out.printf("%-4s", i);
  37. for (int j = 0; j < 8; j++) {
  38. System.out.printf("%-4s", board[i][j]);
  39. }
  40. System.out.println();
  41. }
  42. System.out.println();
  43. System.out.println(numSteps + " squares were visited");
  44. }
  45.  
  46. private static void move() {
  47. int minIndex = 0;
  48. Random rand = new Random();
  49. int currentPos = 0;
  50. for (int i = 1; i < possible.size(); i++) {
  51. currentPos = possible.get(i);
  52. if (access[row + moves[currentPos][0]][colum
  53. + moves[currentPos][1]] < access[row + moves[possible.get(minIndex)][0]][colum
  54. + moves[possible.get(minIndex)][1]]) {
  55. minIndex = i;
  56. } else if (access[row + moves[currentPos][0]][colum
  57. + moves[currentPos][1]] == access[row + moves[possible.get(minIndex)][0]][colum
  58. + moves[possible.get(minIndex)][1]]
  59. && !rand.nextBoolean()) {
  60. minIndex = i;
  61. } else {
  62. ;
  63. }
  64. }
  65. int temp = 0;
  66. for (int j = 0; j < possible.size(); j++) {
  67. temp = access[row + moves[possible.get(j)][0]][colum + moves[possible.get(j)][1]];
  68. access[row + moves[possible.get(j)][0]][colum + moves[possible.get(j)][1]] = temp - 1;
  69. }
  70. row += moves[possible.get(minIndex)][0];
  71. colum += moves[possible.get(minIndex)][1];
  72. }
  73.  
  74. private static boolean canMove() {
  75. possible.clear();
  76. for (int i = 0; i < 8; i++) {
  77. if (moves[i][0] + row < 8 && moves[i][0] + row >= 0) {
  78. if (moves[i][1] + colum < 8 && moves[i][1] + colum >= 0) {
  79. if (board[moves[i][0] + row][moves[i][1] + colum] == 0) {
  80. possible.add(i);
  81. }
  82. }
  83. }
  84. }
  85. if (possible.size() > 0) {
  86. return true;
  87. }
  88. return false;
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement