Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Random;
- import java.awt.geom.*;
- import java.util.Scanner;
- import java.io.*;
- public class KnightsTourABourai {
- int[][] chessBoard = new int[9][9];
- int[][] accessArray = new int [9][9];
- int e = 1;
- int f = 1;
- int myX = 1;
- int myY = 1;
- int row = 0;
- int col = 0;
- int knightMoveCount;
- ArrayList <Point2D.Double> possibleDirections = new ArrayList <Point2D.Double> ();
- ArrayList <Point2D.Double> workingDirections = new ArrayList <Point2D.Double> ();
- Random rand = new Random();
- public KnightsTourABourai() {
- }
- public void possibleMoves(){
- possibleDirections.add(new Point2D.Double(1,-2));
- possibleDirections.add(new Point2D.Double(2,-1));
- possibleDirections.add(new Point2D.Double(2,1));
- possibleDirections.add(new Point2D.Double(1,2));
- possibleDirections.add(new Point2D.Double(-1,2));
- possibleDirections.add(new Point2D.Double(-2,1));
- possibleDirections.add(new Point2D.Double(-2,-1));
- possibleDirections.add(new Point2D.Double(-1,-2));
- }
- public void scanAccess(){
- try{
- Scanner in = new Scanner (new File("access.txt"));
- e = 1;
- f = 1;
- while(in.hasNext()){
- for(e = 1; e < 9; e ++){
- for(f = 1; f < 9;f++){
- accessArray[e][f] = in.nextInt();
- }
- }
- }
- }
- catch(IOException e){
- System.out.println(e.getMessage());
- }
- System.out.println(Arrays.deepToString(accessArray));
- }
- public void moveKnight(){
- for(int a = 0; a < possibleDirections.size(); a++){
- double y = possibleDirections.get(a).getY();
- double x = possibleDirections.get(a).getX();
- if(myY + y > 0 && myY + y < 9 && myX + x > 0 && myX + x < 9){
- if(chessBoard[myY + (int)y][myX + (int)x] == 0){
- workingDirections.add(new Point2D.Double(myY + y,myX + x));
- }
- }
- }
- }
- public void addBoard(){
- moveKnight();
- while(workingDirections.size() > 0){
- int b = rand.nextInt(workingDirections.size());
- knightMoveCount++;
- myX = (int)workingDirections.get(b).getX();
- myY = (int)workingDirections.get(b).getY();
- for(int c = 0; c < workingDirections.size(); c++){
- if(accessArray[(int)workingDirections.get(c).getX()][(int)workingDirections.get(c).getY()] > 0){
- accessArray[(int)workingDirections.get(c).getX()][(int)workingDirections.get(c).getY()]--;
- }
- }
- for(int c = 0; c < workingDirections.size(); c++){
- if(accessArray[(int)workingDirections.get(c).getX()][(int)workingDirections.get(c).getY()] < 9 && accessArray[(int)workingDirections.get(c).getX()][(int)workingDirections.get(c).getY()] > 0){
- int d = accessArray[(int)workingDirections.get(c).getX()][(int)workingDirections.get(c).getY()];
- if(accessArray[(int)workingDirections.get(c).getX()][(int)workingDirections.get(c).getY()] <= d){
- myY = (int)workingDirections.get(c).getX();
- myX = (int)workingDirections.get(c).getY();
- }
- }
- }
- chessBoard[myY][myX] = knightMoveCount;
- workingDirections.clear();
- }
- // System.out.println(Arrays.deepToString(accessArray));
- }
- public void printBoard(){
- System.out.println();
- System.out.print(" ");
- for(int i = 1; i <= 8; i++){
- System.out.print(i + " ");
- }
- System.out.println();
- for(int rowNumber = 1; rowNumber < 9; rowNumber++){
- System.out.println();
- System.out.printf("%-4d", rowNumber);
- for(int colNumber = 1; colNumber < 9; colNumber++){
- System.out.printf("%-3d ", chessBoard[rowNumber][colNumber]);
- }
- }
- System.out.println();
- System.out.println();
- System.out.println(knightMoveCount + " squares visted");
- }
- }
- class driver{
- public static void main(String[] args){
- KnightsTourABourai knight = new KnightsTourABourai();
- knight.possibleMoves();
- knight.scanAccess();
- for(int o = 0; o < 15; o++){
- for(int i = 0; i < 64; i++){
- knight.addBoard();
- }
- knight.printBoard();
- }
- }
- }
Add Comment
Please, Sign In to add comment