예제 #1
0
        /// <summary>
        /// Gets the <see cref="BoardRow"/> with the specified zero based row index.
        /// </summary>
        /// <value>
        /// The <see cref="BoardRow"/> at index <paramref name="zeroBasedRowIndex"/>.
        /// </value>
        /// <param name="zeroBasedRowIndex">Index of the row.</param>
        public BoardRow this[int zeroBasedRowIndex]
        {
            get
            {
                ContractExtensions.IsNotDisposed(this, this.IsDisposed);
                ContractExtensions.IsValidIndex(zeroBasedRowIndex, "zeroBasedRowIndex");
                Contract.EndContractBlock();

                return(this.rows[zeroBasedRowIndex]);
            }
        }
예제 #2
0
        /// <summary>
        /// Resets a cell's value to unset.
        /// </summary>
        /// <param name="zeroBasedRowIndex">Index of the row.</param>
        /// <param name="zeroBasedColumnIndex">Index of the column.</param>
        public void ResetCell(int zeroBasedRowIndex, int zeroBasedColumnIndex)
        {
            ContractExtensions.IsNotDisposed(this, this.IsDisposed);
            ContractExtensions.IsValidIndex(zeroBasedRowIndex, "zeroBasedRowIndex");
            ContractExtensions.IsValidIndex(zeroBasedColumnIndex, "zeroBasedColumnIndex");
            Contract.EndContractBlock();

            this.contentLock.EnterWriteLock();
            try
            {
                this.content[Board.CalculateIndexFromRowAndColumn(zeroBasedRowIndex, zeroBasedColumnIndex)] = 0;
            }
            finally
            {
                this.contentLock.ExitWriteLock();
            }
        }
예제 #3
0
        /// <summary>
        /// Gets the value of a cell.
        /// </summary>
        /// <param name="zeroBasedRowIndex">Index of the row.</param>
        /// <param name="zeroBasedColumnIndex">Index of the column.</param>
        /// <returns>Value of the cell at the specified row and column index.</returns>
        /// <remarks>
        /// Zero is returned for an unset cell.
        /// </remarks>
        public byte GetCell(int zeroBasedRowIndex, int zeroBasedColumnIndex)
        {
            ContractExtensions.IsNotDisposed(this, this.IsDisposed);
            ContractExtensions.IsValidIndex(zeroBasedRowIndex, "zeroBasedRowIndex");
            ContractExtensions.IsValidIndex(zeroBasedColumnIndex, "zeroBasedColumnIndex");
            Contract.EndContractBlock();

            // Note that we use a read lock this time. This makes sure that many threads
            // can read the board's content at the same time.
            this.contentLock.EnterReadLock();
            try
            {
                return(this.content[Board.CalculateIndexFromRowAndColumn(zeroBasedRowIndex, zeroBasedColumnIndex)]);
            }
            finally
            {
                this.contentLock.ExitReadLock();
            }
        }
예제 #4
0
        /// <summary>
        /// Converts <see cref="Board"/> instance to bytes.
        /// </summary>
        /// <param name="source">Source board.</param>
        /// <returns>Board data as a byte array.</returns>
        public static byte[] ToBytes(Board source)
        {
            ContractExtensions.IsNotNull(source, "source");
            ContractExtensions.IsNotDisposed(source, source.IsDisposed);
            Contract.EndContractBlock();

            var result = new byte[9 * 9];

            source.contentLock.EnterReadLock();
            try
            {
                source.content.CopyTo(result, 0);
                return(result);
            }
            finally
            {
                source.contentLock.ExitReadLock();
            }
        }
예제 #5
0
        public byte[] GetCopyOfRow(int zeroBasedRowIndex)
        {
            ContractExtensions.IsNotDisposed(this, this.IsDisposed);
            ContractExtensions.IsValidIndex(zeroBasedRowIndex, "zeroBasedRowIndex");
            Contract.EndContractBlock();

            var result = new byte[9];

            this.contentLock.EnterReadLock();
            try
            {
                Array.Copy(this.content, zeroBasedRowIndex * 9, result, 0, 9);
            }
            finally
            {
                this.contentLock.ExitReadLock();
            }

            return(result);
        }
예제 #6
0
        /// <summary>
        /// Tries the set the value of a cell.
        /// </summary>
        /// <param name="zeroBasedRowIndex">Index of the row.</param>
        /// <param name="zeroBasedColumnIndex">Index of the column.</param>
        /// <param name="value">The new value.</param>
        /// <returns><c>True</c> if the value could be set successfully, otherwise <c>false</c>.</returns>
        /// <remarks>
        /// <para>
        /// Note that <paramref name="value"/> must not be zero. If you want to unset a cell,
        /// use <see cref="ResetCell"/> instead.
        /// </para>
        /// <para>
        /// Note the TryXXX naming pattern. <see cref="TrySetCell"/> does the same as <see cref="SetCell"/>
        /// except that it does not throw an exception if the value could not be set.
        /// </para>
        /// </remarks>
        public bool TrySetCell(int zeroBasedRowIndex, int zeroBasedColumnIndex, byte value)
        {
            ContractExtensions.IsNotDisposed(this, this.IsDisposed);
            ContractExtensions.IsValidIndex(zeroBasedRowIndex, "zeroBasedRowIndex");
            ContractExtensions.IsValidIndex(zeroBasedColumnIndex, "zeroBasedColumnIndex");
            ContractExtensions.IsValidValue(value, "value");
            Contract.EndContractBlock();

            // Note that we use a write lock this time. This makes sure that only one thread
            // can write to the board bytes at the same time.
            this.contentLock.EnterWriteLock();
            try
            {
                // Call internal implementation.
                return(Board.TrySetCellInternal(this.content, zeroBasedRowIndex, zeroBasedColumnIndex, value));
            }
            finally
            {
                this.contentLock.ExitWriteLock();
            }
        }