Guest
Public paste!

Untitled

By: a guest | Mar 18th, 2010 | Syntax: None | Size: 4.08 KB | Hits: 38 | Expires: Never
Copy text to clipboard
  1. import java.util.*;
  2. public class KnightsTour1{
  3.         Random random = new Random();
  4.     ArrayList<Integer> count;
  5.     ArrayList<Integer> available;
  6.     int[][] board;
  7.     int maxrow = 0;
  8.     int maxcolumn = 0;
  9.     int position = 1;
  10.     public void create(){
  11.         board = new int[8][8];
  12.                 for(int i = 0; i < 8; i ++){
  13.                         for(int j = 0; j < 8; j ++){
  14.                                 board[i][j] = 0;
  15.                 }      
  16.             }
  17.     }
  18.     public void movement(){
  19.         board[maxrow][maxcolumn] = position;      
  20.             while(count(maxrow,maxcolumn).isEmpty() != true){
  21.                 if(count(maxrow, maxcolumn).size() > 0){
  22.                         int rand = random.nextInt((count(maxrow, maxcolumn)).size());
  23.                         available = count(maxrow, maxcolumn);
  24.                         int movement = (available.get(rand));
  25.                         if(movement == 1){
  26.                                 maxrow = maxrow - 2;
  27.                                 maxcolumn = maxcolumn + 1;
  28.                                 board[maxrow][maxcolumn] = position ++;
  29.                         }
  30.                         if(movement == 2){
  31.                                 maxrow = maxrow - 1;
  32.                                 maxcolumn = maxcolumn + 2;
  33.                                 board[maxrow][maxcolumn] = position ++;
  34.                             }
  35.                             if(movement == 3){
  36.                                 maxrow = maxrow + 1;
  37.                                 maxcolumn = maxcolumn + 2;
  38.                                 board[maxrow][maxcolumn] = position ++;
  39.                             }
  40.                             if(movement == 4){
  41.                                 maxrow = maxrow + 2;
  42.                                         maxcolumn = maxcolumn + 1;
  43.                                         board[maxrow][maxcolumn] = position ++;
  44.                             }
  45.                             if(movement == 5){
  46.                                 maxrow = maxrow + 2;
  47.                                 maxcolumn = maxcolumn - 1;
  48.                                 board[maxrow][maxcolumn] = position ++;
  49.                             }
  50.                             if(movement == 6){
  51.                                 maxrow = maxrow + 1;
  52.                                 maxcolumn = maxcolumn - 2;
  53.                                 board[maxrow][maxcolumn] = position ++;
  54.                             }
  55.                             if(movement == 7){
  56.                                 maxrow = maxrow - 1;
  57.                                 maxcolumn = maxcolumn - 2;
  58.                                 board[maxrow][maxcolumn] = position ++;
  59.                             }
  60.                             if(movement == 8){
  61.                                 maxrow = maxrow - 2;
  62.                                 maxcolumn = maxcolumn - 1;
  63.                                 board[maxrow][maxcolumn] = position ++;
  64.                             }
  65.                     }  
  66.             }          
  67.     }
  68.     public void print(){
  69.         int a = 1;
  70.                 System.out.println("----------THE KNIGHT'S TOUR #1-----------");
  71.                 for(int i = 0; i < 8; i ++){
  72.                         for(int j = 0; j < 8; j ++){
  73.                                 if(a != 8){
  74.                                         System.out.printf("%3d", board[i][j]);
  75.                                         a ++;
  76.                 }
  77.                 else{
  78.                         System.out.printf("%3d", board[i][j]);
  79.                         System.out.println();  
  80.                         a = 1;
  81.                 }
  82.             }
  83.         }
  84.         int b = 0;
  85.         for(int k = 0; k < 8; k ++){
  86.                 for(int l = 0; l < 8; l ++){
  87.                         if(board[k][l] > b){
  88.                                 b = board[k][l];
  89.                         }
  90.                 }
  91.         }
  92.         System.out.println("Steps taken by the Knight : " + b);
  93.     }
  94.     public ArrayList count(int row, int column){
  95.         count = new ArrayList<Integer>();
  96.         if(row - 2 >= 0 && column + 1 < 8){
  97.                 if(board[row - 2][column + 1] == 0){
  98.                         count.add(1);
  99.             }
  100.         }
  101.         if(row - 1 >= 0 && column + 2 < 8){
  102.                 if(board[row - 1][column + 2] == 0){
  103.                         count.add(2);
  104.             }          
  105.         }    
  106.         if(row + 1 < 8 && column + 2 < 8){
  107.                 if(board[row + 1][column + 2] == 0){
  108.                         count.add(3);
  109.             }              
  110.         }    
  111.         if(row + 2 < 8 && column + 1 < 8){
  112.                 if(board[row + 2][column + 1] == 0){
  113.                         count.add(4);
  114.             }              
  115.         }    
  116.         if(row + 2 < 8 && column - 1 >= 0){
  117.                 if(board[row + 2][column - 1] == 0){
  118.                         count.add(5);
  119.             }      
  120.         }          
  121.         if(row + 1 < 8 && column - 2 >= 0){
  122.                 if(board[row + 1][column - 1] == 0){
  123.                         count.add(6);
  124.             }          
  125.         }      
  126.         if(row - 1 >= 0 && column - 2 >= 0){
  127.                 if(board[row - 1][column - 2] == 0){
  128.                         count.add(7);
  129.             }
  130.         }      
  131.         if(row - 2 >= 0 && column - 1 >= 0){
  132.                 if(board[row - 2][column - 1] == 0){
  133.                         count.add(8);
  134.             }
  135.         }
  136.         return count;
  137.     }      
  138.     public static void main(String[] args){
  139.                 KnightsTour1 a = new KnightsTour1();
  140.                 a.create();
  141.                 a.movement();
  142.                 a.print();
  143.     }