Advertisement
denyshubh

Java Code

Mar 14th, 2023
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.76 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class Main {
  4.   private static final String UPPER_LEFT = "UL";
  5.   private static final String UPPER_RIGHT = "UR";
  6.   private static final String LOWER_LEFT = "LL";
  7.   private static final String LOWER_RIGHT = "LR";
  8.   private static final String MISSING_SQUARE = "MS";
  9.  
  10.   private static void tromino(String[][] board, int x_board, int y_board, int x_missing, int y_missing, int board_size) {
  11.       if (board_size == 1) {
  12.           return;
  13.       }
  14.  
  15.       int half_size = board_size / 2;
  16.       int x_center = x_board + half_size;
  17.       int y_center = y_board + half_size;
  18.  
  19.       // Upper left subboard
  20.       if (x_missing < x_center && y_missing >= y_center) {
  21.           board[x_center - 1][y_center] = LOWER_RIGHT;
  22.           board[x_center][y_center] = UPPER_RIGHT;
  23.           board[x_center][y_center - 1] = LOWER_LEFT;
  24.           tromino(board, x_board, y_board + half_size, x_missing, y_missing, half_size);
  25.           tromino(board, x_board, y_board, x_center - 1, y_center, half_size);
  26.           tromino(board, x_board + half_size, y_board + half_size, x_center, y_center, half_size);
  27.           tromino(board, x_board + half_size, y_board, x_center, y_center - 1, half_size);
  28.       }
  29.       // Upper right subboard
  30.       else if (x_missing < x_center && y_missing < y_center) {
  31.           board[x_center - 1][y_center - 1] = LOWER_LEFT;
  32.           board[x_center][y_center] = UPPER_LEFT;
  33.           board[x_center][y_center - 1] = LOWER_RIGHT;
  34.           tromino(board, x_board, y_board + half_size, x_center - 1, y_center, half_size);
  35.           tromino(board, x_board, y_board, x_missing, y_missing, half_size);
  36.           tromino(board, x_board + half_size, y_board + half_size, x_center, y_center, half_size);
  37.           tromino(board, x_board + half_size, y_board, x_center, y_center - 1, half_size);
  38.       }
  39.       // Lower left subboard
  40.       else if (x_missing >= x_center && y_missing >= y_center) {
  41.           board[x_center - 1][y_center - 1] = UPPER_RIGHT;
  42.           board[x_center][y_center] = LOWER_LEFT;
  43.           board[x_center - 1][y_center] = UPPER_LEFT;
  44.           tromino(board, x_board, y_board + half_size, x_center - 1, y_center, half_size);
  45.           tromino(board, x_board + half_size, y_board + half_size, x_missing, y_missing, half_size);
  46.           tromino(board, x_board, y_board, x_center, y_center - 1, half_size);
  47.           tromino(board, x_board + half_size, y_board, x_center, y_center, half_size);
  48.       }
  49.       // Lower right subboard
  50.       else {
  51.           board[x_center - 1][y_center - 1] = UPPER_LEFT;
  52.           board[x_center][y_center - 1] = LOWER_RIGHT;
  53.           board[x_center - 1][y_center] = LOWER_LEFT;
  54.           tromino(board, x_board, y_board + half_size, x_center - 1, y_center, half_size);
  55.           tromino(board, x_board, y_board, x_center, y_center - 1, half_size);
  56.           tromino(board, x_board + half_size, y_board + half_size, x_missing, y_missing, half_size);
  57.           tromino(board, x_board + half_size, y_board, x_center, y_center, half_size);
  58.       }
  59.   }
  60.   public static void main(String[] args) {
  61.       int board_size = 8;
  62.       String[][] board = new String[board_size][board_size];
  63.       int x_missing = 2;
  64.       int y_missing = 2;
  65.       for (int i = 0; i < board_size; i++) {
  66.           for (int j = 0; j < board_size; j++) {
  67.               if (i == x_missing && j == y_missing) {
  68.                   board[i][j] = MISSING_SQUARE;
  69.               } else {
  70.                   board[i][j] = "";
  71.               }
  72.           }
  73.       }
  74.       tromino(board, 0, 0, x_missing, y_missing, board_size);
  75.       for (int i = 0; i < board_size; i++) {
  76.           System.out.println("\n");
  77.           for (int j = 0; j < board_size; j++) {
  78.               System.out.print(board[j][i] + "\t");
  79.           }
  80.       }
  81.   }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement