public void RemoveVariation(Variation variation) { if (variation == null) { throw new ArgumentNullException(nameof(variation)); } if (0 <= variation.VariationIndex && variation.VariationIndex < Variations.Count && ReferenceEquals(variation, Variations[variation.VariationIndex])) { // Remove and re-index the variations after. Variations.RemoveAt(variation.VariationIndex); if (Variations.Count == 0) { // Replace by an empty main line. Variations.Add(null); } else { for (int i = variation.VariationIndex; i < Variations.Count; ++i) { Variations[i].VariationIndex = i; } } Debug.Assert(CheckVariationIndexes()); } }
/// <summary> /// Validates a move against the current position and optionally performs it. /// </summary> /// <param name="moveInfo"> /// The move to validate and optionally perform. /// </param> /// <param name="make"> /// True if the move must actually be made, false if only validated. /// </param> /// <returns> /// A valid legal <see cref="Move"/> structure if <see cref="MoveInfo.Result"/> is equal to /// <see cref="MoveCheckResult.OK"/>, or an incomplete <see cref="Move"/> if one of the other <see cref="MoveCheckResult"/> values. /// If <paramref name="make"/> is true, the move is only made if <see cref="MoveCheckResult.OK"/> is returned. /// </returns> /// <exception cref="ArgumentOutOfRangeException"> /// Thrown when any of the move's members have an enumeration value which is outside of the allowed range. /// </exception> public Move TryMakeMove(ref MoveInfo moveInfo, bool make) { Move move = currentPosition.TryMakeMove(ref moveInfo, make); if (make && moveInfo.Result == MoveCheckResult.OK) { // Move to an existing variation, or create a new one. Variation variation = ActiveTree.GetOrAddVariation(move); ActiveTree = variation.MoveTree; } return(move); }
public Variation GetOrAddVariation(Move move) { foreach (Variation branch in Variations) { if (branch != null && branch.Move.CreateMoveInfo().InputEquals(move.CreateMoveInfo())) { return(branch); } } if (Variations[0] == null) { Variations[0] = new Variation(this, 0, move); Debug.Assert(CheckVariationIndexes()); return(Variations[0]); } else { Variation newBranch = new Variation(this, Variations.Count, move); Variations.Add(newBranch); Debug.Assert(CheckVariationIndexes()); return(newBranch); } }
internal MoveTree(bool blackToMove) { Variations.Add(null); PlyCount = blackToMove ? 1 : 0; ParentVariation = null; }
internal MoveTree(Variation parentVariation) { Variations.Add(null); PlyCount = parentVariation.ParentTree.PlyCount + 1; ParentVariation = parentVariation; }