Jeu de dame

  1. package checkers;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5.  
  6. /**
  7.  * The main purpose of this class is to display the board. It is composed by 64 black and white squares. Each of them is identified by two coordinate system: Col and Row or X and Y. The first couple is used all over the game to identify the position of a square; the second are just used in the SMR request.
  8.  */
  9. public class Grid  extends JPanel {
  10.     /**
  11.      * size of the board
  12.      */    
  13.     final static public int ROW_MIN = 0;
  14.     /**
  15.      * size of the board
  16.      */    
  17.     final static public int COL_MIN = 0;
  18.     /**
  19.      * size of the board
  20.      */    
  21.     final static public int ROW_MAX = 8;
  22.     /**
  23.      * size of the board
  24.      */    
  25.     final static public int COL_MAX = 8;
  26.     static private Square[][] square = new Square[ROW_MAX][COL_MAX];
  27.     static private Grid grid = new Grid();
  28.     static private Square outside = grid.new Square();
  29.    
  30.         /**
  31.          *This Method initilize the grid and places the gound square on the board.
  32.          *
  33.          */
  34.  
  35.     private Grid() {
  36.         boolean rowFirstColor = Square.WHITE;
  37.         for (int x=ROW_MIN;x<ROW_MAX ;x++) {
  38.             rowFirstColor = !rowFirstColor;
  39.             boolean SQcolor = rowFirstColor;
  40.             for (int y=COL_MIN;y<COL_MAX ;y++) {
  41.                 SQcolor = !SQcolor;
  42.                 square[x][y] = new Square(x, y, SQcolor);
  43.                 add(square[x][y]);
  44.             }
  45.         }
  46.     }
  47.    
  48.     /**
  49.      * Panel size
  50.      * @return an instance of Dimension that represents the minimum size of this container.
  51.      */
  52.     public Dimension getMinimumSize() {
  53.         return new Dimension((ROW_MAX +1) * Square.edge,(COL_MAX +1)* Square.edge);
  54.     }
  55.    
  56.     /**
  57.      * Panel size
  58.      * @return an instance of Dimension that represents the preferred size of this container.
  59.      */
  60.     public Dimension getPreferredSize() {
  61.         return new Dimension((ROW_MAX +1) * Square.edge,(COL_MAX +1)* Square.edge);
  62.     }
  63.         /**
  64.          * This method is used to check if a piece is locatet at the upperside side of board
  65.          * @return True if the piece is loacated at the upperside side of the board false otherwise.
  66.          * @param square Position to be tested
  67.          * @param color Color of the piece
  68.          */
  69.     static public boolean backLine(Square square,boolean color){
  70.         boolean retValue = false;
  71.         retValue = (square.getRow() == ROW_MIN && color == Piece.WHITE);
  72.         retValue |= (square.getRow() == (ROW_MAX-1) && color == Piece.BLACK);
  73.         return retValue;
  74.     }
  75.         /**
  76.          * This method is used to check if information about a square and what it contains
  77.          * @return the square with information if it is located inside the board.
  78.          * @param col coordinate of the square
  79.          * @param row coordinate of the square
  80.          */
  81.    
  82.     static public Square getSquare(int row, int col) {
  83.         if (row >= ROW_MIN && row < ROW_MAX
  84.         && col >= COL_MIN && col < COL_MAX)
  85.             return square[row][col];
  86.         else return outside;
  87.     }
  88.  
  89.         /**
  90.          * This method is used to printout the information of the square in a string
  91.          * @return a string with square information converted to a text string.
  92.          */
  93.                
  94.     public String toString() {
  95.         String retValue = "The Grid \r\n";
  96.         for(int x=ROW_MIN;x<ROW_MAX;x++)                {
  97.            
  98.             for(int y=COL_MIN;y<COL_MAX;y++){
  99.                 retValue += square[x][y];
  100.             }
  101.             retValue += "\r\n";
  102.         }
  103.         return  retValue;
  104.     }
  105.  
  106.     /**
  107.      * This method is used get Grid's object.
  108.      * @return the information about the grid.
  109.      */
  110.  
  111.     static public Grid getInstance() {
  112.         return grid;
  113.     }
  114.  
  115.     /**
  116.      * This Class is used to initilize the squares and is a extendstion of Canvas. It contains all the information of the squares.
  117.      */
  118.  
  119.    
  120.     public class Square  extends Canvas {
  121.         final static boolean WHITE = true;
  122.         final static boolean BLACK = false;
  123.         final static int edge = 30;
  124.         private boolean isFree = true;
  125.         private Piece piece;
  126.         private int col;
  127.         private int row;
  128.         private int x;
  129.         private int y;
  130.         private boolean SQcolor = WHITE;
  131.         private boolean outside = false;
  132.        
  133.         final Font f = new Font("SansSerif",Font.BOLD,12);
  134.  
  135.         /**
  136.          * Construtor of the Square the set all square outside the board.
  137.          */        
  138.         public Square(){
  139.             outside = true;
  140.         }
  141.        
  142.         /**
  143.          *This Method initilize all the square with the information they need.
  144.          *
  145.          */
  146.  
  147.         private Square(final int row, final int col, boolean SQcolor){
  148.             this.row = row;
  149.             this.col = col;
  150.             this.SQcolor = SQcolor;
  151.             this.x = (row-7 + col);
  152.             this.y = (col-row);
  153.             setSize(edge,edge);
  154.             setBackground(SQcolor ? Color.white : Color.black);
  155.            
  156.             addMouseListener(new MouseAdapter() {
  157.                 public void mousePressed(MouseEvent e) {
  158.                     if ((e.getModifiers()) > 0)
  159.                         System.out.println("Square " + row + " "+ col );
  160.                 }
  161.             });
  162.         }
  163.        
  164.         /**
  165.          * Causes this container to lay out its components.
  166.          */
  167.         public void doLayout() {
  168.             setBounds(row * edge , col * edge, (row + 1) * edge, (col + 1) * edge);
  169.         }
  170.        
  171.         /**
  172.          * This Method is to tell the color of the square black or white
  173.          * @return the color of the square
  174.          */
  175.      
  176.         public boolean isBlack() {
  177.             return SQcolor;
  178.         }
  179.      
  180.         /**
  181.          * This method is used to find out if a square is within the board and if it is black.
  182.          * @return true if the conditions are meet else false.
  183.          */  
  184.         public boolean isLegal(){
  185.             if ((Math.abs(getX())+Math.abs(getY()) <= 7) && SQcolor == BLACK && !outside)
  186.                 return true;
  187.             else return false;
  188.         }
  189.        
  190.         /**
  191.          * This Method finds out if there is a piece on the square
  192.          * @return true if there is no piece on the square else false if it is also outside the board.
  193.          */
  194.  
  195.         public boolean isFree(){
  196.             return (isFree && !outside);
  197.         }
  198.  
  199.         /**
  200.          * This Method is used to set a square free of a piece or set a piece on it and it repaint the interface.
  201.          * @param isFree Value to be altered
  202.          */
  203.        
  204.  
  205.         public void setFree(boolean isFree) {
  206.             this.isFree = isFree;
  207.             piece = Board.select(row, col);
  208.             repaint();
  209.         }
  210.         /**
  211.          * This method turns the coordinate of the square into a sting.
  212.          * @return the string containing the coordinats of the square.
  213.          */
  214.        
  215.         public String toString() {
  216.             return "Square ("+row+", "+col+") ";
  217.         }
  218.                 /**
  219.                  * This method is used find out if the squares are equal
  220.                  * @return true if they are else false.
  221.                  * @param anObject object to be compared to
  222.                  */
  223.         public boolean equals(Object anObject) {
  224.             if (anObject != null && anObject instanceof Square) {
  225.                 Square square = (Square) anObject;
  226.                 return (square.getCol() == col && square.getRow() == row);
  227.             }
  228.             else
  229.                 return false;
  230.         }
  231.                 /**
  232.                  * This method finds out if the piece on the square has the same colour of the parameter
  233.                  * @return true if they are the same colour else false.
  234.                  * @param color color to be tested
  235.                  */
  236.         public boolean PieceHasNotTheColor(boolean color) {
  237.             if (piece == null)
  238.                 return false;
  239.             if (color != piece.getColor())
  240.                 return true;
  241.             return false;
  242.         }
  243.  
  244.         /**
  245.          * This method is used find out the information about the piece on the piece on the square.
  246.          * @return the information of the piece.
  247.          */
  248.  
  249.            
  250.         public Piece getPiece() {
  251.             return piece;
  252.         }
  253.  
  254.         /**
  255.          * This method is used to get the coloum of the square.
  256.          * @return the coloum.
  257.          */
  258.                
  259.         public int getCol() {
  260.             return col;
  261.         }
  262.  
  263.         /**
  264.          * This method is used to get the row of the square.
  265.          * @return the row.
  266.          */
  267.        
  268.         public int getRow() {
  269.             return row;
  270.         }
  271.  
  272.         /**
  273.          * This method is used to get the x- coordinat of the square (the SMR coordinatsystem.
  274.          * @return the x-coordinat.
  275.          */
  276.        
  277.         public int getX() {
  278.             return x;
  279.         }
  280.  
  281.  
  282.         /**
  283.          * This method is used to get the y- coordinat of the square (the SMR coordinatsystem.)
  284.          * @return the y-coordinat.
  285.          */        
  286.         public int getY() {
  287.             return y;
  288.         }
  289.  
  290.         /**
  291.          * This method is used to get the colour of the square
  292.          * @return true if it white and false if it is black
  293.          */
  294.  
  295.         public boolean getColor() {
  296.             return SQcolor;
  297.         }
  298.  
  299.         /**
  300.          * This method is used TO paint the graphical user interface by drawing it.
  301.          * @param g Graphic object
  302.          */
  303.        
  304.         public void paint(Graphics g) {
  305.             g.clearRect(0,0,edge,edge);
  306.            
  307.             if (!isFree) {
  308.                 if (piece == null)
  309.                     return;
  310.                
  311.                 g.setColor(piece.getColor() ? Color.white : Color.red);
  312.                
  313.                 g.fillOval(3,3,edge-7,edge-7);
  314.                
  315.                 if (piece.isKing()) {
  316.                     g.setColor(Color.YELLOW);
  317.                     g.fillOval(8,8,edge-15,edge-15);
  318.                 }
  319.                
  320.                 g.setColor(Color.black);
  321.                 g.setFont(f);
  322.                 FontMetrics fm = getFontMetrics(f);
  323.                 int number = piece.getNumber();
  324.                 int w = fm.charWidth(number);
  325.                 int h = fm.getHeight();
  326.                 g.drawString(""+number,((edge-w)/2),((edge+h)/2));
  327.                
  328.             }
  329.         }
  330.     }
  331. }
  332.  

contact - faire un lien