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.
This commit is contained in:
Marco 2023-07-11 22:29:55 +02:00
parent da986c8d9b
commit 105b6e7565
3 changed files with 15 additions and 2 deletions

View File

@ -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,

View File

@ -36,6 +36,7 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
}
void initBoard(InitBoard event, Emitter<ChessBoardState> emit) {
turnColor = ChessColor.white;
ChessPosition.getInstance().resetToStartingPosition();
emit(ChessBoardState(ChessColor.white, ChessColor.white,
ChessPosition.getInstance().currentPosition));

View File

@ -16,6 +16,14 @@ class PromotionBloc extends Bloc<PromotionEvent, PromotionState> {
PawnMovedToPromotionField event,
Emitter<PromotionState> 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));