Advertisement
Guest User

April knights tour 1 code

a guest
Jan 29th, 2015
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class KnightsTour1AYePeriod4 {
  4. private static final int row = 8;
  5. private static final int col = 8;
  6. private int count = 0;
  7. private int currentPosX = 1;
  8. private int currentPosY = 1;
  9. private int horizontal[] = { 1, 2, 2, 1, -1, -2, -2, -1 };
  10. private int vertical[] = { -2, -1, 1, 2, 2, 1, -1, -2 };
  11.  
  12. private int array[][];
  13.  
  14. public KnightsTour1AYePeriod4() {
  15. array = new int[row][col];
  16. }
  17.  
  18. public void board() {
  19.  
  20. for (int x = 0; x < row; x++) {
  21. for (int y = 0; y < col; y++) {
  22. System.out.print(" " + array[x][y]);
  23. }
  24. System.out.println();
  25. }
  26. System.out.println();
  27.  
  28. }
  29.  
  30. public static int nextInt(int a, int b) {
  31. Random chooser = new Random();
  32. return chooser.nextInt(b - a + 1) + a;
  33. }
  34.  
  35. public boolean onBoard(int x, int y) {
  36. if (x >= 1 && x < row && y >= 1 && y < row) {
  37. return true;
  38. }
  39. return false;
  40. }
  41.  
  42. public void moves() {
  43. int randNum = nextInt(1, 8);
  44. int row2 = currentPosX;
  45. System.out.println("row2 = " + row2);
  46. System.out.println("randNum = " + randNum);
  47. int col2 = currentPosY;
  48. System.out.println("col2 = " + col2);
  49. int move = 0;
  50. for (int i = 1; i <= 64; i++) {
  51. if (onBoard(row2 + vertical[randNum], col2 + horizontal[randNum]) == true) {
  52. row2 = row2 + vertical[randNum];
  53. System.out.println("row2 = " + row2);
  54. col2 = col2 + horizontal[randNum];
  55. System.out.println("hello");
  56.  
  57. System.out.println("row2 h = " + row2);
  58. System.out.println("col2 h= " + col2);
  59. // if ()
  60. // if (onBoard(row2,col2)) {
  61. array[row2][col2] = move;
  62.  
  63. // if(onBoard (array[row2][col2])
  64. System.out.println(array[row2][col2]);
  65. } else {
  66. randNum = nextInt(1, 8);
  67. System.out.println("hello");
  68. }
  69. }
  70. }
  71.  
  72. public static void main(String[] args) {
  73. KnightsTour1AYePeriod4 chess = new KnightsTour1AYePeriod4();
  74. chess.board();
  75. chess.moves();
  76. }
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement