Guest User

Untitled

a guest
May 22nd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.09 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.Random;
  4. import java.awt.geom.*;
  5. import java.util.Scanner;
  6. import java.io.*;
  7.  
  8. public class KnightsTourABourai {
  9.    
  10.     int[][] chessBoard = new int[9][9];
  11.     int[][] accessArray = new int [9][9];
  12.     int e = 1;
  13.     int f = 1;
  14.     int myX = 1;
  15.     int myY = 1;
  16.     int row = 0;
  17.     int col = 0;
  18.     int knightMoveCount;
  19.     ArrayList <Point2D.Double> possibleDirections = new ArrayList <Point2D.Double> ();
  20.     ArrayList <Point2D.Double> workingDirections = new ArrayList <Point2D.Double> ();
  21.  
  22.     Random rand = new Random();
  23.  
  24.     public KnightsTourABourai() {
  25.     }
  26.    
  27.     public void possibleMoves(){
  28.         possibleDirections.add(new Point2D.Double(1,-2));
  29.         possibleDirections.add(new Point2D.Double(2,-1));
  30.         possibleDirections.add(new Point2D.Double(2,1));
  31.         possibleDirections.add(new Point2D.Double(1,2));
  32.         possibleDirections.add(new Point2D.Double(-1,2));
  33.         possibleDirections.add(new Point2D.Double(-2,1));
  34.         possibleDirections.add(new Point2D.Double(-2,-1));
  35.         possibleDirections.add(new Point2D.Double(-1,-2));
  36.     }
  37.    
  38.     public void scanAccess(){
  39.         try{
  40.             Scanner in = new Scanner (new File("access.txt"));
  41.             e = 1;
  42.             f = 1;
  43.             while(in.hasNext()){
  44.                 for(e = 1; e < 9; e ++){
  45.                     for(f = 1; f < 9;f++){
  46.                         accessArray[e][f] = in.nextInt();
  47.                     }
  48.                 }
  49.             }
  50.         }
  51.         catch(IOException e){
  52.             System.out.println(e.getMessage());
  53.         }
  54.         System.out.println(Arrays.deepToString(accessArray));
  55.     }
  56.    
  57.     public void moveKnight(){
  58.         for(int a = 0; a < possibleDirections.size(); a++){
  59.             double y = possibleDirections.get(a).getY();
  60.             double x = possibleDirections.get(a).getX();
  61.             if(myY + y > 0 && myY + y < 9 && myX + x > 0 && myX + x < 9){
  62.                 if(chessBoard[myY + (int)y][myX + (int)x] == 0){
  63.                     workingDirections.add(new Point2D.Double(myY + y,myX + x));
  64.                 }
  65.             }
  66.         }      
  67.     }
  68.    
  69.     public void addBoard(){
  70.         moveKnight();
  71.         while(workingDirections.size() > 0){
  72.             int b = rand.nextInt(workingDirections.size());
  73.             knightMoveCount++;
  74.             myX = (int)workingDirections.get(b).getX();
  75.             myY = (int)workingDirections.get(b).getY();
  76.             for(int c = 0; c < workingDirections.size(); c++){
  77.                 if(accessArray[(int)workingDirections.get(c).getX()][(int)workingDirections.get(c).getY()] > 0){
  78.                     accessArray[(int)workingDirections.get(c).getX()][(int)workingDirections.get(c).getY()]--;
  79.    
  80.                 }
  81.             }
  82.             for(int c = 0; c < workingDirections.size(); c++){
  83.                 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){
  84.                     int d = accessArray[(int)workingDirections.get(c).getX()][(int)workingDirections.get(c).getY()];
  85.                     if(accessArray[(int)workingDirections.get(c).getX()][(int)workingDirections.get(c).getY()] <= d){
  86.                         myY = (int)workingDirections.get(c).getX();
  87.                         myX = (int)workingDirections.get(c).getY();
  88.                     }
  89.                        
  90.                 }
  91.             }
  92.             chessBoard[myY][myX] = knightMoveCount;
  93.             workingDirections.clear();
  94.         }
  95. //      System.out.println(Arrays.deepToString(accessArray));
  96.  
  97.     }
  98.    
  99.     public void printBoard(){
  100.         System.out.println();
  101.         System.out.print("    ");
  102.         for(int i = 1; i <= 8; i++){
  103.             System.out.print(i + "   ");
  104.         }
  105.         System.out.println();
  106.         for(int rowNumber = 1; rowNumber < 9; rowNumber++){
  107.             System.out.println();
  108.             System.out.printf("%-4d", rowNumber);
  109.                 for(int colNumber = 1; colNumber < 9; colNumber++){
  110.                     System.out.printf("%-3d ", chessBoard[rowNumber][colNumber]);
  111.                 }
  112.         }
  113.         System.out.println();
  114.         System.out.println();
  115.         System.out.println(knightMoveCount + " squares visted");
  116.     }
  117.    
  118.  
  119. }
  120.  
  121.  
  122. class driver{  
  123.     public static void main(String[] args){
  124.         KnightsTourABourai knight = new KnightsTourABourai();
  125.         knight.possibleMoves();
  126.         knight.scanAccess();
  127.         for(int o = 0; o < 15; o++){
  128.        
  129.         for(int i = 0; i < 64; i++){   
  130.             knight.addBoard();
  131.         }
  132.         knight.printBoard();
  133.         }
  134.        
  135.        
  136.     }
  137. }
Add Comment
Please, Sign In to add comment