Jeu de dame

  1. package checkers;
  2. import jess.*;
  3. import java.beans.PropertyChangeListener;
  4. import java.beans.PropertyChangeSupport;
  5. import java.io.Serializable;
  6. /**
  7.  * This object represents a piece on a board and is used in both java and jess. The piece is defined by its position (on a Square), its colour, its number and its status (king or men). Each piece also knows their eight neighbour squares: the potential positions into which one the piece can move.
  8.  * @author k380h11
  9.  */
  10. public class Piece  extends Object implements Serializable{
  11.     final static boolean WHITE = true;
  12.     final static boolean BLACK = false;
  13.  
  14.         /**
  15.          * This Method is used to set the number and color of a piece
  16.          * And it makes a call to the Board to also set it.
  17.          * @param number Piece'number
  18.          * @param color Piece's color
  19.          */
  20.     public Piece(int number, boolean color) {
  21.         this.color = color;
  22.         this.number = number;
  23.         Board.getInstance().add(this);
  24.     }
  25.       /**
  26.        * This Method is also to set the number and color of a piece but it also
  27.        * sets the level and square
  28.        * And it makes a call method calculPos.
  29.        * @param number Piece'number
  30.        * @param color Piece's color
  31.        * @param square square move to
  32.        * @param level Piece's level
  33.        */
  34.     public Piece(int number, boolean color, Grid.Square square, int level) {
  35.         this.color = color;
  36.         this.number = number;
  37.         this.level = level;
  38.         this.square = square;
  39.         this.virtual = true;
  40.         calculPos();
  41.     }
  42.      
  43.       /**
  44.          * This Method initilizes the pieces where they are placed and of what color they are
  45.          *
  46.          */
  47.     public void init() {
  48.         int row = (int) Math.ceil((number)/4);
  49.         int col = (((row+1)%2)+(number%4)*2);
  50.         if (color == Piece.WHITE) {
  51.             row = Grid.ROW_MAX-row-1;
  52.             col = Grid.COL_MAX-col-1;
  53.         }
  54.         move( Grid.getSquare(row, col) );
  55.     }
  56.  
  57.       /**
  58.        * jump the piece over a square
  59.        * @param squareToJump square to be jumped
  60.        */
  61.  
  62.     public void jump(Grid.Square squareToJump) {
  63.         Piece pieceToJump = Board.select(squareToJump.getRow(),squareToJump.getCol());
  64.         Board.getInstance().remove(pieceToJump);
  65.         squareToJump.setFree(true);
  66.        
  67.         int newRow =squareToJump.getRow() + squareToJump.getRow() - square.getRow();
  68.         int newCol =squareToJump.getCol()+ squareToJump.getCol() - square.getCol() ;
  69.         Grid.Square moveto = Grid.getSquare(newRow, newCol);
  70.         move(moveto);
  71.         Grid.getInstance().repaint();
  72.     }
  73.       /**
  74.        * This Method is used to move a piece from one square to another.
  75.        * @param square square to meve the piece to
  76.        */
  77.    
  78.     public void move(Grid.Square square) {      
  79.         if (!virtual)
  80.             this.square.setFree(true);
  81.         this.square = square;
  82.         if (!virtual)
  83.             this.square.setFree(false);
  84.    
  85.         calculPos();
  86.     }
  87.       /**
  88.        * This Method is used to convert the piece information to a string
  89.        * @return a string containing the information of the piece.
  90.        */
  91.    
  92.     public String toString() {
  93.         String retString = (king ? "King" : "men")+
  94.         " " + (color ? "white":"red") +" "+ number + " on "+ square;
  95.         return retString ;
  96.     }
  97.       /**
  98.        * This Method finds out if two piece are equal same colour, number, square, level
  99.        * @return True if they are else false
  100.        * @param anObject object to be compared
  101.        */
  102.    
  103.     public boolean equals(Object anObject) {
  104.         if (anObject != null && anObject instanceof Piece) {
  105.             Piece piece = (Piece) anObject;
  106.             return ((piece.getNumber()  == number && piece.getColor() == color)
  107.             && (piece.square.equals(square)) && (piece.level == level));
  108.         }
  109.         else
  110.             return false;
  111.     }
  112.       /**
  113.        * This Method is used to get the number of a piece
  114.        * @return the number of a piece
  115.        */
  116.    
  117.     public int getNumber() {
  118.         return number;
  119.     }
  120.       /**
  121.        * This Method is used to set the number of a piece, from an older location
  122.        * @param number the number of the piece
  123.        */
  124.    
  125.     public void setNumber(int number) {
  126.         this.number = number;
  127.     }
  128.       /**
  129.        * This Method is used to get the square of a piece, from an older location
  130.        * @return the square.
  131.        */
  132.    
  133.     public Grid.Square getSquare() {
  134.         return square;
  135.     }
  136.       /**
  137.        * This Method is used to get the colour of a piece, form an older location
  138.        * @return the colour of the piece.
  139.        */
  140.    
  141.     public boolean getColor() {
  142.         return color;
  143.     }
  144.       /**
  145.        * This Method is used to get the next left square of the piece, from an ekstern location
  146.        * @return the next left squares location
  147.        */
  148.    
  149.     public Grid.Square getNLS() {
  150.         return nls;
  151.     }
  152.       /**
  153.        * This Method is used to get the next right square of the piece, from an ekstern location
  154.        * @return the next right squares location
  155.        */
  156.    
  157.     public Grid.Square getNRS() {
  158.         return nrs;
  159.     }
  160.       /**
  161.        * This Method is used to get the next-next left square of the piece, from an ekstern location
  162.        * @return the next-next left squares location
  163.        */
  164.    
  165.     public Grid.Square getNNLS() {
  166.         return nnls;
  167.     }
  168.       /**
  169.        * This Method is used to get the next-next right square of the piece, from an ekstern location
  170.        * @return the next-next right squares location
  171.        */
  172.    
  173.     public Grid.Square getNNRS() {
  174.         return nnrs;
  175.     }
  176.       /**
  177.        * This Method is used to get the previous left square of the piece, from an ekstern location
  178.        * @return the previous left squares location
  179.        */
  180.    
  181.     public Grid.Square getPLS() {
  182.         return pls;
  183.     }
  184.       /**
  185.        * This Method is used to get the previous right square of the piece, from an ekstern location
  186.        * @return the previous right squares location
  187.        */
  188.    
  189.     public Grid.Square getPRS() {
  190.         return prs;
  191.     }
  192.       /**
  193.        * This Method is used to get the 2*previous left square of the piece, from an ekstern location
  194.        * @return the 2*previous left squares location
  195.        */
  196.    
  197.     public Grid.Square getPPLS() {
  198.         return ppls;
  199.     }
  200.       /**
  201.        * This Method is used to get the 2*previous right square of the piece, from an ekstern location
  202.        * @return the 2*previous right squares location
  203.        */
  204.    
  205.     public Grid.Square getPPRS() {
  206.         return pprs;
  207.     }
  208.       /**
  209.        * This Method is used to get the level of the piece, from an ekstern location
  210.        * @return the level of the piece
  211.        */
  212.    
  213.     public int getLevel() {
  214.         return level;
  215.     }
  216.  
  217.       /**
  218.        * This Method is used to set the level of the piece, for an ekstern location
  219.        * Set the level of the piece to level in the parameter
  220.        * @param level the level of the piece
  221.        */
  222.  
  223.     public void setLevel(int level){
  224.         this.level = level;
  225.     }
  226.    
  227.       /**
  228.        * This Method is used to set the virtual effekt of a piece (but is not used)
  229.        * @param virtual if virtual
  230.        */
  231.  
  232.     public void setVirtual(boolean virtual) {
  233.         this.virtual = virtual;
  234.     }
  235.  
  236.       /**
  237.        * This Method is used to read if a piece is virtual or not (but is not used)
  238.        * @return the virtual informantion TRUE or FALSE
  239.        */
  240.  
  241.     public boolean isVirtual() {
  242.         return virtual;
  243.     }
  244.    
  245.  
  246.       /**
  247.        * This Method is used to find out if a piece is a king or just a "man"
  248.        * @return the true if the piece is a king else false
  249.        */
  250.  
  251.     public boolean isKing() {
  252.         return king;
  253.     }
  254.  
  255.       /**
  256.        * This Method is to add a propertychangelistener
  257.        * @param pcl the propertychangelistener object
  258.        */
  259.            
  260.     public void addPropertyChangeListener(PropertyChangeListener pcl) {
  261.         pcs.addPropertyChangeListener(pcl);
  262.     }
  263.  
  264.       /**
  265.        * This Method is to remove a Propertychangelistener.
  266.        * @param pcl the propertychangelistener object
  267.        */
  268.    
  269.     public void removePropertyChangeListener(PropertyChangeListener pcl) {
  270.         pcs.removePropertyChangeListener(pcl);
  271.     }
  272.  
  273.       /**
  274.          * This Method calculate the next legal positions of the piece and
  275.          * it set the piece to be a king if is on the backline of the board
  276.          * The pcs.    
  277.          *
  278.          *
  279.          */
  280.    
  281.     private void calculPos() {
  282.         int mov = (color ? -1 : 1);
  283.         nls = Grid.getSquare(square.getRow() + mov, square.getCol() + mov);
  284.         nrs = Grid.getSquare(square.getRow() + mov, square.getCol() - mov);
  285.         pls = Grid.getSquare(square.getRow() - mov, square.getCol() + mov);
  286.         prs = Grid.getSquare(square.getRow() - mov, square.getCol() - mov);
  287.         mov = mov * 2;
  288.         nnls = Grid.getSquare(square.getRow() + mov, square.getCol() + mov);
  289.         nnrs = Grid.getSquare(square.getRow() + mov, square.getCol() - mov);
  290.         ppls = Grid.getSquare(square.getRow() - mov, square.getCol() + mov);
  291.         pprs = Grid.getSquare(square.getRow() - mov, square.getCol() - mov);
  292.        
  293.         if (Grid.backLine(square, color))
  294.             king = true;
  295.        
  296.         pcs.firePropertyChange("square", square, square);
  297.         pcs.firePropertyChange("nls", nls, nls);
  298.         pcs.firePropertyChange("nrs", nrs, nrs);
  299.         pcs.firePropertyChange("pls", pls, pls);
  300.         pcs.firePropertyChange("prs", prs, prs);
  301.         pcs.firePropertyChange("nnls", nnls, nnls);
  302.         pcs.firePropertyChange("nnrs", nnrs, nnrs);
  303.         pcs.firePropertyChange("ppls", ppls, ppls);
  304.         pcs.firePropertyChange("pprs", pprs, pprs);
  305.         pcs.firePropertyChange("king", king, king);
  306.     }
  307.    
  308.     private PropertyChangeSupport pcs = new PropertyChangeSupport(this);
  309.    
  310.          /**
  311.           * The default values of the piece's parameters.
  312.           */
  313.     private boolean color;
  314.     private int actionPoint = 0;
  315.     private int number = 0;
  316.     private boolean king = false;
  317.     private boolean virtual = false;
  318.     private int level = 0;
  319.     private Grid.Square square = Grid.getInstance().new Square();
  320.     private Grid.Square nls = Grid.getInstance().new Square();
  321.     private Grid.Square nrs = Grid.getInstance().new Square();
  322.     private Grid.Square nnls = Grid.getInstance().new Square();
  323.     private Grid.Square nnrs = Grid.getInstance().new Square();
  324.     private Grid.Square pls = Grid.getInstance().new Square();
  325.     private Grid.Square prs = Grid.getInstance().new Square();
  326.     private Grid.Square ppls = Grid.getInstance().new Square();
  327.     private Grid.Square pprs = Grid.getInstance().new Square();
  328. }
  329.  
  330.  

contact - faire un lien