diff --git a/src/main/java/de/mannheim/th/chess/domain/Game.java b/src/main/java/de/mannheim/th/chess/domain/Game.java index b756492..6a888b4 100644 --- a/src/main/java/de/mannheim/th/chess/domain/Game.java +++ b/src/main/java/de/mannheim/th/chess/domain/Game.java @@ -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 moves; + private ArrayList movelist = new ArrayList<>(); public Game() { board = new Board(); - clock = new Clock(); + clock = new Clock("blitz"); } @@ -29,6 +34,44 @@ public class Game{ public String toFEN() { return board.getFen(); } + + + + /** + * 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 getLegalMoves(Square square) { + return this.board.legalMoves().stream() + .filter(move -> move.getFrom() == square) + .collect(Collectors.toList()); + + } diff --git a/src/main/java/de/mannheim/th/chess/ui/SpielFrame.java b/src/main/java/de/mannheim/th/chess/ui/SpielFrame.java index 7c98419..51cc7e8 100644 --- a/src/main/java/de/mannheim/th/chess/ui/SpielFrame.java +++ b/src/main/java/de/mannheim/th/chess/ui/SpielFrame.java @@ -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 buttons = new ArrayList<>(); + private HashMap buttons = new HashMap<>(); + private HashMap positions = new HashMap<>(); private HashMap 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()); } + + }