Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.10 KB | None | 0 0
  1. // TiK
  2. // Knights.java
  3. // infinite loop an example of brute orce, Try to get a Knight to touch each square on the board.
  4. import java.util.Random;
  5.  
  6.     public class Knights
  7.     {
  8.        
  9.         private static int board[][];
  10.         private static int x, y,origx,origy,specialCase;
  11.         private static int numberOfMoves = 1;
  12.         private static int highest = 0;
  13.         private static int[] value = new int[8];
  14.         private static int[][] trail = new int[8][8];
  15.        
  16.         public Knights()
  17.         {
  18.                     board = new int[8][8];
  19.         }
  20.        
  21.         public static int   getX(){         return x;}
  22.         public static void setX(int x){     Knights.x = x;}
  23.        
  24.         public static int   getY(){         return y;}
  25.         public static void setY(int y){ Knights.y = y;}
  26.        
  27.         public static boolean moveKnight(int x,int y)
  28.         {
  29.             Random myRandom = new Random();
  30.             int moveIndex, rChoice;
  31.             boolean solved = false;
  32.             int counter = 0;
  33.             int iCounter = 0;
  34.             origx = x;
  35.                         origy = y;
  36.                         for(int i=0;i<8;i++)
  37.                 value[i]=0;
  38.             setupBoard();
  39.             board[7][3]=4;
  40.             printBoard();
  41.             trail[origx][origy] = 1;
  42.                        
  43.             while(!solved) {   
  44.                
  45.                 if(checkMove(Knights.x+1, Knights.y+2)) {      //Move 1 up 2 right
  46.                         value[0] = boardValue(Knights.x+1,Knights.x+2);
  47.                    
  48.                 }if(checkMove(Knights.x+1, Knights.y-2)) {  //Move 2 up 2 left
  49.                         value[1] = boardValue(Knights.x+1,Knights.y-2);
  50.                        
  51.                 }if(checkMove(Knights.x+2, Knights.y+1)) { //Move 3 right 2 up
  52.                         value[2] = boardValue(Knights.x+2,Knights.y+1);
  53.                        
  54.                 }if(checkMove(Knights.x+2, Knights.y-1)) {  //Move 4 right 2 down
  55.                         value[3] = boardValue(Knights.x+2,Knights.y-1);
  56.                        
  57.                 }if(checkMove(Knights.x-1, Knights.y-2)) {  //Move 5 down 2 left
  58.                         value[4] = boardValue(Knights.x-1,Knights.y-2);
  59.                        
  60.                 }if(checkMove(Knights.x-1, Knights.y+2)) {  //Move 6 down 2 right
  61.                         value[5] = boardValue(Knights.x-1,Knights.y+2);
  62.                        
  63.                 }if(checkMove(Knights.x-2, Knights.y+1)) {//Move 7 left 2 up
  64.                         value[6] = boardValue(Knights.x-2,Knights.y+1);
  65.                            
  66.                 }if(checkMove(Knights.x-2, Knights.y-1)) {  // Move 8 left 2 down
  67.                         value[7] = boardValue(Knights.x-2,Knights.y-1);
  68.                        
  69.                 }
  70.                
  71.                 //System.out.print("\nPrinting out value[]\n");
  72. //                  for(int i=0;i<8;i++) {
  73. //                      System.out.print(+value[i]);
  74. //                  }
  75.                 for(int i=0;i<8;i++) {
  76.                     if(value[i] > highest)
  77.                         highest = value[i];
  78.                 }
  79.                 //System.out.print("\njust exited highest loop: " +highest);
  80.                 if(highest == 0) {
  81.                     System.out.print("knight stuck, starting over\n");
  82.                                         clearTrail();
  83.                                         numberOfMoves = 1;
  84.                                         printBoard();
  85.                                         setupBoard();
  86.                                         setX(origx);
  87.                                         setY(origy);
  88.                                         break;
  89.                 }
  90.                 for(int i=0;i<8;i++) {
  91.                     if(highest == value[i]) {
  92.                         counter++;
  93.                     }
  94.                 }
  95.                        
  96.                 //System.out.print("\nJust exited counter loop " +counter);
  97.                
  98.                
  99.                     if(counter == 1) {
  100.                         for(specialCase=0;specialCase<8;specialCase++) {
  101.                             if(value[specialCase] == highest) {
  102.                                 break;
  103.                             }
  104.                         }
  105.                         //System.out.print("\nspecialCase: "+specialCase);
  106.                         knightSwitch(specialCase);
  107.                     }else {
  108.                         rChoice = myRandom.nextInt(counter) + 1;
  109.                         //System.out.print("\nRandom number "+rChoice);
  110.                         iCounter = 0;
  111.                
  112.                         for(moveIndex=0;moveIndex<8;moveIndex++) {
  113.                             if(highest == value[moveIndex]) {
  114.                                 iCounter++;
  115.                                 //System.out.print("\niCounter: "+iCounter);
  116.                                 if(iCounter == rChoice) {
  117.                                     //System.out.print("moveIndex: "+moveIndex);
  118.                                     knightSwitch(moveIndex);
  119.                                 }
  120.                             }
  121.                         }
  122.                     }
  123.            
  124.             //System.out.print("\nClearing value");
  125.             for(int j=0;j<8;j++) // clear value
  126.                 value[j]=0;
  127.             //System.out.print("\nReseting counter");
  128.             counter = 0;
  129.             //System.out.print("Clearing highest");
  130.             highest = 0;
  131.        
  132.         if(numberOfMoves == 63) {
  133.             solved = true;
  134.                         printTrail();
  135.         }
  136.             }
  137.             if(numberOfMoves == 63)
  138.                 return false;
  139.             return true;
  140.                
  141.     }
  142.  
  143.        
  144.         public static void  knightSwitch(int move) {
  145.            
  146.             switch (move)
  147.             {
  148.             case 0:
  149.                     makeMove(x+1, y+2); // up 2 right
  150.                     break;
  151.             case 1:
  152.                     makeMove(x+1, y-2); // down 2 right
  153.                     break;
  154.             case 2:
  155.                     makeMove(x+2, y+1); // right 2 up
  156.                     break; 
  157.             case 3:
  158.                    
  159.                     makeMove(x+2, y-1); // right 2 down
  160.                     break;
  161.             case 4:
  162.                     makeMove(x-1, y-2); // down 2 left
  163.                     break;
  164.             case 5:
  165.                     makeMove(x-1, y+2); // down 2 right
  166.                     break;
  167.             case 6:
  168.                     makeMove(x-2, y+1); // left 2 up
  169.                     break;
  170.             case 7:
  171.                     makeMove(x-2, y-1); // left 2 down
  172.                     break;
  173.             }
  174.    
  175.         }
  176.         public static boolean checkMove(int x, int y)
  177.         {
  178.             if((y >= 0) && (y < 8) && (x >= 0) && (x < 8))
  179.                     if(!(board[x][y] == 0))
  180.                         return true;
  181.             return false;  
  182.         }
  183.        
  184.         public static void makeMove(int x, int y)
  185.         {
  186.             System.out.printf("\nMove %d from (%d,%d) to (%d,%d)\n", numberOfMoves++, Knights.x, Knights.y, x, y);
  187.            
  188.            
  189.             board[x][y] = 0;
  190.                         trail[x][y] = numberOfMoves;
  191.             Knights.x = x;
  192.             Knights.y = y;
  193.             reDraw();
  194.             //printBoard();
  195.         }
  196.         public static int boardValue(int x,int y) {
  197.                 //  System.out.printf("Value of Square %d %d choices %d ", x, y, board[x][y]);
  198.                 //  System.out.println("");
  199.             if(checkMove(x,y))
  200.                 return board[x][y];
  201.             return 0;
  202.         }
  203.         public static void reDraw() {
  204.             //System.out.print("\nRedrawing Board");
  205.             if(checkMove(x+1, y+2)) {// move 1 up 2 right
  206.                 board[x+1][y+2]--;  //boardValue(x+1,y+2) - 1;
  207.                
  208.             }if(checkMove(x+1, y-2)) { // move 2 up 2 left
  209.                 board[x+1][y-2]--;  //boardValue(x+1,y-2) - 1;
  210.        
  211.             }if(checkMove(x+2, y+1)) {// move 3 right 2 up
  212.                 board[x+2][y+1]--;  //boardValue(x+2,y+1) - 1;
  213.                
  214.             }if(checkMove(x+2, y-1)) {// move 4 right 2 down
  215.                 board[x+2][y-1]--;  //boardValue(x+2,y-1) - 1;
  216.                
  217.             }if(checkMove(x-1, y-2)) {// move 5 down 2 left
  218.                 board[x-1][y-2]--;  //boardValue(x-1,y-2) - 1;
  219.                
  220.             }if(checkMove(x-1, y+2)) {// move 6 down 2 right
  221.                 board[x-1][y+2]--;  //boardValue(x-1,y-2) - 1;
  222.                
  223.             }if(checkMove(x-2, y+1)) {// move 7 left 2 up
  224.                 board[x-2][y+1]--;  //boardValue(x-2,y+1) - 1;
  225.                
  226.             }if(checkMove(x-2, y-1)) {// move 8 left 2 down
  227.                 board[x-2][y-1]--;  //boardValue(x-2,y-1) - 1;
  228.             }
  229.         }
  230.         public static void printBoard()
  231.         {
  232.             System.out.println("\nState of board\n");
  233.             for (int i=0;i<8;i++) {
  234.                 for (int j=0;j<8;j++) {
  235.                     System.out.print(board[i][j] +" ");
  236.                 }
  237.                 System.out.println("");
  238.                     }
  239.                 }
  240.         public static void printTrail() {
  241.                     System.out.println("Moves by the Knight\n");
  242.             for (int i=0;i<8;i++) {
  243.                 for (int j=0;j<8;j++) {
  244.                     System.out.print(trail[i][j] +"   ");
  245.                 }
  246.                 System.out.println("");
  247.                   }
  248.  
  249.                     moveKnight(origx,origy);
  250.                 }
  251.                 public static void clearTrail() {
  252.                     for (int i=0;i<8;i++) {
  253.                 for (int j=0;j<8;j++) {
  254.                     trail[i][j] = 0;
  255.                         }
  256.                     }
  257.                 }
  258.                 public static void setupBoard() {
  259.                    
  260.            
  261.             board[0][0]= 2; board[0][1]= 3; board[0][2] = 4; board[0][3] = 4; board[0][4] = 4; board[0][5] = 4; board[0][6] = 3; board[0][7] = 2;
  262.             board[1][0]= 3; board[1][1]= 4; board[1][2] = 6; board[1][3] = 6; board[1][4] = 6; board[1][5] = 6; board[1][6] = 4; board[1][7] = 3;
  263.             board[2][0]= 4; board[2][1]= 6; board[2][2] = 8; board[2][3] = 8; board[2][4] = 8; board[2][5] = 8; board[2][6] = 6; board[3][7] = 4;
  264.             board[3][0]= 4; board[3][1]= 6; board[3][2] = 8; board[3][3] = 8; board[3][4] = 8; board[3][5] = 8; board[3][6] = 6; board[3][7] = 4;
  265.             board[4][0]= 4; board[4][1]= 6; board[4][2] = 8; board[4][3] = 8; board[5][4] = 8; board[4][5] = 8; board[4][6] = 6; board[4][7] = 4;
  266.             board[5][0]= 4; board[5][1]= 6; board[5][2] = 8; board[5][3] = 8; board[5][4] = 8; board[5][5] = 8; board[5][6] = 6; board[5][7] = 4;
  267.             board[6][0]= 3; board[6][1]= 4; board[6][2] = 6; board[6][3] = 6; board[6][4] = 6; board[6][5] = 6; board[6][6] = 4; board[6][7] = 3;
  268.             board[7][0]= 2; board[7][1]= 3; board[7][2] = 4; board[7][3] = 4; board[7][4] = 4; board[7][5] = 4; board[7][6] = 3; board[7][7] = 2;
  269.            
  270.                         board[x][y] = 0;
  271.                 }
  272.              
  273.     }
  274. // main.java
  275. public class main{
  276.  
  277.     public static void main(String args[])
  278.     {
  279.         Knights myKnightsTour = new Knights();
  280.         while(1 == 1) {
  281.                 for(int i=0;i<8;i++)
  282.                     for(int j=0;j<8;j++)
  283.                         if(myKnightsTour.moveKnight(i,j)) {
  284.                         }
  285.             }
  286.  
  287.     }
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement