Advertisement
ausbrumm96

ChessBoard

Apr 6th, 2019
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.48 KB | None | 0 0
  1.  
  2. package chess;
  3.  
  4. import boardgame.*;
  5.  
  6. import java.util.HashSet;
  7. import java.util.Vector;
  8.  
  9. import static boardgame.Application.*;
  10.  
  11. public class ChessBoard extends Board implements Cloneable{
  12.  
  13.     private final String positionRegex = "[A-Ha-h][1-8]";
  14.     ChessPiece[][] board;
  15.     AlgebraicNotationParser parser;
  16.  
  17.     public ChessBoard(int size) {
  18.         board = new ChessPiece[size][size];
  19.         setBoardSize( size );
  20.         initEmpty();
  21.         init();
  22.     }
  23.  
  24.     public ChessBoard(ChessPiece[][] board) {
  25.         ChessPiece[][] tmp = new ChessPiece[boardSize][boardSize];
  26.         for(int i = 0; i < boardSize; i++)
  27.             for(int j = 0; j < boardSize; j++)
  28.                 tmp[i][j] = board[i][j];
  29.     }
  30.  
  31.     public HashSet<ChessMove> generateMoves(Player player){
  32.         HashSet<ChessMove> moves = new HashSet<>(  );
  33.         for(int i = 0; i < boardSize; i++){
  34.             for(int j = 0; j < boardSize; j++){
  35.                 if(board[i][j].hasCellColor( Colors.WHITE ) && player.getColor().equals( Colors.WHITE ))
  36.                     moves.addAll( board[i][j].addMoves( this ) );
  37.                 if(board[i][j].hasCellColor( Colors.BLACK ) && player.getColor().equals( Colors.BLACK ))
  38.                     moves.addAll( board[i][j].addMoves( this ) );
  39.             }
  40.         }
  41.         return moves;
  42.     }
  43.  
  44.     public HashSet<ChessMove> generateMoves(int player){
  45.         HashSet<ChessMove> moves = new HashSet<>(  );
  46.         for(int i = 0; i < boardSize; i++){
  47.             for(int j = 0; j < boardSize; j++){
  48.                 if(board[i][j].hasCellColor( Colors.WHITE ) && player == 1)
  49.                     moves.addAll( board[i][j].addMoves( this ) );
  50.                 if(board[i][j].hasCellColor( Colors.BLACK ) && player == 2)
  51.                     moves.addAll( board[i][j].addMoves( this ) );
  52.             }
  53.         }
  54.         return moves;
  55.     }
  56.  
  57.     public boolean tryPlayingPosition(Player player, String input) {
  58.         parser = new AlgebraicNotationParser( positionRegex, input );
  59.         if (!parser.isValidMoveString( input )) {
  60.             System.out.println( "Invalid move. Try Again." );
  61.             return false;
  62.         }
  63.  
  64.         String[] strings = input.split( "-" );
  65.         Position from = parser.parsePosition( strings[0] );
  66.         Position to = parser.parsePosition( strings[1] );
  67.         return checkPosition( player, from, to);
  68.     }
  69.  
  70.     private boolean checkPosition(Player player, Position pos1, Position pos2) {
  71.         if (!withinBounds( pos1 ) || !withinBounds( pos2 )) {
  72.             System.out.println( "Out of bounds. Try again." );
  73.             return false;
  74.         } else {
  75.             ChessMove move = new ChessMove( pos1, pos2);
  76.  
  77.             if (move(player, move)) {
  78.                 checkPawns();
  79.                 return true;
  80.             }
  81.         }
  82.         return false;
  83.     }
  84.  
  85.     private boolean move(Player player, ChessMove move) {
  86.         HashSet<ChessMove> moves;
  87.         Position in = new Position( move.getFrom().getX(), move.getFrom().getY() );
  88.         Position out = new Position( move.getTo().getX(), move.getTo().getY() );
  89.         moves = generateMoves( player );
  90.         if(hasMove( moves, move )){
  91.             if(move.getCastle()){
  92.                 if(move.getTo().getY()-move.getFrom().getY() == 2){
  93.                     move(player, new ChessMove(move.getFrom(), move.getTo()));
  94.                     move(player, new ChessMove( new Position( move.getFrom().getX(), move.getTo().getY()-2),
  95.                             new Position( move.getFrom().getX(), move.getTo().getY())));
  96.                 } else{
  97.                     if(move.getTo().getY()-move.getFrom().getY() == -2){
  98.                         move(player, new ChessMove(move.getFrom(), move.getTo()));
  99.                         move(player, new ChessMove( new Position( move.getFrom().getX(), move.getTo().getY()-2),
  100.                                 new Position( move.getFrom().getX(), move.getTo().getY())));
  101.                     }
  102.                 }
  103.             } else {
  104.                 if (player.getColor().equals(Colors.BLACK)) {
  105.                     ChessPiece tmp = board[in.getX()][in.getY()];
  106.                     board[in.getX()][in.getY()] = createPiece( genCell( in.getX(), in.getY() ) );
  107.                     tmp.setNewLocation( out );
  108.                     board[out.getX()][out.getY()] = tmp;
  109.                     board[out.getX()][out.getY()].setMoved();
  110.                 } else if(player.getColor() == Colors.WHITE) {
  111.                     ChessPiece tmp = board[in.getX()][in.getY()];
  112.                     board[in.getX()][in.getY()] = createPiece( genCell( in.getX(), in.getY() ) );
  113.                     tmp.setNewLocation( out );
  114.                     board[out.getX()][out.getY()] = tmp;
  115.                     board[out.getX()][out.getY()].setMoved();
  116.                 }
  117.             }
  118.             return true;
  119.         }
  120.  
  121.         return false;
  122.     }
  123.  
  124.     @Override
  125.     public void print() {
  126.         char ch = 'A';
  127.         System.out.print( "    " );
  128.         for (int i = 0; i < boardSize; i++) {
  129.             System.out.print( ch );
  130.             System.out.print( "  " );
  131.             ch++;
  132.         }
  133.         System.out.println(  );
  134.         for (int i = 0; i < boardSize; i++) {
  135.             if (i < 9)
  136.                 System.out.print( " " );
  137.             System.out.print( boardSize-i );
  138.             System.out.print( "  " );
  139.             for (int j = 7; j >= 0; j--) {
  140.                 if (board[7-i][7-j].toString().equalsIgnoreCase( "_" ))
  141.                     System.out.print( ANSI_BLUE + board[7-i][7-j].toString() + ANSI_RESET );
  142.                 else if (board[7-i][7-j].toString().equalsIgnoreCase( "#" ))
  143.                     System.out.print( ANSI_RED + board[7-i][7-j].toString() + ANSI_RESET );
  144.                 else{
  145.                     System.out.print( board[7-i][7-j].toString() );
  146.                 }
  147.                 System.out.print( "  " );
  148.             }
  149.             System.out.print( boardSize-i );
  150.             System.out.println();
  151.         }
  152.         ch = 'A';
  153.         System.out.print( "    " );
  154.         for (int i = 0; i < boardSize; i++) {
  155.             System.out.print( ch );
  156.             System.out.print( "  " );
  157.             ch++;
  158.         }
  159.         System.out.println();
  160.     }
  161.     private boolean hasMove(HashSet<ChessMove> moves, ChessMove move) {
  162.         for (ChessMove each : moves) {
  163.             if (move.equals( each )) {
  164.                 return true;
  165.             }
  166.         }
  167.         return false;
  168.     }
  169.     public boolean pieceAt(Position position){
  170.         if(board[position.getX()][position.getY()].hasCellColor( Colors.WHITE )
  171.                 || board[position.getX()][position.getY()].hasCellColor( Colors.BLACK ))
  172.             return true;
  173.         return false;
  174.     }
  175.     public boolean checkCapture(Position position, Colors playerColor){
  176.         if(playerColor.equals( Colors.BLACK ))
  177.             return whitePieceAt( position );
  178.         else if(playerColor.equals( Colors.WHITE ))
  179.             return blackPieceAt( position );
  180.         return false;
  181.     }
  182.  
  183.     public boolean movedPieceAt(Position position){
  184.         boolean moved = board[position.getX()][position.getY()].hasMoved();
  185.         if(moved)
  186.             return true;
  187.         return false;
  188.     }
  189.     @Override
  190.     public void init() {
  191.         for (int row = 0; row < boardSize; row++) {
  192.             for (int column = 0; column < boardSize; column++) {
  193.                 board[row][column] = genCell( row, column );
  194.                 if(row == 1 || row == 6) {
  195.                     if (row == 6)
  196.                         board[row][column] =
  197.                                 new PawnPiece( new Position( row, column ), Colors.BLACK );
  198.                     else if (row == 1)
  199.                         board[row][column] =
  200.                                 new PawnPiece( new Position( row, column ), Colors.WHITE );
  201.                 }
  202.                 if(row == 0 || row == 7){
  203.                     if(column == 0 || column == 7)
  204.                         board[row][column] =
  205.                                 new RookPiece( new Position( row, column ),  (row == 0)?Colors.WHITE:Colors.BLACK );
  206.  
  207.                     if(column == 1 || column == 6)
  208.                         board[row][column] =
  209.                                 new KnightPiece( new Position( row, column ),  (row == 0)?Colors.WHITE:Colors.BLACK  );
  210.                     if(column == 2 || column == 5)
  211.                         board[row][column] =
  212.                                 new BishopPiece( new Position( row, column ), (row == 0)?Colors.WHITE:Colors.BLACK  );
  213.                     if(column == 3)
  214.                         board[row][column] =
  215.                                 new QueenPiece( new Position( row, column ), (row==0)?Colors.WHITE:Colors.BLACK );
  216.                     if(column == 4)
  217.                         board[row][column] =
  218.                                 new KingPiece( new Position( row, column ), (row==0)?Colors.WHITE:Colors.BLACK );
  219.                 }
  220.             }
  221.         }
  222.     }
  223.     private void initEmpty() {
  224.         for (int row = 0; row < boardSize; row++) {
  225.             for (int column = 0; column < boardSize; column++) {
  226.                 this.board[row][column] = genCell( row, column );
  227.             }
  228.         }
  229.     }
  230.     protected ChessPiece genCell(int row, int col) {
  231.         if ((row + col) % 2 == 0) {
  232.             ChessPiece cell = new EmptyChessPiece( new Position( row, col ), Colors.UNDER );
  233.             return cell;
  234.         }
  235.         ChessPiece cell = new EmptyChessPiece( new Position( row, col ), Colors.POUND );
  236.         return cell;
  237.     }
  238.  
  239.     public boolean playerInCheck(Player player) {
  240.         int tmpPlayer = (player.getColor().equals( Colors.BLACK ) ? 1 : 2);
  241.         Colors colors = (player.getColor().equals( Colors.BLACK ) ? Colors.WHITE : Colors.BLACK);
  242.         HashSet<ChessMove> moves = this.generateMoves( tmpPlayer );
  243.  
  244.         for(ChessMove each : moves) {
  245.             System.out.println( each );
  246.             if (this.checkCapture( each.getTo(), colors ) && this.kingAt( each.getTo() )) {
  247.                 return true;
  248.             }
  249.         }
  250.         return false;
  251.  
  252.     }
  253.  
  254.     private boolean kingAt(Position to) {
  255.             for(ChessPiece [] x : board) {
  256.                 for (ChessPiece y : x) {
  257.                     if(y.isKing() && y.getPosition().toString().equals(to.toString() ))
  258.                         return true;
  259.                 }
  260.             }
  261.         return false;
  262.     }
  263.     void checkPawns(){
  264.         for(int i = 0; i < 8; i++){
  265.             for(int j = 0; j < 8; j++) {
  266.                 if (board[i][j].checkPawn())
  267.                     setQueen( board[i][j]);
  268.             }
  269.         }
  270.     }
  271.  
  272.     private void setQueen(ChessPiece piece) {
  273.         Position position = piece.getPosition();
  274.         board[position.getX()][position.getY()] = new QueenPiece( position, piece.getColors() );
  275.     }
  276.     public void printAvailableMoves(HashSet<ChessMove> move) {
  277.         System.out.println( "Available Moves: " + move );
  278.     }
  279.  
  280.     @Override
  281.     public ChessBoard clone(){
  282.         return new ChessBoard(board);
  283.     }
  284.  
  285.     public HashSet<ChessMove> removeBadMoves(HashSet<ChessMove> initialMoves, Player currentPlayer) {
  286.         HashSet<ChessMove> newMoves = new HashSet<>(  );
  287.         for(ChessMove each : initialMoves)
  288.             if(testMove(each, currentPlayer))
  289.                 if(each.getCastle()){
  290.                     if(!playerInCheck( currentPlayer ))
  291.                         newMoves.add( each );
  292.                 }else newMoves.add( each );
  293.         return newMoves;
  294.     }
  295.  
  296.     private boolean testMove(ChessMove each, Player currentPlayer) {
  297.         move(currentPlayer, each );
  298.         return !playerInCheck( currentPlayer );
  299.     }
  300.  
  301.     private ChessPiece createPiece(ChessPiece piece){
  302.         ChessPiece newPiece;
  303.         if(piece.pieceName.equals( Colors.PAWN ))
  304.             newPiece = new PawnPiece( piece.getPosition(), piece.getColors());
  305.         else if(piece.pieceName.equals( Colors.BISHOP ))
  306.             newPiece = new BishopPiece( piece.getPosition(), piece.getColors());
  307.         else if(piece.pieceName.equals( Colors.ROOK ))
  308.             newPiece = new RookPiece( piece.getPosition(), piece.getColors());
  309.         else if(piece.pieceName.equals( Colors.QUEEN ))
  310.             newPiece = new QueenPiece( piece.getPosition(), piece.getColors());
  311.         else if(piece.pieceName.equals( Colors.KING ))
  312.             newPiece = new KingPiece( piece.getPosition(), piece.getColors());
  313.         else if(piece.pieceName.equals( Colors.KNIGHT ))
  314.             newPiece = new KnightPiece( piece.getPosition(), piece.getColors());
  315.         else
  316.             newPiece = new EmptyChessPiece( piece.getPosition(),
  317.                     genCell( piece.getPosition().getX(), piece.getPosition().getY() ).getColors());
  318.         return newPiece;
  319.  
  320.  
  321.     }
  322.  
  323.     public boolean whitePieceAt(Position pos){
  324.  
  325.         if(board[pos.getX()][pos.getY()].hasCellColor( Colors.WHITE ))
  326.             return true;
  327.         return false;
  328.     }
  329.  
  330.     public boolean blackPieceAt(Position pos){
  331.         if(board[pos.getX()][pos.getY()].hasCellColor( Colors.BLACK ))
  332.             return true;
  333.         return false;
  334.     }
  335.  
  336.     private Player changePlayer(Player player){
  337.         String name =
  338.                 (player.getName().equalsIgnoreCase( "player 1" ))
  339.                         ? "Player 2" : "Player 1";
  340.         Colors color = (player.getColor().equals( Colors.BLACK )) ? Colors.WHITE : Colors.BLACK;
  341.         Player tmpPlayer = new Player( name, color );
  342.         return tmpPlayer;
  343.     }
  344. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement