From 105b6e7565e3fd391ac7c3d118741fb057bc7523 Mon Sep 17 00:00:00 2001 From: Marco Date: Tue, 11 Jul 2023 22:29:55 +0200 Subject: [PATCH] Fix two bugs 1. Fix the bug that made black move first in a new game when the old game was ended during blacks turn. 2. Fix bug that offered the promotion dialog to the player when a pawn was moved on the last rank from any square Also, a late initializer error was fixed because the wrong move variable was used when a pawn reached the last rank. --- lib/chess/chess_square.dart | 8 ++++++-- lib/chess_bloc/chess_bloc.dart | 1 + lib/chess_bloc/promotion_bloc.dart | 8 ++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/chess/chess_square.dart b/lib/chess/chess_square.dart index e46b7e2..5b4046e 100644 --- a/lib/chess/chess_square.dart +++ b/lib/chess/chess_square.dart @@ -83,8 +83,12 @@ class ChessSquare extends StatelessWidget { } }, builder: (context, candidateData, rejectedData) { - var maxDrags = - kDebugMode ? 1 : (ChessBloc.turnColor == ChessBloc.myColor ? 1 : 0); + var maxDrags = kDebugMode + ? 1 + : ((ChessBloc.turnColor == ChessBloc.myColor) && + (containedPiece?.color == ChessBloc.turnColor) + ? 1 + : 0); return Container( color: color, diff --git a/lib/chess_bloc/chess_bloc.dart b/lib/chess_bloc/chess_bloc.dart index 4d7acae..fe3bf62 100644 --- a/lib/chess_bloc/chess_bloc.dart +++ b/lib/chess_bloc/chess_bloc.dart @@ -36,6 +36,7 @@ class ChessBloc extends Bloc { } void initBoard(InitBoard event, Emitter emit) { + turnColor = ChessColor.white; ChessPosition.getInstance().resetToStartingPosition(); emit(ChessBoardState(ChessColor.white, ChessColor.white, ChessPosition.getInstance().currentPosition)); diff --git a/lib/chess_bloc/promotion_bloc.dart b/lib/chess_bloc/promotion_bloc.dart index 4a6498c..ffb84ad 100644 --- a/lib/chess_bloc/promotion_bloc.dart +++ b/lib/chess_bloc/promotion_bloc.dart @@ -16,6 +16,14 @@ class PromotionBloc extends Bloc { PawnMovedToPromotionField event, Emitter emit, ) { + switch (event.colorMoved) { + case ChessColor.white: + if (event.move.from.row != 7) return; + break; + case ChessColor.black: + if (event.move.from.row != 2) return; + break; + } move = event.move; emit(PromotionState( showPromotionDialog: true, colorMoved: event.colorMoved));