Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.zabuza.exercise;
- /**
- * Exercise. Representates a custom chess board. Can calculate recursive
- * which cells a knight can reach in a maximum amount of draws. Also can
- * calculate how much draws are needed to reach a destination.
- *
- * @author Zabuza
- *
- */
- public final class ChessRecursion {
- /**
- * Demo-usage of a chess board.
- *
- * @param args
- * Not supported.
- */
- public static void main(final String[] args) {
- ChessRecursion chess1 = new ChessRecursion(9, 9);
- chess1.markCellsKnight(4, 4, 2);
- chess1.printBoard();
- ChessRecursion chess2 = new ChessRecursion(15, 15);
- System.out.println();
- System.out
- .println("Field 14|14 is reached in "
- + ChessRecursion.shortestDraw(chess2, 0, 0, 14, 14)
- + " moves.");
- }
- /**
- * Calculates how much draws are needed to reach a destination from a custom
- * start on a chess board. This method is very very expensive. It is very
- * easy to create more efficient methods for this problem but the method is
- * fine for the given task.
- *
- * @param chess
- * Chess board where to calculate
- * @param startX
- * Starting x-coordinate
- * @param startY
- * Starting y-coordinate
- * @param destX
- * Destination x-coordinate
- * @param destY
- * Destination y-coordinate
- * @return Amount of draws which are needed to reach the destination
- */
- public static int shortestDraw(final ChessRecursion chess,
- final int startX, final int startY, final int destX, final int destY) {
- if (startX < 0 || startY < 0 || destX < 0 || destY < 0
- || startX >= chess.getWidth() || destX >= chess.getWidth()
- || startY >= chess.getHeight() || destY >= chess.getHeight()) {
- return -1;
- }
- int k = 0;
- while (chess.getCell(destX, destY) != 1) {
- chess.clearBoard();
- chess.markCellsKnight(startX, startY, k);
- k++;
- }
- return k;
- }
- /**
- * Chess board as integer matrix.
- */
- private int[][] board;
- /**
- * Width of chess board.
- */
- private int width;
- /**
- * Height of chess board.
- */
- private int height;
- /**
- * Creates a new chess board with custom width and height.
- *
- * @param thatWidth
- * Width of chess board
- * @param thatHeight
- * Height of chess board
- */
- public ChessRecursion(final int thatWidth, final int thatHeight) {
- this.width = thatWidth;
- this.height = thatHeight;
- board = new int[width][height];
- clearBoard();
- }
- /**
- * Clears the chess board.
- */
- public void clearBoard() {
- for (int x = 0; x < width; x++) {
- for (int y = 0; y < height; y++) {
- board[x][y] = 0;
- }
- }
- }
- /**
- * Gets a cell of the chess board.
- *
- * @param x
- * x-coordinate of the cell
- * @param y
- * y-coordinate of the cell
- * @return The cell
- */
- public int getCell(final int x, final int y) {
- return board[x][y];
- }
- /**
- * Gets the height of the chess board.
- *
- * @return Height of the chess board
- */
- public int getHeight() {
- return height;
- }
- /**
- * Gets the width of the chess board.
- *
- * @return Width of the chess board
- */
- public int getWidth() {
- return width;
- }
- /**
- * Calculates recursive which cells a knight can reach maximal in a custom
- * amount of draws.
- *
- * @param x
- * Starting coordinate x
- * @param y
- * Starting coordinate y
- * @param k
- * Maximal amount of draws
- */
- public void markCellsKnight(final int x, final int y, final int k) {
- if (x < 0 || x >= width || y < 0 || y >= height) {
- return;
- }
- board[x][y] = 1;
- if (k <= 0) {
- return;
- }
- markCellsKnight(x + 2, y + 1, k - 1);
- markCellsKnight(x + 2, y - 1, k - 1);
- markCellsKnight(x - 2, y + 1, k - 1);
- markCellsKnight(x - 2, y - 1, k - 1);
- markCellsKnight(x + 1, y + 2, k - 1);
- markCellsKnight(x - 1, y + 2, k - 1);
- markCellsKnight(x + 1, y - 2, k - 1);
- markCellsKnight(x - 1, y - 2, k - 1);
- }
- /**
- * Prints the chess board to System.out.
- */
- public void printBoard() {
- for (int x = 0; x < width; x++) {
- System.out.print("-");
- for (int y = 0; y < height; y++) {
- System.out.print(board[x][y] + "-");
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement