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

  1. import java.util.Vector;
  2. import Board;
  3. import Piece;
  4. import Parser;
  5.  
  6.  
  7. // class: Pawn
  8. // This class is used to provide the chess logic needed to parse the PGN file. This
  9. // class is only utilized suring the parsing of the file, and is not used while
  10. // running the animation.
  11.  
  12. public class Pawn extends Piece {
  13.  
  14.     public static Parser m_parser = null;
  15.     
  16.     int m_EnPassantLocs[][] = new int[2][2];
  17.     
  18.     Pawn(int myColor,int xPos, int yPos){
  19.         super(myColor, xPos, yPos);
  20.         m_type = PAWN;
  21.         ClearEnPassante();  
  22.     }
  23.     
  24.     
  25.     Vector GetLegalMoves() {
  26.         
  27.         Vector result = new Vector();
  28.         
  29.         int yDelta;
  30.         
  31.         if (m_color == WHITE )
  32.             yDelta = 1;
  33.         else
  34.             yDelta = -1;
  35.         
  36.         int x = m_xPos;
  37.         int y = m_yPos + yDelta;
  38.         
  39.         //see if we can move forward
  40.         if (Parser.board().IsOnBoard(x,y) && (Parser.board().GetPiece(x, y) == null)) {
  41.             result.insertElementAt(Board.CoordToString(x,y),0);
  42.             y = y + yDelta;
  43.             if (!m_moved && Parser.board().IsOnBoard(x,y)) {
  44.                 if (Parser.board().GetPiece(x,y) == null)
  45.                     result.insertElementAt(Board.CoordToString(x,y),0);
  46.             }   
  47.         }
  48.         
  49.         //check to see if we can capture a piece
  50.         y = m_yPos + yDelta;
  51.         x = m_xPos + 1;
  52.         
  53.         if (Parser.board().IsOnBoard(x,y) && (Parser.board().GetPiece(x, y) != null)
  54.             && (Parser.board().GetPiece(x, y).GetColor() != m_color)) {
  55.             result.insertElementAt(Board.CoordToString(x,y),0);
  56.         }
  57.         
  58.         y = m_yPos + yDelta;
  59.         x = m_xPos - 1;
  60.         
  61.         if (Parser.board().IsOnBoard(x,y) && (Parser.board().GetPiece(x, y) != null)
  62.             && (Parser.board().GetPiece(x, y).GetColor() != m_color)) {
  63.             result.insertElementAt(Board.CoordToString(x,y),0);
  64.         }
  65.         
  66.         // and en passant moves if we have them
  67.         if (m_EnPassantLocs[0][0] != -1){
  68.             result.insertElementAt(Board.CoordToString(m_EnPassantLocs[0][0], m_EnPassantLocs[0][1]),0);
  69.             if (m_EnPassantLocs[1][0] != -1)
  70.                 result.insertElementAt(Board.CoordToString(m_EnPassantLocs[1][0], m_EnPassantLocs[1][1]),0);
  71.         }
  72.         
  73.         
  74.         return result;
  75.     }
  76.     
  77.     
  78.     
  79.     void Move(int x, int y) {
  80.         m_moved = true;
  81.         int xp;
  82.         
  83.         int yDir = (m_color == WHITE) ? 1 : -1;
  84.         
  85.         if (Math.abs(y - m_yPos) == 2) {
  86.             // check for possibility of being captured en passant
  87.             xp = x + 1;
  88.             if (Parser.board().IsOnBoard(xp,y) &&
  89.                 (Parser.board().GetPiece(xp,y) != null) &&
  90.                 (Parser.board().GetPiece(xp,y).GetColor() != m_color) &&
  91.                 (Parser.board().GetPiece(xp,y).GetType() == PAWN)){
  92.                 ((Pawn)Parser.board().GetPiece(xp,y)).AddEnPassante(x, y - yDir);
  93.             }
  94.             
  95.             xp = x - 1;
  96.             if (Parser.board().IsOnBoard(xp,y) &&
  97.                 (Parser.board().GetPiece(xp,y) != null) &&
  98.                 (Parser.board().GetPiece(xp,y).GetColor() != m_color) &&
  99.                 (Parser.board().GetPiece(xp,y).GetType() == PAWN)){
  100.                 ((Pawn)Parser.board().GetPiece(xp,y)).AddEnPassante(x, y - yDir );
  101.             }
  102.         }
  103.         m_xPos = x;
  104.         m_yPos = y;
  105.         
  106.         // check to see if we just captured someone en-passant
  107.         
  108.         if (x == m_EnPassantLocs[0][0]){
  109.             Parser.board().KillPiece(m_EnPassantLocs[0][0],m_EnPassantLocs[0][1] - yDir);
  110.             m_parser.NotifyOfCapture(m_EnPassantLocs[0][0],m_EnPassantLocs[0][1] - yDir);
  111.         }
  112.         if (y == m_EnPassantLocs[1][0]){
  113.             Parser.board().KillPiece(m_EnPassantLocs[1][0],m_EnPassantLocs[1][1] - yDir);
  114.             m_parser.NotifyOfCapture(m_EnPassantLocs[1][0],m_EnPassantLocs[1][1] - yDir);
  115.         }
  116.         
  117.     }
  118.     
  119.     
  120.     ////////////////////////////////////////////////////////////////
  121.     //
  122.     // Function Name: AddEnPassante
  123.     // Parameters: 
  124.     // Returns: 
  125.     // Effects: m_EnPassantLocs stores the location that this piece can move to
  126.     // and capture a piece En Passante
  127.     //
  128.     /////////////////////////////////-------------------------------
  129.     
  130.     
  131.     void AddEnPassante(int x, int y){
  132.         if (m_EnPassantLocs[0][0] == -1){
  133.             m_EnPassantLocs[0][0] = x;
  134.             m_EnPassantLocs[0][1] = y;
  135.         } else {
  136.             m_EnPassantLocs[1][0] = x;
  137.             m_EnPassantLocs[1][1] = y;
  138.         }
  139.     }
  140.     
  141.     void ClearEnPassante(){
  142.         
  143.         m_EnPassantLocs[0][0] = -1;
  144.         m_EnPassantLocs[1][0] = -1;
  145.     }
  146.     
  147.     
  148. }
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.