Schach/src/main/java/de/mannheim/th/chess/domain/Game.java

107 lines
2.5 KiB
Java

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.
*/
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<Move> getLegalMoves(Square square) {
return this.board.legalMoves().stream()
.filter(move -> move.getFrom() == square)
.collect(Collectors.toList());
}
}