Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package GuiChess;
- import boardgame.AlgebraicNotationParser;
- import boardgame.Colors;
- import boardgame.Player;
- import boardgame.Position;
- import chess.ChessBoard;
- import chess.ChessGameplay;
- import chess.ChessMove;
- import chess.ChessPiece;
- import javax.swing.*;
- import javax.swing.border.EmptyBorder;
- import java.awt.*;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.io.File;
- import java.util.HashSet;
- public class GUIChessBoard extends ChessGameplay {
- public JFrame frame;
- public JPanel contentPane;
- public JPanel[][] cells;
- private String move;
- private ChessMain chessMain = new ChessMain();
- private DefaultListModel<String> model;
- public GUIChessBoard(Player currentPlayer, Player otherPlayer){
- super(currentPlayer, otherPlayer);
- initialize();
- // chessMain.board.init();
- }
- private void initialize(){
- frame = new JFrame();
- frame.setTitle("Chess GUI");
- frame.setResizable( false );
- frame.setBounds(100,100,900,800);
- frame.setMinimumSize(new Dimension(900, 800));
- frame.setLayout(new BorderLayout());
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.pack();
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5,5,5,5));
- frame.setContentPane(contentPane);
- JPanel moves = new JPanel();
- JPanel boardGUI = new JPanel();
- boardGUI.setLayout(new GridLayout(10,10,0,0));
- GroupLayout glContentPane = new GroupLayout(contentPane);
- glContentPane.setHorizontalGroup(
- glContentPane.createParallelGroup(GroupLayout.Alignment.LEADING)
- .addGroup(glContentPane.createSequentialGroup()
- .addComponent(boardGUI, GroupLayout.DEFAULT_SIZE, 744, Short.MAX_VALUE)
- .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(moves, GroupLayout.DEFAULT_SIZE, 424, Short.MAX_VALUE)
- .addGap(0))
- );
- glContentPane.setVerticalGroup(
- glContentPane.createParallelGroup(GroupLayout.Alignment.LEADING)
- .addComponent(boardGUI, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 801, Short.MAX_VALUE)
- .addComponent(moves, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 801, Short.MAX_VALUE)
- );
- model = new DefaultListModel<>();
- JList<String> list = new JList<String>(model);
- list.setFont(new Font("Courier New", Font.PLAIN, 12));
- list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
- JScrollPane scrollPane = new JScrollPane();
- scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
- scrollPane.setViewportView(list);
- GroupLayout glMoves = new GroupLayout(moves);
- glMoves.setHorizontalGroup(
- glMoves.createParallelGroup(GroupLayout.Alignment.LEADING)
- .addGroup(glMoves.createSequentialGroup()
- .addContainerGap()
- .addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 404, Short.MAX_VALUE)
- .addContainerGap())
- );
- glMoves.setVerticalGroup(
- glMoves.createParallelGroup(GroupLayout.Alignment.LEADING)
- .addGroup(glMoves.createSequentialGroup()
- .addGap(5)
- .addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 785, Short.MAX_VALUE)
- .addContainerGap())
- );
- moves.setLayout(glMoves);
- moves.setFont(new Font("Courier New", Font.PLAIN, 12));
- model.addElement("Moves:");
- model.addElement("It is Player 1's turn.");
- cells = new JPanel[10][10];
- JLabel label;
- String[] chars = {"","A", "B", "C", "D", "E", "F", "G", "H",""};
- String[] nums = {"","8", "7", "6","5", "4", "3", "2", "1",""};
- contentPane.setLayout(glContentPane);
- for(int i = 0; i < 10; i++){
- for(int j = 0; j < 10; j++) {
- cells[i][j] = new JPanel();
- if (i % 2 == j % 2 && i != 0 && j != 0 && i != 9 && j != 9) {
- cells[i][j].setBackground(new Color( 102, 51, 0, 190) );
- cells[i][j].setBorder(BorderFactory.createLineBorder( Color.BLACK, 2));
- cells[i][j].addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- ((JComponent)e.getComponent())
- .setBorder(BorderFactory.createLineBorder(Color.GREEN, 2));
- int i = boardGUI.getWidth()/10;
- int j = 0;
- while (boardGUI.getMousePosition().getX()>i){
- i+=boardGUI.getWidth()/10;
- j++;
- }
- int column = j;
- i = boardGUI.getHeight()/10;
- j = 0;
- while(boardGUI.getMousePosition().getY()>i){
- i+=boardGUI.getHeight()/10;
- j++;
- }
- int row = 9-j;
- if(move == null)
- move = chars[column]+row+'-';
- else if(move.length() == 3){
- move +=chars[column]+ row;
- if(turn(currentPlayer, getCurrentPlayerPieces(currentPlayer))){
- model.addElement(move);
- model.addElement("It is "+ currentPlayer.getName()+"'s turn");
- }
- move=null;
- }
- }
- });
- }
- else if(i != 0 && j != 0 && i != 9 && j != 9){
- cells[i][j].setBackground(Color.WHITE);
- cells[i][j].setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
- cells[i][j].addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- ((JComponent)e.getComponent())
- .setBorder(BorderFactory.createLineBorder(Color.GREEN, 2));
- int i = boardGUI.getWidth()/10;
- int j = 0;
- while (boardGUI.getMousePosition().getX()>i){
- i+=boardGUI.getWidth()/10;
- j++;
- }
- int column = j;
- i = boardGUI.getHeight()/10;
- j = 0;
- while(boardGUI.getMousePosition().getY()>i){
- i+=boardGUI.getHeight()/10;
- j++;
- }
- int row = (9-j);
- if(move == null)
- move = chars[column]+row+'-';
- else if(move.length() == 3){
- move +=chars[column]+ row;
- if(turn(currentPlayer, getCurrentPlayerPieces(currentPlayer))){
- model.addElement(move);
- model.addElement("It is "+ currentPlayer.getName()+"'s turn");
- }
- move=null;
- }
- }
- });
- }
- else {
- cells[i][j].setBackground(Color.GRAY);
- if(j > 0 && j < 9) {
- label = new JLabel(chars[j]);
- label.setHorizontalTextPosition(SwingConstants.CENTER);
- label.setVerticalTextPosition(SwingConstants.BOTTOM);
- cells[i][j].add(label);
- }
- else if( i > 0 && i < 9){
- label = new JLabel(nums[i]);
- label.setHorizontalTextPosition(SwingConstants.CENTER);
- label.setVerticalTextPosition(SwingConstants.BOTTOM);
- cells[i][j].add(label);
- }
- }
- boardGUI.add(cells[i][j]);
- }
- }
- update(chessMain.getWhite(), chessMain.getBlack());
- }
- public boolean turn(Player player, HashSet<ChessPiece> pieces){
- ChessBoard testBoard = chessMain.getBoard().clone();
- HashSet<ChessMove> moves;
- moves = chessMain.board.generateMoves( player );
- moves = testBoard.removeBadMoves( moves, player );
- try{
- if (chessMain.board.tryPlayingPosition( player, this.move, moves )) {
- chessMain.board.updateBoard(chessMain.getWhite(), chessMain.getBlack());
- resetBoard();
- nextPlayer();
- update(chessMain.getWhite(), chessMain.getBlack());
- if(chessMain.board.playerInCheck(player)){
- if(moves.size()==0) {
- win();
- }
- model.addElement( "Check!" );
- }
- else if(chessMain.board.generateMoves( player ).size()==0 )
- guiStalemate();
- return true;
- }else{
- resetBoard();
- return false;
- }
- }catch (IndexOutOfBoundsException e){
- resetBoard();
- return false;
- }catch (NullPointerException e){
- resetBoard();
- return false;
- }
- }
- private void guiStalemate() {
- model.addElement( "It is a stalemate. Game over!" );
- }
- private void win() {
- model.addElement("Checkmate! " + currentPlayer+"has won!" );
- }
- private String getMove() {
- return move;
- }
- private HashSet<ChessPiece> getCurrentPlayerPieces(Player player){
- if(player.getColor().equals(Colors.BLACK))
- return chessMain.getBlack();
- else
- return chessMain.getWhite();
- }
- public void update(HashSet<ChessPiece> p1, HashSet<ChessPiece> p2) {
- for(int i = 1; i < 9; i++)
- for(int j = 1; j < 9; j++) {
- cells[i][j].removeAll();
- }
- for (ChessPiece each : p1) {
- ImageIcon img = new ImageIcon("src"+File.separator+"GuiChess"+File.separator+sortImages(each.toString()));
- JLabel label = new JLabel(img);
- cells[7-each.getPosition().getX()+1][each.getPosition().getY()+1].add(label);
- cells[7-each.getPosition().getX()+1][each.getPosition().getY()+1].validate();
- }
- for (ChessPiece each : p2) {
- ImageIcon img = new ImageIcon("src"+File.separator+"GuiChess"+File.separator+sortImages(each.toString()));
- JLabel label = new JLabel(img);
- cells[7-each.getPosition().getX()+1][each.getPosition().getY()+1].add(label);
- cells[7-each.getPosition().getX()+1][each.getPosition().getY()+1].validate();
- }
- }
- public String sortImages(String pieceName) {
- if (pieceName.equals("k"))
- return "King_White.png";
- else if (pieceName.equals("K"))
- return "King_Black.png";
- else if (pieceName.equals("q"))
- return "Queen_White.png";
- else if (pieceName.equals("Q"))
- return "Queen_Black.png";
- else if(pieceName.equals("r"))
- return "Rook_White.png";
- else if(pieceName.equals("R"))
- return "Rook_Black.png";
- else if (pieceName.equals("n"))
- return "Knight_White.png";
- else if(pieceName.equals("N"))
- return "Knight_Black.png";
- else if(pieceName.equals("b"))
- return "Bishop_White.png";
- else if(pieceName.equals("B"))
- return "Bishop_Black.png";
- else if(pieceName.equals("p"))
- return "Pawn_White.png";
- else if(pieceName.equals("P"))
- return "Pawn_Black.png";
- else
- return "";
- }
- public void resetBoard(){
- for(int i = 1; i < 9; i++)
- for(int j = 1; j < 9; j++)
- cells[i][j].setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
- }
- public void showGUI() {
- frame.setVisible(true);
- }
- public class ChessMain{
- private HashSet<ChessPiece> black;
- private HashSet<ChessPiece> white;
- private ChessBoard board;
- public ChessMain(){
- this.board = new ChessBoard(8);
- this.white = board.getWhitePieces();
- this.black = board.getBlackPieces();
- }
- public ChessBoard getBoard(){return board;}
- public HashSet<ChessPiece> getWhite(){return white;}
- public HashSet<ChessPiece> getBlack(){return black;}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement