private string GetAlgebraicNotation(Square source, Square target, Board board) { var sb = new StringBuilder(); var playerTurn = Math.Ceiling(this.Turn / 2.0); sb.Append(playerTurn + ". "); if (this.Move.Type == MoveType.EnPassant) { var file = source.ToString()[0]; sb.Append(file + "x" + target + "e.p"); } else if (this.Move.Type == MoveType.Castling) { if (target.ToString()[0] == 'g') { sb.Append("0-0"); } else { sb.Append("0-0-0"); } } else if (this.Move.Type == MoveType.PawnPromotion) { sb.Append(target + "=Q"); } else if (target.Piece == null || target.Piece.Color != source.Piece.Color) { if (source.Piece is Pawn) { if (target.Piece == null) { sb.Append(target); } else { var file = source.ToString()[0]; sb.Append(file + "x" + target); } } else { var targetWithOldIsAttackedValue = board.Matrix.SelectMany(x => x).Where(y => y.Name == target.Name).FirstOrDefault(); if (targetWithOldIsAttackedValue.IsAttacked.Count(piece => piece.Color == source.Piece.Color && piece.Name.Contains(source.Piece.Name)) > 1) { var secondPieceSquare = board.Matrix.SelectMany(x => x).Where(square => square.Piece != null && square.Piece.Color == source.Piece.Color && square.Piece.Name.Contains(source.Piece.Name) && square.Name != source.Name).FirstOrDefault(); var xCheck = target.Piece == null ? string.Empty : "x"; if (secondPieceSquare.Position.File.Equals(source.Position.File)) { var rank = source.Name[1]; if (target.Piece == null) { sb.Append($"{source.Piece.Symbol}{rank}{xCheck}{target}"); } else { sb.Append($"{source.Piece.Symbol}{rank}{xCheck}{target}"); } } else { var file = source.Name[0]; sb.Append($"{source.Piece.Symbol}{file}{xCheck}{target}"); } } else { var check = target.Piece == null ? string.Empty : "x"; sb.Append($"{source.Piece.Symbol}{check}{target}"); } } } if (this.Opponent.IsCheck) { if (!this.Opponent.IsCheckMate) { sb.Append("+"); } else { sb.Append("#"); } } return(sb.ToString()); }
private void UpdateMoveHistory(Square source, Square target, Board board) { string notation = this.GetAlgebraicNotation(source, target, board); this.OnMoveComplete?.Invoke(this.MovingPlayer, new MoveCompleteEventArgs(notation)); }