From fea24c827411d5af9e17bcbba895ceb160fcb820 Mon Sep 17 00:00:00 2001 From: Marco Date: Wed, 5 Jul 2023 21:16:01 +0200 Subject: [PATCH] Make castling work. --- lib/chess_bloc/chess_bloc.dart | 43 +++++++++++++++++++++++++++++++++- lib/utils/chess_utils.dart | 25 ++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/lib/chess_bloc/chess_bloc.dart b/lib/chess_bloc/chess_bloc.dart index ba47f5b..d9c7533 100644 --- a/lib/chess_bloc/chess_bloc.dart +++ b/lib/chess_bloc/chess_bloc.dart @@ -49,9 +49,48 @@ class ChessBloc extends Bloc { void moveHandler(ReceivedMove event, Emitter emit) { log('opponentMoveHandler()'); + + var move = ChessMove(from: event.startSquare, to: event.endSquare); + bool wasEnPassant = move.wasEnPassant(); + bool wasCastling = move.wasCastling(); + + var oldPosition = ChessPosition.getInstance().copyOfCurrentPosition; ChessPosition.getInstance().recordMove(event.startSquare, event.endSquare); var newPosition = ChessPosition.getInstance().currentPosition; + if (wasEnPassant) { + if (turnColor == ChessColor.white) { + newPosition[ChessCoordinate( + event.endSquare.column, event.endSquare.row - 1)] = + const ChessPiece.none(); + } else { + newPosition[ChessCoordinate( + event.endSquare.column, event.endSquare.row + 1)] = + const ChessPiece.none(); + } + } else if (wasCastling) { + ChessPiece rookToMove; + ChessPiece kingToMove; + if (move.to.column == 7) { + rookToMove = oldPosition[ChessCoordinate(8, move.to.row)]!; + newPosition[ChessCoordinate(6, move.to.row)] = rookToMove; + newPosition[ChessCoordinate(8, move.to.row)] = const ChessPiece.none(); + + kingToMove = oldPosition[ChessCoordinate(5, move.to.row)]!; + newPosition[ChessCoordinate(7, move.to.row)] = kingToMove; + newPosition[ChessCoordinate(5, move.to.row)] = const ChessPiece.none(); + } + if (move.to.column == 3) { + rookToMove = oldPosition[ChessCoordinate(1, move.to.row)]!; + newPosition[ChessCoordinate(4, move.to.row)] = rookToMove; + newPosition[ChessCoordinate(1, move.to.row)] = const ChessPiece.none(); + + kingToMove = oldPosition[ChessCoordinate(5, move.to.row)]!; + newPosition[ChessCoordinate(3, move.to.row)] = kingToMove; + newPosition[ChessCoordinate(5, move.to.row)] = const ChessPiece.none(); + } + } + turnColor = state.newTurnColor == ChessColor.white ? ChessColor.black : ChessColor.white; @@ -66,7 +105,9 @@ class ChessBloc extends Bloc { } void promotionHandler( - ReceivedPromotion event, Emitter emit) { + ReceivedPromotion event, + Emitter emit, + ) { var pieceAtStartSquare = ChessPosition.getInstance().getPieceAt( ChessCoordinate(event.startSquare.column, event.startSquare.row)); if (pieceAtStartSquare == null) { diff --git a/lib/utils/chess_utils.dart b/lib/utils/chess_utils.dart index ac5b2c8..4ae0e38 100644 --- a/lib/utils/chess_utils.dart +++ b/lib/utils/chess_utils.dart @@ -4,6 +4,8 @@ import 'package:mchess/api/move.dart'; import 'package:mchess/api/websocket_message.dart'; import 'package:quiver/core.dart'; +import '../chess_bloc/chess_position.dart'; + enum ChessPieceClass { none, pawn, @@ -304,6 +306,29 @@ class ChessMove { int get hashCode { return hash2(from, to); } + + bool wasEnPassant() { + var pieceMoved = ChessPosition.getInstance().getPieceAt(from); + var pieceAtEndSquare = ChessPosition.getInstance().getPieceAt(to); + if (pieceMoved != null && + pieceMoved.pieceClass == ChessPieceClass.pawn && + pieceAtEndSquare == null && + from.column != to.column) { + return true; + } + return false; + } + + bool wasCastling() { + var pieceMoved = ChessPosition.getInstance().getPieceAt(from); + if (pieceMoved != null && pieceMoved.pieceClass == ChessPieceClass.king) { + var colDiff = (from.column - to.column).abs(); + if (colDiff == 2) { + return true; + } + } + return false; + } } class PieceDragged {