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

View File

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

View File

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

View File

@ -88,12 +88,14 @@ class ServerConnection {
var move = ChessMove.fromApiMove(apiMessage.move!); var move = ChessMove.fromApiMove(apiMessage.move!);
if (apiMessage.position != null) { if (apiMessage.position != null) {
ChessBloc.getInstance().add(ReceivedPosition( ChessBloc.getInstance().add(ReceivedMove(
position: startSquare: move.from,
ChessPosition.getInstance().fromPGNString(apiMessage.position!))); 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) { void handleInvalidMoveMessage(ApiWebsocketMessage apiMessage) {