stuckd 2025-06-24 10:17:48 +02:00
parent e8bb9e1eee
commit 724cde3fd0
2 changed files with 120 additions and 21 deletions

View File

@ -7,6 +7,8 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.github.bhlangonijr.chesslib.Board;
import com.github.bhlangonijr.chesslib.Piece;
import com.github.bhlangonijr.chesslib.Rank;
import com.github.bhlangonijr.chesslib.Side;
import com.github.bhlangonijr.chesslib.Square;
import com.github.bhlangonijr.chesslib.move.Move;
@ -151,11 +153,16 @@ public class Game {
public List<Move> getLegalMoves(Square square) {
return this.board.legalMoves().stream().filter(move -> move.getFrom() == square).collect(Collectors.toList());
}
public void stopClock() {
clock.endGame();
}
}
public void stopClock() {
clock.endGame();
}
public boolean isPromotionMove(Move move) {
return ((move.getTo().getRank().equals(Rank.RANK_8) || move.getTo().getRank().equals(Rank.RANK_1)) &&
(board.getPiece(move.getFrom()) == Piece.BLACK_PAWN || board.getPiece(move.getFrom()) == Piece.WHITE_PAWN));
}
/**
* Retrieves a list of all legal moveable squares from the current board state.
@ -166,21 +173,55 @@ public class Game {
return this.board.legalMoves().stream().map(move -> move.getFrom()).distinct().collect(Collectors.toList());
}
/**
* Retrieves a list of legal moveable squares for a given square.
*
* @param square the Square from which to retrieve legal moveable squares
* @return a List of Square objects representing the legal moveable squares from
* the specified square.
*/
public List<Square> getLegalMoveableSquares(Square square) {
return this.board.legalMoves().stream().filter(move -> move.getFrom() == square).map(move -> move.getTo())
.collect(Collectors.toList());
}
public String getUnicodeFromMove(Move move) {
return board.getPiece(move.getTo()).getFanSymbol().toUpperCase();
}
/**
* Retrieves a list of legal moveable squares for a given square.
*
* @param square the Square from which to retrieve legal moveable squares
* @return a List of Square objects representing the legal moveable squares
* from the specified square.
*/
public List<Square> getLegalMoveableSquares(Square square) {
return this.board.legalMoves().stream()
.filter(move -> move.getFrom() == square)
.map(move -> move.getTo())
.collect(Collectors.toList());
}
public void doPromotionMove(int piece, Square origin, Square destination) {
System.out.println(piece);
Piece promotedTo;
switch(piece) {
case 7:
promotedTo = Piece.BLACK_KNIGHT;
break;
case 4:
promotedTo = Piece.BLACK_QUEEN;
break;
case 5:
promotedTo = Piece.BLACK_ROOK;
break;
case 6:
promotedTo = Piece.BLACK_BISHOP;
break;
case 3:
promotedTo = Piece.WHITE_KNIGHT;
break;
case 0:
promotedTo = Piece.WHITE_QUEEN;
break;
case 1:
promotedTo = Piece.WHITE_ROOK;
break;
case 2:
promotedTo = Piece.WHITE_BISHOP;
break;
default:
promotedTo = Piece.WHITE_QUEEN;
}
Move promotionMove = new Move(origin, destination, promotedTo);
board.doMove(promotionMove);
movelist.add(promotionMove);
}
public String toFEN() {
//board.toString();

View File

@ -3,6 +3,7 @@ package de.mannheim.th.chess.ui;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.github.bhlangonijr.chesslib.Piece;
import com.github.bhlangonijr.chesslib.Square;
import com.github.bhlangonijr.chesslib.move.Move;
import com.github.bhlangonijr.chesslib.move.MoveList;
@ -24,6 +25,7 @@ import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
@ -243,7 +245,32 @@ public class SpielFrame extends JFrame {
final Move move = new Move(selectedSquare, square);
b.setEnabled(true);
b.setBackground(new Color(230, 100, 100));
b.addActionListener(new ButtonMovePieceListener(this, this.game, move));
for (ActionListener al : b.getActionListeners()) {
b.removeActionListener(al);
}
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(game.isPromotionMove(move)) {
game.doPromotionMove(showPromotion(), selectedSquare, square);
} else {
game.playMove(move);
}
if (game.isDraw()) {
game.stopClock();
mode = BoardMode.finished;
showDraw();
} else if (game.isMate()) {
game.stopClock();
mode = BoardMode.finished;
showWin(game.getActivePlayer());
}
mode = BoardMode.normal;
setCursor(null);
erstelleBrett();
}
});
}
break;
@ -287,6 +314,37 @@ public class SpielFrame extends JFrame {
frame.add(jl);
frame.setVisible(true);
}
private int showPromotion() {
final int[] result = {-1};
JDialog dialog = new JDialog(this, "Wähle eine Figur", true);
dialog.setLayout(new GridLayout(2, 2));
dialog.setSize(300, 200);
int[] pictures = {81, 82, 66, 78, 113, 114, 98, 110};
for (int i = 0; i < 4; i++) {
int index = (game.getActivePlayer() - 1) * 4 + i;
JButton jb = new JButton();
jb.setIcon(new ImageIcon("src/main/resources/" + pictures[index] + ".png"));
int selectedPiece = index;
jb.addActionListener(e -> {
System.out.println("Test");
result[0] = selectedPiece;
dialog.dispose();
});
dialog.add(jb);
}
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
return result[0];
}
private JPanel getUiPlayerTwo() {