add simple board creation logic
parent
c341e9dd81
commit
dba9354121
|
|
@ -1,20 +1,106 @@
|
||||||
package de.mannheim.th.chess.domain;
|
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.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;
|
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{
|
public class Game {
|
||||||
|
|
||||||
private Board bord = new Board();
|
private Board board;
|
||||||
private Clock clockPlayer1 = new Clock();
|
private Clock clockPlayer1;
|
||||||
private Clock clockPlayer2 = new Clock();
|
private Clock clockPlayer2;
|
||||||
|
|
||||||
|
private MoveList movelist;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Conststructs a new standard GameBoard.
|
||||||
|
*/
|
||||||
public Game() {
|
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());
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -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());
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue