Fix the current position in ChessPositionManager, now that we rely on position instead of detecting en passant ourselves.

This commit is contained in:
Marco 2023-08-15 00:39:10 +02:00
parent c213d9b1f3
commit 43d8d77abc
4 changed files with 40 additions and 46 deletions

View File

@ -20,8 +20,7 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
ChessBloc._internal() : super(ChessBoardState.init()) {
on<InitBoard>(initBoard);
on<ColorDetermined>(flipBoard);
on<ReceivedMove>(moveHandler);
on<ReceivedPosition>(positionHandler);
on<ReceivedMove>(moveAndPositionHandler);
on<OwnPieceMoved>(ownMoveHandler);
on<OwnPromotionPlayed>(ownPromotionHandler);
on<InvalidMovePlayed>(invalidMoveHandler);
@ -37,9 +36,9 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
void initBoard(InitBoard event, Emitter<ChessBoardState> emit) {
turnColor = ChessColor.white;
ChessPosition.getInstance().resetToStartingPosition();
ChessPositionManager.getInstance().resetToStartingPosition();
emit(ChessBoardState(ChessColor.white, ChessColor.white,
ChessPosition.getInstance().currentPosition));
ChessPositionManager.getInstance().currentPosition));
}
void flipBoard(ColorDetermined event, Emitter<ChessBoardState> emit) {
@ -49,13 +48,13 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
}
void moveHandler(ReceivedMove event, Emitter<ChessBoardState> emit) {
ChessPosition.getInstance().recordMove(event.startSquare, event.endSquare);
}
void positionHandler(
ReceivedPosition event,
void moveAndPositionHandler(
ReceivedMove event,
Emitter<ChessBoardState> emit,
) {
ChessPositionManager.getInstance().recordMove(event.startSquare, event.endSquare, event.position);
turnColor = state.newTurnColor == ChessColor.white
? ChessColor.black
: ChessColor.white;
@ -79,7 +78,7 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
//Temporary chess position until server responds with acknowledgement
var move = ChessMove.fromApiMove(apiMove);
var tempPosition = ChessPosition.getInstance().copyOfCurrentPosition;
var tempPosition = ChessPositionManager.getInstance().copyOfCurrentPosition;
tempPosition[move.to] = tempPosition[move.from] ?? const ChessPiece.none();
tempPosition[move.from] = const ChessPiece.none();
@ -115,7 +114,7 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
ChessBoardState(
state.bottomColor,
turnColor,
ChessPosition.getInstance().currentPosition,
ChessPositionManager.getInstance().currentPosition,
),
);
}
@ -124,7 +123,7 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
class ChessBoardState {
late ChessColor bottomColor;
final ChessColor newTurnColor;
final Map<ChessCoordinate, ChessPiece> position;
final ChessPosition position;
ChessBoardState._(this.bottomColor, this.newTurnColor, this.position);
@ -140,10 +139,10 @@ class ChessBoardState {
ChessColor bottomColor = ChessColor.white;
ChessColor turnColor = ChessColor.white;
ChessPosition.getInstance().resetToStartingPosition();
ChessPositionManager.getInstance().resetToStartingPosition();
return ChessBoardState(
bottomColor, turnColor, ChessPosition.getInstance().currentPosition);
bottomColor, turnColor, ChessPositionManager.getInstance().currentPosition);
}
void logPosition(Map<ChessCoordinate, ChessPiece> pos) {

View File

@ -4,16 +4,11 @@ import 'package:mchess/utils/chess_utils.dart';
abstract class ChessEvent {}
class ReceivedMove extends ChessEvent {
final ChessCoordinate startSquare;
final ChessCoordinate endSquare;
final ChessCoordinate startSquare;
final ChessCoordinate endSquare;
final ChessPosition position;
ReceivedMove({required this.startSquare, required this.endSquare});
}
class ReceivedPosition extends ChessEvent {
final ChessPositionType position;
ReceivedPosition({required this.position});
ReceivedMove({required this.startSquare, required this.endSquare, required this.position});
}
class OwnPieceMoved extends ChessEvent {

View File

@ -3,29 +3,29 @@ import 'dart:developer';
import 'package:mchess/utils/chess_utils.dart';
typedef ChessMoveHistory = List<ChessMove>;
typedef ChessPositionType = Map<ChessCoordinate, ChessPiece>;
typedef ChessPosition = Map<ChessCoordinate, ChessPiece>;
class ChessPosition {
static ChessPosition _instance = ChessPosition._internal();
class ChessPositionManager {
static ChessPositionManager _instance = ChessPositionManager._internal();
static ChessMoveHistory history = ChessMoveHistory.empty(growable: true);
final ChessPositionType position;
ChessPosition position;
ChessPosition({required this.position});
ChessPositionManager({required this.position});
factory ChessPosition._internal() {
return ChessPosition(position: _getStartingPosition());
factory ChessPositionManager._internal() {
return ChessPositionManager(position: _getStartingPosition());
}
ChessPositionType get copyOfCurrentPosition => Map.from(position);
ChessPosition get copyOfCurrentPosition => Map.from(position);
ChessPositionType get currentPosition => position;
ChessPosition get currentPosition => position;
ChessMove? get lastMove {
if (history.isEmpty) return null;
return history.last;
}
ChessPositionType fromPGNString(String pgn) {
ChessPositionType pos = {};
ChessPosition fromPGNString(String pgn) {
ChessPosition pos = {};
List<String> rowStrings;
rowStrings = pgn.split('/');
@ -53,7 +53,7 @@ class ChessPosition {
}
}
void logPosition(ChessPositionType p) {
void logPosition(ChessPosition p) {
String logString = '';
for (int row = 8; row > 0; row--) {
@ -71,28 +71,26 @@ class ChessPosition {
log(logString);
}
void recordMove(ChessCoordinate from, ChessCoordinate to) {
position[to] = position[from] ?? const ChessPiece.none();
position[from] = const ChessPiece.none();
void recordMove(ChessCoordinate from, ChessCoordinate to, ChessPosition pos) {
position = pos;
history.add(ChessMove(from: from, to: to));
logPosition(position);
logHistory(history);
}
void resetToStartingPosition() {
history = ChessMoveHistory.empty(growable: true);
_instance = ChessPosition(position: _getStartingPosition());
_instance = ChessPositionManager(position: _getStartingPosition());
}
static ChessPosition getInstance() {
static ChessPositionManager getInstance() {
return _instance;
}
static ChessPositionType _getStartingPosition() {
ChessPositionType pos = {};
static ChessPosition _getStartingPosition() {
ChessPosition pos = {};
for (int i = 1; i <= 8; i++) {
pos[ChessCoordinate(i, 7)] =

View File

@ -88,12 +88,14 @@ class ServerConnection {
var move = ChessMove.fromApiMove(apiMessage.move!);
if (apiMessage.position != null) {
ChessBloc.getInstance().add(ReceivedPosition(
position:
ChessPosition.getInstance().fromPGNString(apiMessage.position!)));
ChessBloc.getInstance().add(ReceivedMove(
startSquare: move.from,
endSquare: move.to,
position: ChessPositionManager.getInstance()
.fromPGNString(apiMessage.position!)));
} else {
log('Error: no position received');
}
ChessBloc.getInstance()
.add(ReceivedMove(startSquare: move.from, endSquare: move.to));
}
void handleInvalidMoveMessage(ApiWebsocketMessage apiMessage) {