Jeu de dame

  1. package checkers;
  2. import java.util.LinkedList;
  3. import java.util.ListIterator;
  4. import jess.*;
  5.  
  6. /**
  7.  * The board is the representation of the whole 24 pieces. When this one is first declared, all the piece are instantiated, added into the Jess database, placed at their initial position and finally asserted into the parent LinkedList
  8.  */
  9. public class Board extends LinkedList{
  10.     static private Board board = new Board ();
  11.    
  12.    /**
  13.     * This method calls the super method.
  14.     */ 
  15.     private Board () {
  16.         super ();
  17.     }
  18.    /**
  19.      * This method is used to initilize the board.
  20.      * @throws JessException if it is not able to execute the jess command
  21.      */
  22.     void init () {      
  23.         for (int number=0; number<12; number++) {
  24.             boolean color=Piece.BLACK;
  25.             for (int c=0; c<2; c++) {
  26.                 color = !color;
  27.                 String Jesscmd = "(definstance Piece " +
  28.                 "(new checkers.Piece "+ number +" "+Boolean.toString (color).toUpperCase () +")" +
  29.                 " dynamic)";
  30.                 try {
  31.                     Checker.r.executeCommand (Jesscmd);
  32.                 } catch (JessException e) {System.err.println (e);}
  33.                 Piece piece = Board.select (number,color);
  34.                 piece.init ();
  35.             }
  36.         }
  37.     }
  38.     /**
  39.      * This method finds which piece is located in at specify square.
  40.      * @return the piece if there is one else null.
  41.      * @param row square's row position
  42.      * @param col square's col position
  43.      */
  44.  
  45.     static public Piece select (int row, int col){
  46.         ListIterator it = board.listIterator ();
  47.         while (it.hasNext ()) {
  48.             Piece piece = (Piece) it.next ();
  49.             if ((piece.getSquare ().getRow() == row)
  50.                 && (piece.getSquare ().getCol() == col)) {
  51.                 return (Piece) piece;
  52.             }
  53.         }
  54.         return null;
  55.     }
  56.    
  57.     /**
  58.      * This method finds a spicified piece on the board if it exits
  59.      * @return the piece if there is one else null.
  60.      * @param number the number of the player's piece looked for
  61.      * @param color the color of the player's piece looked for
  62.      */
  63.  
  64.     static public Piece select (int number, boolean color){
  65.         ListIterator it = board.listIterator ();
  66.         while (it.hasNext ()) {
  67.             Piece piece = (Piece) it.next ();
  68.             if (piece.getNumber () == number && piece.getColor() == color) {
  69.                 return (Piece) piece;
  70.             }
  71.         }
  72.         return null;
  73.     }
  74.    
  75.     /**
  76.      * This method count how many pieces a player has on the board.
  77.      * @return how many pieces the player is left with.
  78.      * @param color the color of the player.
  79.      */
  80.  
  81.     static public int count (boolean color){
  82.         ListIterator it = board.listIterator ();
  83.         int count = 0;
  84.         while (it.hasNext ()) {
  85.             Piece piece = (Piece) it.next ();
  86.             if (piece.getColor() == color) {
  87.                 count ++;
  88.             }
  89.         }
  90.         return count;
  91.     }
  92.    
  93.     /**
  94.      * This method takes an object turns it into a string
  95.      * @return a string that contains the text version of containts in the fact address
  96.      */
  97.  
  98.     public String toString () {
  99.         ListIterator it = listIterator ();
  100.         String retString = new String();
  101.         while (it.hasNext ()) {
  102.             retString += (Piece) it.next ();
  103.         }
  104.         return retString;
  105.     }
  106.    
  107.     /**
  108.      * This method is used get all the information there is about the board.
  109.      * @return the information about the board.
  110.      */
  111.        
  112.     static public Board getInstance () {
  113.         return board;
  114.     }
  115. }
  116.  
  117.  

contact - faire un lien