Marco
105b6e7565
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.
77 lines
2.0 KiB
Dart
77 lines
2.0 KiB
Dart
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:mchess/chess_bloc/chess_bloc.dart';
|
|
import 'package:mchess/chess_bloc/chess_events.dart';
|
|
import 'package:mchess/utils/chess_utils.dart';
|
|
|
|
class PromotionBloc extends Bloc<PromotionEvent, PromotionState> {
|
|
static final PromotionBloc _instance = PromotionBloc._internal();
|
|
static late ChessMove move;
|
|
|
|
PromotionBloc._internal() : super(PromotionState.init()) {
|
|
on<PawnMovedToPromotionField>(promotionMoveHandler);
|
|
on<PieceChosen>(pieceChosenHandler);
|
|
}
|
|
|
|
void promotionMoveHandler(
|
|
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));
|
|
}
|
|
|
|
void pieceChosenHandler(
|
|
PieceChosen event,
|
|
Emitter<PromotionState> emit,
|
|
) {
|
|
ChessBloc.getInstance()
|
|
.add(OwnPromotionPlayed(pieceClass: event.pieceClass, move: move));
|
|
emit(PromotionState(showPromotionDialog: false, colorMoved: event.color));
|
|
}
|
|
|
|
factory PromotionBloc.getInstance() {
|
|
return PromotionBloc();
|
|
}
|
|
|
|
factory PromotionBloc() {
|
|
return _instance;
|
|
}
|
|
}
|
|
|
|
abstract class PromotionEvent {}
|
|
|
|
class PawnMovedToPromotionField extends PromotionEvent {
|
|
final ChessMove move;
|
|
final ChessColor colorMoved;
|
|
|
|
PawnMovedToPromotionField({required this.move, required this.colorMoved});
|
|
}
|
|
|
|
class PieceChosen extends PromotionEvent {
|
|
final ChessPieceClass pieceClass;
|
|
final ChessColor color;
|
|
|
|
PieceChosen({required this.pieceClass, required this.color});
|
|
}
|
|
|
|
class PromotionState {
|
|
final bool showPromotionDialog;
|
|
final ChessColor colorMoved;
|
|
|
|
PromotionState({required this.showPromotionDialog, required this.colorMoved});
|
|
|
|
factory PromotionState.init() {
|
|
return PromotionState(
|
|
showPromotionDialog: false, colorMoved: ChessColor.white);
|
|
}
|
|
}
|