Advertisement
Guest User

Untitled

a guest
Apr 13th, 2018
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.57 KB | None | 0 0
  1. //
  2. // Source code recreated from a .class file by IntelliJ IDEA
  3. // (powered by Fernflower decompiler)
  4. //
  5.  
  6. package Rules;
  7.  
  8. import java.util.ArrayList;
  9. import java.util.HashMap;
  10. import java.util.Iterator;
  11.  
  12. public class Rules {
  13.     public Rules() {
  14.     }
  15.  
  16.     public static HashMap<String, Move> getPossibleMovesMap(Chessboard state) {
  17.         HashMap<String, Move> moves = new HashMap();
  18.         Iterator var2 = getPossibleMoves(state, true, false).iterator();
  19.  
  20.         while(var2.hasNext()) {
  21.             Move move = (Move)var2.next();
  22.             moves.put(move.toString(), move);
  23.         }
  24.  
  25.         return moves;
  26.     }
  27.  
  28.     public static ArrayList<Move> getPossibleMoves(Chessboard state) {
  29.         return getPossibleMoves(state, true, false);
  30.     }
  31.  
  32.     protected static ArrayList<Move> getPossibleMoves(Chessboard state, boolean onlyLegal, boolean recursive) {
  33.         ArrayList<Move> moves = new ArrayList();
  34.         if (!recursive && onlyLegal && state.getGameStatus() != 4) {
  35.             return moves;
  36.         } else {
  37.             int[][] board = state.getBoard();
  38.  
  39.             int k;
  40.             for(k = 0; k < 8; ++k) {
  41.                 for(int j = 0; j < 8; ++j) {
  42.                     if (state.getColor() == 1) {
  43.                         switch(board[k][j]) {
  44.                         case 1:
  45.                             moves.addAll(getPawnMoves(board, k, j, 1, state));
  46.                             break;
  47.                         case 2:
  48.                             moves.addAll(getBishopMoves(board, k, j, 1, state));
  49.                             break;
  50.                         case 3:
  51.                             moves.addAll(getKnightMoves(board, k, j, 1, state));
  52.                             break;
  53.                         case 4:
  54.                             moves.addAll(getRookMoves(board, k, j, 1, state));
  55.                             break;
  56.                         case 5:
  57.                             moves.addAll(getQueenMoves(board, k, j, 1, state));
  58.                             break;
  59.                         case 6:
  60.                             moves.addAll(getKingMoves(board, k, j, 1, state));
  61.                         }
  62.                     } else {
  63.                         switch(board[k][j]) {
  64.                         case -6:
  65.                             moves.addAll(getKingMoves(board, k, j, -1, state));
  66.                             break;
  67.                         case -5:
  68.                             moves.addAll(getQueenMoves(board, k, j, -1, state));
  69.                             break;
  70.                         case -4:
  71.                             moves.addAll(getRookMoves(board, k, j, -1, state));
  72.                             break;
  73.                         case -3:
  74.                             moves.addAll(getKnightMoves(board, k, j, -1, state));
  75.                             break;
  76.                         case -2:
  77.                             moves.addAll(getBishopMoves(board, k, j, -1, state));
  78.                             break;
  79.                         case -1:
  80.                             moves.addAll(getPawnMoves(board, k, j, -1, state));
  81.                         }
  82.                     }
  83.                 }
  84.             }
  85.  
  86.             if (onlyLegal) {
  87.                 for(k = 0; k < moves.size() && k > -1; ++k) {
  88.                     boolean removed = false;
  89.                     Move move = (Move)moves.get(k);
  90.                     state.makeMove(move, false);
  91.                     board = state.getBoard();
  92.  
  93.                     for(int i = 0; i < 8; ++i) {
  94.                         for(int j = 0; j < 8; ++j) {
  95.                             if (!removed) {
  96.                                 if (board[i][j] * state.getColor() == 6 && isCovered(board, i, j, -state.getColor())) {
  97.                                     moves.remove(k);
  98.                                     removed = true;
  99.                                     --k;
  100.                                 }
  101.  
  102.                                 if (board[i][j] * state.getColor() == -6 && isCovered(board, i, j, state.getColor())) {
  103.                                     if (state.getMovesLeft() != 0 && Chessboard.getRuleSet() == 0) {
  104.                                         moves.remove(k);
  105.                                         removed = true;
  106.                                         --k;
  107.                                     } else if (!recursive) {
  108.                                         move.checkmate = 1;
  109.                                         state.switchColors();
  110.                                         if (getPossibleMoves(state, true, true).size() == 0) {
  111.                                             move.checkmate = 2;
  112.                                         }
  113.  
  114.                                         state.switchColors();
  115.                                     }
  116.                                 }
  117.                             }
  118.                         }
  119.                     }
  120.  
  121.                     state.reverseMove(move, false);
  122.                 }
  123.             }
  124.  
  125.             return moves;
  126.         }
  127.     }
  128.  
  129.     public static ArrayList<Move> getRookMoves(int[][] board, int i, int j, int color, Chessboard state) {
  130.         ArrayList<Move> moves = new ArrayList();
  131.         int[][] directions = new int[][]{{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
  132.         int[][] var7 = directions;
  133.         int var8 = directions.length;
  134.  
  135.         for(int var9 = 0; var9 < var8; ++var9) {
  136.             int[] direction = var7[var9];
  137.             int x = i;
  138.             int y = j;
  139.  
  140.             while(true) {
  141.                 x += direction[0];
  142.                 y += direction[1];
  143.                 if (x < 0 || y < 0 || x > 7 || y > 7 || board[x][y] * color > 0) {
  144.                     break;
  145.                 }
  146.  
  147.                 if (board[x][y] == 0) {
  148.                     moves.add(new Move(i, j, x, y, 0, 0, state, board[x][y]));
  149.                 } else if (board[x][y] * color < 0) {
  150.                     moves.add(new Move(i, j, x, y, 0, 0, state, board[x][y]));
  151.                     break;
  152.                 }
  153.             }
  154.         }
  155.  
  156.         return moves;
  157.     }
  158.  
  159.     public static ArrayList<Move> getBishopMoves(int[][] board, int i, int j, int color, Chessboard state) {
  160.         ArrayList<Move> moves = new ArrayList();
  161.         int[][] directions = new int[][]{{1, 1}, {-1, 1}, {-1, -1}, {1, -1}};
  162.         int[][] var7 = directions;
  163.         int var8 = directions.length;
  164.  
  165.         for(int var9 = 0; var9 < var8; ++var9) {
  166.             int[] direction = var7[var9];
  167.             int x = i;
  168.             int y = j;
  169.  
  170.             while(true) {
  171.                 x += direction[0];
  172.                 y += direction[1];
  173.                 if (x < 0 || y < 0 || x > 7 || y > 7 || board[x][y] * color > 0) {
  174.                     break;
  175.                 }
  176.  
  177.                 if (board[x][y] == 0) {
  178.                     moves.add(new Move(i, j, x, y, 0, 0, state, board[x][y]));
  179.                 } else if (board[x][y] * color < 0) {
  180.                     moves.add(new Move(i, j, x, y, 0, 0, state, board[x][y]));
  181.                     break;
  182.                 }
  183.             }
  184.         }
  185.  
  186.         return moves;
  187.     }
  188.  
  189.     public static ArrayList<Move> getQueenMoves(int[][] board, int i, int j, int color, Chessboard state) {
  190.         ArrayList<Move> moves = getRookMoves(board, i, j, color, state);
  191.         moves.addAll(getBishopMoves(board, i, j, color, state));
  192.         return moves;
  193.     }
  194.  
  195.     public static ArrayList<Move> getKingMoves(int[][] board, int i, int j, int color, Chessboard state) {
  196.         ArrayList<Move> moves = new ArrayList();
  197.         int[][] directions = new int[][]{{1, 1}, {-1, 1}, {-1, -1}, {1, -1}, {1, 0}, {0, 1}, {-1, 0}, {0, -1}};
  198.         int[][] var7 = directions;
  199.         int var8 = directions.length;
  200.  
  201.         for(int var9 = 0; var9 < var8; ++var9) {
  202.             int[] direction = var7[var9];
  203.             int x = i + direction[0];
  204.             int y = j + direction[1];
  205.             if (x >= 0 && y >= 0 && x < 8 && y < 8 && (board[x][y] == 0 || board[x][y] * color < 0)) {
  206.                 moves.add(new Move(i, j, x, y, 0, 0, state, board[x][y]));
  207.             }
  208.         }
  209.  
  210.         if (i == 0 && j == 4) {
  211.             if (!state.e1 && !state.a1 && board[0][1] == 0 && board[0][2] == 0 && board[0][3] == 0 && board[0][0] != 0 && !isCovered(board, 0, 4, color * -1) && !isCovered(board, 0, 3, color * -1)) {
  212.                 moves.add(new Move(i, j, i, j - 2, 0, 0, 2, state, 0));
  213.             }
  214.  
  215.             if (!state.e1 && !state.h1 && board[0][5] == 0 && board[0][6] == 0 && board[0][7] != 0 && !isCovered(board, 0, 4, color * -1) && !isCovered(board, 0, 5, color * -1)) {
  216.                 moves.add(new Move(i, j, i, j + 2, 0, 0, 1, state, 0));
  217.             }
  218.         }
  219.  
  220.         if (i == 7 && j == 4) {
  221.             if (!state.e8 && !state.a8 && board[7][1] == 0 && board[7][2] == 0 && board[7][3] == 0 && board[7][0] != 0 && !isCovered(board, 7, 4, color * -1) && !isCovered(board, 7, 3, color * -1)) {
  222.                 moves.add(new Move(i, j, i, j - 2, 0, 0, 2, state, 0));
  223.             }
  224.  
  225.             if (!state.e8 && !state.h8 && board[7][5] == 0 && board[7][6] == 0 && board[7][7] != 0 && !isCovered(board, 7, 4, color * -1) && !isCovered(board, 7, 5, color * -1)) {
  226.                 moves.add(new Move(i, j, i, j + 2, 0, 0, 1, state, 0));
  227.             }
  228.         }
  229.  
  230.         return moves;
  231.     }
  232.  
  233.     public static ArrayList<Move> getKnightMoves(int[][] board, int i, int j, int color, Chessboard state) {
  234.         ArrayList<Move> moves = new ArrayList();
  235.         int[][] directions = new int[][]{{1, 2}, {2, 1}, {-1, 2}, {2, -1}, {-1, -2}, {-2, -1}, {-2, 1}, {1, -2}};
  236.         int[][] var7 = directions;
  237.         int var8 = directions.length;
  238.  
  239.         for(int var9 = 0; var9 < var8; ++var9) {
  240.             int[] direction = var7[var9];
  241.             int x = i + direction[0];
  242.             int y = j + direction[1];
  243.             if (x >= 0 && y >= 0 && x < 8 && y < 8 && (board[x][y] == 0 || board[x][y] * color < 0)) {
  244.                 moves.add(new Move(i, j, x, y, 0, 0, state, board[x][y]));
  245.             }
  246.         }
  247.  
  248.         return moves;
  249.     }
  250.  
  251.     public static ArrayList<Move> getPawnMoves(int[][] board, int i, int j, int color, Chessboard state) {
  252.         ArrayList<Move> moves = new ArrayList();
  253.         if (i + color >= 0 && i + color < 8 && board[i + color * 1][j] == 0) {
  254.             if ((i + color != 0 || color != -1) && (i + color != 7 || color != 1)) {
  255.                 moves.add(new Move(i, j, i + color, j, 0, 0, state, 0));
  256.             } else {
  257.                 moves.add(new Move(i, j, i + color, j, 0, 5, state, 0));
  258.                 moves.add(new Move(i, j, i + color, j, 0, 3, state, 0));
  259.                 moves.add(new Move(i, j, i + color, j, 0, 2, state, 0));
  260.                 moves.add(new Move(i, j, i + color, j, 0, 4, state, 0));
  261.             }
  262.         }
  263.  
  264.         if ((i == 1 && color == 1 || i == 6 && color == -1) && board[i + color][j] == 0 && board[i + color * 2][j] == 0) {
  265.             moves.add(new Move(i, j, i + 2 * color, j, 0, 0, state, 0));
  266.         }
  267.  
  268.         if (i + color >= 0 && i + color < 8) {
  269.             if (j != 7 && board[i + color][j + 1] * color < 0) {
  270.                 if ((i + color != 0 || color != -1) && (i + color != 7 || color != 1)) {
  271.                     moves.add(new Move(i, j, i + color, j + 1, 0, 0, state, board[i + color][j + 1]));
  272.                 } else {
  273.                     moves.add(new Move(i, j, i + color, j + 1, 0, 5, state, board[i + color][j + 1]));
  274.                     moves.add(new Move(i, j, i + color, j + 1, 0, 3, state, board[i + color][j + 1]));
  275.                     moves.add(new Move(i, j, i + color, j + 1, 0, 2, state, board[i + color][j + 1]));
  276.                     moves.add(new Move(i, j, i + color, j + 1, 0, 4, state, board[i + color][j + 1]));
  277.                 }
  278.             }
  279.  
  280.             if (j != 0 && board[i + color][j - 1] * color < 0) {
  281.                 if ((i + color != 0 || color != -1) && (i + color != 7 || color != 1)) {
  282.                     moves.add(new Move(i, j, i + color, j - 1, 0, 0, state, board[i + color][j - 1]));
  283.                 } else {
  284.                     moves.add(new Move(i, j, i + color, j - 1, 0, 5, state, board[i + color][j - 1]));
  285.                     moves.add(new Move(i, j, i + color, j - 1, 0, 3, state, board[i + color][j - 1]));
  286.                     moves.add(new Move(i, j, i + color, j - 1, 0, 2, state, board[i + color][j - 1]));
  287.                     moves.add(new Move(i, j, i + color, j - 1, 0, 4, state, board[i + color][j - 1]));
  288.                 }
  289.             }
  290.         }
  291.  
  292.         return moves;
  293.     }
  294.  
  295.     public static boolean isCovered(int[][] board, int i, int j, int color) {
  296.         boolean covered = false;
  297.         int[][] directions = new int[][]{{1, 1}, {-1, 1}, {-1, -1}, {1, -1}, {1, 0}, {0, 1}, {-1, 0}, {0, -1}};
  298.         int[][] directions2 = directions;
  299.         int var7 = directions.length;
  300.  
  301.         int var8;
  302.         int x;
  303.         int x;
  304.         for(var8 = 0; var8 < var7; ++var8) {
  305.             int[] direction = directions2[var8];
  306.             x = i + direction[0];
  307.             x = j + direction[1];
  308.             if (x >= 0 && x >= 0 && x < 8 && x < 8 && board[x][x] * color == 6) {
  309.                 covered = true;
  310.                 break;
  311.             }
  312.         }
  313.  
  314.         directions2 = new int[][]{{1, 2}, {2, 1}, {-1, 2}, {2, -1}, {-1, -2}, {-2, -1}, {-2, 1}, {1, -2}};
  315.         int[][] directions3 = directions2;
  316.         var8 = directions2.length;
  317.  
  318.         int x;
  319.         int var17;
  320.         for(var17 = 0; var17 < var8; ++var17) {
  321.             int[] direction = directions3[var17];
  322.             x = i + direction[0];
  323.             x = j + direction[1];
  324.             if (x >= 0 && x >= 0 && x < 8 && x < 8 && board[x][x] * color == 3) {
  325.                 covered = true;
  326.                 break;
  327.             }
  328.         }
  329.  
  330.         directions3 = new int[][]{{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
  331.         int[][] directions4 = directions3;
  332.         var17 = directions3.length;
  333.  
  334.         int x;
  335.         for(x = 0; x < var17; ++x) {
  336.             int[] direction = directions4[x];
  337.             x = i;
  338.             x = j;
  339.  
  340.             while(true) {
  341.                 x += direction[0];
  342.                 x += direction[1];
  343.                 if (x < 0 || x < 0 || x > 7 || x > 7 || board[x][x] * color < 0) {
  344.                     break;
  345.                 }
  346.  
  347.                 if (board[x][x] * color == 4 || board[x][x] * color == 5) {
  348.                     covered = true;
  349.                     break;
  350.                 }
  351.  
  352.                 if (board[x][x] * color > 0) {
  353.                     break;
  354.                 }
  355.             }
  356.         }
  357.  
  358.         directions4 = new int[][]{{1, 1}, {-1, 1}, {-1, -1}, {1, -1}};
  359.         int[][] var19 = directions4;
  360.         x = directions4.length;
  361.  
  362.         for(x = 0; x < x; ++x) {
  363.             int[] direction = var19[x];
  364.             x = i;
  365.             int y = j;
  366.  
  367.             while(true) {
  368.                 x += direction[0];
  369.                 y += direction[1];
  370.                 if (x < 0 || y < 0 || x > 7 || y > 7 || board[x][y] * color < 0) {
  371.                     break;
  372.                 }
  373.  
  374.                 if (board[x][y] * color == 2 || board[x][y] * color == 5) {
  375.                     covered = true;
  376.                     break;
  377.                 }
  378.  
  379.                 if (board[x][y] * color > 0) {
  380.                     break;
  381.                 }
  382.             }
  383.         }
  384.  
  385.         if (i - color >= 0 && i - color < 8) {
  386.             if (j != 0 && board[i - color][j - 1] * color == 1) {
  387.                 covered = true;
  388.             }
  389.  
  390.             if (j != 7 && board[i - color][j + 1] * color == 1) {
  391.                 covered = true;
  392.             }
  393.         }
  394.  
  395.         return covered;
  396.     }
  397. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement