Advertisement
ausbrumm96

GUIChessBoard

Apr 19th, 2019
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.53 KB | None | 0 0
  1. package GuiChess;
  2.  
  3. import boardgame.AlgebraicNotationParser;
  4. import boardgame.Colors;
  5. import boardgame.Player;
  6. import boardgame.Position;
  7. import chess.ChessBoard;
  8. import chess.ChessGameplay;
  9. import chess.ChessMove;
  10. import chess.ChessPiece;
  11.  
  12. import javax.swing.*;
  13. import javax.swing.border.EmptyBorder;
  14. import java.awt.*;
  15. import java.awt.event.MouseAdapter;
  16. import java.awt.event.MouseEvent;
  17. import java.io.File;
  18. import java.util.HashSet;
  19.  
  20. public class GUIChessBoard extends ChessGameplay {
  21.     public JFrame frame;
  22.     public JPanel contentPane;
  23.     public JPanel[][] cells;
  24.     private String move;
  25.     private ChessMain chessMain = new ChessMain();
  26.     private DefaultListModel<String> model;
  27.  
  28.     public GUIChessBoard(Player currentPlayer, Player otherPlayer){
  29.         super(currentPlayer, otherPlayer);
  30.         initialize();
  31.      //   chessMain.board.init();
  32.     }
  33.  
  34.     private void initialize(){
  35.         frame = new JFrame();
  36.         frame.setTitle("Chess GUI");
  37.         frame.setResizable( false );
  38.         frame.setBounds(100,100,900,800);
  39.         frame.setMinimumSize(new Dimension(900, 800));
  40.         frame.setLayout(new BorderLayout());
  41.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  42.         frame.pack();
  43.         contentPane = new JPanel();
  44.         contentPane.setBorder(new EmptyBorder(5,5,5,5));
  45.         frame.setContentPane(contentPane);
  46.         JPanel moves = new JPanel();
  47.         JPanel boardGUI = new JPanel();
  48.         boardGUI.setLayout(new GridLayout(10,10,0,0));
  49.         GroupLayout glContentPane = new GroupLayout(contentPane);
  50.         glContentPane.setHorizontalGroup(
  51.                 glContentPane.createParallelGroup(GroupLayout.Alignment.LEADING)
  52.                         .addGroup(glContentPane.createSequentialGroup()
  53.                                 .addComponent(boardGUI, GroupLayout.DEFAULT_SIZE, 744, Short.MAX_VALUE)
  54.                                 .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
  55.                                 .addComponent(moves, GroupLayout.DEFAULT_SIZE, 424, Short.MAX_VALUE)
  56.                                 .addGap(0))
  57.         );
  58.         glContentPane.setVerticalGroup(
  59.                 glContentPane.createParallelGroup(GroupLayout.Alignment.LEADING)
  60.                         .addComponent(boardGUI, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 801, Short.MAX_VALUE)
  61.                         .addComponent(moves, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 801, Short.MAX_VALUE)
  62.         );
  63.  
  64.         model = new DefaultListModel<>();
  65.         JList<String> list = new JList<String>(model);
  66.         list.setFont(new Font("Courier New", Font.PLAIN, 12));
  67.         list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  68.  
  69.         JScrollPane scrollPane = new JScrollPane();
  70.         scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  71.         scrollPane.setViewportView(list);
  72.         GroupLayout glMoves = new GroupLayout(moves);
  73.         glMoves.setHorizontalGroup(
  74.                 glMoves.createParallelGroup(GroupLayout.Alignment.LEADING)
  75.                         .addGroup(glMoves.createSequentialGroup()
  76.                                 .addContainerGap()
  77.                                 .addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 404, Short.MAX_VALUE)
  78.                                 .addContainerGap())
  79.         );
  80.         glMoves.setVerticalGroup(
  81.                 glMoves.createParallelGroup(GroupLayout.Alignment.LEADING)
  82.                         .addGroup(glMoves.createSequentialGroup()
  83.                                 .addGap(5)
  84.                                 .addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 785, Short.MAX_VALUE)
  85.                                 .addContainerGap())
  86.         );
  87.  
  88.         moves.setLayout(glMoves);
  89.         moves.setFont(new Font("Courier New", Font.PLAIN, 12));
  90.         model.addElement("Moves:");
  91.         model.addElement("It is Player 1's turn.");
  92.         cells = new JPanel[10][10];
  93.         JLabel label;
  94.         String[] chars = {"","A", "B", "C", "D", "E", "F", "G", "H",""};
  95.         String[] nums = {"","8", "7", "6","5", "4", "3", "2", "1",""};
  96.         contentPane.setLayout(glContentPane);
  97.         for(int i = 0; i < 10; i++){
  98.             for(int j = 0; j < 10; j++) {
  99.                 cells[i][j] = new JPanel();
  100.                 if (i % 2 == j % 2 && i != 0 && j != 0 && i != 9 && j != 9) {
  101.                     cells[i][j].setBackground(new Color( 102, 51, 0, 190) );
  102.                     cells[i][j].setBorder(BorderFactory.createLineBorder( Color.BLACK, 2));
  103.                     cells[i][j].addMouseListener(new MouseAdapter() {
  104.                         @Override
  105.                         public void mouseClicked(MouseEvent e) {
  106.                             ((JComponent)e.getComponent())
  107.                                     .setBorder(BorderFactory.createLineBorder(Color.GREEN, 2));
  108.                             int i = boardGUI.getWidth()/10;
  109.                             int j = 0;
  110.                             while (boardGUI.getMousePosition().getX()>i){
  111.                                 i+=boardGUI.getWidth()/10;
  112.                                 j++;
  113.                             }
  114.                             int column = j;
  115.                             i = boardGUI.getHeight()/10;
  116.                             j = 0;
  117.                             while(boardGUI.getMousePosition().getY()>i){
  118.                                 i+=boardGUI.getHeight()/10;
  119.                                 j++;
  120.                             }
  121.                             int row = 9-j;
  122.                             if(move == null)
  123.                                 move = chars[column]+row+'-';
  124.                             else if(move.length() == 3){
  125.                                 move +=chars[column]+ row;
  126.                                 if(turn(currentPlayer, getCurrentPlayerPieces(currentPlayer))){
  127.                                     model.addElement(move);
  128.                                     model.addElement("It is "+ currentPlayer.getName()+"'s turn");
  129.                                 }
  130.                                 move=null;
  131.                             }
  132.                         }
  133.                     });
  134.                 }
  135.                 else if(i != 0 && j != 0 && i != 9 && j != 9){
  136.                     cells[i][j].setBackground(Color.WHITE);
  137.                     cells[i][j].setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
  138.                     cells[i][j].addMouseListener(new MouseAdapter() {
  139.                         @Override
  140.                         public void mouseClicked(MouseEvent e) {
  141.                             ((JComponent)e.getComponent())
  142.                                     .setBorder(BorderFactory.createLineBorder(Color.GREEN, 2));
  143.                             int i = boardGUI.getWidth()/10;
  144.                             int j = 0;
  145.                             while (boardGUI.getMousePosition().getX()>i){
  146.                                 i+=boardGUI.getWidth()/10;
  147.                                 j++;
  148.                             }
  149.                             int column = j;
  150.                             i = boardGUI.getHeight()/10;
  151.                             j = 0;
  152.                             while(boardGUI.getMousePosition().getY()>i){
  153.                                 i+=boardGUI.getHeight()/10;
  154.                                 j++;
  155.                             }
  156.                             int row = (9-j);
  157.                             if(move == null)
  158.                                 move = chars[column]+row+'-';
  159.                             else if(move.length() == 3){
  160.                                 move +=chars[column]+ row;
  161.                                 if(turn(currentPlayer, getCurrentPlayerPieces(currentPlayer))){
  162.                                     model.addElement(move);
  163.                                     model.addElement("It is "+ currentPlayer.getName()+"'s turn");
  164.                                    
  165.                                 }
  166.                                 move=null;
  167.                             }
  168.                         }
  169.                     });
  170.                 }
  171.                 else {
  172.                     cells[i][j].setBackground(Color.GRAY);
  173.                     if(j > 0 && j < 9) {
  174.                         label = new JLabel(chars[j]);
  175.                         label.setHorizontalTextPosition(SwingConstants.CENTER);
  176.                         label.setVerticalTextPosition(SwingConstants.BOTTOM);
  177.                         cells[i][j].add(label);
  178.                     }
  179.                     else if( i > 0 && i < 9){
  180.                         label = new JLabel(nums[i]);
  181.                         label.setHorizontalTextPosition(SwingConstants.CENTER);
  182.                         label.setVerticalTextPosition(SwingConstants.BOTTOM);
  183.                         cells[i][j].add(label);
  184.                     }
  185.  
  186.                 }
  187.                 boardGUI.add(cells[i][j]);
  188.             }
  189.         }
  190.         update(chessMain.getWhite(), chessMain.getBlack());
  191.     }
  192.  
  193.     public boolean turn(Player player, HashSet<ChessPiece> pieces){
  194.         ChessBoard testBoard = chessMain.getBoard().clone();
  195.         HashSet<ChessMove> moves;
  196.         moves = chessMain.board.generateMoves( player );
  197.         moves = testBoard.removeBadMoves( moves, player );
  198.         try{
  199.             if (chessMain.board.tryPlayingPosition( player, this.move, moves )) {
  200.  
  201.                 chessMain.board.updateBoard(chessMain.getWhite(), chessMain.getBlack());
  202.                 resetBoard();
  203.                 nextPlayer();
  204.                 update(chessMain.getWhite(), chessMain.getBlack());
  205.  
  206.                 if(chessMain.board.playerInCheck(player)){
  207.                     if(moves.size()==0) {
  208.                         win();
  209.                     }
  210.                     model.addElement( "Check!" );
  211.                 }
  212.                 else if(chessMain.board.generateMoves( player ).size()==0 )
  213.                     guiStalemate();
  214.                 return true;
  215.             }else{
  216.                 resetBoard();
  217.                 return false;
  218.             }
  219.         }catch (IndexOutOfBoundsException e){
  220.             resetBoard();
  221.             return false;
  222.         }catch (NullPointerException e){
  223.             resetBoard();
  224.             return false;
  225.         }
  226.  
  227.  
  228.     }
  229.  
  230.     private void guiStalemate() {
  231.         model.addElement( "It is a stalemate. Game over!" );
  232.     }
  233.  
  234.     private void win() {
  235.         model.addElement("Checkmate! " + currentPlayer+"has won!" );
  236.     }
  237.  
  238.     private String getMove() {
  239.         return move;
  240.     }
  241.  
  242.     private HashSet<ChessPiece> getCurrentPlayerPieces(Player player){
  243.         if(player.getColor().equals(Colors.BLACK))
  244.             return chessMain.getBlack();
  245.         else
  246.             return chessMain.getWhite();
  247.     }
  248.  
  249.     public void update(HashSet<ChessPiece> p1, HashSet<ChessPiece> p2) {
  250.         for(int i = 1; i < 9; i++)
  251.             for(int j = 1; j < 9; j++) {
  252.                 cells[i][j].removeAll();
  253.             }
  254.         for (ChessPiece each : p1) {
  255.             ImageIcon img = new ImageIcon("src"+File.separator+"GuiChess"+File.separator+sortImages(each.toString()));
  256.             JLabel label = new JLabel(img);
  257.             cells[7-each.getPosition().getX()+1][each.getPosition().getY()+1].add(label);
  258.             cells[7-each.getPosition().getX()+1][each.getPosition().getY()+1].validate();
  259.         }
  260.         for (ChessPiece each : p2) {
  261.             ImageIcon img = new ImageIcon("src"+File.separator+"GuiChess"+File.separator+sortImages(each.toString()));
  262.             JLabel label = new JLabel(img);
  263.             cells[7-each.getPosition().getX()+1][each.getPosition().getY()+1].add(label);
  264.             cells[7-each.getPosition().getX()+1][each.getPosition().getY()+1].validate();
  265.         }
  266.     }
  267.     public String sortImages(String pieceName) {
  268.         if (pieceName.equals("k"))
  269.             return "King_White.png";
  270.         else if (pieceName.equals("K"))
  271.             return "King_Black.png";
  272.         else if (pieceName.equals("q"))
  273.             return "Queen_White.png";
  274.         else if (pieceName.equals("Q"))
  275.             return "Queen_Black.png";
  276.         else if(pieceName.equals("r"))
  277.             return "Rook_White.png";
  278.         else if(pieceName.equals("R"))
  279.             return "Rook_Black.png";
  280.         else if (pieceName.equals("n"))
  281.             return "Knight_White.png";
  282.         else if(pieceName.equals("N"))
  283.             return "Knight_Black.png";
  284.         else if(pieceName.equals("b"))
  285.             return "Bishop_White.png";
  286.         else if(pieceName.equals("B"))
  287.             return "Bishop_Black.png";
  288.         else if(pieceName.equals("p"))
  289.             return "Pawn_White.png";
  290.         else if(pieceName.equals("P"))
  291.             return "Pawn_Black.png";
  292.         else
  293.             return "";
  294.     }
  295.     public void resetBoard(){
  296.         for(int i = 1; i < 9; i++)
  297.             for(int j = 1; j < 9; j++)
  298.                 cells[i][j].setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
  299.     }
  300.  
  301.     public void showGUI() {
  302.         frame.setVisible(true);
  303.     }
  304.  
  305.     public class ChessMain{
  306.         private HashSet<ChessPiece> black;
  307.         private HashSet<ChessPiece> white;
  308.         private ChessBoard board;
  309.         public ChessMain(){
  310.             this.board = new ChessBoard(8);
  311.             this.white = board.getWhitePieces();
  312.             this.black = board.getBlackPieces();
  313.         }
  314.         public ChessBoard getBoard(){return board;}
  315.         public HashSet<ChessPiece> getWhite(){return white;}
  316.         public HashSet<ChessPiece> getBlack(){return black;}
  317.  
  318.     }
  319.  
  320.  
  321.  
  322.  
  323. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement