示例#1
0
 private void detectDangerousSquare() {
   for (ISquare square : this.chessboard) {
     if (!square.isEmpty()) {
       IPiece piece = square.getPiece();
       if (piece.getType() != this.playerTurn.getColor()) {
         piece.detectMoves(this.chessboard, false);
       }
     }
   }
   for (ISquare square : this.chessboard) {
     if (!square.isEmpty()) {
       IPiece piece = square.getPiece();
       if (piece.getType() == this.playerTurn.getColor()) {
         if (piece.getClass().getName().equals(King.class.getName())) {
           King myKing = (King) piece;
           currentKingCanMove = myKing.canMove();
           System.out.println("MY KING CAN MOVE : " + currentKingCanMove);
         }
       }
     }
   }
 }
示例#2
0
  @Override
  public boolean onTouch(View v, MotionEvent event) {
    int line, column;
    column = (int) ((event.getX() - margin / 2) * NB_SQUARE_PAR_LINE / ref);
    line = (int) ((event.getY() - margin / 2) * NB_SQUARE_PAR_LINE / ref);

    boolean left, top, right, bot;
    left = event.getX() - margin / 2 > 0;
    top = event.getY() - margin / 2 > 0;
    right = event.getX() - margin / 2 < ref;
    bot = event.getY() - margin / 2 < ref;

    if (left && top && right && bot) {
      ISquare square = getSquare(line, column);
      int leftImage =
          margin / 2 + ref / NB_SQUARE_PAR_LINE * column + (int) (ref / NB_SQUARE_PAR_LINE * 0.2);
      int topImage =
          margin / 2 + ref / NB_SQUARE_PAR_LINE * line + (int) (ref / NB_SQUARE_PAR_LINE * 0.2);

      if (selectedSquare != null) {
        switch (square.getStatus()) {
          case ISquare.STATUS_MOVE:
            // Move
            square.add(selectedSquare.getPiece());
            selectedSquare.getPiece().animate(leftImage, topImage);
            selectedSquare.remove();
            upgradePiece(square);
            addMoves(square.getPiece(), line, column);
            changeTurn();
            break;
          case ISquare.STATUS_TARGETABLE:
            // Eat
            square.getPiece().hideImage();
            square.add(selectedSquare.getPiece());
            square.getPiece().animate(leftImage, topImage);
            selectedSquare.remove();
            upgradePiece(square);
            addMoves(square.getPiece(), line, column);
            changeTurn();
            break;
        }

        // addMoves(square.getPiece(),line,column);
        // changeTurn();
      }

      this.resetSquares();
      if (selectedSquare == null) {
        IPiece piece = square.getPiece();
        if (piece != null && piece.getType() == playerTurn.getColor()) {
          detectDangerousSquare();
          piece.detectMoves(this.chessboard, true);
          selectedSquare = square;
        }
        square.setStatus(ISquare.STATUS_SELECTED);
      } else {
        selectedSquare = null;
      }

      this.invalidate();
    }
    return false;
  }