Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // TiK
- // Knights.java
- // infinite loop an example of brute orce, Try to get a Knight to touch each square on the board.
- import java.util.Random;
- public class Knights
- {
- private static int board[][];
- private static int x, y,origx,origy,specialCase;
- private static int numberOfMoves = 1;
- private static int highest = 0;
- private static int[] value = new int[8];
- private static int[][] trail = new int[8][8];
- public Knights()
- {
- board = new int[8][8];
- }
- public static int getX(){ return x;}
- public static void setX(int x){ Knights.x = x;}
- public static int getY(){ return y;}
- public static void setY(int y){ Knights.y = y;}
- public static boolean moveKnight(int x,int y)
- {
- Random myRandom = new Random();
- int moveIndex, rChoice;
- boolean solved = false;
- int counter = 0;
- int iCounter = 0;
- origx = x;
- origy = y;
- for(int i=0;i<8;i++)
- value[i]=0;
- setupBoard();
- board[7][3]=4;
- printBoard();
- trail[origx][origy] = 1;
- while(!solved) {
- if(checkMove(Knights.x+1, Knights.y+2)) { //Move 1 up 2 right
- value[0] = boardValue(Knights.x+1,Knights.x+2);
- }if(checkMove(Knights.x+1, Knights.y-2)) { //Move 2 up 2 left
- value[1] = boardValue(Knights.x+1,Knights.y-2);
- }if(checkMove(Knights.x+2, Knights.y+1)) { //Move 3 right 2 up
- value[2] = boardValue(Knights.x+2,Knights.y+1);
- }if(checkMove(Knights.x+2, Knights.y-1)) { //Move 4 right 2 down
- value[3] = boardValue(Knights.x+2,Knights.y-1);
- }if(checkMove(Knights.x-1, Knights.y-2)) { //Move 5 down 2 left
- value[4] = boardValue(Knights.x-1,Knights.y-2);
- }if(checkMove(Knights.x-1, Knights.y+2)) { //Move 6 down 2 right
- value[5] = boardValue(Knights.x-1,Knights.y+2);
- }if(checkMove(Knights.x-2, Knights.y+1)) {//Move 7 left 2 up
- value[6] = boardValue(Knights.x-2,Knights.y+1);
- }if(checkMove(Knights.x-2, Knights.y-1)) { // Move 8 left 2 down
- value[7] = boardValue(Knights.x-2,Knights.y-1);
- }
- //System.out.print("\nPrinting out value[]\n");
- // for(int i=0;i<8;i++) {
- // System.out.print(+value[i]);
- // }
- for(int i=0;i<8;i++) {
- if(value[i] > highest)
- highest = value[i];
- }
- //System.out.print("\njust exited highest loop: " +highest);
- if(highest == 0) {
- System.out.print("knight stuck, starting over\n");
- clearTrail();
- numberOfMoves = 1;
- printBoard();
- setupBoard();
- setX(origx);
- setY(origy);
- break;
- }
- for(int i=0;i<8;i++) {
- if(highest == value[i]) {
- counter++;
- }
- }
- //System.out.print("\nJust exited counter loop " +counter);
- if(counter == 1) {
- for(specialCase=0;specialCase<8;specialCase++) {
- if(value[specialCase] == highest) {
- break;
- }
- }
- //System.out.print("\nspecialCase: "+specialCase);
- knightSwitch(specialCase);
- }else {
- rChoice = myRandom.nextInt(counter) + 1;
- //System.out.print("\nRandom number "+rChoice);
- iCounter = 0;
- for(moveIndex=0;moveIndex<8;moveIndex++) {
- if(highest == value[moveIndex]) {
- iCounter++;
- //System.out.print("\niCounter: "+iCounter);
- if(iCounter == rChoice) {
- //System.out.print("moveIndex: "+moveIndex);
- knightSwitch(moveIndex);
- }
- }
- }
- }
- //System.out.print("\nClearing value");
- for(int j=0;j<8;j++) // clear value
- value[j]=0;
- //System.out.print("\nReseting counter");
- counter = 0;
- //System.out.print("Clearing highest");
- highest = 0;
- if(numberOfMoves == 63) {
- solved = true;
- printTrail();
- }
- }
- if(numberOfMoves == 63)
- return false;
- return true;
- }
- public static void knightSwitch(int move) {
- switch (move)
- {
- case 0:
- makeMove(x+1, y+2); // up 2 right
- break;
- case 1:
- makeMove(x+1, y-2); // down 2 right
- break;
- case 2:
- makeMove(x+2, y+1); // right 2 up
- break;
- case 3:
- makeMove(x+2, y-1); // right 2 down
- break;
- case 4:
- makeMove(x-1, y-2); // down 2 left
- break;
- case 5:
- makeMove(x-1, y+2); // down 2 right
- break;
- case 6:
- makeMove(x-2, y+1); // left 2 up
- break;
- case 7:
- makeMove(x-2, y-1); // left 2 down
- break;
- }
- }
- public static boolean checkMove(int x, int y)
- {
- if((y >= 0) && (y < 8) && (x >= 0) && (x < 8))
- if(!(board[x][y] == 0))
- return true;
- return false;
- }
- public static void makeMove(int x, int y)
- {
- System.out.printf("\nMove %d from (%d,%d) to (%d,%d)\n", numberOfMoves++, Knights.x, Knights.y, x, y);
- board[x][y] = 0;
- trail[x][y] = numberOfMoves;
- Knights.x = x;
- Knights.y = y;
- reDraw();
- //printBoard();
- }
- public static int boardValue(int x,int y) {
- // System.out.printf("Value of Square %d %d choices %d ", x, y, board[x][y]);
- // System.out.println("");
- if(checkMove(x,y))
- return board[x][y];
- return 0;
- }
- public static void reDraw() {
- //System.out.print("\nRedrawing Board");
- if(checkMove(x+1, y+2)) {// move 1 up 2 right
- board[x+1][y+2]--; //boardValue(x+1,y+2) - 1;
- }if(checkMove(x+1, y-2)) { // move 2 up 2 left
- board[x+1][y-2]--; //boardValue(x+1,y-2) - 1;
- }if(checkMove(x+2, y+1)) {// move 3 right 2 up
- board[x+2][y+1]--; //boardValue(x+2,y+1) - 1;
- }if(checkMove(x+2, y-1)) {// move 4 right 2 down
- board[x+2][y-1]--; //boardValue(x+2,y-1) - 1;
- }if(checkMove(x-1, y-2)) {// move 5 down 2 left
- board[x-1][y-2]--; //boardValue(x-1,y-2) - 1;
- }if(checkMove(x-1, y+2)) {// move 6 down 2 right
- board[x-1][y+2]--; //boardValue(x-1,y-2) - 1;
- }if(checkMove(x-2, y+1)) {// move 7 left 2 up
- board[x-2][y+1]--; //boardValue(x-2,y+1) - 1;
- }if(checkMove(x-2, y-1)) {// move 8 left 2 down
- board[x-2][y-1]--; //boardValue(x-2,y-1) - 1;
- }
- }
- public static void printBoard()
- {
- System.out.println("\nState of board\n");
- for (int i=0;i<8;i++) {
- for (int j=0;j<8;j++) {
- System.out.print(board[i][j] +" ");
- }
- System.out.println("");
- }
- }
- public static void printTrail() {
- System.out.println("Moves by the Knight\n");
- for (int i=0;i<8;i++) {
- for (int j=0;j<8;j++) {
- System.out.print(trail[i][j] +" ");
- }
- System.out.println("");
- }
- moveKnight(origx,origy);
- }
- public static void clearTrail() {
- for (int i=0;i<8;i++) {
- for (int j=0;j<8;j++) {
- trail[i][j] = 0;
- }
- }
- }
- public static void setupBoard() {
- 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;
- 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;
- 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;
- 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;
- 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;
- 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;
- 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;
- 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;
- board[x][y] = 0;
- }
- }
- // main.java
- public class main{
- public static void main(String args[])
- {
- Knights myKnightsTour = new Knights();
- while(1 == 1) {
- for(int i=0;i<8;i++)
- for(int j=0;j<8;j++)
- if(myKnightsTour.moveKnight(i,j)) {
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement