Advertisement
Guest User

Untitled

a guest
May 19th, 2013
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 19.67 KB | None | 0 0
  1. /* Chess game, by [REDACTED]
  2.  * written for [REDACTED]
  3.  * [REDACTED]
  4.  * (c) 2013
  5.  *
  6.  * All code is original, pretty much all of it is pretty sloppy.
  7.  *
  8.  * GRAND EXPLANATIONS OF METHODS:
  9.  *
  10.  * Chess(): Sets size and makes the board obviously. Creates
  11.  *          ContentPane.
  12.  * CreateTitlePanel():  Creates the word "Chess" at the top.
  13.  * CreateBoardPanel():  Uses the 2D Array Board to create an 8x8
  14.  *                      grid that holds all the buttons. Sets the
  15.  *                      actionCommand according to this four character
  16.  *                      code: <color><piece><row><column>
  17.  * CreateInstructionPanel():    Creates the instruction panel and all
  18.  *                              the game-effecting buttons.
  19.  * actionPerformed():   Makes the buttons work, but more importantly,
  20.  *                      divides user clicks into "piece1" and "piece2",
  21.  *                      piece1 being the first click they clicked and
  22.  *                      piece2 being the second one, then calls the
  23.  *                      methods to make sure the move is legal and,
  24.  *                      if it is, goes about changing it. Then, it
  25.  *                      checks if there's a pawn promotion, if anyone
  26.  *                      won, etc etc.
  27.  * rules(): Clicking the rules button calls this. All it does is explains
  28.  *          how to move, castle etc.
  29.  * backHandler():   If you click the back button this calls. It resets the
  30.  *                  board to it's previous position using the storeBoard
  31.  *                  3D Array
  32.  * fowardHandler(): The opposite. If you go undo going back this will
  33.  *                  move you up the storeBoard array.
  34.  * checkSide(): Makes sure you're moving the correct color. Prevents
  35.  *              anyone from going twice.
  36.  * CheckDir():  Makes sure the piece is the right direction. Bishops
  37.  *              can't go straight etc etc.
  38.  * CheckCol():  Makes sure the piece pathing is clear, nothing is
  39.  *              in the way.
  40.  * Castle():    Handles castling.
  41.  * verify():    Changes the board array if the move is legal. Also stores
  42.  *              the board configuration in storeBoard in case you want to
  43.  *              go back.
  44.  * makeChanges():   Redraws the board.
  45.  * CheckWon():  Checks if any side is missing its king.
  46.  * CheckPromo():    Checks if a pawn managed to get to the opposite side.
  47.  * promo(int, int): Promotes a pawn by changing its ID in the board
  48.  *                  array.
  49.  * winMessage(String):  Tells you you won if you won.
  50.  * initialFill():   Sets up the board to start. Adds all the IDs on the
  51.  *                  3D Arrays in the correct places.
  52.  * main(String[]):  Creates the board and sets it up, sets the color to
  53.  *                  white to start the game.
  54.  *
  55.  * BOARD IDS:
  56.  *      They are in the format <color><piece>. Here's the characters:
  57.  *
  58.  *      W: White
  59.  *      B: Black
  60.  *
  61.  *      P: Pawn
  62.  *      R: Rook
  63.  *      H: Knight (Horse)
  64.  *      B: Bishop
  65.  *      Q: Queen
  66.  *      K: King
  67.  *     
  68.  *      piece1 and piece2 are in a similar format, but the 3rd character
  69.  *      is the row and the 4th is the column. Numbers go from the
  70.  *      upper left, so the upper left hand corner is 0,0 and the
  71.  *      lower right hand corner is 7,7.
  72.  */
  73.  
  74. import java.awt.BorderLayout;
  75. import java.awt.Color;
  76. import java.awt.Container;
  77. import java.awt.FlowLayout;
  78. import java.awt.Font;
  79. import java.awt.GridLayout;
  80. import java.awt.event.ActionEvent;
  81. import java.awt.event.ActionListener;
  82.  
  83. import javax.swing.ImageIcon;
  84. import javax.swing.JButton;
  85. import javax.swing.JFrame;
  86. import javax.swing.JLabel;
  87. import javax.swing.JOptionPane;
  88. import javax.swing.JPanel;
  89.  
  90. public class Chess extends JFrame implements ActionListener {
  91.    
  92.     public static char color;
  93.     public static String[][] board = new String[8][8];
  94.     public static String piece1 = "";
  95.     public static String piece2 = "";
  96.    
  97.     public static String[][][] storeBoard = new String[8][8][1000];
  98.     public static int curLoc = 0;
  99.     public static int storedLoc = 0;
  100.     public static boolean overFlow = true;
  101.    
  102.     public Container contentPane;
  103.     public JPanel boardPanel;
  104.     public JPanel instructionPanel;
  105.    
  106.     public Chess() {
  107.         super();
  108.         contentPane = new Container();
  109.         setSize(800, 800);
  110.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  111.        
  112.         setTitle("Chess");
  113.         contentPane = getContentPane();
  114.         contentPane.setBackground(Color.white);
  115.         contentPane.setLayout(new BorderLayout());
  116.        
  117.         createTitlePanel();
  118.         createBoardPanel();
  119.         createInstructionPanel();
  120.     }
  121.    
  122.     public void createTitlePanel() {
  123.         Font fontType = new Font("Georgia", Font.BOLD, 50);
  124.        
  125.         JPanel titlePanel = new JPanel();
  126.         titlePanel.setLayout(new FlowLayout());
  127.         titlePanel.setBackground(Color.white);
  128.        
  129.         JLabel label1 = new JLabel("Chess");
  130.         label1.setFont(fontType);
  131.         label1.setForeground(Color.black);
  132.         titlePanel.add(label1);
  133.        
  134.         contentPane.add(titlePanel, BorderLayout.NORTH);
  135.     }
  136.    
  137.     public void createBoardPanel() {
  138.         boardPanel = new JPanel();
  139.         boardPanel.setLayout(new GridLayout(8,8));
  140.         boardPanel.setBackground(Color.white);
  141.        
  142.         for(int r=0;r<8;r++)
  143.             for(int c=0;c<8;c++){
  144.                 ImageIcon image;
  145.                 if(board[r][c].equals("BP"))
  146.                     image = new ImageIcon("blackpawn.png");
  147.                 else if(board[r][c].equals("BR"))
  148.                     image = new ImageIcon("blackrook.png");
  149.                 else if(board[r][c].equals("BH"))
  150.                     image = new ImageIcon("blackknight.png");
  151.                 else if(board[r][c].equals("BB"))
  152.                     image = new ImageIcon("blackbishop.png");
  153.                 else if(board[r][c].equals("BQ"))
  154.                     image = new ImageIcon("blackqueen.png");
  155.                 else if(board[r][c].equals("BK"))
  156.                     image = new ImageIcon("blackking.png");
  157.                 else if(board[r][c].equals("WP"))
  158.                     image = new ImageIcon("whitepawn.png");
  159.                 else if(board[r][c].equals("WR"))
  160.                     image = new ImageIcon("whiterook.png");
  161.                 else if(board[r][c].equals("WH"))
  162.                     image = new ImageIcon("whiteknight.png");
  163.                 else if(board[r][c].equals("WB"))
  164.                     image = new ImageIcon("whitebishop.png");
  165.                 else if(board[r][c].equals("WQ"))
  166.                     image = new ImageIcon("whitequeen.png");
  167.                 else if(board[r][c].equals("WK"))
  168.                     image = new ImageIcon("whiteking.png");
  169.                 else image = new ImageIcon("");
  170.                 JButton button = new JButton();
  171.                 button.setIcon(image);
  172.                 button.addActionListener(this);
  173.                 button.setBackground(Color.white);
  174.                 button.setActionCommand(board[r][c]+r+c);
  175.                 boardPanel.add(button);
  176.             }
  177.         contentPane.add(boardPanel, BorderLayout.CENTER);
  178.     }
  179.    
  180.     public void createInstructionPanel(){
  181.         Font fontType = new Font("Georgia", Font.BOLD, 17);
  182.        
  183.         instructionPanel = new JPanel();
  184.         instructionPanel.setLayout(new BorderLayout());
  185.         instructionPanel.setBackground(Color.white);
  186.        
  187.         JPanel topPanel = new JPanel();
  188.         topPanel.setLayout(new GridLayout(1,2));
  189.         topPanel.setBackground(Color.white);
  190.        
  191.         JPanel bottomPanel = new JPanel();
  192.         bottomPanel.setLayout(new GridLayout(1,3));
  193.         bottomPanel.setBackground(Color.white);
  194.        
  195.         if(color=='B'){
  196.             JLabel label = new JLabel("Black turn. " +
  197.                     "Pick piece and destination.");
  198.             label.setFont(fontType);
  199.             label.setForeground(Color.black);
  200.             topPanel.add(label);
  201.         }
  202.         else {
  203.             JLabel label = new JLabel
  204.                     ("White turn. Pick piece and destination.");
  205.             label.setFont(fontType);
  206.             label.setForeground(Color.black);
  207.             topPanel.add(label);
  208.         }
  209.        
  210.         JButton rulesButton = new JButton("How to Play");
  211.         rulesButton.setFont(fontType);
  212.         rulesButton.addActionListener(this);
  213.         rulesButton.setBackground(Color.white);
  214.         topPanel.add(rulesButton);
  215.        
  216.         JButton clearButton = new JButton
  217.                 ("Clear last click.");
  218.         clearButton.setFont(fontType);
  219.         clearButton.addActionListener(this);
  220.         clearButton.setBackground(Color.white);
  221.         bottomPanel.add(clearButton);
  222.        
  223.         JButton backButton = new JButton("Go back one move.");
  224.         backButton.setFont(fontType);
  225.         backButton.addActionListener(this);
  226.         backButton.setBackground(Color.white);
  227.         bottomPanel.add(backButton);
  228.        
  229.         JButton fowardButton = new JButton("Go foward one move.");
  230.         fowardButton.setFont(fontType);
  231.         fowardButton.addActionListener(this);
  232.         fowardButton.setBackground(Color.white);
  233.         bottomPanel.add(fowardButton);
  234.        
  235.         instructionPanel.add(topPanel, BorderLayout.NORTH);
  236.         instructionPanel.add(bottomPanel, BorderLayout.SOUTH);
  237.        
  238.         contentPane.add(instructionPanel, BorderLayout.SOUTH);
  239.     }
  240.    
  241.     public void actionPerformed(ActionEvent event) {
  242.         if(event.getActionCommand().equals("How to Play"))
  243.             rules();
  244.         else if(event.getActionCommand().equals
  245.                 ("Clear last click.")){
  246.             piece1 = "";
  247.             piece2 = "";
  248.         }
  249.         else if(event.getActionCommand().equals
  250.                 ("Go back one move.")) {
  251.             if(curLoc>0 || !overFlow) backHandler();
  252.         }
  253.         else if(event.getActionCommand().equals
  254.                 ("Go foward one move.")) {
  255.             if(curLoc<storedLoc) fowardHandler();
  256.         }
  257.         else {
  258.             if(piece1.isEmpty()) piece1 = event.getActionCommand();
  259.             else piece2 = event.getActionCommand();
  260.             if(piece1.length()>0 && piece2.length()>0) {
  261.                 if(giveUp()) System.exit(0);
  262.                 if((piece1!=piece2 && checkSide() && checkDir()
  263.                         && checkCol()) || castle()) {
  264.                     if(!castle()) verify();
  265.                     if(color=='B') color = 'W';
  266.                     else color = 'B';
  267.                     makeChanges();
  268.                     checkPromo();
  269.                     makeChanges();
  270.                     String won = checkWon();
  271.                     if(!won.equals("nope")) {
  272.                         winMessage(won);
  273.                         System.exit(0);
  274.                     }
  275.                 }
  276.                 piece1 = "";
  277.                 piece2 = "";
  278.             }
  279.         }
  280.     }
  281.     public static void rules() {
  282.         JOptionPane.showMessageDialog(null,"How to play David's Chess:" +
  283.                 "\nClick a piece and where to move it to move." +
  284.                 "\nTo castle, click the king and the rook\n" +
  285.                 "when there is no spaces between them and\n" +
  286.                 "neither piece has moved.\n\n" +
  287.                 "Clear last click will resolve most problems.");
  288.     }
  289.     public void backHandler() {
  290.         if(curLoc==0 && overFlow) {
  291.             curLoc=1000;
  292.             overFlow=false;
  293.         }
  294.         else curLoc--;
  295.        
  296.         for(int r=0;r<8;r++)
  297.             for(int c=0;c<8;c++)
  298.                 board[r][c]=storeBoard[r][c][curLoc];
  299.        
  300.         if(color=='B') color = 'W';
  301.         else color = 'B';
  302.         makeChanges();
  303.     }
  304.    
  305.     public void fowardHandler() {
  306.         if(curLoc==1000 && storedLoc>1000){
  307.             curLoc=0;
  308.             overFlow=true;
  309.         }
  310.         else curLoc++;
  311.        
  312.         for(int r=0;r<8;r++)
  313.             for(int c=0;c<8;c++)
  314.                 board[r][c]=storeBoard[r][c][curLoc];
  315.        
  316.         if(color=='B') color = 'W';
  317.         else color = 'B';
  318.         makeChanges();
  319.     }
  320.    
  321.     public static boolean giveUp() {
  322.         if(piece1.equals(piece2) &&
  323.                 piece1.charAt(0)==color &&
  324.                 piece1.charAt(1)=='K'){
  325.             String[] choices = {"Yes","No"};
  326.             int choice = JOptionPane.showOptionDialog
  327.                     (null, "Really give up?","Chess", 0, 3,
  328.                     null, choices, null);
  329.             if(choice == 0) return true;
  330.         }
  331.         return false;
  332.     }
  333.    
  334.     public static boolean checkSide() {
  335.         return color==piece1.charAt(0);
  336.     }
  337.     public static boolean checkDir() {
  338.         //rook
  339.         if(piece1.charAt(1)=='R')
  340.             if(piece1.charAt(2)==piece2.charAt(2)
  341.                     || piece1.charAt(3)==piece2.charAt(3))
  342.                 return true;
  343.         //horsey
  344.         if(piece1.charAt(1)=='H'){
  345.             if(Math.abs((int)(piece1.charAt(2))-
  346.                     (int)(piece2.charAt(2)))==2 &&
  347.                     Math.abs((int)(piece1.charAt(3))
  348.                     -(int)(piece2.charAt(3)))==1)
  349.                 return true;
  350.             if(Math.abs((int)(piece1.charAt(3))-
  351.                     (int)(piece2.charAt(3)))==2 &&
  352.                     Math.abs((int)(piece1.charAt(2))
  353.                     -(int)(piece2.charAt(2)))==1)
  354.                 return true;
  355.         }
  356.         //bishop
  357.         if(piece1.charAt(1)=='B'){
  358.             if(Math.abs((int)(piece1.charAt(2))-
  359.                     (int)(piece2.charAt(2)
  360.                     ))==Math.abs((int)(piece1.charAt(3))
  361.                     -(int)(piece2.charAt(3))))
  362.                 return true;
  363.         }
  364.         //queen
  365.         if(piece1.charAt(1)=='Q'){
  366.             if(Math.abs((int)(piece1.charAt(2))-
  367.                     (int)(piece2.charAt(2)
  368.                     ))==Math.abs((int)(piece1.charAt(3))
  369.                     -(int)(piece2.charAt(3))))
  370.                 return true;
  371.             if(piece1.charAt(2)==piece2.charAt(2)
  372.                         || piece1.charAt(3)==piece2.charAt(3))
  373.                 return true;
  374.            
  375.         }
  376.         //king
  377.         if(piece1.charAt(1)=='K'){
  378.             if(Math.abs((int)(piece1.charAt(2))
  379.                     -(int)(piece2.charAt(2)))<=1
  380.                     && Math.abs((int)(piece1.charAt(3))
  381.                     -(int)(piece2.charAt(3)))<=1)
  382.                 return true;
  383.         }
  384.         //pawn
  385.         if(piece1.charAt(1)=='P'){
  386.             if(piece1.charAt(2)==piece2.charAt(2)) return false;
  387.             if(Math.abs((int)(piece1.charAt(2))
  388.                     -(int)(piece2.charAt(2)))==1
  389.                     && piece1.charAt(3)==(int)
  390.                     (piece2.charAt(3)) &&
  391.                     piece2.charAt(0)==' ')
  392.                 return true;
  393.             else if(Math.abs((int)(piece1.charAt(2))
  394.                     -(int)(piece2.charAt(2)))==2
  395.                     && piece1.charAt(3)==(int)
  396.                     (piece2.charAt(3)) &&
  397.                     (piece1.charAt(2)=='6'
  398.                     || piece1.charAt(2)=='1' &&
  399.                     piece2.charAt(0)==' '))
  400.                 return true;
  401.             char opColor;
  402.             if(color=='W') opColor = 'B';
  403.             else opColor='W';
  404.             if(Math.abs((int)(piece1.charAt(2))
  405.                     -(int)(piece2.charAt(2)))==1
  406.                     && Math.abs((int)(piece1.charAt(3))
  407.                     -(int)(piece2.charAt(3)))==1
  408.                     && piece2.charAt(0)==opColor)
  409.                 return true;
  410.         }
  411.         return false;
  412.     }
  413.    
  414.     public static boolean checkCol() {
  415.         //knight
  416.         if(piece1.charAt(1)=='H'){
  417.             if(piece2.charAt(0)!=color)
  418.                 return true;
  419.             else return false;
  420.         }
  421.         //horizontal check
  422.         else if(piece1.charAt(2)==piece2.charAt(2)) {
  423.             int row = (int)(piece1.charAt(2))-48;
  424.             int cLow = Math.min((int)(piece1.charAt(3))-48,
  425.                     (int)(piece2.charAt(3))-48);
  426.             int cHigh = Math.max((int)(piece1.charAt(3))-48,
  427.                     (int)(piece2.charAt(3))-48);
  428.             for(int i=cLow;i<=cHigh;i++)
  429.                 if(board[row][i].charAt(0)==color &&
  430.                         !board[row][i].equals
  431.                         (piece1.substring(0,2)))
  432.                     return false;
  433.         }
  434.         //vertical check
  435.         else if(piece1.charAt(3)==piece2.charAt(3)) {
  436.             int col = (int)(piece1.charAt(3))-48;
  437.             int rLow = Math.min((int)(piece1.charAt(2))-48,
  438.                     (int)(piece2.charAt(2))-48);
  439.             int rHigh = Math.max((int)(piece1.charAt(2))-48,
  440.                     (int)(piece2.charAt(2))-48);
  441.             for(int i=rLow;i<=rHigh;i++)
  442.                 if(board[i][col].charAt(0)==color &&
  443.                         !board[i][col].equals
  444.                         (piece1.substring(0,2)))
  445.                     return false;
  446.         }
  447.         else if(piece1.charAt(2)==piece2.charAt(2)) {
  448.             int row = (int)(piece1.charAt(2))-48;
  449.             int cLow = Math.min((int)(piece1.charAt(3))-48,
  450.                     (int)(piece2.charAt(3))-48);
  451.             int cHigh = Math.max((int)(piece1.charAt(3))-48,
  452.                     (int)(piece2.charAt(3))-48);
  453.             for(int i=cLow;i<=cHigh;i++)
  454.                 if(board[row][i].charAt(0)==color &&
  455.                         !board[row][i].equals
  456.                         (piece1.substring(0,2)))
  457.                     return false;
  458.         }
  459.         //diagonal check
  460.         else {
  461.             int r1 = piece1.charAt(2)-48;
  462.             int c1 = piece1.charAt(3)-48;
  463.             int r2 = piece1.charAt(2)-48;
  464.             int c2 = piece2.charAt(2)-48;
  465.            
  466.             if(piece1.charAt(2)>piece2.charAt(2) &&
  467.                     piece1.charAt(3)>piece2.charAt(3)) {
  468.                 while(r1>=r2 && c1>=c2) {
  469.                     if(board[r1][c1].charAt(0)==color &&
  470.                             !board[r1][c1].equals
  471.                             (piece1.substring(0,2)))
  472.                         return false;
  473.                     r1--;
  474.                     c1--;
  475.                 }
  476.             }
  477.            
  478.             if(piece1.charAt(2)>piece2.charAt(2) &&
  479.                     piece1.charAt(3)<piece2.charAt(3)) {
  480.                 while(r1>=r2 && c1<=c2) {
  481.                     if(board[r1][c1].charAt(0)==color &&
  482.                             !board[r1][c1].equals
  483.                             (piece1.substring(0,2)))
  484.                         return false;
  485.                     r1--;
  486.                     c1++;
  487.                 }
  488.             }
  489.            
  490.             if(piece1.charAt(2)<piece2.charAt(2) &&
  491.                     piece1.charAt(3)<piece2.charAt(3)) {
  492.                 while(r1<=r2 && c1<=c2) {
  493.                     if(board[r1][c1].charAt(0)==color &&
  494.                             !board[r1][c1].equals
  495.                             (piece1.substring(0,2)))
  496.                         return false;
  497.                     r1++;
  498.                     c1++;
  499.                 }
  500.             }
  501.             if(piece1.charAt(2)<piece2.charAt(2) &&
  502.                     piece1.charAt(3)>piece2.charAt(3)) {
  503.                 while(r1<=r2 && c1>=c2) {
  504.                     if(board[r1][c1].charAt(0)==color &&
  505.                             !board[r1][c1].equals
  506.                             (piece1.substring(0,2)))
  507.                         return false;
  508.                     r1++;
  509.                     c1--;
  510.                 }
  511.             }
  512.         }
  513.        
  514.         return true;
  515.     }
  516.    
  517.     public boolean castle() {
  518.         String kingPiece = "";
  519.         String rookPiece = "";
  520.         if(piece1.charAt(1)=='K') kingPiece = piece1;
  521.         if(piece2.charAt(1)=='K') kingPiece = piece2;
  522.         if(piece1.charAt(1)=='R') rookPiece = piece1;
  523.         if(piece2.charAt(1)=='R') rookPiece = piece2;
  524.        
  525.         if(kingPiece.isEmpty() || rookPiece.isEmpty()) return false;
  526.        
  527.         //white side
  528.         if(kingPiece.charAt(0)=='W' && rookPiece.charAt(0)=='W')
  529.             if(kingPiece.equals("WK74")){
  530.                 if(rookPiece.equals("WR77"))
  531.                     if(board[7][5].equals("  ") &&
  532.                             board[7][6].equals("  ")){
  533.                         board[7][5]="WR";
  534.                         board[7][6]="WK";
  535.                         board[7][7]="  ";
  536.                         board[7][4]="  ";
  537.                         return true;
  538.                     }
  539.                 if(rookPiece.equals("WR70"))
  540.                     if(board[7][1].equals("  ")
  541.                             && board[7][2].equals("  ")
  542.                             && board[7][3].equals("  ")){
  543.                         board[7][3]="WR";
  544.                         board[7][2]="WK";
  545.                         board[7][4]="  ";
  546.                         board[7][0]="  ";
  547.                         return true;
  548.                     }          
  549.             }
  550.        
  551.         //black side
  552.         if(kingPiece.charAt(0)=='B' && rookPiece.charAt(0)=='B')
  553.             if(kingPiece.equals("BK04")){
  554.                 if(rookPiece.equals("BR07"))
  555.                     if(board[0][5].equals("  ") &&
  556.                             board[0][6].equals("  ")){
  557.                         board[0][5]="BR";
  558.                         board[0][6]="BK";
  559.                         board[0][7]="  ";
  560.                         board[0][4]="  ";
  561.                         return true;
  562.                     }
  563.                 if(rookPiece.equals("BR00"))
  564.                     if(board[0][1].equals("  ")
  565.                             && board[0][2].equals("  ")
  566.                             && board[0][3].equals("  ")){
  567.                         board[0][3]="BR";
  568.                         board[0][2]="BK";
  569.                         board[0][4]="  ";
  570.                         board[0][0]="  ";
  571.                         return true;
  572.                     }          
  573.             }
  574.        
  575.         makeChanges();
  576.        
  577.         return false;
  578.     }
  579.    
  580.     public static void verify() {
  581.         int c1 = (int)(piece1.charAt(2))-48;
  582.         int r1 = (int)(piece1.charAt(3))-48;
  583.        
  584.         int c2 = (int)(piece2.charAt(2))-48;
  585.         int r2 = (int)(piece2.charAt(3))-48;
  586.        
  587.         board[c2][r2]=board[c1][r1];
  588.         board[c1][r1]="  ";
  589.        
  590.         curLoc++;
  591.         storedLoc++;
  592.         if(curLoc>1000) {
  593.             overFlow = true;
  594.             curLoc=0;
  595.         }
  596.         for(int r=0;r<8;r++)
  597.             for(int c=0;c<8;c++)
  598.                 storeBoard[r][c][curLoc]=board[r][c];
  599.     }
  600.    
  601.     public void makeChanges() {
  602.         contentPane.remove(boardPanel);
  603.         contentPane.remove(instructionPanel);
  604.         createBoardPanel();
  605.         createInstructionPanel();
  606.         contentPane.add(boardPanel, BorderLayout.CENTER);
  607.         contentPane.add(instructionPanel, BorderLayout.SOUTH);
  608.         contentPane.validate();
  609.     }
  610.    
  611.     public static String checkWon() {
  612.         int wKing = 0;
  613.         int bKing = 0;
  614.         for(int r=0;r<board.length;r++)
  615.             for(int c=0;c<board.length;c++)
  616.                 if(board[r][c].charAt(1)=='K') {
  617.                     if(board[r][c].charAt(0)=='B') bKing++;
  618.                     else wKing++;
  619.                 }
  620.         if(wKing==0) return "black";
  621.         else if(bKing==0) return "white";
  622.         else return "nope";
  623.     }
  624.    
  625.     public void checkPromo() {
  626.         for(int c=0;c<8;c++) {
  627.             if(board[0][c].equals("WP"))
  628.                 promo(0,c);
  629.             if(board[7][c].equals("BP"))
  630.                 promo(7,c);
  631.         }
  632.     }
  633.    
  634.     public void promo(int r, int c) {
  635.         System.out.println("Promotion time");
  636.         String[] choices = {"Queen","Knight","Rook","Bishop","Pawn"};
  637.         int choice = JOptionPane.showOptionDialog(null,
  638.                 "What piece would you like to promote into?",
  639.                 "Chess", 0, 3, null, choices, null);
  640.         char pCol = board[r][c].charAt(0);
  641.         switch(choice)
  642.         {
  643.         case 0:board[r][c]=String.valueOf(pCol)+"Q";break;
  644.         case 1:board[r][c]=String.valueOf(pCol)+"H";break;
  645.         case 2:board[r][c]=String.valueOf(pCol)+"R";break;
  646.         case 3:board[r][c]=String.valueOf(pCol)+"B";break;
  647.         case 4:JOptionPane.showMessageDialog
  648.                         (null,"Why would you do that?");
  649.                 break;
  650.         }
  651.     }
  652.     public static void winMessage(String won) {
  653.         JOptionPane.showMessageDialog
  654.                 (null,"Congratulations "+won+", you win!");
  655.     }
  656.     public static void initialFill(){
  657.         for(int c=0;c<8;c++){
  658.             board[1][c]="BP";
  659.             board[2][c]="  ";
  660.             board[3][c]="  ";
  661.             board[4][c]="  ";
  662.             board[5][c]="  ";
  663.             board[6][c]="WP";
  664.         }
  665.         for(int c=0;c<8;c++){
  666.             if(c==0 || c==7){
  667.                 board[0][c]="BR";
  668.                 board[7][c]="WR";
  669.             }
  670.             if(c==1 || c==6){
  671.                 board[0][c]="BH";
  672.                 board[7][c]="WH";
  673.             }
  674.             if(c==2 || c==5){
  675.                 board[0][c]="BB";
  676.                 board[7][c]="WB";
  677.             }
  678.             if(c==3){
  679.                 board[0][c]="BQ";
  680.                 board[7][c]="WQ";
  681.             }
  682.             if(c==4){
  683.                 board[0][c]="BK";
  684.                 board[7][c]="WK";
  685.             }
  686.         }
  687.        
  688.         for(int r=0;r<8;r++)
  689.             for(int c=0;c<8;c++)
  690.                 storeBoard[r][c][curLoc]=board[r][c];
  691.     }
  692.    
  693.     public static void main(String[] args) {
  694.         color = 'W';
  695.         initialFill();
  696.        
  697.         Chess chess = new Chess();
  698.         chess.setVisible(true);
  699.     }
  700. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement