示例#1
0
  public void onclick(int row, int column) {
    if (endOfGame) {
      FacesMessage fm =
          new FacesMessage(FacesMessage.SEVERITY_INFO, "", "The game is already over.");
      FacesContext.getCurrentInstance().addMessage(null, fm);
    } else {
      if (!blackIsTop) {
        row = 7 - row;
        column = 7 - column;
      }
      if (isPieceSelected) {
        isPieceSelected = false;
        // chessboard.findLegalMovesIgnoringCheck(); // debug
        if (chessboard.isMovePossible(selectedPieceRow, selectedPieceColumn, row, column)) {
          int piece = chessboard.getChessPiece(selectedPieceRow, selectedPieceColumn);
          int capturedPiece = chessboard.getChessPiece(row, column);

          chessboard =
              chessboard.moveChessPiece(
                  selectedPieceRow, selectedPieceColumn, row, column, ChessConstants.W_QUEEN);
          boolean chess = chessboard.isWhiteKingThreatened || chessboard.isBlackKingThreatened;
          Move m =
              new Move(
                  piece,
                  selectedPieceRow,
                  selectedPieceColumn,
                  row,
                  column,
                  Chessboard.s_MATERIAL_VALUE[1] - Chessboard.s_MATERIAL_VALUE[0],
                  chess,
                  capturedPiece > ChessConstants.W_EMPTY,
                  capturedPiece);
          info = m.toString();
          String move = m.getNotation();
          if (history.size() % 2 == 0) {
            move = String.valueOf((history.size() / 2) + 1) + ". " + move;
          }
          history.add(move);
          selectedPieceRow = 0;
          selectedPieceColumn = 0;
          // opponentsMove();
          startOpponentsMove = true;
        }

      } else {
        int piece = chessboard.getChessPiece(row, column);
        if (piece != ChessConstants.B_EMPTY && piece != ChessConstants.W_EMPTY) {
          if (chessboard.isActivePlayersPiece(piece)) {
            isPieceSelected = true;
            selectedPieceRow = row;
            selectedPieceColumn = column;
          }
        }
      }
    }
    redraw();
  }
示例#2
0
 public void opponentsMove(ActionEvent event) {
   if (endOfGame) {
     FacesMessage fm =
         new FacesMessage(FacesMessage.SEVERITY_INFO, "", "The game is already over.");
     FacesContext.getCurrentInstance().addMessage(null, fm);
     startOpponentsMove = false;
   } else {
     if (startOpponentsMove) {
       startOpponentsMove = false;
       try {
         Move m = chessboard.findBestMove();
         String move = m.getNotation();
         if (history.size() % 2 == 0) {
           move = String.valueOf((history.size() / 2) + 1) + ". " + move;
         }
         history.add(move);
         info = move.toString();
         chessboard =
             chessboard.moveChessPiece(
                 m.fromRow, m.fromColumn, m.toRow, m.toColumn, ChessConstants.W_QUEEN);
       } catch (EndOfGameException e) {
         FacesMessage fm =
             new FacesMessage(FacesMessage.SEVERITY_INFO, "", "The game is already over.");
         FacesContext.getCurrentInstance().addMessage(null, fm);
         endOfGame = true;
         gameOverException = e;
         info = e.getClass().getSimpleName();
       }
       elapsedTime = ((Chessboard.realTimeOfCalculation / 1000) / 1000.0d) + "ms";
       cpuTime = ((Chessboard.totalTime / 1000) / 1000) + " ms";
       evaluatedMoves = NumberFormat.getInstance().format(Chessboard.evaluatedPositions);
       averageEvaluation = Chessboard.totalTime / chessboard.evaluatedPositions + " ns";
     }
   }
   redraw();
 }