Exemplo n.º 1
0
        public string SaveGame(bool addNewLines, GameLoadOptions options, bool includeNonDismissed)
        {
            if ((options == GameLoadOptions.None) ||
                (options == GameLoadOptions.All))
            {
                throw new ArgumentException("Options must specify EITHER the user values OR the actual values - not both.", "options");
            }

            StringBuilder gameData = new StringBuilder();

            for (int i = 0; i < _squares.Length; i++)
            {
                if ((options & GameLoadOptions.SolutionValues) == GameLoadOptions.SolutionValues)
                {
                    if (_squares[i].ActualValue.HasValue)
                    {
                        gameData.Append(_squares[i].ActualValue.Value);
                    }
                    else
                    {
                        if (includeNonDismissed)
                        {
                            gameData.AppendFormat("({0})", string.Join("", _squares[i].NonDismissedValues.ConvertAll <string>(
                                                                           delegate(int val) { return(val.ToString()); }).ToArray()));
                        }
                        else
                        {
                            gameData.Append(".");
                        }
                    }
                }
                else if ((options & GameLoadOptions.UserValues) == GameLoadOptions.UserValues)
                {
                    if (_squares[i].UserValue.HasValue)
                    {
                        gameData.Append(_squares[i].UserValue.Value);
                    }
                    else
                    {
                        if (includeNonDismissed)
                        {
                            gameData.AppendFormat("({0})", string.Join("", _squares[i].UserNonDismissedValues.ConvertAll <string>(
                                                                           delegate(int val) { return(val.ToString()); }).ToArray()));
                        }
                        else
                        {
                            gameData.Append(".");
                        }
                    }
                }

                if (addNewLines && ((i + 1) % 9 == 0))
                {
                    gameData.Append(Environment.NewLine);
                }
            }
            return(gameData.ToString());
        }
Exemplo n.º 2
0
        public void LoadGame(string gameData, GameLoadOptions options)
        {
            int  position = 0;
            bool readingNonDismissedValues = false;

            foreach (char c in gameData)
            {
                if (char.IsDigit(c))
                {
                    if (!readingNonDismissedValues)
                    {
                        if ((options & GameLoadOptions.SolutionValues) == GameLoadOptions.SolutionValues)
                        {
                            _squares[position].ActualValue = Convert.ToInt32(c.ToString()); //we're assuming that the values being read in are correct
                        }
                        if ((options & GameLoadOptions.UserValues) == GameLoadOptions.UserValues)
                        {
                            _squares[position].UserValue = Convert.ToInt32(c.ToString());
                        }

                        position++;
                    }
                    else
                    {
                        if ((options & GameLoadOptions.SolutionValues) == GameLoadOptions.SolutionValues)
                        {
                            _squares[position].UpdateDismissedValue(Convert.ToInt32(c.ToString()), false);
                        }
                        if ((options & GameLoadOptions.UserValues) == GameLoadOptions.UserValues)
                        {
                            _squares[position].UpdateUserDismissedValue(Convert.ToInt32(c.ToString()), false);
                        }
                    }
                }
                else if (c == '.')
                {
                    position++;
                }
                else if (c == '(')
                {
                    readingNonDismissedValues = true;

                    if ((options & GameLoadOptions.SolutionValues) == GameLoadOptions.SolutionValues)
                    {
                        _squares[position].UpdateDismissedValues(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, true);
                    }
                    if ((options & GameLoadOptions.UserValues) == GameLoadOptions.UserValues)
                    {
                        _squares[position].UpdateUserDismissedValues(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, true);
                    }
                }
                else if (c == ')')
                {
                    readingNonDismissedValues = false;
                    position++;
                }
            }
        }
Exemplo n.º 3
0
 public string SaveGame(bool addNewLines, GameLoadOptions options)
 {
     return(SaveGame(addNewLines, options, false));
 }