mycell.cs

来自「Sudoku as a CSP: Using algorithms and te」· CS 代码 · 共 125 行

CS
125
字号
using System;
using System.Collections.Generic;
using System.Text;

namespace MySudoku
{
    /// <summary>
    /// ...
    /// </summary>
    class MyCell
    {
        public string Value;
        public List<Pos>[]  Units;
        public List<Pos>  Peers;
        public bool assigned;

        #region Constractors :  
        /// <summary>
        /// Initializes a new instance of the <see cref="MyCell"/> class.
        /// </summary>
        /// <param name="cell">The cell.</param>
        public MyCell(MyCell cell)
        {
            Value = cell.Value;
            Units = new List<Pos>[3];
            Peers = new List<Pos>();
            assigned = cell.assigned;

            for (int i = 0; i < 3; i++)
            {
                Units[i] = new List<Pos>();
                for (int j = 0; j < cell.Units[i].Count; j++)
                    Units[i].Add(cell.Units[i][j]);
            }

            for (int i = 0; i < cell.Peers.Count; i++)
                Peers.Add(cell.Peers[i]);

        }
        /// <summary>
        /// Initializes a new instance of the <see cref="MyCell"/> class.
        /// </summary>
        /// <param name="val">The val.</param>
        /// <param name="ln">The ln.</param>
        /// <param name="col">The col.</param>
        /// <param name="gridSize">Size of the grid.</param>
        public MyCell(string val, int ln, int col, int gridSize)
        {
            Value = val;
            Peers = new List<Pos>();
            Units = new List<Pos>[3];
            
            
            assigned = (val.Length == 1);

            SetUnitsAndPeersToCell(ln, col, gridSize);

        }
        #endregion
       
        private void SetUnitsAndPeersToCell(int ln, int col, int gridSize)
        {
            for (int i = 0; i < 3; i++)
            {
                Units[i] = new List<Pos>();
            }

            SetLineUnitAndPeers(ln, col, gridSize);

            SetRowUnitAndPeers(ln, col, gridSize);

            SetSquareUnitAndPeers(ln, col, gridSize);
        }

        private void SetSquareUnitAndPeers(int ln, int col, int gridSize)
        {
            int dx = col - (col % (int)Math.Sqrt(gridSize));
            int dy = ln - (ln % (int)Math.Sqrt(gridSize));

            for (int i = dy; i < dy + (int)Math.Sqrt(gridSize); i++)
                for (int j = dx; j < dx + (int)Math.Sqrt(gridSize); j++)
                {
                    Pos p = new Pos();
                    p.ln = i;
                    p.col = j;

                    Units[2].Add(p);
                    if ((i == ln) || (j == col)) continue;
                    Peers.Add(p);

                }
        }

        private void SetRowUnitAndPeers(int ln, int col, int gridSize)
        {
            for (int i = 0; i < gridSize; i++)
            {
                Pos p = new Pos();
                p.ln = i;
                p.col = col;
                Units[1].Add(p);
                if (i == ln) continue;
                Peers.Add(p);
            }
        }

        private void SetLineUnitAndPeers(int ln, int col, int gridSize)
        {
            for (int i = 0; i < gridSize; i++)
            {
                Pos p = new Pos();
                p.ln = ln;
                p.col = i;

                Units[0].Add(p);
                if (i == col) continue;
                Peers.Add(p);
            }
        }
       
    

    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?