public void pawnPromotion(Point p) { if (b.get(p) instanceof Pawn && p.getY() == 8) { b.remove(p); b.put(p, new Queen(true)); } else if (b.get(p) instanceof Pawn && p.getY() == 1) { b.remove(p); b.put(p, new Queen(false)); } }
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; }
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; }
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; }
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; }
/** * Paints the graphic component * * @param g Graphic component */ public void paint(Graphics g) { if (environment != null) { Sudoku env = (Sudoku) environment; Board board = env.getBoard(); int n = SudokuLanguage.DIGITS; int sqrt_n = (int) (Math.sqrt(n) + 0.1); g.setColor(Color.lightGray); Font font = g.getFont(); g.setFont(new Font(font.getName(), font.getStyle(), 20)); for (int i = 0; i < n; i++) { int ci = getCanvasValue(i); if (i % sqrt_n == 0) { g.drawLine(ci, DRAW_AREA_SIZE + MARGIN, ci, MARGIN); g.drawLine(DRAW_AREA_SIZE + MARGIN, ci, MARGIN, ci); } for (int j = 0; j < n; j++) { int cj = getCanvasValue(j); int value = board.get(i, j); if (value > 0) { g.setColor(Color.black); g.drawString("" + value, cj + CELL_SIZE / 5, ci + CELL_SIZE); g.setColor(Color.lightGray); } } } g.drawLine(DRAW_AREA_SIZE + MARGIN, DRAW_AREA_SIZE + MARGIN, DRAW_AREA_SIZE + MARGIN, MARGIN); g.drawLine(DRAW_AREA_SIZE + MARGIN, DRAW_AREA_SIZE + MARGIN, MARGIN, DRAW_AREA_SIZE + MARGIN); } }
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; }
public void run() { for (Behavior bee : io.getBehaviors()) { // Getting Pieces if (bee.getPiece() != null) { placePiece(bee.getStartPoint(), bee.getPiece()); System.out.println(bee.toString()); } else if (isWhitesTurn == b.get(bee.getStartPoint()).getColor()) { // make into methods // Taking pieces if (this.b.get(bee.getEndPoint()) != null) { if (takePiece(bee.getStartPoint(), bee.getEndPoint())) { System.out.println(bee.toString()); } else { System.err.println("Cannot take " + bee.toString()); } pawnPromotion(bee.getEndPoint()); } // castling else if (bee.getP3() != null && bee.getP4() != null) { if (castling(bee.getStartPoint(), bee.getEndPoint(), bee.getP3(), bee.getP4())) { System.out.println(bee.toString()); } else { System.err.println("Cannot move " + bee.toString()); } } // moving pieces else if (this.b.get(bee.getEndPoint()) == null) { if (movePiece(bee.getStartPoint(), bee.getEndPoint())) { System.out.println(bee.toString()); } else { System.err.println("Cannot move " + bee.toString()); } pawnPromotion(bee.getEndPoint()); } isWhitesTurn = !isWhitesTurn; if (checkForCheckMate() && isWhitesTurn) { System.out.println("Checkmate. Black wins!"); } else if (checkForCheckMate() && !isWhitesTurn) { System.out.println("Checkmate. White wins!"); } else if (checkForCheckMate() && !isKingInCheck()) { System.out.println("StaleMate."); } else if (isKingInCheck()) { System.out.println("The King is in check"); } } else { if (isWhitesTurn) { System.out.println("Its not Black's turn!"); } else if (!isWhitesTurn) { System.out.println("Its not White's turn!"); } } cd.displayBoard(); } }