Legale felder die angeklickte Figur zu bewegen werden rot markiert

devGameUi
Marius Gündel 2025-06-12 14:09:42 +02:00
parent fd8c025c49
commit d2798b977b
2 changed files with 60 additions and 3 deletions

View File

@ -2,8 +2,12 @@ package de.mannheim.th.chess.domain;
import com.github.bhlangonijr.chesslib.Board;
import com.github.bhlangonijr.chesslib.Square;
import com.github.bhlangonijr.chesslib.move.Move;
import com.github.bhlangonijr.chesslib.move.MoveList;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import de.mannheim.th.chess.utl.Clock;
@ -15,10 +19,11 @@ public class Game{
private Board board;
private Clock clock;
private ArrayList<String> moves;
private ArrayList<Move> movelist = new ArrayList<>();
public Game() {
board = new Board();
clock = new Clock();
clock = new Clock("blitz");
}
@ -32,4 +37,42 @@ public class Game{
/**
* Plays the move on the board and adds it to the movelist
*
* @param move the move to be played
*/
public void playMove(Move move) {
this.board.doMove(move);
this.movelist.add(move);
}
/**
* Plays the move on the board and adds it to the movelist
*
* @param origin The square from wich it moves from.
* @param desination The square where it will move to.
*/
public void playMove(Square origin, Square desination) {
Move move = new Move(origin, desination);
this.board.doMove(move);
this.movelist.add(move);
}
/**
* Retrieves a list of legal moves originating from the specified square.
*
* @param square The square from which to retrieve legal moves.
*
* @return A list of legal moves that originate from the specified square.
*/
public List<Move> getLegalMoves(Square square) {
return this.board.legalMoves().stream()
.filter(move -> move.getFrom() == square)
.collect(Collectors.toList());
}
}

View File

@ -4,6 +4,9 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.github.bhlangonijr.chesslib.Board;
import com.github.bhlangonijr.chesslib.File;
import com.github.bhlangonijr.chesslib.Rank;
import com.github.bhlangonijr.chesslib.Square;
import de.mannheim.th.chess.App;
import de.mannheim.th.chess.domain.Game;
@ -34,7 +37,8 @@ public class SpielFrame extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private ArrayList<JButton> buttons = new ArrayList<>();
private HashMap<Integer, JButton> buttons = new HashMap<>();
private HashMap<JButton, Integer> positions = new HashMap<>();
private HashMap<JButton, String> belegungen = new HashMap<>();
private JPanel panelLinks, panelRechts;
private Game game;
@ -126,6 +130,13 @@ public class SpielFrame extends JFrame {
//wenn richtiger spieler dran:
setCursor(figurCursor);
int position = positions.get(clickedButton);
game.getLegalMoves(Square.encode(Rank.allRanks[7 - position / 8], File.allFiles[position % 8])).stream()
.peek(System.out::println)
.map(m -> m.getTo())
.peek(System.out::println)
.map(s -> 56 - s.getRank().ordinal() * 8 + s.getFile().ordinal())
.forEach(i -> buttons.get(i).setBackground(Color.RED));
//Button Icon zurücksetzen
@ -147,7 +158,8 @@ public class SpielFrame extends JFrame {
});
panelLinks.add(b);
buttons.add(b);
buttons.put(i, b);
positions.put(b, i);
}
game = new Game();
@ -182,4 +194,6 @@ public class SpielFrame extends JFrame {
//System.out.println(belegungen.toString());
}
}