protected bool Step(HopOptions hopOptions, Func <Hop, bool> callback) { while (hopOptions.OptionAvailable) { Hop hop = hopOptions.CurrrentHop; UndoStack.Push(hop); if (callback(hop)) { return(false); } List <Hop> allowedHops = board.GetAllowedHops(); bool cont = Step(new HopOptions(allowedHops), callback); if (!cont) { return(false); } UndoStack.Pop(); board.UndoHopPeg(hop); hopOptions.NextOptionIndex(); } return(true); }
/// <summary> /// Move a peg to the empty "to" cell, removing the peg hopped over. /// </summary> public void HopPeg(Hop hop) { Assert.That(flatView[hop.FromCellIndex].HasPeg, "Expected from cell to be have a peg."); Assert.That(flatView[hop.ToCellIndex].IsEmpty, "Expected to cell to be empty."); Assert.That(flatView[hop.HoppedCellIndex].HasPeg, "Expected to cell have a peg."); // Perform hop. From cell is emptied, cell being hopped is emptied, to cell is occupied. flatView[hop.FromCellIndex].HasPeg = false; flatView[hop.HoppedCellIndex].HasPeg = false; flatView[hop.ToCellIndex].HasPeg = true; // Save color state of from/hopped/to cells hop.FromCellColor = flatView[hop.FromCellIndex].Color; hop.HoppedCellColor = flatView[hop.HoppedCellIndex].Color; hop.ToCellColor = flatView[hop.ToCellIndex].Color; // Update color of To with color of From. // We can ignore the others because the UI handles that. flatView[hop.ToCellIndex].Color = hop.FromCellColor; DoHop.Fire(this, new CellChangeEventArgs() { Hop = hop, State = true }); }
protected bool SingleStepCallback(Hop hop) { board.HopPeg(hop); Solved = board.RemainingPegs == 1; hopSemaphore.WaitOne(); return(Solved); }
/// <summary> /// Return true if a solution has been found, which cancels the recursion. /// </summary> protected bool Callback(Hop hop) { ++Iterations; board.HopPeg(hop); Solved = board.RemainingPegs == 1; return(Solved); }
public override Hop PushHop() { Hop hop = HopStack.Peek().CurrrentHop; board.HopPeg(hop); UndoStack.Push(hop); return(hop); }
public override void Run() { while (board.RemainingPegs > 1 && Step()) { Hop hop = PushHop(); } Solved = board.RemainingPegs == 1; }
public override bool Step() { ++Iterations; bool ret = hopEnumerator.MoveNext(); if (ret) { Hop hop = hopEnumerator.Current; board.HopPeg(hop); ret = !(board.RemainingPegs == 1); } return(ret); }
protected bool Unwind() { bool more = false; while (!more && HopStack.Count > 0) { Hop undoHop = UndoStack.Pop(); board.UndoHopPeg(undoHop); more = HopStack.Peek().NextOptionIndex(); if (!more) { HopStack.Pop(); } } return(more); }
/// <summary> /// The reverse process, restores pegs in the to cell, hopped cell, and removes the peg in the from cell. /// </summary> public void UndoHopPeg(Hop hop) { Assert.That(flatView[hop.FromCellIndex].IsEmpty, "Expected from cell to be empty."); Assert.That(flatView[hop.ToCellIndex].HasPeg, "Expected to cell to have a peg."); Assert.That(flatView[hop.HoppedCellIndex].IsEmpty, "Expected to cell to be empty."); flatView[hop.FromCellIndex].HasPeg = true; flatView[hop.HoppedCellIndex].HasPeg = true; flatView[hop.ToCellIndex].HasPeg = false; // Restore colors: flatView[hop.FromCellIndex].Color = hop.FromCellColor; flatView[hop.HoppedCellIndex].Color = hop.HoppedCellColor; flatView[hop.ToCellIndex].Color = hop.ToCellColor; UndoHop.Fire(this, new CellChangeEventArgs() { Hop = hop, State = false }); }
protected IEnumerable <Hop> Step(HopOptions hopOptions) { while (hopOptions.OptionAvailable) { Hop hop = hopOptions.CurrrentHop; UndoStack.Push(hop); yield return(hop); List <Hop> allowedHops = board.GetAllowedHops(); foreach (Hop nextHop in Step(new HopOptions(allowedHops))) { yield return(nextHop); } UndoStack.Pop(); board.UndoHopPeg(hop); hopOptions.NextOptionIndex(); } }
/// <summary> /// We clone the Hop so that the color state is preserved for this *specific* hop. /// Otherwise, the Hop instance might be re-used in a later iteration and the previous /// Hop instance's color state will be overwritten by the new hop. /// </summary> /// <returns></returns> public Hop Clone() { Hop hop = new Hop(FromCellIndex, ToCellIndex, HoppedCellIndex); return(hop); }