Guest User

Untitled

a guest
May 22nd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.96 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.io.*;
  3. import java.util.Arrays;
  4.  
  5. import java.util.ArrayList;
  6. import java.util.Arrays;
  7. import java.util.Random;
  8. import java.awt.geom.*;
  9. import java.util.Scanner;
  10. import java.io.*;
  11.  
  12. public class KnightsTourABourai {
  13.    
  14.     int[][] chessBoard = new int[9][9];
  15.     int[][] accessArray = new int [9][9];
  16.     int e = 1;
  17.     int f = 1;
  18.     int myX = 1;
  19.     int myY = 1;
  20.     int row = 0;
  21.     int col = 0;
  22.     int knightMoveCount;
  23.     ArrayList <Point2D.Double> possibleDirections = new ArrayList <Point2D.Double> ();
  24.     ArrayList <Point2D.Double> workingDirections = new ArrayList <Point2D.Double> ();
  25.  
  26.     Random rand = new Random();
  27.  
  28.     public KnightsTourABourai() {
  29.     }
  30.    
  31.     public void possibleMoves(){
  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.         possibleDirections.add(new Point2D.Double(-1,2));
  37.         possibleDirections.add(new Point2D.Double(-2,1));
  38.         possibleDirections.add(new Point2D.Double(-2,-1));
  39.         possibleDirections.add(new Point2D.Double(-1,-2));
  40.     }
  41.    
  42.     public void scanAccess(){
  43.         try{
  44.             Scanner in = new Scanner (new File("access.txt"));
  45.             e = 1;
  46.             f = 1;
  47.             while(in.hasNext()){
  48.                 for(e = 1; e < 9; e ++){
  49.                     for(f = 1; f < 9;f++){
  50.                         accessArray[e][f] = in.nextInt();
  51.                     }
  52.                 }
  53.             }
  54.         }
  55.         catch(IOException e){
  56.             System.out.println(e.getMessage());
  57.         }
  58.         System.out.println(Arrays.deepToString(accessArray));
  59.     }
  60.    
  61.     public void moveKnight(){
  62.         for(int a = 0; a < possibleDirections.size(); a++){
  63.             double y = possibleDirections.get(a).getY();
  64.             double x = possibleDirections.get(a).getX();
  65.             if(myY + y > 0 && myY + y < 9 && myX + x > 0 && myX + x < 9){
  66.                 if(chessBoard[myY + (int)y][myX + (int)x] == 0){
  67.                     workingDirections.add(new Point2D.Double(myY + y,myX + x));
  68.                 }
  69.             }
  70.         }      
  71.     }
  72.    
  73.     public void addBoard(){
  74.         moveKnight();
  75.         while(workingDirections.size() > 0){
  76.             int b = rand.nextInt(workingDirections.size());
  77.             knightMoveCount++;
  78.             myX = (int)workingDirections.get(b).getX();
  79.             myY = (int)workingDirections.get(b).getY();
  80.             for(int c = 0; c < workingDirections.size(); c++){
  81.                 if(accessArray[(int)workingDirections.get(c).getX()][(int)workingDirections.get(c).getY()] > 0){
  82.                     accessArray[(int)workingDirections.get(c).getX()][(int)workingDirections.get(c).getY()]--;
  83.    
  84.                 }
  85.             }
  86.             for(int c = 0; c < workingDirections.size(); c++){
  87.                 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){
  88.                     int d = accessArray[(int)workingDirections.get(c).getX()][(int)workingDirections.get(c).getY()];
  89.                     if(accessArray[(int)workingDirections.get(c).getX()][(int)workingDirections.get(c).getY()] <= d){
  90.                         myY = (int)workingDirections.get(c).getX();
  91.                         myX = (int)workingDirections.get(c).getY();
  92.                     }
  93.                        
  94.                 }
  95.             }
  96.             chessBoard[myY][myX] = knightMoveCount;
  97.             workingDirections.clear();
  98.         }
  99. //      System.out.println(Arrays.deepToString(accessArray));
  100.  
  101.     }
  102.    
  103.     public void printBoard(){
  104.         System.out.println();
  105.         System.out.print("    ");
  106.         for(int i = 1; i <= 8; i++){
  107.             System.out.print(i + "   ");
  108.         }
  109.         System.out.println();
  110.         for(int rowNumber = 1; rowNumber < 9; rowNumber++){
  111.             System.out.println();
  112.             System.out.printf("%-4d", rowNumber);
  113.                 for(int colNumber = 1; colNumber < 9; colNumber++){
  114.                     System.out.printf("%-3d ", chessBoard[rowNumber][colNumber]);
  115.                 }
  116.         }
  117.         System.out.println();
  118.         System.out.println();
  119.         System.out.println(knightMoveCount + " squares visted");
  120.     }
  121.    
  122.  
  123. }
  124.  
  125.  
  126. class driver{  
  127.     public static void main(String[] args){
  128.         KnightsTourABourai knight = new KnightsTourABourai();
  129.         knight.possibleMoves();
  130.         knight.scanAccess();
  131.         for(int o = 0; o < 15; o++){
  132.         for(int i = 0; i < 64; i++){   
  133.             knight.addBoard();
  134.         }
  135.         knight.printBoard();
  136.         }
  137.        
  138.        
  139.     }
  140. }
  141.    
  142.  
  143. public class LifeABourai {
  144.     boolean[][] currentArray;
  145.     boolean[][] nextArray;
  146.     int count;
  147.     int total;
  148.     int totalRowTen;
  149.     int totalColumnTen;
  150.     int x;
  151.     int y;
  152.     Scanner in;
  153.  
  154.     public LifeABourai(){
  155.     }
  156.  
  157.     public void readFile(){
  158.         try{
  159.             in = new Scanner(new File("life100.txt"));
  160.             x = in.nextInt();
  161.             y = in.nextInt();
  162.             currentArray = new boolean[x][y];
  163.             nextArray = new boolean[x][y];
  164.             while(in.hasNext()){
  165.                 int rows = in.nextInt();
  166.                 int columns = in.nextInt();
  167.                 currentArray[rows - 1][columns - 1] = true;
  168.             }
  169.         }catch(IOException i){
  170.             System.out.println("Error: " + i.getMessage());
  171.         }
  172.     }
  173.  
  174.     public void firstGeneration(){
  175.         for(int row = 0; row < currentArray.length; row++){
  176.             for(int col = 0; col < currentArray[0].length; col++){
  177.                 count = 0;
  178.                 if(row > 0 && row < currentArray.length - 1){
  179.                     if(currentArray[row + 1][col] == true){
  180.                         count++;
  181.                     }
  182.                     if(currentArray[row - 1][col] == true){
  183.                         count++;
  184.                     }
  185.                     if(col > 0){
  186.                         if(currentArray[row - 1][col - 1] == true){
  187.                             count++;
  188.                         }
  189.                         if(currentArray[row][col - 1] == true){
  190.                             count++;
  191.                         }
  192.                         if(currentArray[row + 1][col - 1] == true){
  193.                             count++;
  194.                         }
  195.                     }
  196.                     if(col < currentArray[0].length - 1){
  197.                         if(currentArray[row][col + 1] == true){
  198.                             count++;
  199.                         }
  200.                         if(currentArray[row - 1][col + 1] == true){
  201.                             count++;
  202.                         }
  203.                         if(currentArray[row + 1][col + 1] == true){
  204.                             count++;
  205.                         }
  206.                     }
  207.                    
  208.                 }
  209.                 else if(row == 0){
  210.                     if(currentArray[row + 1][col] == true){
  211.                         count++;
  212.                     }
  213.                     if(col > 0){
  214.                         if(currentArray[row + 1][col - 1] == true){
  215.                             count++;
  216.                         }  
  217.                         if(currentArray[row][col - 1] == true){
  218.                             count++;
  219.                         }
  220.                     }
  221.                    
  222.                     if(col != currentArray[0].length- 1){
  223.                         if(currentArray[row][col + 1] == true){
  224.                             count++;
  225.                         }
  226.                         if(currentArray[row + 1][col + 1] == true){
  227.                             count++;
  228.                         }
  229.                     }
  230.                 }
  231.                 else if(row == currentArray.length - 1){
  232.                     if(currentArray[row - 1][col] == true){
  233.                         count++;
  234.                     }
  235.                     if(col > 0){
  236.                         if(currentArray[row][col - 1] == true){
  237.                             count++;
  238.                         }
  239.                         if(currentArray[row - 1][col - 1] == true){
  240.                             count++;
  241.                         }
  242.                     }
  243.                     if(col != currentArray[0].length - 1){
  244.                         if(currentArray[row][col + 1] == true){
  245.                             count++;
  246.                         }
  247.                         if(currentArray[row - 1][col + 1] == true){
  248.                             count++;
  249.                         }
  250.                     }
  251.                 }
  252.                 if(currentArray[row][col] == true){
  253.                     if((count == 0 || count == 1 || count >= 4)){
  254.                         nextArray[row][col] = false;
  255.                     }
  256.                     else if(count == 2 || count == 3){
  257.                         nextArray[row][col] = true;
  258.                     }
  259.                 }
  260.                 else if(currentArray[row][col] == false && count == 3){
  261.                     nextArray[row][col] = true;
  262.                 }
  263.             }
  264.         }
  265.         currentArray = nextArray;
  266.         nextArray = new boolean[x][y];
  267.     }
  268.    
  269.     public int countRowTen(){
  270.         totalRowTen = 0;
  271.         for(int newColumn = 0; newColumn < currentArray[10].length; newColumn++){
  272.             if(currentArray[9][newColumn] == true){
  273.                 totalRowTen++;
  274.             }
  275.         }
  276.         return totalRowTen;
  277.     }
  278.    
  279.     public int countColumnTen(){
  280.         totalColumnTen = 0;
  281.         for(int newRow = 0; newRow < currentArray[10].length; newRow++){
  282.             if(currentArray[newRow][9] == true){
  283.                 totalColumnTen++;
  284.             }
  285.         }
  286.         return totalColumnTen;
  287.     }
  288.    
  289.     public int countTotalLiving(){
  290.         total = 0;
  291.         for(int r = 0; r < currentArray.length; r++){
  292.             for(int c = 0; c < currentArray[0].length; c++){
  293.                 if(currentArray[r][c] == true){
  294.                     total++;
  295.                 }
  296.             }
  297.         }
  298.         return total;
  299.     }
  300.  
  301.     public void printGeneration(){
  302.         System.out.println();
  303.         System.out.print("    ");
  304.         for(int i = 1; i <= currentArray.length; i++){
  305.             System.out.print(i % 10);
  306.         }
  307.         System.out.println();
  308.         for(int rowNumber = 0; rowNumber < currentArray.length; rowNumber++){
  309.             System.out.println();
  310.             System.out.printf("%-4d", rowNumber + 1);
  311.                 for(int colNumber = 0; colNumber < currentArray[0].length; colNumber++){
  312.                     if(currentArray[rowNumber][colNumber] == true){
  313.                         System.out.print('*');
  314.                     }
  315.                     else{
  316.                         System.out.print(" ");
  317.                     }
  318.                 }
  319.         }
  320.         System.out.println();
  321.         System.out.println();
  322.         System.out.print("Number in Row 10: " + countRowTen());
  323.         System.out.println();
  324.         System.out.print("Number in Column 10: " + countColumnTen());
  325.         System.out.println();
  326.         System.out.println("Number total: " + countTotalLiving());
  327.     }
  328. }
  329. class driver{
  330.     public static void main(String[] args){
  331.         int a = 0;
  332.         LifeABourai l= new LifeABourai();
  333.         l.readFile();
  334.         while(a < 5){
  335.             l.firstGeneration();
  336.             a++;
  337.             l.printGeneration();
  338.         }
  339.        
  340.     }
  341. }
Add Comment
Please, Sign In to add comment