示例#1
0
        public static void IsValidValue(int value, string argumentName)
        {
            ContractExtensions.IsArgumentInRange(value > 0, argumentName, "Value has to be > 0");
            ContractExtensions.IsArgumentInRange(value <= 9, argumentName, "Value has to be <= 9");

            Contract.EndContractBlock();
        }
示例#2
0
        public static void IsValidIndex(int zeroBasedIndex, string argumentName)
        {
            ContractExtensions.IsArgumentInRange(zeroBasedIndex >= 0, argumentName, "Index has to be >= 0");
            ContractExtensions.IsArgumentInRange(zeroBasedIndex < 9, argumentName, "Index has to be < 9");

            Contract.EndContractBlock();
        }
示例#3
0
        public static Board FromBytes(byte[] source)
        {
            // The following lines represent the contracts that are checked even in release builds.
            // Commonly used contracts were extracted into the ContractExtensions helper class.
            // Note the use of the ContractArgumentValidator attribute in the helper methods.

            // Note that you can set the 'Emit contracts into XML doc file' project option for
            // code contracts. If you do so, Sandcastle will include the contract in the generated
            // documentation.

            ContractExtensions.IsNotNull(source, "source");
            ContractExtensions.IsArgumentInRange(source.Length == 9 * 9, "source", "Source does not have correct size (9 * 9 = 81 elements)");
            if (Contract.ForAll(source, cellValue => cellValue < 0 || cellValue > 9))
            {
                throw new ArgumentException("Source contains invalid values");
            }

            Contract.EndContractBlock();

            var result = new Board();

            try
            {
                for (byte rowIndex = 0, index = 0; rowIndex < 9; rowIndex++)
                {
                    for (byte columnIndex = 0; columnIndex < 9; columnIndex++, index++)
                    {
                        // Store content of source[index] in a variable because it is accessed multiple times.
                        var cellValue = source[index];

                        // Ignore zero
                        if (cellValue != 0)
                        {
                            // Note that it is not necessary to protect board bytes because result
                            // has just been created and nobody else has a reference on it.
                            if (!Board.TrySetCellInternal(result.content, rowIndex, columnIndex, cellValue))
                            {
                                throw new BoardException(string.Format(
                                                             CultureInfo.InvariantCulture,
                                                             "Specified source contains invalid value {0} in cell (row {1}, column {2})",
                                                             cellValue,
                                                             rowIndex,
                                                             columnIndex));
                            }
                        }
                    }
                }
            }
            catch
            {
                // Dispose created board in case of an exception
                result.Dispose();
                throw;
            }

            return(result);
        }