Make castling work.

This commit is contained in:
Marco 2023-07-05 21:16:01 +02:00
parent c3d747a60e
commit fea24c8274
2 changed files with 67 additions and 1 deletions

View File

@ -49,9 +49,48 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
void moveHandler(ReceivedMove event, Emitter<ChessBoardState> emit) { void moveHandler(ReceivedMove event, Emitter<ChessBoardState> emit) {
log('opponentMoveHandler()'); 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); ChessPosition.getInstance().recordMove(event.startSquare, event.endSquare);
var newPosition = ChessPosition.getInstance().currentPosition; 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 turnColor = state.newTurnColor == ChessColor.white
? ChessColor.black ? ChessColor.black
: ChessColor.white; : ChessColor.white;
@ -66,7 +105,9 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
} }
void promotionHandler( void promotionHandler(
ReceivedPromotion event, Emitter<ChessBoardState> emit) { ReceivedPromotion event,
Emitter<ChessBoardState> emit,
) {
var pieceAtStartSquare = ChessPosition.getInstance().getPieceAt( var pieceAtStartSquare = ChessPosition.getInstance().getPieceAt(
ChessCoordinate(event.startSquare.column, event.startSquare.row)); ChessCoordinate(event.startSquare.column, event.startSquare.row));
if (pieceAtStartSquare == null) { if (pieceAtStartSquare == null) {

View File

@ -4,6 +4,8 @@ import 'package:mchess/api/move.dart';
import 'package:mchess/api/websocket_message.dart'; import 'package:mchess/api/websocket_message.dart';
import 'package:quiver/core.dart'; import 'package:quiver/core.dart';
import '../chess_bloc/chess_position.dart';
enum ChessPieceClass { enum ChessPieceClass {
none, none,
pawn, pawn,
@ -304,6 +306,29 @@ class ChessMove {
int get hashCode { int get hashCode {
return hash2(from, to); 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 { class PieceDragged {