From 35f815e82f3e1f491d56aaf739bd748223a501be Mon Sep 17 00:00:00 2001 From: Marco Date: Sat, 19 Nov 2022 16:55:25 +0100 Subject: [PATCH] Fix bug that removed every piece except the moved one. --- lib/chess_bloc/chess_bloc.dart | 42 ++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/lib/chess_bloc/chess_bloc.dart b/lib/chess_bloc/chess_bloc.dart index 79cfe89..eebd254 100644 --- a/lib/chess_bloc/chess_bloc.dart +++ b/lib/chess_bloc/chess_bloc.dart @@ -21,7 +21,7 @@ class ChessBloc extends Bloc { } FutureOr moveHandler(PieceMoved event, Emitter emit) { - Map newPosition = {}; + Map newPosition = state.position; ServerConnection.getInstance().send( 'mv ${event.startSquare.toString()} ${event.endSquare.toString()}'); @@ -68,8 +68,46 @@ class ChessBoardState { ChessColor turnColor = ChessColor.white; Map 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); + 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); }