All pastes #1310162 Raw Edit

Chess

public java v1 · immutable
#1310162 ·published 2009-01-16 16:52 UTC
rendered paste body
import java.awt.event.*;import javax.swing.*;import java.awt.*;public class Chess extends JApplet implements ActionListener {    JPanel outputPanel = new JPanel();    JPanel inputPanel = new JPanel();    JPanel mainPanel = new JPanel();    JLabel status = new JLabel();    ImageIcon wpawn = new ImageIcon("wpawn.gif");    ImageIcon wknight = new ImageIcon("wknight.gif");    ImageIcon wbishop = new ImageIcon("wbishop.gif");    ImageIcon wrook = new ImageIcon("wrook.gif");    ImageIcon wqueen = new ImageIcon("wqueen.gif");    ImageIcon wking = new ImageIcon("wking.gif");    ImageIcon bpawn = new ImageIcon("bpawn.gif");    ImageIcon bknight = new ImageIcon("bknight.gif");    ImageIcon bbishop = new ImageIcon("bbishop.gif");    ImageIcon brook = new ImageIcon("brook.gif");    ImageIcon bqueen = new ImageIcon("bqueen.gif");    ImageIcon bking = new ImageIcon("bking.gif");    ImageIcon blank = new ImageIcon("blank.gif");    JButton[][] uiboard = new JButton[8][8];    pawn pawn = new pawn();    knight knight = new knight();    king king = new king();    rook rook = new rook();    bishop bishop = new bishop();    queen queen = new queen();    String board[][] = { {"brook", "bknight", "bbishop", "bqueen",                       "bking", "bbishop", "bknight", "brook"}, {"bpawn",                       "bpawn", "bpawn", "bpawn", "bpawn", "bpawn", "bpawn",                       "bpawn"}, {" ", " ", " ", " ", " ", " ", " ", " "}, {" ",                       " ", " ", " ", " ", " ", " ", " "}, {" ", " ", " ",                       " ",                       " ", " ",                       " ", " "}, {" ", " ", " ", " ", " ", " ", " ", " "},                       {"wpawn", "wpawn",                       "wpawn", "wpawn", "wpawn",                       "wpawn", "wpawn", "wpawn"}, {"wrook", "wknight",                       "wbishop", "wqueen", "wking", "wbishop", "wknight",                       "wrook"}    };    int[][] location = new int[8][8], avail = new int[8][8];    int x = 0, y = 0, turn = 1, pick = 0, y2 = 0, x2 = 0, check = 0;    ;    public void init() {        setContentPane(mainPanel);        mainPanel.add(inputPanel);        mainPanel.add(outputPanel);        outputPanel.add(status);        inputPanel.setLayout(new GridLayout(0, 8));        for (int q = 0; q <= 7; q++) {            for (int a = 0; a <= 7; a++) {                uiboard[q][a] = new JButton();                uiboard[q][a].addActionListener(this);                inputPanel.add(uiboard[q][a]);                uiboard[q][a].setBackground(Color.white);            }        }        setui();    }    public void actionPerformed(ActionEvent event) {        if (pick == 0) {            for (int q = 0; q <= 7; q++) {                for (int a = 0; a <= 7; a++) {                    if (event.getSource() == uiboard[q][a]) {                        x = a;                        y = q;                        break;                    }                }            }            if (board[y][x].substring(0, 1).equals("w") && turn % 2 == 0) {                showStatus("you can't select that piece. Its blacks turn");            } else if (board[y][x].substring(0, 1).equals("b") && turn % 2 == 1) {                showStatus("you can't select that piece. Its whites turn");            } else if (board[y][x].equals(" ")) {                showStatus("Please select a piece");            } else {                if (board[y][x].substring(1).equals("pawn")) {                    showStatus(board[y][x] + " is selected");                    avail = pawn.check(x, y, board);                } else if (board[y][x].substring(1).equals("king")) {                    showStatus(board[y][x] + " is selected");                    avail = king.check(x, y, board);                } else if (board[y][x].substring(1).equals("queen")) {                    showStatus(board[y][x] + " is selected");                    avail = queen.check(x, y, board);                } else if (board[y][x].substring(1).equals("rook")) {                    showStatus(board[y][x] + " is selected");                    avail = rook.check(x, y, board);                } else if (board[y][x].substring(1).equals("knight")) {                    showStatus(board[y][x] + " is selected");                    avail = knight.check(x, y, board);                } else if (board[y][x].substring(1).equals("bishop")) {                    showStatus(board[y][x] + " is selected");                    avail = bishop.check(x, y, board);                }                for (int u = 0; u < 8; u++) {                    for (int c = 0; c < 8; c++) {                        if (avail[u][c] == 1) {                            uiboard[u][c].setBackground(Color.RED);                        } else {                            uiboard[u][c].setBackground(Color.WHITE);                        }                    }                }                pick = 1;            }        } else {            /////////////SETTING X AND Y FOR SECOND POSITION///////////////            for (int q = 0; q <= 7; q++) {                for (int a = 0; a <= 7; a++) {                    if (event.getSource() == uiboard[q][a]) {                        x2 = a;                        y2 = q;                        break;                    }                }            }            //////////////////////////////////////////////////////////////            loop:                    if (uiboard[y2][x2].getBackground() == Color.RED) {                ///////////////CHECK FOR WIN///////////////////////////////                if (board[y2][x2].substring(1).equals("king")) {                    board[y2][x2] = board[y][x];                    board[y][x] = " ";                    setui();                    if (board[y2][x2].substring(0, 1).equals("b")) {                        JOptionPane.showMessageDialog(null, "BLACK WINS!!!!!!");                    } else {                        JOptionPane.showMessageDialog(null, "WHITE WINS!!!!!!");                    }                    turn = 0;                    break loop;                }                //////////////////////////////////////////////////////////////                ///////////////CHECK FOR QUEEN PROMOTION/////////////////////                if ((y2 == 0 || y2 == 7) &&                    (board[y][x].substring(1).equals("pawn"))) {                    if (board[y][x].substring(0, 1).equals("b") && y2 == 7) {                        board[y2][x2] = board[y][x].substring(0, 1) + "queen";                    }                    if (board[y][x].substring(0, 1).equals("w") && y2 == 0) {                        board[y2][x2] = board[y][x].substring(0, 1) + "queen";                    }                    setui();                }                //////////////////////////////////////////////////////////////                /////////////////////MOVING PIECE//////////////////////////////                board[y2][x2] = board[y][x];                                board[y][x] = " ";                                turn++;//////////////////////////////////////////////////////////////////////////////                check();                if (turn% 2 ==0){            status.setText(" It's Black's turn");          }          else{            status.setText("It's White's turn");          }                ///////////////////////CHECK FOR ILLEGAL MOVES////////////////            } else {                showStatus("Please make a legal move" + turn);            }            for (int u = 0; u < 8; u++) {                for (int c = 0; c < 8; c++) {                    uiboard[u][c].setBackground(Color.WHITE);                }            }            ///////////////////////////////////////////////////////////////////            pick = 0;        }        setui();    }///////////////////////RESETTING THE BOARD//////////////////////////////////////    public void setui() {        for (int u = 0; u < 8; u++) {            for (int c = 0; c < 8; c++) {                if (board[u][c].equals("wpawn")) {                    uiboard[u][c].setIcon(wpawn);                } else if (board[u][c].equals("wrook")) {                    uiboard[u][c].setIcon(wrook);                } else if (board[u][c].equals("wknight")) {                    uiboard[u][c].setIcon(wknight);                } else if (board[u][c].equals("wbishop")) {                    uiboard[u][c].setIcon(wbishop);                } else if (board[u][c].equals("wqueen")) {                    uiboard[u][c].setIcon(wqueen);                } else if (board[u][c].equals("wking")) {                    uiboard[u][c].setIcon(wking);                } else if (board[u][c].equals("bpawn")) {                    uiboard[u][c].setIcon(bpawn);                } else if (board[u][c].equals("brook")) {                    uiboard[u][c].setIcon(brook);                } else if (board[u][c].equals("bknight")) {                    uiboard[u][c].setIcon(bknight);                } else if (board[u][c].equals("bbishop")) {                    uiboard[u][c].setIcon(bbishop);                } else if (board[u][c].equals("bqueen")) {                    uiboard[u][c].setIcon(bqueen);                } else if (board[u][c].equals("bking")) {                    uiboard[u][c].setIcon(bking);                } else if (board[u][c].equals(" ")) {                    uiboard[u][c].setIcon(blank);                }            }        }    }    //////////////////////////////////////////////////////////////////////////    public int check() {      check = 0;        ///////////////////CHECK CHECKING///////////////////////////        String type = "";        if (turn % 2 == 0) {            type = "b";        } else {            type = "w";        }        for (int u = 0; u < 8; u++) {            for (int o = 0; o < 8; o++) {                if (board[u][o].substring(1).equals("pawn")) {                    if (pawn.checkcheck(o, u, board, type)) {                        check = 1;                        break;                    }                } else if (board[u][o].substring(1).equals("queen")) {                    if (queen.checkcheck(o, u, board, type)) {                        check = 1;                        break;                    }                } else if (board[u][o].substring(1).equals("rook")) {                    if (rook.checkcheck(o, u, board, type)) {                        check = 1;                        break;                    }                } else if (board[u][o].substring(1).equals("knight")) {                    if (knight.checkcheck(o, u, board, type)) {                        check = 1;                        break;                    }                } else if (board[u][o].substring(1).equals("bishop")) {                    if (bishop.checkcheck(o, u, board, type)) {                        check = 1;                        break;                    }                }            }        }        if (check == 1) {            JOptionPane.showMessageDialog(null,                                          board[y][x].substring(0, 1) +                                          " has put opponent into check!");        }        ////////////////////////////////////////////////////////////// return check;    }}