Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Source code recreated from a .class file by IntelliJ IDEA
- // (powered by Fernflower decompiler)
- //
- package Rules;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Iterator;
- public class Rules {
- public Rules() {
- }
- public static HashMap<String, Move> getPossibleMovesMap(Chessboard state) {
- HashMap<String, Move> moves = new HashMap();
- Iterator var2 = getPossibleMoves(state, true, false).iterator();
- while(var2.hasNext()) {
- Move move = (Move)var2.next();
- moves.put(move.toString(), move);
- }
- return moves;
- }
- public static ArrayList<Move> getPossibleMoves(Chessboard state) {
- return getPossibleMoves(state, true, false);
- }
- protected static ArrayList<Move> getPossibleMoves(Chessboard state, boolean onlyLegal, boolean recursive) {
- ArrayList<Move> moves = new ArrayList();
- if (!recursive && onlyLegal && state.getGameStatus() != 4) {
- return moves;
- } else {
- int[][] board = state.getBoard();
- int k;
- for(k = 0; k < 8; ++k) {
- for(int j = 0; j < 8; ++j) {
- if (state.getColor() == 1) {
- switch(board[k][j]) {
- case 1:
- moves.addAll(getPawnMoves(board, k, j, 1, state));
- break;
- case 2:
- moves.addAll(getBishopMoves(board, k, j, 1, state));
- break;
- case 3:
- moves.addAll(getKnightMoves(board, k, j, 1, state));
- break;
- case 4:
- moves.addAll(getRookMoves(board, k, j, 1, state));
- break;
- case 5:
- moves.addAll(getQueenMoves(board, k, j, 1, state));
- break;
- case 6:
- moves.addAll(getKingMoves(board, k, j, 1, state));
- }
- } else {
- switch(board[k][j]) {
- case -6:
- moves.addAll(getKingMoves(board, k, j, -1, state));
- break;
- case -5:
- moves.addAll(getQueenMoves(board, k, j, -1, state));
- break;
- case -4:
- moves.addAll(getRookMoves(board, k, j, -1, state));
- break;
- case -3:
- moves.addAll(getKnightMoves(board, k, j, -1, state));
- break;
- case -2:
- moves.addAll(getBishopMoves(board, k, j, -1, state));
- break;
- case -1:
- moves.addAll(getPawnMoves(board, k, j, -1, state));
- }
- }
- }
- }
- if (onlyLegal) {
- for(k = 0; k < moves.size() && k > -1; ++k) {
- boolean removed = false;
- Move move = (Move)moves.get(k);
- state.makeMove(move, false);
- board = state.getBoard();
- for(int i = 0; i < 8; ++i) {
- for(int j = 0; j < 8; ++j) {
- if (!removed) {
- if (board[i][j] * state.getColor() == 6 && isCovered(board, i, j, -state.getColor())) {
- moves.remove(k);
- removed = true;
- --k;
- }
- if (board[i][j] * state.getColor() == -6 && isCovered(board, i, j, state.getColor())) {
- if (state.getMovesLeft() != 0 && Chessboard.getRuleSet() == 0) {
- moves.remove(k);
- removed = true;
- --k;
- } else if (!recursive) {
- move.checkmate = 1;
- state.switchColors();
- if (getPossibleMoves(state, true, true).size() == 0) {
- move.checkmate = 2;
- }
- state.switchColors();
- }
- }
- }
- }
- }
- state.reverseMove(move, false);
- }
- }
- return moves;
- }
- }
- public static ArrayList<Move> getRookMoves(int[][] board, int i, int j, int color, Chessboard state) {
- ArrayList<Move> moves = new ArrayList();
- int[][] directions = new int[][]{{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
- int[][] var7 = directions;
- int var8 = directions.length;
- for(int var9 = 0; var9 < var8; ++var9) {
- int[] direction = var7[var9];
- int x = i;
- int y = j;
- while(true) {
- x += direction[0];
- y += direction[1];
- if (x < 0 || y < 0 || x > 7 || y > 7 || board[x][y] * color > 0) {
- break;
- }
- if (board[x][y] == 0) {
- moves.add(new Move(i, j, x, y, 0, 0, state, board[x][y]));
- } else if (board[x][y] * color < 0) {
- moves.add(new Move(i, j, x, y, 0, 0, state, board[x][y]));
- break;
- }
- }
- }
- return moves;
- }
- public static ArrayList<Move> getBishopMoves(int[][] board, int i, int j, int color, Chessboard state) {
- ArrayList<Move> moves = new ArrayList();
- int[][] directions = new int[][]{{1, 1}, {-1, 1}, {-1, -1}, {1, -1}};
- int[][] var7 = directions;
- int var8 = directions.length;
- for(int var9 = 0; var9 < var8; ++var9) {
- int[] direction = var7[var9];
- int x = i;
- int y = j;
- while(true) {
- x += direction[0];
- y += direction[1];
- if (x < 0 || y < 0 || x > 7 || y > 7 || board[x][y] * color > 0) {
- break;
- }
- if (board[x][y] == 0) {
- moves.add(new Move(i, j, x, y, 0, 0, state, board[x][y]));
- } else if (board[x][y] * color < 0) {
- moves.add(new Move(i, j, x, y, 0, 0, state, board[x][y]));
- break;
- }
- }
- }
- return moves;
- }
- public static ArrayList<Move> getQueenMoves(int[][] board, int i, int j, int color, Chessboard state) {
- ArrayList<Move> moves = getRookMoves(board, i, j, color, state);
- moves.addAll(getBishopMoves(board, i, j, color, state));
- return moves;
- }
- public static ArrayList<Move> getKingMoves(int[][] board, int i, int j, int color, Chessboard state) {
- ArrayList<Move> moves = new ArrayList();
- int[][] directions = new int[][]{{1, 1}, {-1, 1}, {-1, -1}, {1, -1}, {1, 0}, {0, 1}, {-1, 0}, {0, -1}};
- int[][] var7 = directions;
- int var8 = directions.length;
- for(int var9 = 0; var9 < var8; ++var9) {
- int[] direction = var7[var9];
- int x = i + direction[0];
- int y = j + direction[1];
- if (x >= 0 && y >= 0 && x < 8 && y < 8 && (board[x][y] == 0 || board[x][y] * color < 0)) {
- moves.add(new Move(i, j, x, y, 0, 0, state, board[x][y]));
- }
- }
- if (i == 0 && j == 4) {
- 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)) {
- moves.add(new Move(i, j, i, j - 2, 0, 0, 2, state, 0));
- }
- 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)) {
- moves.add(new Move(i, j, i, j + 2, 0, 0, 1, state, 0));
- }
- }
- if (i == 7 && j == 4) {
- 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)) {
- moves.add(new Move(i, j, i, j - 2, 0, 0, 2, state, 0));
- }
- 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)) {
- moves.add(new Move(i, j, i, j + 2, 0, 0, 1, state, 0));
- }
- }
- return moves;
- }
- public static ArrayList<Move> getKnightMoves(int[][] board, int i, int j, int color, Chessboard state) {
- ArrayList<Move> moves = new ArrayList();
- int[][] directions = new int[][]{{1, 2}, {2, 1}, {-1, 2}, {2, -1}, {-1, -2}, {-2, -1}, {-2, 1}, {1, -2}};
- int[][] var7 = directions;
- int var8 = directions.length;
- for(int var9 = 0; var9 < var8; ++var9) {
- int[] direction = var7[var9];
- int x = i + direction[0];
- int y = j + direction[1];
- if (x >= 0 && y >= 0 && x < 8 && y < 8 && (board[x][y] == 0 || board[x][y] * color < 0)) {
- moves.add(new Move(i, j, x, y, 0, 0, state, board[x][y]));
- }
- }
- return moves;
- }
- public static ArrayList<Move> getPawnMoves(int[][] board, int i, int j, int color, Chessboard state) {
- ArrayList<Move> moves = new ArrayList();
- if (i + color >= 0 && i + color < 8 && board[i + color * 1][j] == 0) {
- if ((i + color != 0 || color != -1) && (i + color != 7 || color != 1)) {
- moves.add(new Move(i, j, i + color, j, 0, 0, state, 0));
- } else {
- moves.add(new Move(i, j, i + color, j, 0, 5, state, 0));
- moves.add(new Move(i, j, i + color, j, 0, 3, state, 0));
- moves.add(new Move(i, j, i + color, j, 0, 2, state, 0));
- moves.add(new Move(i, j, i + color, j, 0, 4, state, 0));
- }
- }
- if ((i == 1 && color == 1 || i == 6 && color == -1) && board[i + color][j] == 0 && board[i + color * 2][j] == 0) {
- moves.add(new Move(i, j, i + 2 * color, j, 0, 0, state, 0));
- }
- if (i + color >= 0 && i + color < 8) {
- if (j != 7 && board[i + color][j + 1] * color < 0) {
- if ((i + color != 0 || color != -1) && (i + color != 7 || color != 1)) {
- moves.add(new Move(i, j, i + color, j + 1, 0, 0, state, board[i + color][j + 1]));
- } else {
- moves.add(new Move(i, j, i + color, j + 1, 0, 5, state, board[i + color][j + 1]));
- moves.add(new Move(i, j, i + color, j + 1, 0, 3, state, board[i + color][j + 1]));
- moves.add(new Move(i, j, i + color, j + 1, 0, 2, state, board[i + color][j + 1]));
- moves.add(new Move(i, j, i + color, j + 1, 0, 4, state, board[i + color][j + 1]));
- }
- }
- if (j != 0 && board[i + color][j - 1] * color < 0) {
- if ((i + color != 0 || color != -1) && (i + color != 7 || color != 1)) {
- moves.add(new Move(i, j, i + color, j - 1, 0, 0, state, board[i + color][j - 1]));
- } else {
- moves.add(new Move(i, j, i + color, j - 1, 0, 5, state, board[i + color][j - 1]));
- moves.add(new Move(i, j, i + color, j - 1, 0, 3, state, board[i + color][j - 1]));
- moves.add(new Move(i, j, i + color, j - 1, 0, 2, state, board[i + color][j - 1]));
- moves.add(new Move(i, j, i + color, j - 1, 0, 4, state, board[i + color][j - 1]));
- }
- }
- }
- return moves;
- }
- public static boolean isCovered(int[][] board, int i, int j, int color) {
- boolean covered = false;
- int[][] directions = new int[][]{{1, 1}, {-1, 1}, {-1, -1}, {1, -1}, {1, 0}, {0, 1}, {-1, 0}, {0, -1}};
- int[][] directions2 = directions;
- int var7 = directions.length;
- int var8;
- int x;
- int x;
- for(var8 = 0; var8 < var7; ++var8) {
- int[] direction = directions2[var8];
- x = i + direction[0];
- x = j + direction[1];
- if (x >= 0 && x >= 0 && x < 8 && x < 8 && board[x][x] * color == 6) {
- covered = true;
- break;
- }
- }
- directions2 = new int[][]{{1, 2}, {2, 1}, {-1, 2}, {2, -1}, {-1, -2}, {-2, -1}, {-2, 1}, {1, -2}};
- int[][] directions3 = directions2;
- var8 = directions2.length;
- int x;
- int var17;
- for(var17 = 0; var17 < var8; ++var17) {
- int[] direction = directions3[var17];
- x = i + direction[0];
- x = j + direction[1];
- if (x >= 0 && x >= 0 && x < 8 && x < 8 && board[x][x] * color == 3) {
- covered = true;
- break;
- }
- }
- directions3 = new int[][]{{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
- int[][] directions4 = directions3;
- var17 = directions3.length;
- int x;
- for(x = 0; x < var17; ++x) {
- int[] direction = directions4[x];
- x = i;
- x = j;
- while(true) {
- x += direction[0];
- x += direction[1];
- if (x < 0 || x < 0 || x > 7 || x > 7 || board[x][x] * color < 0) {
- break;
- }
- if (board[x][x] * color == 4 || board[x][x] * color == 5) {
- covered = true;
- break;
- }
- if (board[x][x] * color > 0) {
- break;
- }
- }
- }
- directions4 = new int[][]{{1, 1}, {-1, 1}, {-1, -1}, {1, -1}};
- int[][] var19 = directions4;
- x = directions4.length;
- for(x = 0; x < x; ++x) {
- int[] direction = var19[x];
- x = i;
- int y = j;
- while(true) {
- x += direction[0];
- y += direction[1];
- if (x < 0 || y < 0 || x > 7 || y > 7 || board[x][y] * color < 0) {
- break;
- }
- if (board[x][y] * color == 2 || board[x][y] * color == 5) {
- covered = true;
- break;
- }
- if (board[x][y] * color > 0) {
- break;
- }
- }
- }
- if (i - color >= 0 && i - color < 8) {
- if (j != 0 && board[i - color][j - 1] * color == 1) {
- covered = true;
- }
- if (j != 7 && board[i - color][j + 1] * color == 1) {
- covered = true;
- }
- }
- return covered;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement