- import java.util.*;
- public class KnightsTour1{
- Random random = new Random();
- ArrayList<Integer> count;
- ArrayList<Integer> available;
- int[][] board;
- int maxrow = 0;
- int maxcolumn = 0;
- int position = 1;
- public void create(){
- board = new int[8][8];
- for(int i = 0; i < 8; i ++){
- for(int j = 0; j < 8; j ++){
- board[i][j] = 0;
- }
- }
- }
- public void movement(){
- board[maxrow][maxcolumn] = position;
- while(count(maxrow,maxcolumn).isEmpty() != true){
- if(count(maxrow, maxcolumn).size() > 0){
- int rand = random.nextInt((count(maxrow, maxcolumn)).size());
- available = count(maxrow, maxcolumn);
- int movement = (available.get(rand));
- if(movement == 1){
- maxrow = maxrow - 2;
- maxcolumn = maxcolumn + 1;
- board[maxrow][maxcolumn] = position ++;
- }
- if(movement == 2){
- maxrow = maxrow - 1;
- maxcolumn = maxcolumn + 2;
- board[maxrow][maxcolumn] = position ++;
- }
- if(movement == 3){
- maxrow = maxrow + 1;
- maxcolumn = maxcolumn + 2;
- board[maxrow][maxcolumn] = position ++;
- }
- if(movement == 4){
- maxrow = maxrow + 2;
- maxcolumn = maxcolumn + 1;
- board[maxrow][maxcolumn] = position ++;
- }
- if(movement == 5){
- maxrow = maxrow + 2;
- maxcolumn = maxcolumn - 1;
- board[maxrow][maxcolumn] = position ++;
- }
- if(movement == 6){
- maxrow = maxrow + 1;
- maxcolumn = maxcolumn - 2;
- board[maxrow][maxcolumn] = position ++;
- }
- if(movement == 7){
- maxrow = maxrow - 1;
- maxcolumn = maxcolumn - 2;
- board[maxrow][maxcolumn] = position ++;
- }
- if(movement == 8){
- maxrow = maxrow - 2;
- maxcolumn = maxcolumn - 1;
- board[maxrow][maxcolumn] = position ++;
- }
- }
- }
- }
- public void print(){
- int a = 1;
- System.out.println("----------THE KNIGHT'S TOUR #1-----------");
- for(int i = 0; i < 8; i ++){
- for(int j = 0; j < 8; j ++){
- if(a != 8){
- System.out.printf("%3d", board[i][j]);
- a ++;
- }
- else{
- System.out.printf("%3d", board[i][j]);
- System.out.println();
- a = 1;
- }
- }
- }
- int b = 0;
- for(int k = 0; k < 8; k ++){
- for(int l = 0; l < 8; l ++){
- if(board[k][l] > b){
- b = board[k][l];
- }
- }
- }
- System.out.println("Steps taken by the Knight : " + b);
- }
- public ArrayList count(int row, int column){
- count = new ArrayList<Integer>();
- if(row - 2 >= 0 && column + 1 < 8){
- if(board[row - 2][column + 1] == 0){
- count.add(1);
- }
- }
- if(row - 1 >= 0 && column + 2 < 8){
- if(board[row - 1][column + 2] == 0){
- count.add(2);
- }
- }
- if(row + 1 < 8 && column + 2 < 8){
- if(board[row + 1][column + 2] == 0){
- count.add(3);
- }
- }
- if(row + 2 < 8 && column + 1 < 8){
- if(board[row + 2][column + 1] == 0){
- count.add(4);
- }
- }
- if(row + 2 < 8 && column - 1 >= 0){
- if(board[row + 2][column - 1] == 0){
- count.add(5);
- }
- }
- if(row + 1 < 8 && column - 2 >= 0){
- if(board[row + 1][column - 1] == 0){
- count.add(6);
- }
- }
- if(row - 1 >= 0 && column - 2 >= 0){
- if(board[row - 1][column - 2] == 0){
- count.add(7);
- }
- }
- if(row - 2 >= 0 && column - 1 >= 0){
- if(board[row - 2][column - 1] == 0){
- count.add(8);
- }
- }
- return count;
- }
- public static void main(String[] args){
- KnightsTour1 a = new KnightsTour1();
- a.create();
- a.movement();
- a.print();
- }
