Пример #1
0
 public boolean validTake(Point start, Point end) {
   Piece piece = b.get(start);
   Piece ending = b.get(end);
   if (ending != null
       && piece.validCapture(start, end)
       && checkIfPathIsClear(start, end)
       && piece.getColor() != ending.getColor()) {
     return true;
   }
   return false;
 }
Пример #2
0
 public boolean movePiece(Point startLocation, Point endLocation) {
   Piece piece = b.get(startLocation);
   if (piece.pieceMovement(startLocation, endLocation)
       && checkIfPathIsClear(startLocation, endLocation)) {
     b.put(endLocation, b.get(startLocation));
     b.remove(startLocation);
     piece.setHasNotMoved(false);
     return true;
   }
   return false;
 }
Пример #3
0
  public ArrayList<Point> validMove(Point p) {
    ArrayList<Point> validMoves = new ArrayList<Point>();
    Piece piece = b.get(p);

    for (int i = 1; i <= 8; i++) {
      for (int j = 1; j <= 8; j++) {
        Piece p2 = b.get(new Point(i, j));
        if ((p2 == null && piece.pieceMovement(p, new Point(i, j))
                || (validTake(p, new Point(i, j))))
            && !isKingInCheck()
            && checkIfPathIsClear(p, new Point(i, j))) {
          validMoves.add(new Point(i, j));
          //	System.out.println(p + " " + i + " " + j );
        }
      }
    }
    return validMoves;
  }
Пример #4
0
 public boolean takePiece(Point startLocation, Point endLocation) {
   Piece piece = b.get(startLocation);
   Piece ending = b.get(endLocation);
   if (piece.validCapture(startLocation, endLocation)
       && checkIfPathIsClear(startLocation, endLocation)
       && piece.getColor() != ending.getColor()) {
     b.remove(endLocation);
     b.put(endLocation, b.get(startLocation));
     b.remove(startLocation);
     piece.setHasNotMoved(false);
     return true;
   }
   return false;
 }
Пример #5
0
  public boolean castling(Point k1, Point k2, Point r1, Point r2) {
    Piece k = b.get(k1);
    Piece r = b.get(r1);

    if (checkIfPathIsClear(k1, r1) && k.pieceMovement(k1, k2) && r.pieceMovement(r1, r2)) {
      b.put(k2, b.get(k1));
      b.put(r2, b.get(r1));
      b.remove(k1);
      b.remove(r1);
      k.setHasNotMoved(false);
      r.setHasNotMoved(false);
      return true;
    }
    return false;
  }
  @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);
        }
      }
    }
  }