private int MoveOffUsingSpaces(int from, int fromRow, int to, int remainingSuits, int n) { int suits = Math.Min(remainingSuits, n); if (Diagnostics) { Utils.WriteLine("MOUS: {0} -> {1}: {2}", from, to, suits); } for (int i = n - suits; i < n; i++) { // Move as much as possible but not too much. Pile fromPile = Tableau[from]; int currentFromRow = fromPile.Count - Tableau.GetRunUp(from, fromPile.Count); if (currentFromRow < fromRow) { currentFromRow = fromRow; } int runLength = fromPile.Count - currentFromRow; MakeSimpleMove(from, -runLength, Spaces[i]); MoveStack.Push(new Move(Spaces[i], -runLength, to)); } for (int i = n - 2; i >= n - suits; i--) { int runLength = Tableau[Spaces[i]].Count; MakeSimpleMove(Spaces[i], -runLength, Spaces[n - 1]); MoveStack.Push(new Move(Spaces[n - 1], -runLength, Spaces[i])); } return(suits); }
private void MakeMove(Node node, int i) { Move move = node.Moves[i]; bool toEmpty = move.Type == MoveType.Basic && WorkingTableau[move.To].Count == 0; MoveStack.Clear(); for (int next = move.HoldingNext; next != -1; next = node.SupplementaryList[next].Next) { Move holdingMove = node.SupplementaryList[next]; WorkingTableau.Move(new Move(MoveType.Basic, MoveFlags.Holding, holdingMove.From, holdingMove.FromRow, holdingMove.To)); int undoTo = holdingMove.From == move.From ? move.To : move.From; MoveStack.Push(new Move(MoveType.Basic, MoveFlags.UndoHolding, holdingMove.To, -holdingMove.ToRow, undoTo)); } WorkingTableau.Move(new Move(move.Type, move.From, move.FromRow, move.To, move.ToRow)); if (!toEmpty) { while (MoveStack.Count > 0) { Move holdingMove = MoveStack.Pop(); if (!WorkingTableau.IsValid(holdingMove)) { break; } WorkingTableau.Move(holdingMove); } } }
private void UnloadToSpaces(int from, int fromRow, int to) { if (Diagnostics) { Utils.WriteLine("ULTS: {0}/{1} -> {2}", from, fromRow, to); } int numberOfSpaces = Tableau.NumberOfSpaces; int suits = Tableau.CountSuits(from, fromRow); if (suits > ExtraSuits(numberOfSpaces)) { throw new InvalidMoveException("insufficient spaces"); } Spaces.Copy(Tableau.Spaces); int totalSuits = Tableau.CountSuits(from, fromRow); int remainingSuits = totalSuits; int currrentFromRow = Tableau[from].Count; for (int n = 0; n < numberOfSpaces; n++) { int m = Math.Min(numberOfSpaces, n + remainingSuits); for (int i = m - 1; i >= n; i--) { int runLength = Tableau.GetRunUp(from, currrentFromRow); currrentFromRow -= runLength; currrentFromRow = Math.Max(currrentFromRow, fromRow); MakeSimpleMove(from, -runLength, Spaces[i]); MoveStack.Push(new Move(Spaces[i], -runLength, to)); remainingSuits--; } for (int i = n + 1; i < m; i++) { int runLength = Tableau[Spaces[i]].Count; MakeSimpleMove(Spaces[i], -runLength, Spaces[n]); MoveStack.Push(new Move(Spaces[n], -runLength, Spaces[i])); } if (remainingSuits == 0) { break; } } }