mchess-client/lib/chess_bloc/chess_bloc.dart
Marco aaaf40bb2b Remove a redundant call to ChessBloc that caused the position to be renewed twice.
Fix a index error (Column started with 0 while rows started with 1).
2022-12-18 23:43:06 +01:00

121 lines
4.0 KiB
Dart

import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:mchess/chess_bloc/chess_events.dart';
import 'package:mchess/chessapp/chess_utils.dart';
import 'dart:developer';
class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
static final ChessBloc _instance = ChessBloc._internal();
late ChessColor? myColor;
ChessBloc._internal() : super(ChessBoardState.init()) {
on<InitBoard>(initBoard);
on<ColorDetermined>(flipBoard);
on<PieceMoved>(moveHandler);
}
factory ChessBloc.getInstance() {
return ChessBloc();
}
factory ChessBloc() {
return _instance;
}
void initBoard(InitBoard event, Emitter<ChessBoardState> emit) {
emit(ChessBoardState.init());
}
void flipBoard(ColorDetermined event, Emitter<ChessBoardState> emit) {
emit(ChessBoardState(event.myColor, state.newTurnColor, state.position));
}
void moveHandler(PieceMoved event, Emitter<ChessBoardState> emit) {
Map<ChessCoordinate, ChessPiece> newPosition = state.position;
newPosition[event.endSquare] = state.position[event.startSquare]!;
newPosition[event.startSquare] = const ChessPiece.none();
var newTurnColor = state.newTurnColor == ChessColor.white
? ChessColor.black
: ChessColor.white;
log('emitting new state with position $newPosition');
emit(
ChessBoardState(
state.bottomColor,
newTurnColor,
newPosition,
),
);
}
}
class ChessBoardState {
late ChessColor bottomColor;
final ChessColor newTurnColor;
final Map<ChessCoordinate, ChessPiece> position;
ChessBoardState._(this.bottomColor, this.newTurnColor, this.position);
factory ChessBoardState(
ChessColor bottomColor,
ChessColor turnColor,
Map<ChessCoordinate, ChessPiece> position,
) {
return ChessBoardState._(bottomColor, turnColor, position);
}
factory ChessBoardState.init() {
ChessColor bottomColor = ChessColor.white;
ChessColor turnColor = ChessColor.white;
Map<ChessCoordinate, ChessPiece> position = {};
for (int i = 1; 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._(bottomColor, turnColor, position);
}
void logPosition(Map<ChessCoordinate, ChessPiece> pos) {
// for (int i = 0; i < 7; i++)
}
}