private void movePiece(Piece p, Square destinationSquare, Square originSquare) { p.setVisible(false); originSquare.removeAll(); destinationSquare.add(p); p.setSquare(destinationSquare); p.setVisible(true); }
public String castleQueenside(Square candidateSquare) { if (piece == 0) { int row; int column; if (color == 1) { row = 1; column = 1; } else { row = 8; column = 1; } int cell = square.getCellFromCoord(row, column); Square sq = (Square) board.getComponent(cell); Piece pc = (Piece) sq.getComponent(0); int newCell; if (pc.getColor() == 1) { newCell = square.getCellFromCoord(1, 4); } else { newCell = square.getCellFromCoord(8, 4); } Square newSquare = (Square) board.getComponent(newCell); // move rook movePiece(pc, newSquare, sq); return ("0-0-0"); } return ""; }
public Knight(Color col, boolean white, int x, int y) { this.color = col; this.x = x; this.y = y; this.icon = new ImageIcon(Piece.loadImage("knight", color, 64, 64)); setIcon(this.icon); setBorderPainted(false); setOpaque(true); int[][] pieceSquareConfigW = { {-50, -40, -30, -30, -30, -30, -40, -50}, {-40, -20, 0, 0, 0, 0, -20, -40}, {-30, 0, 10, 15, 15, 10, 0, -30}, {-30, 5, 15, 20, 20, 15, 5, -30}, {-30, 0, 15, 20, 20, 15, 0, -30}, {-30, 5, 10, 15, 15, 10, 5, -30}, {-40, -20, 0, 5, 5, 0, -20, -40}, {-50, -40, -30, -30, -30, -30, -40, -50} }; super.pieceSquareConfigW = pieceSquareConfigW; int[][] pieceSquareConfigB = new int[8][8]; for (int r = 0, r2 = 7; r < 8; r++, r2--) { for (int c = 0; c < 8; c++) { pieceSquareConfigB[r][c] = pieceSquareConfigW[r2][c]; } } super.pieceSquareConfigB = pieceSquareConfigB; if (white) { setBackground(new Color(192, 192, 192)); } else { setBackground(new Color(128, 128, 128)); } setPreferredSize(new Dimension(80, 76)); }
/** *************************************************************************************** */ public void selectCell() { if (p != null) { p.highlight = true; } }
/** *************************************************************************************** */ public void deSelect() { if (p != null) { p.deHighlight(); } }
private void setListForKing() { moveList.clear(); squareList.clear(); int row = square.getRow(); int column = square.getColumn(); int c1, c2, c3, c4, c5, c6, c7, c8; String cell1, cell2, cell3, cell4, cell5, cell6, cell7, cell8; if (((row + 1) < 9) && (column + 1) < 9) { c1 = square.getCellFromCoord(row + 1, column + 1); cell1 = square.getPositionWithArg(c1); Square s = (Square) board.getComponent(c1); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell1); squareList.add(s); } } else { moveList.add(cell1); squareList.add(s); } } if (((row + 1) < 9) && (column - 1) > 0) { c2 = square.getCellFromCoord(row + 1, column - 1); cell2 = square.getPositionWithArg(c2); Square s = (Square) board.getComponent(c2); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell2); squareList.add(s); } } else { moveList.add(cell2); squareList.add(s); } } if ((row + 1) < 9) { c3 = square.getCellFromCoord(row + 1, column); cell3 = square.getPositionWithArg(c3); Square s = (Square) board.getComponent(c3); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell3); squareList.add(s); } } else { moveList.add(cell3); squareList.add(s); } } if ((column + 1) < 9) { c4 = square.getCellFromCoord(row, column + 1); cell4 = square.getPositionWithArg(c4); Square s = (Square) board.getComponent(c4); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell4); squareList.add(s); } } else { moveList.add(cell4); squareList.add(s); } } if ((column - 1) > 0) { c5 = square.getCellFromCoord(row, column - 1); cell5 = square.getPositionWithArg(c5); Square s = (Square) board.getComponent(c5); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell5); squareList.add(s); } } else { moveList.add(cell5); squareList.add(s); } } if (((row - 1) > 0) && (column - 1) > 0) { c6 = square.getCellFromCoord(row - 1, column - 1); cell6 = square.getPositionWithArg(c6); Square s = (Square) board.getComponent(c6); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell6); squareList.add(s); } } else { moveList.add(cell6); squareList.add(s); } } if (((row - 1) > 0) && (column + 1) < 9) { c7 = square.getCellFromCoord(row - 1, column + 1); cell7 = square.getPositionWithArg(c7); Square s = (Square) board.getComponent(c7); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell7); squareList.add(s); } } else { moveList.add(cell7); squareList.add(s); } } if ((row - 1) > 0) { c8 = square.getCellFromCoord(row - 1, column); cell8 = square.getPositionWithArg(c8); Square s = (Square) board.getComponent(c8); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell8); squareList.add(s); } } else { moveList.add(cell8); squareList.add(s); } } if (queensideClear()) { int cell = square.getCellFromCoord(1, 3); String c = square.getPositionWithArg(cell); moveList.add(c); cell = square.getCellFromCoord(8, 3); c = square.getPositionWithArg(cell); moveList.add(c); } if (kingsideClear()) { int cell = square.getCellFromCoord(1, 7); String c = square.getPositionWithArg(cell); moveList.add(c); cell = square.getCellFromCoord(8, 7); c = square.getPositionWithArg(cell); moveList.add(c); } }
private void setListForKnight() { moveList.clear(); squareList.clear(); /*switch(square.getCellNumber()) { case 56 : moveList.add("c2");moveList.add("b3"); break; case 57 : moveList.add("d2");moveList.add("a3");moveList.add("c3"); break; case 58 : moveList.add("a2");moveList.add("e2");moveList.add("b3");moveList.add("d3"); break; case 59 : moveList.add("b2");moveList.add("f2");moveList.add("c3");moveList.add("e3"); break; case 60 : moveList.add("c2");moveList.add("g2");moveList.add("d3");moveList.add("f3"); break; case 61 : moveList.add("d2");moveList.add("h2");moveList.add("e3");moveList.add("g3"); break; case 62 : moveList.add("e2");moveList.add("f3");moveList.add("h3"); break; case 63 : moveList.add("f2");moveList.add("g3"); break; case 48 : moveList.add("c1");moveList.add("c3");moveList.add("b4"); break; case 49 : moveList.add("d1");moveList.add("d3");moveList.add("a4");moveList.add("c4"); break; case 50 : moveList.add("a1");moveList.add("e1");moveList.add("a3");moveList.add("e3");moveList.add("b4");moveList.add("d4"); break; case 51 : moveList.add("b1");moveList.add("f1");moveList.add("b3");moveList.add("f3");moveList.add("c4");moveList.add("e4"); break; case 52 : moveList.add("c1");moveList.add("g1");moveList.add("c3");moveList.add("g3");moveList.add("d4");moveList.add("f4"); break; case 53 : moveList.add("d1");moveList.add("h1");moveList.add("d3");moveList.add("h3");moveList.add("e4");moveList.add("g4"); break; case 54 : moveList.add("e1");moveList.add("e3");moveList.add("f4");moveList.add("h4"); break; case 55 : moveList.add("f1");moveList.add("f3");moveList.add("g4"); break; case 40 : moveList.add("b1");moveList.add("c2");moveList.add("c4");moveList.add("b5"); break; case 41 : moveList.add("a1");moveList.add("c1");moveList.add("d2");moveList.add("d4");moveList.add("a5");moveList.add("c5"); break; case 42 : moveList.add("b1");moveList.add("d1");moveList.add("a2");moveList.add("e2");moveList.add("a4");moveList.add("e4");moveList.add("b5");moveList.add("d5"); break; case 43 : moveList.add("c1");moveList.add("e1");moveList.add("b2");moveList.add("f2");moveList.add("b4");moveList.add("f4");moveList.add("c5");moveList.add("e5"); break; case 44 : moveList.add("d1");moveList.add("f1");moveList.add("c2");moveList.add("g2");moveList.add("c4");moveList.add("g4");moveList.add("d5");moveList.add("f5"); break; case 45 : moveList.add("e1");moveList.add("g1");moveList.add("d2");moveList.add("h2");moveList.add("d4");moveList.add("h4");moveList.add("e5");moveList.add("g5"); break; case 46 : moveList.add("f1");moveList.add("h1");moveList.add("e2");moveList.add("e4");moveList.add("f5");moveList.add("h5"); break; case 47 : moveList.add("g1");moveList.add("f2");moveList.add("f4");moveList.add("g5"); break; case 32 : moveList.add("b2");moveList.add("c3");moveList.add("c5");moveList.add("b6"); break; case 33 : moveList.add("a2");moveList.add("c2");moveList.add("d3");moveList.add("d5");moveList.add("a6");moveList.add("c6"); break; case 34 : moveList.add("b2");moveList.add("d2");moveList.add("a3");moveList.add("e3");moveList.add("a5");moveList.add("e5");moveList.add("b6");moveList.add("d6"); break; case 35 : moveList.add("c2");moveList.add("e2");moveList.add("b3");moveList.add("f3");moveList.add("b5");moveList.add("f5");moveList.add("c6");moveList.add("e6"); break; case 36 : moveList.add("d2");moveList.add("f2");moveList.add("c3");moveList.add("g3");moveList.add("c5");moveList.add("g5");moveList.add("d6");moveList.add("f6"); break; case 37 : moveList.add("e2");moveList.add("g2");moveList.add("d3");moveList.add("h3");moveList.add("d5");moveList.add("h5");moveList.add("e6");moveList.add("g6"); break; case 38 : moveList.add("f2");moveList.add("h2");moveList.add("e3");moveList.add("e5");moveList.add("f6");moveList.add("h6"); break; case 39 : moveList.add("g2");moveList.add("f3");moveList.add("f5");moveList.add("g6"); break; case 24 : moveList.add("b3");moveList.add("c4");moveList.add("c6");moveList.add("b7"); break; case 25 : moveList.add("a3");moveList.add("c3");moveList.add("d4");moveList.add("d6");moveList.add("a7");moveList.add("c7"); break; case 26 : moveList.add("b3");moveList.add("d3");moveList.add("a4");moveList.add("e4");moveList.add("a6");moveList.add("e6");moveList.add("b7");moveList.add("d7"); break; case 27 : moveList.add("c3");moveList.add("e3");moveList.add("b4");moveList.add("f4");moveList.add("b6");moveList.add("f6");moveList.add("c7");moveList.add("e7"); break; case 28 : moveList.add("d3");moveList.add("f3");moveList.add("c4");moveList.add("g4");moveList.add("c6");moveList.add("g6");moveList.add("d7");moveList.add("f7"); break; case 29 : moveList.add("e3");moveList.add("g3");moveList.add("d4");moveList.add("h4");moveList.add("d6");moveList.add("h6");moveList.add("e7");moveList.add("g7"); break; case 30 : moveList.add("f3");moveList.add("h3");moveList.add("e4");moveList.add("e6");moveList.add("f7");moveList.add("h7"); break; case 31 : moveList.add("g3");moveList.add("f4");moveList.add("f6");moveList.add("g7"); break; case 16 : moveList.add("b4");moveList.add("c5");moveList.add("c7");moveList.add("b8"); break; case 17 : moveList.add("a4");moveList.add("c4");moveList.add("d5");moveList.add("d7");moveList.add("a8");moveList.add("c8"); break; case 18 : moveList.add("b4");moveList.add("d4");moveList.add("a5");moveList.add("e5");moveList.add("a7");moveList.add("e7");moveList.add("b8");moveList.add("d8"); break; case 19 : moveList.add("c4");moveList.add("e4");moveList.add("b5");moveList.add("f5");moveList.add("b7");moveList.add("f7");moveList.add("c8");moveList.add("e8"); break; case 20 : moveList.add("d4");moveList.add("f4");moveList.add("c5");moveList.add("g5");moveList.add("c7");moveList.add("g7");moveList.add("d8");moveList.add("f8"); break; case 21 : moveList.add("e4");moveList.add("g4");moveList.add("d5");moveList.add("h5");moveList.add("d7");moveList.add("h7");moveList.add("e8");moveList.add("g8"); break; case 22 : moveList.add("f4");moveList.add("h4");moveList.add("e5");moveList.add("e7");moveList.add("f8");moveList.add("h8"); break; case 23 : moveList.add("g4");moveList.add("f5");moveList.add("f7");moveList.add("g8"); break; case 8 : moveList.add("b5");moveList.add("c6");moveList.add("c8"); break; case 9 : moveList.add("a5");moveList.add("c5");moveList.add("d6");moveList.add("d8"); break; case 10 : moveList.add("b5");moveList.add("d5");moveList.add("a6");moveList.add("e6");moveList.add("a8");moveList.add("e8"); break; case 11 : moveList.add("c5");moveList.add("e5");moveList.add("b6");moveList.add("f6");moveList.add("b8");moveList.add("f8"); break; case 12 : moveList.add("d5");moveList.add("f5");moveList.add("c6");moveList.add("g6");moveList.add("c8");moveList.add("g8"); break; case 13 : moveList.add("e5");moveList.add("g5");moveList.add("d6");moveList.add("h6");moveList.add("d8");moveList.add("h8"); break; case 14 : moveList.add("f5");moveList.add("h5");moveList.add("e6");moveList.add("e8"); break; case 15 : moveList.add("g5");moveList.add("f6");moveList.add("f8"); break; case 0 : moveList.add("b6");moveList.add("c7"); break; case 1 : moveList.add("a6");moveList.add("c6");moveList.add("d7"); break; case 2 : moveList.add("b6");moveList.add("d6");moveList.add("a7");moveList.add("e7"); break; case 3 : moveList.add("c6");moveList.add("e6");moveList.add("b7");moveList.add("f7"); break; case 4 : moveList.add("d6");moveList.add("f6");moveList.add("c7");moveList.add("g7"); break; case 5 : moveList.add("e6");moveList.add("g6");moveList.add("d7");moveList.add("h7"); break; case 6 : moveList.add("f6");moveList.add("h6");moveList.add("e7");moveList.add("NS"); break; case 7 : moveList.add("g6");moveList.add("f7"); break; }*/ int row = square.getRow(); int column = square.getColumn(); int c1, c2, c3, c4, c5, c6, c7, c8; String cell1, cell2, cell3, cell4, cell5, cell6, cell7, cell8; if (((row + 1) < 9) && (column - 2) > 0) { c1 = square.getCellFromCoord(row + 1, column - 2); cell1 = square.getPositionWithArg(c1); Square s = (Square) board.getComponent(c1); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell1); squareList.add(s); } } else { moveList.add(cell1); squareList.add(s); } } if (((row + 2) < 9) && (column - 1) > 0) { c2 = square.getCellFromCoord(row + 2, column - 1); cell2 = square.getPositionWithArg(c2); Square s = (Square) board.getComponent(c2); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell2); squareList.add(s); } } else { moveList.add(cell2); squareList.add(s); } } if (((row + 1) < 9) && (column + 1) < 9) { c3 = square.getCellFromCoord(row + 2, column + 1); cell3 = square.getPositionWithArg(c3); Square s = (Square) board.getComponent(c3); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell3); squareList.add(s); } } else { moveList.add(cell3); squareList.add(s); } } if (((row + 1) < 9) && (column + 2) < 9) { c4 = square.getCellFromCoord(row + 1, column + 2); cell4 = square.getPositionWithArg(c4); Square s = (Square) board.getComponent(c4); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell4); squareList.add(s); } } else { moveList.add(cell4); squareList.add(s); } } if (((row - 1) > 0) && (column - 2) > 0) { c5 = square.getCellFromCoord(row - 1, column - 2); cell5 = square.getPositionWithArg(c5); Square s = (Square) board.getComponent(c5); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell5); squareList.add(s); } } else { moveList.add(cell5); squareList.add(s); } } if (((row - 2) > 0) && (column - 1) > 0) { c6 = square.getCellFromCoord(row - 2, column - 1); cell6 = square.getPositionWithArg(c6); Square s = (Square) board.getComponent(c6); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell6); squareList.add(s); } } else { moveList.add(cell6); squareList.add(s); } } if (((row - 2) > 0) && (column + 1) < 9) { c7 = square.getCellFromCoord(row - 2, column + 1); cell7 = square.getPositionWithArg(c7); Square s = (Square) board.getComponent(c7); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell7); squareList.add(s); } } else { moveList.add(cell7); squareList.add(s); } } if (((row - 1) > 0) && (column + 2) < 9) { c8 = square.getCellFromCoord(row - 1, column + 2); cell8 = square.getPositionWithArg(c8); Square s = (Square) board.getComponent(c8); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell8); squareList.add(s); } } else { moveList.add(cell8); squareList.add(s); } } }
public Set<Move> getAllowedMoves() { // If there is already a winn if (gipfBoardState.players.winner() != null) { return Collections.emptySet(); } // Create a set of incomplete moves containing the starting positions and directions for the // current piece Set<Move> potentialMoves = getPotentialStartMoves(getCurrentPiece()); // If the current piece is a GIPF piece, the player is also allowed to place normal pieces. if (getCurrentPiece().getPieceType() == GIPF) potentialMoves.addAll( getPotentialStartMoves(Piece.of(NORMAL, getCurrentPiece().getPieceColor()))); // These moves are marked as complete so a temporary game won't ask for user input. potentialMoves.stream().forEach(m -> m.isCompleteMove = true); Set<Move> potentialMovesIncludingLineSegmentRemoval = new HashSet<>(); for (Move potentialMove : potentialMoves) { try { Map<Position, Piece> temporaryPieceMap = new HashMap<>(getGipfBoardState().getPieceMap()); temporaryPieceMap.put(potentialMove.startPos, potentialMove.addedPiece); movePiecesTowards( temporaryPieceMap, potentialMove.getStartingPosition(), potentialMove.getDirection()); Set<List<Pair<PieceColor, Line.Segment>>> RLineOrderingsSet = getRemovableLineOrderingsSetFromGipfBoard( temporaryPieceMap, getCurrentPiece().getPieceColor()); if (RLineOrderingsSet.size() > 0) { for (List<Pair<PieceColor, Line.Segment>> RLineOrdering : RLineOrderingsSet) { Set<Position> piecesToWhite = new HashSet<>(); Set<Position> piecesToBlack = new HashSet<>(); Set<Position> piecesRemoved = new HashSet<>(); for (Pair<PieceColor, Line.Segment> RLine : RLineOrdering) { Line.Segment removedSegment = RLine.getValue(); // The color of the player who removed the line PieceColor colorRemoved = RLine.getKey(); // Determine per segment to whom the pieces are given. Pieces can only be given to the // player // who removed the line, or deleted from the game. Set<Position> occupiedPositions = removedSegment.getOccupiedPositions(temporaryPieceMap); Set<Position> piecesFromSegmentBackToReserve = occupiedPositions .stream() .filter( position -> temporaryPieceMap.get(position).getPieceColor() == colorRemoved) .collect(toSet()); Set<Position> piecesFromSegmentRemoved = occupiedPositions .stream() .filter(position -> !piecesFromSegmentBackToReserve.contains(position)) .collect(toSet()); if (colorRemoved == WHITE) piecesToWhite.addAll(piecesFromSegmentBackToReserve); if (colorRemoved == BLACK) piecesToBlack.addAll(piecesFromSegmentBackToReserve); piecesRemoved.addAll(piecesFromSegmentRemoved); } // And finally add the move // the constructor will define this as a complete move, because all the parameters have // a value. potentialMovesIncludingLineSegmentRemoval.add( new Move( potentialMove.addedPiece, potentialMove.startPos, potentialMove.direction, piecesToWhite, piecesToBlack, piecesRemoved)); } } else { // If no line segments can be removed, just add the original move potentialMovesIncludingLineSegmentRemoval.add(potentialMove); } } catch (InvalidMoveException e) { // We don't consider this move if it is invalid } } return potentialMovesIncludingLineSegmentRemoval; }
private JLabel determinePiece(Piece piece) throws IOException { if (piece.getPieceColor() == PieceColor.WHITE) { switch (piece.getPieceType().name()) { case ("KING"): JLabel wKing = new JLabel(new ImageIcon(ImageIO.read(new File("art/wKing.png")))); wKing.setSize(new Dimension(64, 64)); return wKing; case ("QUEEN"): JLabel wQueen = new JLabel(new ImageIcon(ImageIO.read(new File("art/wQueen.png")))); wQueen.setSize(new Dimension(64, 64)); return wQueen; case ("BISHOP"): JLabel wBishop = new JLabel(new ImageIcon(ImageIO.read(new File("art/wBishop.png")))); wBishop.setSize(new Dimension(64, 64)); return wBishop; case ("KNIGHT"): JLabel wKnight = new JLabel(new ImageIcon(ImageIO.read(new File("art/wKnight.png")))); wKnight.setSize(new Dimension(64, 64)); return wKnight; case ("ROOK"): JLabel wRook = new JLabel(new ImageIcon(ImageIO.read(new File("art/wRook.png")))); wRook.setSize(new Dimension(64, 64)); return wRook; case ("PAWN"): JLabel wPawn = new JLabel(new ImageIcon(ImageIO.read(new File("art/wPawn.png")))); wPawn.setSize(new Dimension(64, 64)); return wPawn; default: throw new IllegalArgumentException(); } } else { switch (piece.getPieceType().name()) { case ("KING"): JLabel bKing = new JLabel(new ImageIcon(ImageIO.read(new File("art/bKing.png")))); bKing.setSize(new Dimension(64, 64)); return bKing; case ("QUEEN"): JLabel bQueen = new JLabel(new ImageIcon(ImageIO.read(new File("art/bQueen.png")))); bQueen.setSize(new Dimension(64, 64)); return bQueen; case ("BISHOP"): JLabel bBishop = new JLabel(new ImageIcon(ImageIO.read(new File("art/bBishop.png")))); bBishop.setSize(new Dimension(64, 64)); return bBishop; case ("KNIGHT"): JLabel bKnight = new JLabel(new ImageIcon(ImageIO.read(new File("art/bKnight.png")))); bKnight.setSize(new Dimension(64, 64)); return bKnight; case ("ROOK"): JLabel bRook = new JLabel(new ImageIcon(ImageIO.read(new File("art/bRook.png")))); bRook.setSize(new Dimension(64, 64)); return bRook; case ("PAWN"): JLabel bPawn = new JLabel(new ImageIcon(ImageIO.read(new File("art/bPawn.png")))); bPawn.setSize(new Dimension(64, 64)); return bPawn; default: throw new IllegalArgumentException(); } } }