Fix bug that removed every piece except the moved one.

This commit is contained in:
Marco 2022-11-19 16:55:25 +01:00
parent 8d5cf7592d
commit 35f815e82f

View File

@ -21,7 +21,7 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
} }
FutureOr<void> moveHandler(PieceMoved event, Emitter<ChessBoardState> emit) { FutureOr<void> moveHandler(PieceMoved event, Emitter<ChessBoardState> emit) {
Map<ChessCoordinate, ChessPiece> newPosition = {}; Map<ChessCoordinate, ChessPiece> newPosition = state.position;
ServerConnection.getInstance().send( ServerConnection.getInstance().send(
'mv ${event.startSquare.toString()} ${event.endSquare.toString()}'); 'mv ${event.startSquare.toString()} ${event.endSquare.toString()}');
@ -68,8 +68,46 @@ class ChessBoardState {
ChessColor turnColor = ChessColor.white; ChessColor turnColor = ChessColor.white;
Map<ChessCoordinate, ChessPiece> position = {}; Map<ChessCoordinate, ChessPiece> position = {};
position[ChessCoordinate(1, 1)] = for (int i = 0; i <= 8; i++) {
position[ChessCoordinate(i, 7)] =
ChessPiece(ChessPieceName.blackPawn, ChessColor.black);
position[ChessCoordinate(i, 2)] =
ChessPiece(ChessPieceName.whitePawn, ChessColor.white);
}
position[ChessCoordinate(1, 8)] =
ChessPiece(ChessPieceName.blackRook, ChessColor.black);
position[ChessCoordinate(2, 8)] =
ChessPiece(ChessPieceName.blackKnight, ChessColor.black);
position[ChessCoordinate(3, 8)] =
ChessPiece(ChessPieceName.blackBishop, ChessColor.black);
position[ChessCoordinate(4, 8)] =
ChessPiece(ChessPieceName.blackQueen, ChessColor.black);
position[ChessCoordinate(5, 8)] =
ChessPiece(ChessPieceName.blackKing, ChessColor.black); ChessPiece(ChessPieceName.blackKing, ChessColor.black);
position[ChessCoordinate(6, 8)] =
ChessPiece(ChessPieceName.blackBishop, ChessColor.black);
position[ChessCoordinate(7, 8)] =
ChessPiece(ChessPieceName.blackKnight, ChessColor.black);
position[ChessCoordinate(8, 8)] =
ChessPiece(ChessPieceName.blackRook, ChessColor.black);
position[ChessCoordinate(1, 1)] =
ChessPiece(ChessPieceName.whiteRook, ChessColor.black);
position[ChessCoordinate(2, 1)] =
ChessPiece(ChessPieceName.whiteKnight, ChessColor.black);
position[ChessCoordinate(3, 1)] =
ChessPiece(ChessPieceName.whiteBishop, ChessColor.black);
position[ChessCoordinate(4, 1)] =
ChessPiece(ChessPieceName.whiteQueen, ChessColor.black);
position[ChessCoordinate(5, 1)] =
ChessPiece(ChessPieceName.whiteKing, ChessColor.black);
position[ChessCoordinate(6, 1)] =
ChessPiece(ChessPieceName.whiteBishop, ChessColor.black);
position[ChessCoordinate(7, 1)] =
ChessPiece(ChessPieceName.whiteKnight, ChessColor.black);
position[ChessCoordinate(8, 1)] =
ChessPiece(ChessPieceName.whiteRook, ChessColor.black);
return ChessBoardState._(flipped, turnColor, position); return ChessBoardState._(flipped, turnColor, position);
} }