Advertisement
Guest User

Untitled

a guest
Apr 13th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 17.64 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.Arrays;
  10. import java.util.Iterator;
  11.  
  12. public class Chessboard {
  13.     public static final int PAWN = 1;
  14.     public static final int BISHOP = 2;
  15.     public static final int KNIGHT = 3;
  16.     public static final int ROOK = 4;
  17.     public static final int QUEEN = 5;
  18.     public static final int KING = 6;
  19.     public static final int PAWN_B = -1;
  20.     public static final int BISHOP_B = -2;
  21.     public static final int KNIGHT_B = -3;
  22.     public static final int ROOK_B = -4;
  23.     public static final int QUEEN_B = -5;
  24.     public static final int KING_B = -6;
  25.     public static final int EMPTY = 0;
  26.     public static final int WHITE = 1;
  27.     public static final int BLACK = -1;
  28.     public static final int NONE = 0;
  29.     public static final int CHECK = 1;
  30.     public static final int CHECKMATE = 2;
  31.     public static final int DRAW = 3;
  32.     public static final int GAME = 4;
  33.     public static final int TURN_ENDED = 5;
  34.     public static final int CASTLE_SHORT = 1;
  35.     public static final int CASTLE_LONG = 2;
  36.     public static final int RULES_ITALIAN = 0;
  37.     public static final int RULES_SCOTTISH = 1;
  38.     public static int ruleSet = 1;
  39.     protected int[][] board;
  40.     protected int gameStatus;
  41.     protected int color;
  42.     protected int movesLeft;
  43.     protected boolean a1 = false;
  44.     protected boolean a8 = false;
  45.     protected boolean h1 = false;
  46.     protected boolean h8 = false;
  47.     protected boolean e1 = false;
  48.     protected boolean e8 = false;
  49.     protected boolean movesCalculated = false;
  50.     private Move lastMove;
  51.     private ArrayList<Move> moves;
  52.  
  53.     protected Chessboard(int[][] board, int gameStatus, int color, int movesLeft) {
  54.         this.board = board;
  55.         this.gameStatus = gameStatus;
  56.         this.color = color;
  57.         this.movesLeft = movesLeft;
  58.         this.movesCalculated = false;
  59.     }
  60.  
  61.     public Chessboard(int[][] board, int color, int movesLeft) {
  62.         this.board = board;
  63.         this.color = color;
  64.         this.movesLeft = movesLeft;
  65.         this.movesCalculated = false;
  66.         this.checkGameStatus();
  67.     }
  68.  
  69.     public static int getRuleSet() {
  70.         return ruleSet;
  71.     }
  72.  
  73.     public static void setRuleSet(int ruleSet) {
  74.         if (ruleSet == 0 || ruleSet == 1) {
  75.             ruleSet = ruleSet;
  76.         }
  77.  
  78.     }
  79.  
  80.     public static Chessboard getInitialBoard() {
  81.         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}};
  82.         return new Chessboard(initialBoard, 4, 1, 1);
  83.     }
  84.  
  85.     protected static int transformLetter(char s) {
  86.         if (s == 'a') {
  87.             return 0;
  88.         } else if (s == 'b') {
  89.             return 1;
  90.         } else if (s == 'c') {
  91.             return 2;
  92.         } else if (s == 'd') {
  93.             return 3;
  94.         } else if (s == 'e') {
  95.             return 4;
  96.         } else if (s == 'f') {
  97.             return 5;
  98.         } else {
  99.             return s == 'g' ? 6 : 7;
  100.         }
  101.     }
  102.  
  103.     public static Chessboard getChessboardFromFEN(String FEN) {
  104.         Chessboard board = getInitialBoard();
  105.         String[] tokens = FEN.split(" ");
  106.         int[][] chessboard = getBoardFromFEN(tokens[0]);
  107.         board.board = chessboard;
  108.         board.color = 1;
  109.         if (tokens[1].equals("b")) {
  110.             board.color = -1;
  111.         }
  112.  
  113.         board.movesLeft = Integer.valueOf(tokens[2]);
  114.         board.movesCalculated = false;
  115.         board.checkGameStatus();
  116.         return board;
  117.     }
  118.  
  119.     private static int[][] getBoardFromFEN(String FEN) {
  120.         int[][] chessboard = new int[8][8];
  121.         int count = 0;
  122.  
  123.         for(int i = 0; i < FEN.length(); ++i) {
  124.             if (FEN.charAt(i) == 'P') {
  125.                 chessboard[7 - count / 8][count % 8] = 1;
  126.             } else if (FEN.charAt(i) == 'R') {
  127.                 chessboard[7 - count / 8][count % 8] = 4;
  128.             } else if (FEN.charAt(i) == 'N') {
  129.                 chessboard[7 - count / 8][count % 8] = 3;
  130.             } else if (FEN.charAt(i) == 'B') {
  131.                 chessboard[7 - count / 8][count % 8] = 2;
  132.             } else if (FEN.charAt(i) == 'Q') {
  133.                 chessboard[7 - count / 8][count % 8] = 5;
  134.             } else if (FEN.charAt(i) == 'K') {
  135.                 chessboard[7 - count / 8][count % 8] = 6;
  136.             } else if (FEN.charAt(i) == 'p') {
  137.                 chessboard[7 - count / 8][count % 8] = -1;
  138.             } else if (FEN.charAt(i) == 'r') {
  139.                 chessboard[7 - count / 8][count % 8] = -4;
  140.             } else if (FEN.charAt(i) == 'n') {
  141.                 chessboard[7 - count / 8][count % 8] = -3;
  142.             } else if (FEN.charAt(i) == 'b') {
  143.                 chessboard[7 - count / 8][count % 8] = -2;
  144.             } else if (FEN.charAt(i) == 'q') {
  145.                 chessboard[7 - count / 8][count % 8] = -5;
  146.             } else if (FEN.charAt(i) == 'k') {
  147.                 chessboard[7 - count / 8][count % 8] = -6;
  148.             } else if (FEN.charAt(i) == '/') {
  149.                 --count;
  150.             } else {
  151.                 char var10000 = FEN.charAt(i);
  152.                 int spaces = Integer.valueOf(var10000.makeConcatWithConstants<invokedynamic>(var10000));
  153.  
  154.                 for(int j = 0; j < spaces; ++j) {
  155.                     chessboard[7 - count / 8][count % 8] = 0;
  156.                     ++count;
  157.                 }
  158.  
  159.                 --count;
  160.             }
  161.  
  162.             ++count;
  163.         }
  164.  
  165.         return chessboard;
  166.     }
  167.  
  168.     public int getGameStatus() {
  169.         return this.gameStatus;
  170.     }
  171.  
  172.     public ArrayList<Move> getMoves() {
  173.         if (!this.movesCalculated) {
  174.             this.movesCalculated = true;
  175.             this.moves = Rules.getPossibleMoves(this);
  176.         }
  177.  
  178.         return this.moves;
  179.     }
  180.  
  181.     public int[][] getBoard() {
  182.         return this.board;
  183.     }
  184.  
  185.     public int getColor() {
  186.         return this.color;
  187.     }
  188.  
  189.     protected void setColor(int color) {
  190.         this.color = color;
  191.     }
  192.  
  193.     public int getMovesLeft() {
  194.         return this.movesLeft;
  195.     }
  196.  
  197.     protected void switchColors() {
  198.         this.color *= -1;
  199.     }
  200.  
  201.     public String toString() {
  202.         String display = "";
  203.  
  204.         for(int i = 7; i >= 0; --i) {
  205.             display = display + (i + 1) + "| ";
  206.  
  207.             for(int j = 0; j < 8; ++j) {
  208.                 switch(this.board[i][j]) {
  209.                 case -6:
  210.                     display = display + "k";
  211.                     break;
  212.                 case -5:
  213.                     display = display + "q";
  214.                     break;
  215.                 case -4:
  216.                     display = display + "r";
  217.                     break;
  218.                 case -3:
  219.                     display = display + "n";
  220.                     break;
  221.                 case -2:
  222.                     display = display + "b";
  223.                     break;
  224.                 case -1:
  225.                     display = display + "p";
  226.                     break;
  227.                 case 0:
  228.                 default:
  229.                     display = display + ".";
  230.                     break;
  231.                 case 1:
  232.                     display = display + "P";
  233.                     break;
  234.                 case 2:
  235.                     display = display + "B";
  236.                     break;
  237.                 case 3:
  238.                     display = display + "N";
  239.                     break;
  240.                 case 4:
  241.                     display = display + "R";
  242.                     break;
  243.                 case 5:
  244.                     display = display + "Q";
  245.                     break;
  246.                 case 6:
  247.                     display = display + "K";
  248.                 }
  249.  
  250.                 display = display + " ";
  251.             }
  252.  
  253.             display = display + "\n";
  254.         }
  255.  
  256.         display = display + "   ---------------\n";
  257.         display = display + "   a b c d e f g h\n";
  258.         display = display + "\n";
  259.         return display;
  260.     }
  261.  
  262.     public boolean isMoveValid(Move move) {
  263.         Iterator var2 = this.getMoves().iterator();
  264.  
  265.         Move potential;
  266.         do {
  267.             if (!var2.hasNext()) {
  268.                 return false;
  269.             }
  270.  
  271.             potential = (Move)var2.next();
  272.         } 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);
  273.  
  274.         return true;
  275.     }
  276.  
  277.     private Move getMoveValid(Move move) {
  278.         Iterator var2 = this.getMoves().iterator();
  279.  
  280.         Move potential;
  281.         do {
  282.             if (!var2.hasNext()) {
  283.                 return null;
  284.             }
  285.  
  286.             potential = (Move)var2.next();
  287.         } 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);
  288.  
  289.         return potential;
  290.     }
  291.  
  292.     public void makeMove(Move move) {
  293.         Move valid = this.getMoveValid(move);
  294.         if (valid != null) {
  295.             this.makeMove(valid, true);
  296.         }
  297.  
  298.     }
  299.  
  300.     protected void makeMove(Move move, boolean calculateMoves) {
  301.         if (move.from_y == 0 && move.from_x == 0 && !this.a1) {
  302.             this.a1 = true;
  303.             move.reverseMoved = true;
  304.         }
  305.  
  306.         if (move.from_y == 7 && move.from_x == 0 && !this.h1) {
  307.             this.h1 = true;
  308.             move.reverseMoved = true;
  309.         }
  310.  
  311.         if (move.from_y == 0 && move.from_x == 7 && !this.a8) {
  312.             this.a8 = true;
  313.             move.reverseMoved = true;
  314.         }
  315.  
  316.         if (move.from_y == 7 && move.from_x == 7 && !this.h8) {
  317.             this.h8 = true;
  318.             move.reverseMoved = true;
  319.         }
  320.  
  321.         if (move.from_y == 4 && move.from_x == 0 && !this.e1) {
  322.             this.e1 = true;
  323.             move.reverseMoved = true;
  324.         }
  325.  
  326.         if (move.from_y == 4 && move.from_x == 7 && !this.e8) {
  327.             this.e8 = true;
  328.             move.reverseMoved = true;
  329.         }
  330.  
  331.         if (move.castle == 2) {
  332.             this.board[move.from_x][3] = this.board[move.from_x][0];
  333.             this.board[move.from_x][0] = 0;
  334.             if (move.from_x == 0) {
  335.                 this.a1 = true;
  336.             }
  337.  
  338.             if (move.from_x == 7) {
  339.                 this.a8 = true;
  340.             }
  341.         }
  342.  
  343.         if (move.castle == 1) {
  344.             this.board[move.from_x][5] = this.board[move.from_x][7];
  345.             this.board[move.from_x][7] = 0;
  346.             if (move.from_x == 0) {
  347.                 this.h1 = true;
  348.             }
  349.  
  350.             if (move.from_x == 7) {
  351.                 this.h8 = true;
  352.             }
  353.         }
  354.  
  355.         this.board[move.to_x][move.to_y] = this.board[move.from_x][move.from_y];
  356.         this.board[move.from_x][move.from_y] = 0;
  357.         --this.movesLeft;
  358.         if (move.promotion != 0) {
  359.             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]));
  360.         }
  361.  
  362.         this.lastMove = move;
  363.         this.checkGameStatus();
  364.         this.movesCalculated = false;
  365.     }
  366.  
  367.     public void reverseMove() {
  368.         if (this.lastMove != null) {
  369.             this.reverseMove(this.lastMove, true);
  370.         }
  371.  
  372.     }
  373.  
  374.     public void reverseMove(Move move) {
  375.         this.reverseMove(move, true);
  376.     }
  377.  
  378.     protected void reverseMove(Move move, boolean calculateMoves) {
  379.         if (move.castle == 2) {
  380.             this.board[move.from_x][0] = this.board[move.from_x][3];
  381.             this.board[move.from_x][3] = 0;
  382.             if (move.from_x == 0) {
  383.                 this.a1 = false;
  384.             }
  385.  
  386.             if (move.from_x == 7) {
  387.                 this.a8 = false;
  388.             }
  389.         }
  390.  
  391.         if (move.castle == 1) {
  392.             this.board[move.from_x][7] = this.board[move.from_x][5];
  393.             this.board[move.from_x][5] = 0;
  394.             if (move.from_x == 0) {
  395.                 this.h1 = false;
  396.             }
  397.  
  398.             if (move.from_x == 7) {
  399.                 this.h8 = false;
  400.             }
  401.         }
  402.  
  403.         if (move.from_y == 0 && move.from_x == 0 && move.reverseMoved) {
  404.             this.a1 = false;
  405.         }
  406.  
  407.         if (move.from_y == 7 && move.from_x == 0 && move.reverseMoved) {
  408.             this.h1 = false;
  409.         }
  410.  
  411.         if (move.from_y == 0 && move.from_x == 7 && move.reverseMoved) {
  412.             this.a8 = false;
  413.         }
  414.  
  415.         if (move.from_y == 7 && move.from_x == 7 && move.reverseMoved) {
  416.             this.h8 = false;
  417.         }
  418.  
  419.         if (move.from_y == 4 && move.from_x == 0 && move.reverseMoved) {
  420.             this.e1 = false;
  421.         }
  422.  
  423.         if (move.from_y == 4 && move.from_x == 7 && move.reverseMoved) {
  424.             this.e8 = false;
  425.         }
  426.  
  427.         this.board[move.from_x][move.from_y] = this.board[move.to_x][move.to_y];
  428.         this.board[move.to_x][move.to_y] = move.piece;
  429.         if (move.promotion != 0) {
  430.             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]));
  431.         }
  432.  
  433.         this.lastMove = null;
  434.         ++this.movesLeft;
  435.         this.gameStatus = 4;
  436.         this.movesCalculated = false;
  437.     }
  438.  
  439.     protected boolean hasTurnEnded() {
  440.         return this.movesLeft <= 0;
  441.     }
  442.  
  443.     private void checkGameStatus() {
  444.         int count = 0;
  445.         int queen = 0;
  446.  
  447.         for(int i = 0; i < 8; ++i) {
  448.             for(int j = 0; j < 8; ++j) {
  449.                 if (this.board[i][j] != 0) {
  450.                     ++count;
  451.                     if (this.board[i][j] == 5 || this.board[i][j] == -5) {
  452.                         ++queen;
  453.                     }
  454.  
  455.                     if (this.board[i][j] == 1 || this.board[i][j] == -1) {
  456.                         ++queen;
  457.                     }
  458.                 }
  459.             }
  460.         }
  461.  
  462.         this.gameStatus = 4;
  463.         if (count != 2 && (queen != 0 || count != 3)) {
  464.             if (this.lastMove != null && this.lastMove.checkmate == 2) {
  465.                 this.gameStatus = 2;
  466.             } else if (this.lastMove != null && this.lastMove.checkmate == 1) {
  467.                 this.gameStatus = 1;
  468.             } else if (this.hasTurnEnded()) {
  469.                 this.gameStatus = 5;
  470.             }
  471.         } else {
  472.             this.gameStatus = 3;
  473.         }
  474.  
  475.     }
  476.  
  477.     public Chessboard copy() {
  478.         int[][] nv = new int[this.board.length][this.board[0].length];
  479.  
  480.         for(int i = 0; i < nv.length; ++i) {
  481.             nv[i] = Arrays.copyOf(this.board[i], this.board[i].length);
  482.         }
  483.  
  484.         Chessboard b = new Chessboard(nv, this.gameStatus, this.color, this.movesLeft);
  485.         b.a1 = this.a1;
  486.         b.a8 = this.a8;
  487.         b.e1 = this.e1;
  488.         b.e8 = this.e8;
  489.         b.h1 = this.h1;
  490.         b.h8 = this.h8;
  491.         b.moves = this.moves;
  492.         b.movesCalculated = this.movesCalculated;
  493.         b.lastMove = this.lastMove;
  494.         return b;
  495.     }
  496.  
  497.     public String getFEN() {
  498.         String FEN = this.getPartialFEN();
  499.         if (this.getColor() == 1) {
  500.             FEN = FEN + " w";
  501.         } else {
  502.             FEN = FEN + " b";
  503.         }
  504.  
  505.         FEN = FEN + " " + this.movesLeft;
  506.         return FEN;
  507.     }
  508.  
  509.     private String getPartialFEN() {
  510.         String FEN = "";
  511.  
  512.         for(int i = 7; i >= 0; --i) {
  513.             for(int j = 0; j < 8; ++j) {
  514.                 if (this.board[i][j] == 1) {
  515.                     FEN = FEN + "P";
  516.                 }
  517.  
  518.                 if (this.board[i][j] == 3) {
  519.                     FEN = FEN + "N";
  520.                 }
  521.  
  522.                 if (this.board[i][j] == 2) {
  523.                     FEN = FEN + "B";
  524.                 }
  525.  
  526.                 if (this.board[i][j] == 4) {
  527.                     FEN = FEN + "R";
  528.                 }
  529.  
  530.                 if (this.board[i][j] == 5) {
  531.                     FEN = FEN + "Q";
  532.                 }
  533.  
  534.                 if (this.board[i][j] == 6) {
  535.                     FEN = FEN + "K";
  536.                 }
  537.  
  538.                 if (this.board[i][j] == -1) {
  539.                     FEN = FEN + "p";
  540.                 }
  541.  
  542.                 if (this.board[i][j] == -3) {
  543.                     FEN = FEN + "n";
  544.                 }
  545.  
  546.                 if (this.board[i][j] == -2) {
  547.                     FEN = FEN + "b";
  548.                 }
  549.  
  550.                 if (this.board[i][j] == -4) {
  551.                     FEN = FEN + "r";
  552.                 }
  553.  
  554.                 if (this.board[i][j] == -5) {
  555.                     FEN = FEN + "q";
  556.                 }
  557.  
  558.                 if (this.board[i][j] == -6) {
  559.                     FEN = FEN + "k";
  560.                 }
  561.  
  562.                 if (this.board[i][j] == 0) {
  563.                     if (FEN.length() == 0) {
  564.                         FEN = FEN + "1";
  565.                     } else {
  566.                         char c = FEN.charAt(FEN.length() - 1);
  567.                         if (c != '1' && c != '2' && c != '3' && c != '4' && c != '5' && c != '6' && c != '7' && c != '8') {
  568.                             FEN = FEN + "1";
  569.                         } else {
  570.                             int count = Integer.valueOf(c.makeConcatWithConstants<invokedynamic>(c)) + 1;
  571.                             String var10000 = FEN.substring(0, FEN.length() - 1);
  572.                             FEN = var10000 + count;
  573.                         }
  574.                     }
  575.                 }
  576.             }
  577.  
  578.             if (i != 0) {
  579.                 FEN = FEN + "/";
  580.             }
  581.         }
  582.  
  583.         return FEN;
  584.     }
  585. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement