home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 14 / IOPROG_14.ISO / soft / sdkjava / dxma.exe / DXMA05.cab / samples / da / java / apps / chess / Board.java < prev    next >
Encoding:
Java Source  |  1997-11-13  |  10.6 KB  |  403 lines

  1. import Piece;
  2. import java.util.*;
  3. import Pawn;
  4. import King;
  5. import Queen;
  6. import Bishop;
  7. import Knight;
  8. import Rook;
  9.  
  10.  
  11. // class: Board
  12. // This class is used to provide the chess logic needed to parse the PGN file. This
  13. // class is only utilized suring the parsing of the file, and is not used while
  14. // running the animation.
  15.  
  16. public class Board {
  17.     
  18.     // member variables
  19.     Piece[][]             m_pieceArray = new Piece[8][8];
  20.     Vector                m_pieces = new Vector();
  21.     
  22.     Piece[]               m_Kings = new Piece[2];
  23.     
  24.     int                 m_currentPlayer;
  25.     
  26.     // method definitions
  27.     
  28.     Board()
  29.     {
  30.         
  31.         for (int i = 0; i <= 7; i++){
  32.             for (int j = 0; j <= 7;  j++) {
  33.                 m_pieceArray[i][j] = null;
  34.             }
  35.         }
  36.         
  37.         m_currentPlayer = Piece.WHITE;
  38.         
  39.         SetupBoard();  
  40.     }
  41.     
  42.     
  43.     
  44.     Piece GetPiece(int x, int y){
  45.         return m_pieceArray[x][y];
  46.     }    
  47.     
  48.     void MovePiece(int xFrom, int yFrom, int xTo, int yTo ){
  49.         
  50.         Piece from = GetPiece(xFrom, yFrom);
  51.         
  52.         KillPiece(xTo,yTo);
  53.         m_pieceArray[xTo][yTo] = from;
  54.         from.Move(xTo,yTo);
  55.         m_pieceArray[xFrom][yFrom] = null;
  56.         
  57.         ClearEnPassantes(m_currentPlayer);
  58.     }
  59.  
  60.     void PrintBoard() {
  61.         System.out.println("Board:");
  62.         for (int i = 7; i >= 0; i--) {
  63.             for (int j = 0; j < 8; j++) {
  64.                 if (m_pieceArray[j][i] == null)
  65.                     System.out.print(". ");
  66.                 else{
  67.                     System.out.print(Parser.TypeToChar(m_pieceArray[j][i].GetType()));                
  68.                     System.out.print(' ');
  69.                 }
  70.             }
  71.             System.out.println("");
  72.         }
  73.  
  74.     }
  75.     
  76.     void KillPiece(int x, int y) {
  77.         Piece dead = GetPiece(x,y);
  78.         
  79.         if (dead != null) {
  80.             int i = 0;
  81.             while( i < m_pieces.size()){
  82.                 if (dead == m_pieces.elementAt(i)){
  83.                     m_pieces.removeElementAt(i);
  84.                     continue;
  85.                 }
  86.                 i++;
  87.             }
  88.             m_pieceArray[x][y] = null;
  89.         }
  90.     }
  91.     
  92.     void PromotePiece(int x, int y, int type){
  93.         
  94.         Piece piece = null;
  95.         switch(type){
  96.         case Piece.PAWN:
  97.             piece = new Pawn(GetPiece(x,y).GetColor(),x,y);
  98.             break;
  99.         case Piece.ROOK:
  100.             piece = new Rook(GetPiece(x,y).GetColor(),x,y);
  101.             break;
  102.         case Piece.KNIGHT:
  103.             piece = new Knight(GetPiece(x,y).GetColor(),x,y);
  104.             break;
  105.         case Piece.BISHOP:
  106.             piece = new Bishop(GetPiece(x,y).GetColor(),x,y);
  107.             break;
  108.         case Piece.KING:
  109.             piece = new King(GetPiece(x,y).GetColor(),x,y);
  110.             break;
  111.         case Piece.QUEEN:
  112.             piece = new Queen(GetPiece(x,y).GetColor(),x,y);
  113.             break;
  114.         }
  115.         
  116.         KillPiece(x,y);
  117.         
  118.         m_pieces.insertElementAt(piece,0);
  119.         m_pieceArray[x][y] = piece;  
  120.     }
  121.     
  122.     
  123.     boolean IsOnBoard(int x, int y) {
  124.         return ((x >= 0) && (x <= 7) && (y >= 0) && (y <= 7));
  125.     }
  126.     
  127.     
  128.     ////////////////////////////////////////////////////////////////
  129.     //
  130.     // Function Name: IsKingStillSafe
  131.     // Parameters: from and to location of the piece we are moving,
  132.     //             as well as the color of the king who's safety concerns us
  133.     // Returns: False if the move in question would put the king in check
  134.     // Effects: leaves the board in original configuration
  135.     //
  136.     /////////////////////////////////-------------------------------
  137.     
  138.     
  139.     boolean IsKingStillSafe(int xFrom, int yFrom, int xTo, int yTo, int kingsColor){
  140.         Piece to, from; 
  141.         int i, xKing, yKing;
  142.         boolean result = true;
  143.         
  144.         to = GetPiece(xTo, yTo);
  145.         from = GetPiece(xFrom, yFrom);
  146.         
  147.         xKing = m_Kings[kingsColor].GetX();
  148.         yKing = m_Kings[kingsColor].GetY();
  149.         
  150.         m_pieceArray[xTo][yTo] = from;
  151.         m_pieceArray[xFrom][yFrom] = null;
  152.         
  153.         // check to see if any of the other players pieces can attack the king
  154.         i = 0;
  155.         while( i < m_pieces.size()){
  156.             if ((to != m_pieces.elementAt(i)) &&             // we captured this one (maybe, or it's null)
  157.                 (((Piece)m_pieces.elementAt(i)).GetColor() != kingsColor) && // quick reject
  158.                 (((Piece)m_pieces.elementAt(i)).CanMoveTo(xKing, yKing))) {
  159.                 result = false;
  160.                 break;
  161.             }
  162.             i++;
  163.         }
  164.         
  165.         //move the board back to original state
  166.         m_pieceArray[xTo][yTo] = to;
  167.         m_pieceArray[xFrom][yFrom] = from;
  168.         
  169.         return result;
  170.     }
  171.     
  172.     
  173.     String GetMovesForPiece(int x, int y) {
  174.         Vector moves = GetPiece(x,y).GetLegalMoves();
  175.         String result = "";
  176.         
  177.         Enumeration enum = moves.elements();
  178.         
  179.         while(enum.hasMoreElements()) {
  180.             result = result + enum.nextElement();
  181.         }
  182.         return result;
  183.     }
  184.     
  185.     
  186.     void ClearEnPassantes(int colorToClear){
  187.         int i = 0;
  188.         while (i < m_pieces.size()){
  189.             if ((((Piece)m_pieces.elementAt(i)).GetType() == Piece.PAWN) && (((Piece)m_pieces.elementAt(i)).GetColor() == colorToClear))
  190.                 i = i;//((Pawn)m_pieces.elementAt(i)).ClearEnPassante();
  191.             i++;
  192.         }
  193.     }
  194.     
  195.     
  196.     
  197.     
  198.     ////////////////////////////////////////////////////////////////
  199.     //
  200.     // Function Name: GetPiecesOfType
  201.     // Parameters: type of pieces you want
  202.     // Returns: a list of all pieces of that type (ie: gimme all the Rooks)
  203.     // Effects: 
  204.     //
  205.     /////////////////////////////////-------------------------------
  206.     
  207.     
  208.     Vector GetPiecesOfType(int type){
  209.             
  210.         Vector piecesOfType = new Vector();
  211.         Piece currentPiece;
  212.         
  213.         Enumeration pieces = m_pieces.elements();
  214.         while (pieces.hasMoreElements()){
  215.             currentPiece = (Piece)(pieces.nextElement());
  216.             if (currentPiece.GetType() == type)
  217.                 piecesOfType.insertElementAt(currentPiece,0);
  218.         }
  219.         return piecesOfType;
  220.     }    
  221.     
  222.     
  223.     
  224.     ////////////////////////////////////////////////////////////////
  225.     //
  226.     // Function Name: SetupBoard
  227.     // Parameters: 
  228.     // Returns: 
  229.     // Effects: Sets the initial state of the board.
  230.     //
  231.     /////////////////////////////////-------------------------------
  232.     
  233.     
  234.     void SetupBoard(){
  235.         
  236.         int x,y;
  237.         
  238.         // set up the white ranks
  239.         x = 0; y = 1;
  240.         m_pieceArray[x][y] = new Pawn(Piece.WHITE, x, y);
  241.         m_pieces.insertElementAt(m_pieceArray[x][y],0);
  242.         x = 1; 
  243.         m_pieceArray[x][y] = new Pawn(Piece.WHITE, x, y);
  244.         m_pieces.insertElementAt(m_pieceArray[x][y],0);
  245.         x = 2; 
  246.         m_pieceArray[x][y] = new Pawn(Piece.WHITE, x, y);
  247.         m_pieces.insertElementAt(m_pieceArray[x][y],0);
  248.         x = 3; 
  249.         m_pieceArray[x][y] = new Pawn(Piece.WHITE, x, y);
  250.         m_pieces.insertElementAt(m_pieceArray[x][y],0);
  251.         x = 4; 
  252.         m_pieceArray[x][y] = new Pawn(Piece.WHITE, x, y);
  253.         m_pieces.insertElementAt(m_pieceArray[x][y],0);
  254.         x = 5; 
  255.         m_pieceArray[x][y] = new Pawn(Piece.WHITE, x, y);
  256.         m_pieces.insertElementAt(m_pieceArray[x][y],0);
  257.         x = 6; 
  258.         m_pieceArray[x][y] = new Pawn(Piece.WHITE, x, y);
  259.         m_pieces.insertElementAt(m_pieceArray[x][y],0);
  260.         x = 7; 
  261.         m_pieceArray[x][y] = new Pawn(Piece.WHITE, x, y);
  262.         m_pieces.insertElementAt(m_pieceArray[x][y],0);
  263.         
  264.         x = 0; y = 0;
  265.         m_pieceArray[x][y] = new Rook(Piece.WHITE, x, y);
  266.         m_pieces.insertElementAt(m_pieceArray[x][y],0);
  267.         x = 1; 
  268.         m_pieceArray[x][y] = new Knight(Piece.WHITE, x, y);
  269.         m_pieces.insertElementAt(m_pieceArray[x][y],0);
  270.         x = 2; 
  271.         m_pieceArray[x][y] = new Bishop(Piece.WHITE, x, y);
  272.         m_pieces.insertElementAt(m_pieceArray[x][y],0);
  273.         x = 3; 
  274.         m_pieceArray[x][y] = new Queen(Piece.WHITE, x, y);
  275.         m_pieces.insertElementAt(m_pieceArray[x][y],0);
  276.         x = 4; 
  277.         m_pieceArray[x][y] = new King(Piece.WHITE, x, y);
  278.         m_pieces.insertElementAt(m_pieceArray[x][y],0);
  279.         x = 5; 
  280.         m_pieceArray[x][y] = new Bishop(Piece.WHITE, x, y);
  281.         m_pieces.insertElementAt(m_pieceArray[x][y],0);
  282.         x = 6; 
  283.         m_pieceArray[x][y] = new Knight(Piece.WHITE, x, y);
  284.         m_pieces.insertElementAt(m_pieceArray[x][y],0);
  285.         x = 7; 
  286.         m_pieceArray[x][y] = new Rook(Piece.WHITE, x, y);
  287.         m_pieces.insertElementAt(m_pieceArray[x][y],0);
  288.         
  289.         
  290.         //set up the Black ranks
  291.         x = 0; y = 6;
  292.         m_pieceArray[x][y] = new Pawn(Piece.BLACK, x, y);
  293.         m_pieces.insertElementAt(m_pieceArray[x][y],0);
  294.         x = 1; 
  295.         m_pieceArray[x][y] = new Pawn(Piece.BLACK, x, y);
  296.         m_pieces.insertElementAt(m_pieceArray[x][y],0);
  297.         x = 2; 
  298.         m_pieceArray[x][y] = new Pawn(Piece.BLACK, x, y);
  299.         m_pieces.insertElementAt(m_pieceArray[x][y],0);
  300.         x = 3; 
  301.         m_pieceArray[x][y] = new Pawn(Piece.BLACK, x, y);
  302.         m_pieces.insertElementAt(m_pieceArray[x][y],0);
  303.         x = 4; 
  304.         m_pieceArray[x][y] = new Pawn(Piece.BLACK, x, y);
  305.         m_pieces.insertElementAt(m_pieceArray[x][y],0);
  306.         x = 5; 
  307.         m_pieceArray[x][y] = new Pawn(Piece.BLACK, x, y);
  308.         m_pieces.insertElementAt(m_pieceArray[x][y],0);
  309.         x = 6; 
  310.         m_pieceArray[x][y] = new Pawn(Piece.BLACK, x, y);
  311.         m_pieces.insertElementAt(m_pieceArray[x][y],0);
  312.         x = 7; 
  313.         m_pieceArray[x][y] = new Pawn(Piece.BLACK, x, y);
  314.         m_pieces.insertElementAt(m_pieceArray[x][y],0);
  315.         
  316.         x = 0; y = 7;
  317.         m_pieceArray[x][y] = new Rook(Piece.BLACK, x, y);
  318.         m_pieces.insertElementAt(m_pieceArray[x][y],0);
  319.         x = 1; 
  320.         m_pieceArray[x][y] = new Knight(Piece.BLACK, x, y);
  321.         m_pieces.insertElementAt(m_pieceArray[x][y],0);
  322.         x = 2; 
  323.         m_pieceArray[x][y] = new Bishop(Piece.BLACK, x, y);
  324.         m_pieces.insertElementAt(m_pieceArray[x][y],0);
  325.         x = 3; 
  326.         m_pieceArray[x][y] = new Queen(Piece.BLACK, x, y);
  327.         m_pieces.insertElementAt(m_pieceArray[x][y],0);
  328.         x = 4; 
  329.         m_pieceArray[x][y] = new King(Piece.BLACK, x, y);
  330.         m_pieces.insertElementAt(m_pieceArray[x][y],0);
  331.         x = 5; 
  332.         m_pieceArray[x][y] = new Bishop(Piece.BLACK, x, y);
  333.         m_pieces.insertElementAt(m_pieceArray[x][y],0);
  334.         x = 6; 
  335.         m_pieceArray[x][y] = new Knight(Piece.BLACK, x, y);
  336.         m_pieces.insertElementAt(m_pieceArray[x][y],0);
  337.         x = 7; 
  338.         m_pieceArray[x][y] = new Rook(Piece.BLACK, x, y);
  339.         m_pieces.insertElementAt(m_pieceArray[x][y],0);
  340.         
  341.         // to keep track of the kings
  342.         m_Kings[Piece.WHITE] = m_pieceArray[4][0];
  343.         m_Kings[Piece.BLACK] = m_pieceArray[4][7];
  344. }
  345.  
  346.  
  347. ////////////////////////////////////////////////////////////////
  348. //
  349. // Function Name: CoordToString
  350. // Parameters: 0 based indexes of coord
  351. // Returns: two character, null terminated string signifying location
  352. // 
  353. // You are responsible for deleting the string.
  354. // 
  355. /////////////////////////////////-------------------------------
  356.  
  357.  
  358. static String CoordToString(int x, int y){
  359.     char[] result  = new char[2];
  360.     
  361.     result[0] = (char)('a' + (char)x);
  362.     result[1] = (char)('1' + (char)y);
  363.     
  364.     return new String(result);
  365.     
  366. }
  367.  
  368. static int StringToX(String location) throws Exception{
  369.     int x,y;
  370.     
  371.     x = location.charAt(0) - 'a';
  372.     y = location.charAt(1) - '1';
  373.     
  374.     //  assert((x >= 0) && (x < 8) && (y >= 0) && (y < 8));
  375.     if (!((x >= 0) && (x < 8) && (y >= 0) && (y < 8)))
  376.         throw new Exception("Invalid board location specified");
  377.     return x;
  378. }
  379.  
  380. static int StringToY(String location) throws Exception{
  381.     
  382.     int x,y;
  383.     
  384.     x = location.charAt(0) - 'a';
  385.     y = location.charAt(1) - '1';
  386.     
  387.     if (!((x >= 0) && (x < 8) && (y >= 0) && (y < 8)))
  388.         throw new Exception("Invalid board location specified");
  389.     return y;
  390. }
  391.  
  392. static int OtherPlayer(int color) {
  393.     if (color == Piece.WHITE)
  394.         return Piece.BLACK;
  395.     else
  396.         return Piece.WHITE;
  397.     
  398. }
  399. }
  400.  
  401.  
  402.  
  403.