Advertisement
Guest User

Untitled

a guest
Jan 29th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.51 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class knights_tour_1{
  4.  
  5.     private Square[][] arr;
  6.     private int k = 1;
  7.  
  8.     public static void main(String[] args) {
  9.         knights_tour_1 l = new knights_tour_1(8);
  10.         l.calc(1,1);
  11.         l.print();
  12.  
  13.     }
  14.  
  15.     public void calc(int x, int y){
  16.            
  17.             arr[x-1][y-1].setTouched(k);
  18.             k++;
  19.  
  20.             //use get possible moves method
  21.             int[][] possibleMoves = getPossibleMoves(x,y);
  22.  
  23.             if(possibleMoves.length <= 0) return;
  24.  
  25.             //randomly selected possible move
  26.             Random r = new Random();
  27.             int random = r.nextInt(possibleMoves.length);
  28.  
  29.  
  30.            
  31.             //recursivel call calc(newX,newY)
  32.             calc(x + possibleMoves[random][0],y + possibleMoves[random][1]);
  33.        
  34.  
  35.     }
  36.  
  37.     //this method gets the availale positions the knight can jump to
  38.     //if the knight is at a position x,y
  39.     public int[][] getPossibleMoves(int x, int y){
  40.  
  41.         //list all possible moves
  42.         int[][] possibleMoves = new int[][]{ {1,-2}, {2,-1}, {-1,-2}, {-2,-1}, {1,2}, {2,1}, {-1,2}, {-2,1} };
  43.  
  44.         //stores the indexes of the moves the knight can jump to. for example, if the knight can go x=-1 and y=2 or x=1 and y=2
  45.         //the moveIndexes array will contain numbers 0 and 4
  46.         ArrayList<Integer> moveIndexes = new ArrayList<Integer>();
  47.  
  48.         for(int i = 0; i < possibleMoves.length; i++){
  49.             if(inBounds(x + possibleMoves[i][0]-1, y + possibleMoves[i][1]-1)){
  50.                 if(arr[x+possibleMoves[i][0]-1][y+possibleMoves[i][1]-1].isOpen()){
  51.                     moveIndexes.add(i);
  52.                 }    
  53.             }
  54.         }
  55.  
  56.         int[][] availableMoves = new int[moveIndexes.size()][2];
  57.         for(int i = 0; i < availableMoves.length; i++){
  58.             availableMoves[i] = possibleMoves[moveIndexes.get(i)];
  59.         }
  60.             //randomly selected possible move
  61.  
  62.         return availableMoves;
  63.  
  64.  
  65.     }
  66.  
  67.     //n = width/height
  68.     public knights_tour_1(int n){
  69.         arr = new Square[n][n];
  70.         for(int i = 0; i < n; i++){
  71.             for(int j = 0; j < n; j++){
  72.                 arr[i][j] = new Square();
  73.             }
  74.         }
  75.        
  76.     }
  77.  
  78.     private boolean inBounds(int i, int j){
  79.         if( i < arr.length && i > -1){
  80.             if(j < arr.length && j > -1){
  81.                 return true;
  82.             }
  83.         }
  84.         return false;
  85.     }
  86.  
  87.     public void print(){
  88.         for(int i = 0; i < arr.length; i++){
  89.             for(int j = 0; j < arr.length; j++){
  90.                 System.out.print(arr[i][j] + " ");
  91.             }
  92.             System.out.print("\n");
  93.         }
  94.     }
  95.  
  96.  
  97.  
  98.     class Square{
  99.  
  100.         boolean touched;
  101.         int touchNum = 0;
  102.  
  103.  
  104.         Square(){
  105.             this.touched = false;
  106.         }
  107.  
  108.  
  109.         void setTouched(int k){
  110.             touched = true;
  111.             touchNum = k;
  112.         }
  113.  
  114.         boolean isOpen(){
  115.             return !touched;
  116.         }
  117.  
  118.         public String toString(){
  119.             return "" + touchNum;
  120.         }
  121.  
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement