예제 #1
0
  /** Returns a move that minimizes the maximum utility by this agent. */
  private Move min(Board board, int depth) {
    nodes++;
    // If the node is a terminal, return the heuristic, which is the score advantage for this agent.
    if (depth <= 0) return new Move(board.getScoreAdvantage(ID));
    // Get an array of all legal moves, and test all of them to find the one that maximizes the
    // minimum
    // score advantage of this agent.
    Move[] moves = board.getLegalMoves();
    // If there are no legal moves, return a value reflective of whether the agent has won, tied, or
    // lost.
    if (moves.length == 0)
      return new Move(
          (board.getScoreAdvantage(ID) > 0)
              ? Integer.MAX_VALUE
              : ((board.getScoreAdvantage(ID) == 0) ? 0 : Integer.MIN_VALUE));

    Move min = null;
    for (Move m : moves) {
      // Test the given move and replace min with the move if it has a lower value.
      Move test = max(m.move(board), depth - 1);
      board.unmove(1);
      if (min == null || test.getValue() < min.getValue()) {
        min = m;
        min.setValue(test.getValue());
      }
    }
    // Return the minimum move
    return min;
  }
예제 #2
0
  @Override
  public void doSmth(Move move, Car self, World world, Game game) {
    if (self.getOilCanisterCount() > 0) {
      MovingPoint currentPoint = Utils.getMap().getCurrentPoint(self, world, game);

      if (currentPoint.isTurn()) {
        move.setSpillOil(true);
      }
    }
  }
  @Override
  public void mouseClicked(MouseEvent m) {
    SquarePanel sp = ((SquarePanel) (m.getSource()));
    Piece p = Board.getInstance().getPieceAtSquare(sp.getI(), sp.getJ());

    if (sp.getBackground().equals(Color.red)) {
      sp.getBoardView().recolorBoard();
      Board.getInstance().setSelectedPiece(null);

    } else if (sp.getBackground().equals(PURPLE)) {
      Piece piece = Board.getInstance().getSelectedPiece();
      Board.getInstance().setSelectedPiece(null);
      Move move =
          new Move(piece.getLocation(), Board.getInstance().getSquare(sp.getI(), sp.getJ()), piece);
      Board.getInstance().makeMove(move);
      sp.getBoardView().setUpBoard();
      Board.getInstance().setLastPlayerMove(move);

      Move last = Board.getInstance().getLastPlayerMove();
      if (last.getPiece() instanceof Pawn && last.getEndPositon().getI() == 0) {
        new PromotionWindow(last.getPiece(), sp.getBoardView());
      }

      try {
        Move cpuMove = Search.decision(last);
        Board.getInstance().setLastCpuMove(cpuMove);
        Board.getInstance().makeMove(cpuMove);
        sp.getBoardView().setUpBoard();
        sp.getBoardView()
            .getSquarePanelAt(cpuMove.getEndPositon().getI(), cpuMove.getEndPositon().getJ())
            .setBackground(Color.blue);

        /*ArrayList<Move> opposingPlayer = Search.getAvailableMoves(0);
        if(opposingPlayer.isEmpty()){
        	System.out.println("check mate");
        }*/

        Move lastCpu = Board.getInstance().getLastCpuMove();
        if (lastCpu.getPiece() instanceof Pawn && lastCpu.getEndPositon().getI() == 7) {
          Piece pawn = lastCpu.getPiece();
          Piece newPiece = new PieceFactory().createPiece("bq", pawn.getLocation());
          newPiece.setLocation(pawn.getLocation());
          Board.getInstance().removePiece(pawn, 1);
          Board.getInstance().addPiece(newPiece, 1);
          pawn.setLocation(null);
          sp.getBoardView().setUpBoard();
        }

      } catch (Exception e) {
      }

    } else if (p != null
        && !p.equals(Board.getInstance().getSelectedPiece())
        && p.getPlayer() != 1) {
      Board.getInstance().setSelectedPiece(p);
      sp.getBoardView().recolorBoard();
      sp.setBackground(Color.red);
      ArrayList<Move> moves = p.generateMoves();
      moves = KingSafety.safeMoves(moves, 0);

      if (!moves.isEmpty()) {
        for (Move mv : moves) {
          sp.getBoardView()
              .getSquarePanelAt(mv.getEndPositon().getI(), mv.getEndPositon().getJ())
              .setBackground(Color.green);
        }
      }
    }
  }