Advertisement
Guest User

Untitled

a guest
Jul 15th, 2016
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.27 KB | None | 0 0
  1. package de.zabuza.exercise;
  2.  
  3. /**
  4.  * Exercise. Representates a custom chess board. Can calculate recursive
  5.  * which cells a knight can reach in a maximum amount of draws. Also can
  6.  * calculate how much draws are needed to reach a destination.
  7.  *
  8.  * @author Zabuza
  9.  *
  10.  */
  11. public final class ChessRecursion {
  12.  
  13.     /**
  14.      * Demo-usage of a chess board.
  15.      *
  16.      * @param args
  17.      *            Not supported.
  18.      */
  19.     public static void main(final String[] args) {
  20.         ChessRecursion chess1 = new ChessRecursion(9, 9);
  21.         chess1.markCellsKnight(4, 4, 2);
  22.         chess1.printBoard();
  23.         ChessRecursion chess2 = new ChessRecursion(15, 15);
  24.         System.out.println();
  25.         System.out
  26.                 .println("Field 14|14 is reached in "
  27.                         + ChessRecursion.shortestDraw(chess2, 0, 0, 14, 14)
  28.                         + " moves.");
  29.     }
  30.  
  31.     /**
  32.      * Calculates how much draws are needed to reach a destination from a custom
  33.      * start on a chess board. This method is very very expensive. It is very
  34.      * easy to create more efficient methods for this problem but the method is
  35.      * fine for the given task.
  36.      *
  37.      * @param chess
  38.      *            Chess board where to calculate
  39.      * @param startX
  40.      *            Starting x-coordinate
  41.      * @param startY
  42.      *            Starting y-coordinate
  43.      * @param destX
  44.      *            Destination x-coordinate
  45.      * @param destY
  46.      *            Destination y-coordinate
  47.      * @return Amount of draws which are needed to reach the destination
  48.      */
  49.     public static int shortestDraw(final ChessRecursion chess,
  50.             final int startX, final int startY, final int destX, final int destY) {
  51.         if (startX < 0 || startY < 0 || destX < 0 || destY < 0
  52.                 || startX >= chess.getWidth() || destX >= chess.getWidth()
  53.                 || startY >= chess.getHeight() || destY >= chess.getHeight()) {
  54.             return -1;
  55.         }
  56.  
  57.         int k = 0;
  58.         while (chess.getCell(destX, destY) != 1) {
  59.             chess.clearBoard();
  60.             chess.markCellsKnight(startX, startY, k);
  61.             k++;
  62.         }
  63.         return k;
  64.     }
  65.  
  66.     /**
  67.      * Chess board as integer matrix.
  68.      */
  69.     private int[][] board;
  70.     /**
  71.      * Width of chess board.
  72.      */
  73.     private int width;
  74.  
  75.     /**
  76.      * Height of chess board.
  77.      */
  78.     private int height;
  79.  
  80.     /**
  81.      * Creates a new chess board with custom width and height.
  82.      *
  83.      * @param thatWidth
  84.      *            Width of chess board
  85.      * @param thatHeight
  86.      *            Height of chess board
  87.      */
  88.     public ChessRecursion(final int thatWidth, final int thatHeight) {
  89.         this.width = thatWidth;
  90.         this.height = thatHeight;
  91.         board = new int[width][height];
  92.         clearBoard();
  93.     }
  94.  
  95.     /**
  96.      * Clears the chess board.
  97.      */
  98.     public void clearBoard() {
  99.         for (int x = 0; x < width; x++) {
  100.             for (int y = 0; y < height; y++) {
  101.                 board[x][y] = 0;
  102.             }
  103.         }
  104.     }
  105.  
  106.     /**
  107.      * Gets a cell of the chess board.
  108.      *
  109.      * @param x
  110.      *            x-coordinate of the cell
  111.      * @param y
  112.      *            y-coordinate of the cell
  113.      * @return The cell
  114.      */
  115.     public int getCell(final int x, final int y) {
  116.         return board[x][y];
  117.     }
  118.  
  119.     /**
  120.      * Gets the height of the chess board.
  121.      *
  122.      * @return Height of the chess board
  123.      */
  124.     public int getHeight() {
  125.         return height;
  126.     }
  127.  
  128.     /**
  129.      * Gets the width of the chess board.
  130.      *
  131.      * @return Width of the chess board
  132.      */
  133.     public int getWidth() {
  134.         return width;
  135.     }
  136.  
  137.     /**
  138.      * Calculates recursive which cells a knight can reach maximal in a custom
  139.      * amount of draws.
  140.      *
  141.      * @param x
  142.      *            Starting coordinate x
  143.      * @param y
  144.      *            Starting coordinate y
  145.      * @param k
  146.      *            Maximal amount of draws
  147.      */
  148.     public void markCellsKnight(final int x, final int y, final int k) {
  149.         if (x < 0 || x >= width || y < 0 || y >= height) {
  150.             return;
  151.         }
  152.  
  153.         board[x][y] = 1;
  154.  
  155.         if (k <= 0) {
  156.             return;
  157.         }
  158.  
  159.         markCellsKnight(x + 2, y + 1, k - 1);
  160.         markCellsKnight(x + 2, y - 1, k - 1);
  161.         markCellsKnight(x - 2, y + 1, k - 1);
  162.         markCellsKnight(x - 2, y - 1, k - 1);
  163.         markCellsKnight(x + 1, y + 2, k - 1);
  164.         markCellsKnight(x - 1, y + 2, k - 1);
  165.         markCellsKnight(x + 1, y - 2, k - 1);
  166.         markCellsKnight(x - 1, y - 2, k - 1);
  167.     }
  168.  
  169.     /**
  170.      * Prints the chess board to System.out.
  171.      */
  172.     public void printBoard() {
  173.         for (int x = 0; x < width; x++) {
  174.             System.out.print("-");
  175.             for (int y = 0; y < height; y++) {
  176.                 System.out.print(board[x][y] + "-");
  177.             }
  178.             System.out.println();
  179.         }
  180.     }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement