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

  1. import java.util.*;
  2. import Board;
  3. import Parser;
  4.  
  5. // class: Piece
  6. // This class is used to provide the chess logic needed to parse the PGN file. This
  7. // class is only utilized suring the parsing of the file, and is not used while
  8. // running the animation.
  9.  
  10. // this class is abstract, the Pawn,Rook,Knight,Bishop,King, and Queen classes
  11. // override the GetLegalMoves function.
  12.  
  13. public abstract class Piece
  14. {    
  15.     //define constants
  16.     static final int PAWN = 0;
  17.     static final int KING = 1;
  18.     static final int QUEEN = 2;
  19.     static final int BISHOP = 3;
  20.     static final int KNIGHT = 4;
  21.     static final int ROOK = 5;
  22.  
  23.     static final int WHITE = 0;
  24.     static final int BLACK = 1;
  25.     
  26.         
  27.     // define member variables
  28.     int                   m_xPos = 0;
  29.     int                   m_yPos = 0;
  30.     int                        m_type;
  31.     int                      m_color;
  32.     boolean               m_moved = false;
  33.     
  34.     // method definitions
  35.     Piece( int mycolor, int x, int y){
  36.         this.m_xPos = x;
  37.         this.m_yPos = y;
  38.         this.m_color = mycolor;
  39.     }
  40.     
  41.     
  42.     int GetX() { return m_xPos; }
  43.     int GetY() { return m_yPos; }
  44.  
  45.     abstract Vector GetLegalMoves();
  46.     
  47.   void Move(int x, int y){  
  48.         m_moved = true;
  49.         m_xPos = x;
  50.         m_yPos = y;
  51.     }
  52.     
  53.     
  54.     boolean IsOccupiable(int x,int y){
  55.         
  56.         if (!Parser.board().IsOnBoard(x,y)) {
  57.             return false; 
  58.         }
  59.         if (Parser.board().GetPiece(x,y) == null) return true;
  60.             
  61.         if (Parser.board().GetPiece(x,y).GetColor() != m_color) return true;
  62.         
  63.         return false;
  64.             
  65.     }
  66.     
  67.     void GetSlideMoves(int xStart, int yStart, int yDelta, int xDelta, Vector result) {
  68.  
  69.       int x = xStart;
  70.       int y = yStart;
  71.       
  72.       while (true) {
  73.           x += xDelta;
  74.           y += yDelta;
  75.           
  76.           // if we're off the board, just stop.
  77.           if (!Parser.board().IsOnBoard(x,y)) {
  78.               break; }
  79.           
  80.           if (Parser.board().GetPiece(x,y) != null) {
  81.               //if there is a piece there add it as a move (if capturable) and stop the loop
  82.               if (Parser.board().GetPiece(x,y).GetColor() != m_color)
  83.                   result.insertElementAt(Board.CoordToString(x,y), result.size());
  84.               break;
  85.           } else {
  86.               //otherwise add the piece and keep going
  87.               result.insertElementAt(Board.CoordToString(x,y), result.size());
  88.           }
  89.       }
  90.     }
  91.     
  92.  
  93.     boolean CanMoveTo(int x, int y){
  94.         Vector moves = null;
  95.         int i = 0;
  96.         boolean result = false;
  97.         
  98.         String theMove = Board.CoordToString(x,y);
  99.         
  100.         moves = GetLegalMoves();
  101.         
  102.         for (i = 0; i < moves.size(); i++) {
  103.             if (theMove.equals(moves.elementAt(i))){
  104.                 result = true;
  105.                 break;
  106.             }
  107.         }
  108.         
  109.         return result;
  110.     };
  111.     
  112.     
  113.     int GetColor()              { return m_color; };
  114.     int GetType()           { return m_type; };
  115. }
  116.  
  117.