public static PawnPromotionType?PromotePawn(Player player) { using (var dialog = new DlgPawnPromotion(player)) { if (dialog.ShowDialog() == DialogResult.OK) { return(dialog.SelectedPawnPromotion); } } return(null); }
private void UxChessBoard_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { int boardx = -1, boardy = -1; switch (e.Button) { case MouseButtons.Left: BoardHitTest(e.X, e.Y, ref boardx, ref boardy); var boardLocation = ToBoardLocation(boardx, boardy); //games over pal if (Game.GameIsOver) { return; } if (MovingPiece == ChessPiece.None) { //if the piece does not belong to the current player do nothing if (!Game.Board[boardLocation].BelongsTo(Game.CurrentPlayer)) { return; } //we have a valid piece for this side... MovingPieceBoardLocation = boardLocation; mousex = e.X; mousey = e.Y; MovingPiece = Game.Board[boardLocation]; MovingPiecePossibleMoves = Game.FindMoves().Where(a => a.Source == boardLocation).ToList(); Invalidate(); } else { //we are finishing a move... var destination = ToBoardLocation(boardx, boardy); var availableMoves = MovingPiecePossibleMoves .Where(a => a.Destination == destination) .ToList(); if (availableMoves.Count == 1) { Game.MakeMove(MovingPieceBoardLocation, destination); } else if (availableMoves.Count > 1) { var promotion = DlgPawnPromotion.PromotePawn(Game.CurrentPlayer); if (promotion.HasValue) { Game.MakeMove(MovingPieceBoardLocation, destination, promotion); } } else { //invalid move, notify?? } MovingPiecePossibleMoves = null; Invalidate(); UxChessBoard_MouseDown_CancelMove(e); } break; case MouseButtons.Right: UxChessBoard_MouseDown_CancelMove(e); Invalidate(); break; } }