private static string ToPrettyString(IList <Pile> piles) { string s = ""; int max = 0; for (int i = 0; i < piles.Count; i++) { max = Math.Max(max, piles[i].Count); } for (int j = 0; j < max; j++) { Pile row = new Pile(); for (int i = 0; i < piles.Count; i++) { if (j < piles[i].Count) { row.Add(piles[i][j]); } else { row.Add(Card.Empty); } } s += ToPrettyString(j, row); } return(s); }
public void Update(IList <Pile> pileMap) { NumberOfPiles = pileMap.Count; for (int column = 0; column < count; column++) { Pile pile = pileMap[column]; Update(column, pile); } }
private static string ToAsciiString(Pile row) { string s = ""; for (int i = 0; i < row.Count; i++) { s += row[i].ToAsciiString(); } return(s); }
public void Update(int column, Pile pile) { if (pile.Count == 0) { array[column] = Card.Empty; } else { array[column] = pile[pile.Count - 1]; } }
private static Pile GetPileFromAsciiString(string s) { int n = s.Length / 2; Pile pile = new Pile(); for (int i = 0; i < n; i++) { pile.Add(Card.Parse(s.Substring(2 * i, 2))); } return(pile); }
private static Pile[] GetPilesFromAsciiString(string s) { string[] rows = s.Split(SecondarySeparator); int n = rows.Length; Pile[] piles = new Pile[n]; for (int i = 0; i < n; i++) { piles[i] = GetPileFromAsciiString(rows[i]); } return(piles); }
public static string ToPrettyString(Tableau tableau) { string s = Environment.NewLine; s += " Spider"; s += Environment.NewLine; s += "--------------------------------"; s += Environment.NewLine; Pile discardRow = new Pile(); for (int i = 0; i < tableau.DiscardPiles.Count; i++) { Pile discardPile = tableau.DiscardPiles[i]; discardRow.Add(discardPile[discardPile.Count - 1]); } s += ToPrettyString(-1, discardRow); s += Environment.NewLine; s += ToPrettyString(tableau.DownPiles); s += Environment.NewLine; s += " " + ColumnHeadings(tableau.NumberOfPiles); s += Environment.NewLine; s += ToPrettyString(tableau.UpPiles); s += Environment.NewLine; int rowIndex = 0; Pile row = new Pile(); for (int index = tableau.StockPile.Count - 1; index >= 0; index--) { row.Add(tableau.StockPile[index]); if (row.Count == tableau.NumberOfPiles) { s += ToPrettyString(rowIndex++, row); row.Clear(); } } if (row.Count != 0) { s += ToPrettyString(rowIndex, row); } return(s); }
private static string ToPrettyString(int row, Pile pile) { string s = ""; if (row == -1) { s += " "; } else { s += string.Format("{0,2} ", row); } for (int i = 0; i < pile.Count; i++) { if (i > 0) { s += " "; } s += (pile[i].IsEmpty) ? " " : pile[i].ToString(); } return(s + Environment.NewLine); }
public static string ToAsciiString(Tableau tableau) { Pile discardRow = new Pile(); for (int i = 0; i < tableau.DiscardPiles.Count; i++) { Pile discardPile = tableau.DiscardPiles[i]; discardRow.Add(discardPile[discardPile.Count - 1]); } string s = ""; s += Fence; s += tableau.Variation.ToAsciiString() + PrimarySeparator; s += ToAsciiString(discardRow) + PrimarySeparator; s += ToAsciiString(tableau.DownPiles) + PrimarySeparator; s += ToAsciiString(tableau.UpPiles) + PrimarySeparator; s += ToAsciiString(tableau.StockPile); s += Fence; return(WrapString(s, 60)); }
public void Find(Tableau tableau) { this.tableau = tableau; int n = tableau.NumberOfPiles; for (int column = 0; column < n; column++) { Pile pile = tableau[column]; PileInfo pileInfo = pileInfoArray[column]; int m = pile.Count; pileInfo.Count = m; pileInfo.RunInfoArray[m] = new RunInfo(m, m, 0); if (m == 0) { pileInfo.RunUpAnySuitStart = 0; pileInfo.RunUpAnySuitLength = 0; continue; } if (m == 1) { pileInfo.RunUpAnySuitStart = 0; pileInfo.RunUpAnySuitLength = 1; pileInfo.RunInfoArray[0] = new RunInfo(0, 1, 1); continue; } RunInfo[] runInfoArray = pileInfo.RunInfoArray; int startRow = m - 1; int endRow = m; int suits = 1; Card previousCard = pile[endRow - 1]; for (int currentRow = m - 2; currentRow >= 0; currentRow--) { Card currentCard = pile[currentRow]; if (!currentCard.IsTargetFor(previousCard)) { break; } if (currentCard.Suit == previousCard.Suit) { startRow = currentRow; } else { RunInfo runInfo = new RunInfo(startRow, endRow, suits); for (int row = startRow; row < endRow; row++) { runInfoArray[row] = runInfo; } startRow = currentRow; endRow = currentRow + 1; suits++; } previousCard = currentCard; } { RunInfo runInfo = new RunInfo(startRow, endRow, suits); for (int row = startRow; row < endRow; row++) { runInfoArray[row] = runInfo; } } pileInfo.RunUpAnySuitStart = startRow; pileInfo.RunUpAnySuitLength = m - startRow; } }
public void FromAsciiString(string s) { // Parse string. StringBuilder b = new StringBuilder(); int i; for (i = 0; i < s.Length && s[i] != Fence; i++) { } if (i == s.Length) { throw new Exception("missing opening fence"); } for (i++; i < s.Length && s[i] != Fence; i++) { char c = s[i]; if (!char.IsWhiteSpace(c)) { b.Append(s[i]); } } if (i == s.Length) { throw new Exception("missing closing fence"); } s = b.ToString(); string[] sections = s.Split(PrimarySeparator); if (sections.Length != 5) { throw new Exception("wrong number of sections"); } // Parse sections. Variation variation = Variation.FromAsciiString(sections[0]); Pile discardCards = GetPileFromAsciiString(sections[1]); Pile[] downPiles = GetPilesFromAsciiString(sections[2]); Pile[] upPiles = GetPilesFromAsciiString(sections[3]); Pile stockPile = GetPileFromAsciiString(sections[4]); if (discardCards.Count > variation.NumberOfFoundations) { throw new Exception("too many discard piles"); } if (downPiles.Length > variation.NumberOfPiles) { throw new Exception("wrong number of down piles"); } if (upPiles.Length > variation.NumberOfPiles) { throw new Exception("wrong number of up piles"); } if (stockPile.Count > variation.NumberOfStockCards) { throw new Exception("too many stock pile cards"); } // Prepare game. Tableau.Variation = variation; Tableau.Clear(); foreach (Card discardCard in discardCards) { Pile discardPile = new Pile(); for (Face face = Face.King; face >= Face.Ace; face--) { discardPile.Add(new Card(face, discardCard.Suit)); } Tableau.DiscardPiles.Add(discardPile); } for (int column = 0; column < downPiles.Length; column++) { Tableau.DownPiles[column].Copy(downPiles[column]); } for (int column = 0; column < upPiles.Length; column++) { Tableau.UpPiles[column].Copy(upPiles[column]); } Tableau.StockPile.Copy(stockPile); Tableau.Refresh(); }