add simple board creation logic

gamelogic
dstuck 2025-06-11 09:15:04 +02:00
parent c341e9dd81
commit dba9354121
2 changed files with 125 additions and 12 deletions

View File

@ -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();
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());
}
}

View File

@ -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<Move> 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());
}
}