All pastes #926974 Raw Edit

Move class

public java v1 · immutable
·published 2008-03-03 22:58 UTC
rendered paste body
public class Move {    private int[][] board = new int[8][8]; // holds positions of knight    private int[][] accessibility = {{2, 3, 4, 4, 4, 4, 3, 2},                                            {3, 4, 6, 6, 6, 6, 4, 3},                                            {4, 6, 8, 8, 8, 8, 6, 4},                                            {4, 6, 8, 8, 8, 8, 6, 4},                                            {4, 6, 8, 8, 8, 8, 6, 4},                                            {4, 6, 8, 8, 8, 8, 6, 4},                                            {3, 4, 6, 6, 6, 6, 4, 3},                                            {2, 3, 4, 4, 4, 4, 3, 2}}; // holds accessibility of every square    private int[] horizontal = {2, 1, -1, -2, -2, -1, 1, 2}; // holds possible moves in the horizontal direction    private int[] vertical = {-1, -2, -2, -1, 1, 2, 2, 1}; // holds possible moves in the vertical direction       public Move()    {    }       public int move(int currentRow, int currentColumn) {    	        // initiates array to 0        for (int row = 0; row < board.length; row++)            for (int column = 0; column < (board[row].length); column++)                board[row][column] = 0;        int moveCounter = 1;        int access = 0;        int triedMoves = 0;        do {            for (access = 1; access <= 7; access++) {                int toBreak = 0;                for (int moveNumber = 0; moveNumber <= 7; moveNumber++) {                    triedMoves++;                    if (best(currentRow, currentColumn, moveNumber, access))                    {                        board[currentRow][currentColumn] = moveCounter;                        accessibility[currentRow][currentColumn] = 0;                        updateAccessibility(currentRow, currentColumn);                                               currentRow += vertical[moveNumber];                        currentColumn += horizontal[moveNumber];                        moveCounter++;                                               toBreak = 1;                        break;                    }                    if (toBreak == 1)                        break;                }            }        } while (triedMoves < 4096);        for (int row = 0; row < board.length; row++) {            System.out.println();            for (int column = 0; column < (board[row].length); column++)                System.out.print(board[row][column] + "	");        }                System.out.println();        System.out.println();                return moveCounter;    }       public boolean best(int currentRow, int currentColumn, int moveNumber, int access)    {        if (valid(currentRow, currentColumn, moveNumber)                && (accessibility[currentRow + vertical[moveNumber]][currentColumn                        + horizontal[moveNumber]]) == access)                        return true;        else            return false;                           }       public boolean valid(int currentRow, int currentColumn, int moveNumber)    {        if (((currentRow + vertical[moveNumber])) <= 7                && ((currentRow + vertical[moveNumber])) >= 0                && currentColumn + horizontal[moveNumber] <= 7                && currentColumn + horizontal[moveNumber] >= 0)            return true;        else            return false;                           }       public void updateAccessibility(int currentRow, int currentColumn)    {        for (int moveNumber = 0; moveNumber <= 7; moveNumber++)        {            if ((valid(currentRow, currentColumn, moveNumber) &&             		((currentRow + vertical[moveNumber]) != 0) &&             		(currentColumn + horizontal[moveNumber] != 0)))            {                accessibility[currentRow + (vertical[moveNumber])][currentColumn + horizontal[moveNumber]] -= 1;                System.out.print("i");            }        }       System.out.print(" ");        //     /**        // prints accessibility - for tracing       for (int row = 0; row < board.length; row++) {           System.out.println();           for (int column = 0; column < (board[row].length); column++)               System.out.print(accessibility[row][column] + "	");       }       System.out.println();//       */           }}GLOBAL VARIABLES:*the board*the accessibility matrix*the eight moves*the knight's positionINITIALIZATION:*set all values in board to be 0*initialize the accessbility matrixDO ONE TOUR:*parameters: (x, y)*local variable: number of moves*set the knight's current position as the parameters*set that board square to be 1*while the knight can move, move it and increment number of moves    - call the move knight function to do this*output board and number of movesMOVE THE KNIGHT:*parameter: move number*check to see which of 8 moves are possible    - possible if move stays on board AND square has not yet been visited*generate a pseudo-random move from those 8*if no move is possible, return false (to tell the calling method to quit)*for every move that *was* possible:    - temporarily move there and decrement that square's accessbility value*move the knight to one of those squares*set that square's board value to the move number*return trueGENERATE A PSEUDO-RANDOM MOVE:*parameter: boolean array saying if each move is valid*if all array values are false, return -1*for each possible move:    - temporarily move the knight there    - keep track of the lowest accessbility number*for every possible move:    - if its accessibility number ties with the lowest, store that move*for all moves that tied, either pick the first one or pick a pseudo-random one    - depends on Mr. Pham's specs*return the number of the picked move (0...7)