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 70a1805..8a21219 100644 --- a/src/main/java/de/mannheim/th/chess/domain/Game.java +++ b/src/main/java/de/mannheim/th/chess/domain/Game.java @@ -1,20 +1,106 @@ package de.mannheim.th.chess.domain; +import java.util.List; +import java.util.stream.Collectors; + 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 de.mannheim.th.chess.utl.Clock; /** - * Ist die zentrale Klasse für ein einzelnes Spiel. Ist praktisch die zentrale Steuerung davon. + * Ist die zentrale Klasse für ein einzelnes Spiel. Ist praktisch die zentrale + * Steuerung davon. */ -public class Game{ - - private Board bord = new Board(); - private Clock clockPlayer1 = new Clock(); - private Clock clockPlayer2 = new Clock(); - - public Game() { - - } - -} \ No newline at end of file +public class Game { + + private Board board; + private Clock clockPlayer1; + private Clock clockPlayer2; + + private MoveList movelist; + + /** + * Conststructs a new standard GameBoard. + */ + public Game() { + this.board = new Board(); + + this.movelist = new MoveList(); + + this.clockPlayer1 = new Clock(); + this.clockPlayer2 = new Clock(); + + } + + /** + * Constructs a new standard GameBoard and applies the provides moves. + * + * @param movelist The list of moves that get played. + */ + public Game(MoveList movelist) { + this.board = new Board(); + + this.movelist = movelist; + + for (Move move : movelist) { + this.board.doMove(move); + } + + this.clockPlayer1 = new Clock(); + this.clockPlayer2 = new Clock(); + } + + /** + * Constructs a new GameBoard with the provided fen String as the positions. + * + * @param fen The fen String that provides the customs formation. + */ + public Game(String fen) { + this.board = new Board(); + this.board.loadFromFen(fen); + + this.movelist = new MoveList(); + + this.clockPlayer1 = new Clock(); + this.clockPlayer2 = new Clock(); + } + + /** + * 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/test/java/de/mannheim/th/chess/domain/GameTest.java b/src/test/java/de/mannheim/th/chess/domain/GameTest.java new file mode 100644 index 0000000..4242264 --- /dev/null +++ b/src/test/java/de/mannheim/th/chess/domain/GameTest.java @@ -0,0 +1,27 @@ +package de.mannheim.th.chess.domain; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.List; + +import org.junit.jupiter.api.Test; + +import com.github.bhlangonijr.chesslib.Square; +import com.github.bhlangonijr.chesslib.move.Move; + +public class GameTest { + + @Test + void getLegalMovesTest() { + Game game = new Game(); + List list; + + list = game.getLegalMoves(Square.B1); + assertEquals("b1a3", list.getFirst().toString()); + + list = game.getLegalMoves(Square.A2); + assertEquals("a2a3", list.getFirst().toString()); + assertEquals("a2a4", list.getLast().toString()); + + } +}