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.Arrays;
- import java.util.Iterator;
- public class Chessboard {
- public static final int PAWN = 1;
- public static final int BISHOP = 2;
- public static final int KNIGHT = 3;
- public static final int ROOK = 4;
- public static final int QUEEN = 5;
- public static final int KING = 6;
- public static final int PAWN_B = -1;
- public static final int BISHOP_B = -2;
- public static final int KNIGHT_B = -3;
- public static final int ROOK_B = -4;
- public static final int QUEEN_B = -5;
- public static final int KING_B = -6;
- public static final int EMPTY = 0;
- public static final int WHITE = 1;
- public static final int BLACK = -1;
- public static final int NONE = 0;
- public static final int CHECK = 1;
- public static final int CHECKMATE = 2;
- public static final int DRAW = 3;
- public static final int GAME = 4;
- public static final int TURN_ENDED = 5;
- public static final int CASTLE_SHORT = 1;
- public static final int CASTLE_LONG = 2;
- public static final int RULES_ITALIAN = 0;
- public static final int RULES_SCOTTISH = 1;
- public static int ruleSet = 1;
- protected int[][] board;
- protected int gameStatus;
- protected int color;
- protected int movesLeft;
- protected boolean a1 = false;
- protected boolean a8 = false;
- protected boolean h1 = false;
- protected boolean h8 = false;
- protected boolean e1 = false;
- protected boolean e8 = false;
- protected boolean movesCalculated = false;
- private Move lastMove;
- private ArrayList<Move> moves;
- protected Chessboard(int[][] board, int gameStatus, int color, int movesLeft) {
- this.board = board;
- this.gameStatus = gameStatus;
- this.color = color;
- this.movesLeft = movesLeft;
- this.movesCalculated = false;
- }
- public Chessboard(int[][] board, int color, int movesLeft) {
- this.board = board;
- this.color = color;
- this.movesLeft = movesLeft;
- this.movesCalculated = false;
- this.checkGameStatus();
- }
- public static int getRuleSet() {
- return ruleSet;
- }
- public static void setRuleSet(int ruleSet) {
- if (ruleSet == 0 || ruleSet == 1) {
- ruleSet = ruleSet;
- }
- }
- public static Chessboard getInitialBoard() {
- int[][] initialBoard = new int[][]{{4, 3, 2, 5, 6, 2, 3, 4}, {1, 1, 1, 1, 1, 1, 1, 1}, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}, {-1, -1, -1, -1, -1, -1, -1, -1}, {-4, -3, -2, -5, -6, -2, -3, -4}};
- return new Chessboard(initialBoard, 4, 1, 1);
- }
- protected static int transformLetter(char s) {
- if (s == 'a') {
- return 0;
- } else if (s == 'b') {
- return 1;
- } else if (s == 'c') {
- return 2;
- } else if (s == 'd') {
- return 3;
- } else if (s == 'e') {
- return 4;
- } else if (s == 'f') {
- return 5;
- } else {
- return s == 'g' ? 6 : 7;
- }
- }
- public static Chessboard getChessboardFromFEN(String FEN) {
- Chessboard board = getInitialBoard();
- String[] tokens = FEN.split(" ");
- int[][] chessboard = getBoardFromFEN(tokens[0]);
- board.board = chessboard;
- board.color = 1;
- if (tokens[1].equals("b")) {
- board.color = -1;
- }
- board.movesLeft = Integer.valueOf(tokens[2]);
- board.movesCalculated = false;
- board.checkGameStatus();
- return board;
- }
- private static int[][] getBoardFromFEN(String FEN) {
- int[][] chessboard = new int[8][8];
- int count = 0;
- for(int i = 0; i < FEN.length(); ++i) {
- if (FEN.charAt(i) == 'P') {
- chessboard[7 - count / 8][count % 8] = 1;
- } else if (FEN.charAt(i) == 'R') {
- chessboard[7 - count / 8][count % 8] = 4;
- } else if (FEN.charAt(i) == 'N') {
- chessboard[7 - count / 8][count % 8] = 3;
- } else if (FEN.charAt(i) == 'B') {
- chessboard[7 - count / 8][count % 8] = 2;
- } else if (FEN.charAt(i) == 'Q') {
- chessboard[7 - count / 8][count % 8] = 5;
- } else if (FEN.charAt(i) == 'K') {
- chessboard[7 - count / 8][count % 8] = 6;
- } else if (FEN.charAt(i) == 'p') {
- chessboard[7 - count / 8][count % 8] = -1;
- } else if (FEN.charAt(i) == 'r') {
- chessboard[7 - count / 8][count % 8] = -4;
- } else if (FEN.charAt(i) == 'n') {
- chessboard[7 - count / 8][count % 8] = -3;
- } else if (FEN.charAt(i) == 'b') {
- chessboard[7 - count / 8][count % 8] = -2;
- } else if (FEN.charAt(i) == 'q') {
- chessboard[7 - count / 8][count % 8] = -5;
- } else if (FEN.charAt(i) == 'k') {
- chessboard[7 - count / 8][count % 8] = -6;
- } else if (FEN.charAt(i) == '/') {
- --count;
- } else {
- char var10000 = FEN.charAt(i);
- int spaces = Integer.valueOf(var10000.makeConcatWithConstants<invokedynamic>(var10000));
- for(int j = 0; j < spaces; ++j) {
- chessboard[7 - count / 8][count % 8] = 0;
- ++count;
- }
- --count;
- }
- ++count;
- }
- return chessboard;
- }
- public int getGameStatus() {
- return this.gameStatus;
- }
- public ArrayList<Move> getMoves() {
- if (!this.movesCalculated) {
- this.movesCalculated = true;
- this.moves = Rules.getPossibleMoves(this);
- }
- return this.moves;
- }
- public int[][] getBoard() {
- return this.board;
- }
- public int getColor() {
- return this.color;
- }
- protected void setColor(int color) {
- this.color = color;
- }
- public int getMovesLeft() {
- return this.movesLeft;
- }
- protected void switchColors() {
- this.color *= -1;
- }
- public String toString() {
- String display = "";
- for(int i = 7; i >= 0; --i) {
- display = display + (i + 1) + "| ";
- for(int j = 0; j < 8; ++j) {
- switch(this.board[i][j]) {
- case -6:
- display = display + "k";
- break;
- case -5:
- display = display + "q";
- break;
- case -4:
- display = display + "r";
- break;
- case -3:
- display = display + "n";
- break;
- case -2:
- display = display + "b";
- break;
- case -1:
- display = display + "p";
- break;
- case 0:
- default:
- display = display + ".";
- break;
- case 1:
- display = display + "P";
- break;
- case 2:
- display = display + "B";
- break;
- case 3:
- display = display + "N";
- break;
- case 4:
- display = display + "R";
- break;
- case 5:
- display = display + "Q";
- break;
- case 6:
- display = display + "K";
- }
- display = display + " ";
- }
- display = display + "\n";
- }
- display = display + " ---------------\n";
- display = display + " a b c d e f g h\n";
- display = display + "\n";
- return display;
- }
- public boolean isMoveValid(Move move) {
- Iterator var2 = this.getMoves().iterator();
- Move potential;
- do {
- if (!var2.hasNext()) {
- return false;
- }
- potential = (Move)var2.next();
- } while(potential.from_x != move.from_x || potential.from_y != move.from_y || potential.to_x != move.to_x || potential.to_y != move.to_y || move.promotion != potential.promotion);
- return true;
- }
- private Move getMoveValid(Move move) {
- Iterator var2 = this.getMoves().iterator();
- Move potential;
- do {
- if (!var2.hasNext()) {
- return null;
- }
- potential = (Move)var2.next();
- } while(potential.from_x != move.from_x || potential.from_y != move.from_y || potential.to_x != move.to_x || potential.to_y != move.to_y || move.promotion != potential.promotion);
- return potential;
- }
- public void makeMove(Move move) {
- Move valid = this.getMoveValid(move);
- if (valid != null) {
- this.makeMove(valid, true);
- }
- }
- protected void makeMove(Move move, boolean calculateMoves) {
- if (move.from_y == 0 && move.from_x == 0 && !this.a1) {
- this.a1 = true;
- move.reverseMoved = true;
- }
- if (move.from_y == 7 && move.from_x == 0 && !this.h1) {
- this.h1 = true;
- move.reverseMoved = true;
- }
- if (move.from_y == 0 && move.from_x == 7 && !this.a8) {
- this.a8 = true;
- move.reverseMoved = true;
- }
- if (move.from_y == 7 && move.from_x == 7 && !this.h8) {
- this.h8 = true;
- move.reverseMoved = true;
- }
- if (move.from_y == 4 && move.from_x == 0 && !this.e1) {
- this.e1 = true;
- move.reverseMoved = true;
- }
- if (move.from_y == 4 && move.from_x == 7 && !this.e8) {
- this.e8 = true;
- move.reverseMoved = true;
- }
- if (move.castle == 2) {
- this.board[move.from_x][3] = this.board[move.from_x][0];
- this.board[move.from_x][0] = 0;
- if (move.from_x == 0) {
- this.a1 = true;
- }
- if (move.from_x == 7) {
- this.a8 = true;
- }
- }
- if (move.castle == 1) {
- this.board[move.from_x][5] = this.board[move.from_x][7];
- this.board[move.from_x][7] = 0;
- if (move.from_x == 0) {
- this.h1 = true;
- }
- if (move.from_x == 7) {
- this.h8 = true;
- }
- }
- this.board[move.to_x][move.to_y] = this.board[move.from_x][move.from_y];
- this.board[move.from_x][move.from_y] = 0;
- --this.movesLeft;
- if (move.promotion != 0) {
- this.board[move.to_x][move.to_y] = move.promotion * (this.board[move.to_x][move.to_y] / Math.abs(this.board[move.to_x][move.to_y]));
- }
- this.lastMove = move;
- this.checkGameStatus();
- this.movesCalculated = false;
- }
- public void reverseMove() {
- if (this.lastMove != null) {
- this.reverseMove(this.lastMove, true);
- }
- }
- public void reverseMove(Move move) {
- this.reverseMove(move, true);
- }
- protected void reverseMove(Move move, boolean calculateMoves) {
- if (move.castle == 2) {
- this.board[move.from_x][0] = this.board[move.from_x][3];
- this.board[move.from_x][3] = 0;
- if (move.from_x == 0) {
- this.a1 = false;
- }
- if (move.from_x == 7) {
- this.a8 = false;
- }
- }
- if (move.castle == 1) {
- this.board[move.from_x][7] = this.board[move.from_x][5];
- this.board[move.from_x][5] = 0;
- if (move.from_x == 0) {
- this.h1 = false;
- }
- if (move.from_x == 7) {
- this.h8 = false;
- }
- }
- if (move.from_y == 0 && move.from_x == 0 && move.reverseMoved) {
- this.a1 = false;
- }
- if (move.from_y == 7 && move.from_x == 0 && move.reverseMoved) {
- this.h1 = false;
- }
- if (move.from_y == 0 && move.from_x == 7 && move.reverseMoved) {
- this.a8 = false;
- }
- if (move.from_y == 7 && move.from_x == 7 && move.reverseMoved) {
- this.h8 = false;
- }
- if (move.from_y == 4 && move.from_x == 0 && move.reverseMoved) {
- this.e1 = false;
- }
- if (move.from_y == 4 && move.from_x == 7 && move.reverseMoved) {
- this.e8 = false;
- }
- this.board[move.from_x][move.from_y] = this.board[move.to_x][move.to_y];
- this.board[move.to_x][move.to_y] = move.piece;
- if (move.promotion != 0) {
- this.board[move.from_x][move.from_y] = 1 * (this.board[move.from_x][move.from_y] / Math.abs(this.board[move.from_x][move.from_y]));
- }
- this.lastMove = null;
- ++this.movesLeft;
- this.gameStatus = 4;
- this.movesCalculated = false;
- }
- protected boolean hasTurnEnded() {
- return this.movesLeft <= 0;
- }
- private void checkGameStatus() {
- int count = 0;
- int queen = 0;
- for(int i = 0; i < 8; ++i) {
- for(int j = 0; j < 8; ++j) {
- if (this.board[i][j] != 0) {
- ++count;
- if (this.board[i][j] == 5 || this.board[i][j] == -5) {
- ++queen;
- }
- if (this.board[i][j] == 1 || this.board[i][j] == -1) {
- ++queen;
- }
- }
- }
- }
- this.gameStatus = 4;
- if (count != 2 && (queen != 0 || count != 3)) {
- if (this.lastMove != null && this.lastMove.checkmate == 2) {
- this.gameStatus = 2;
- } else if (this.lastMove != null && this.lastMove.checkmate == 1) {
- this.gameStatus = 1;
- } else if (this.hasTurnEnded()) {
- this.gameStatus = 5;
- }
- } else {
- this.gameStatus = 3;
- }
- }
- public Chessboard copy() {
- int[][] nv = new int[this.board.length][this.board[0].length];
- for(int i = 0; i < nv.length; ++i) {
- nv[i] = Arrays.copyOf(this.board[i], this.board[i].length);
- }
- Chessboard b = new Chessboard(nv, this.gameStatus, this.color, this.movesLeft);
- b.a1 = this.a1;
- b.a8 = this.a8;
- b.e1 = this.e1;
- b.e8 = this.e8;
- b.h1 = this.h1;
- b.h8 = this.h8;
- b.moves = this.moves;
- b.movesCalculated = this.movesCalculated;
- b.lastMove = this.lastMove;
- return b;
- }
- public String getFEN() {
- String FEN = this.getPartialFEN();
- if (this.getColor() == 1) {
- FEN = FEN + " w";
- } else {
- FEN = FEN + " b";
- }
- FEN = FEN + " " + this.movesLeft;
- return FEN;
- }
- private String getPartialFEN() {
- String FEN = "";
- for(int i = 7; i >= 0; --i) {
- for(int j = 0; j < 8; ++j) {
- if (this.board[i][j] == 1) {
- FEN = FEN + "P";
- }
- if (this.board[i][j] == 3) {
- FEN = FEN + "N";
- }
- if (this.board[i][j] == 2) {
- FEN = FEN + "B";
- }
- if (this.board[i][j] == 4) {
- FEN = FEN + "R";
- }
- if (this.board[i][j] == 5) {
- FEN = FEN + "Q";
- }
- if (this.board[i][j] == 6) {
- FEN = FEN + "K";
- }
- if (this.board[i][j] == -1) {
- FEN = FEN + "p";
- }
- if (this.board[i][j] == -3) {
- FEN = FEN + "n";
- }
- if (this.board[i][j] == -2) {
- FEN = FEN + "b";
- }
- if (this.board[i][j] == -4) {
- FEN = FEN + "r";
- }
- if (this.board[i][j] == -5) {
- FEN = FEN + "q";
- }
- if (this.board[i][j] == -6) {
- FEN = FEN + "k";
- }
- if (this.board[i][j] == 0) {
- if (FEN.length() == 0) {
- FEN = FEN + "1";
- } else {
- char c = FEN.charAt(FEN.length() - 1);
- if (c != '1' && c != '2' && c != '3' && c != '4' && c != '5' && c != '6' && c != '7' && c != '8') {
- FEN = FEN + "1";
- } else {
- int count = Integer.valueOf(c.makeConcatWithConstants<invokedynamic>(c)) + 1;
- String var10000 = FEN.substring(0, FEN.length() - 1);
- FEN = var10000 + count;
- }
- }
- }
- }
- if (i != 0) {
- FEN = FEN + "/";
- }
- }
- return FEN;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement