Example #1
0
        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);
        }
Example #2
0
        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);
        }
Example #3
0
        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);
        }
Example #4
0
        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));
        }
Example #5
0
        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();
        }