示例#1
0
 public Group()
 {
     Tiles = new Tile[9];
     Solutions = new List<int[]>();
 }
示例#2
0
        public Puzzle(string filename)
        {
            Rows = new Group[9];
            Columns = new Group[9];
            Grids = new Group[9];
            Solutions = new List<int[,]>();

            int rowIndex = 0;
            using (StreamReader sr = new StreamReader(filename))
            {
                while (!sr.EndOfStream)
                {
                    string line = sr.ReadLine();
                    Group row = new Group();

                    for (int i = 0; i < 9; i++)
                    {
                        Tile tile = new Tile();

                        char c = line[i];
                        if (c == 'B')
                        {
                            tile.Given = false;
                        }
                        else
                        {
                            tile.Given = true;
                            tile.Value = c - 48; // Convert to "face value" of char; 0 is ascii 48.
                        }

                        row.Tiles[i] = tile;
                        tile.RowIndex = rowIndex;
                    }

                    Rows[rowIndex] = row;
                    rowIndex++;
                }
            }

            // Should have all rows at this point; assign tiles in rows to columns
            for (int col = 0; col < 9; col++)
            {
                Group column = new Group();

                for (int row = 0; row < 9; row++)
                {
                    column.Tiles[row] = Rows[row].Tiles[col];
                    column.Tiles[row].ColIndex = col;
                }

                Columns[col] = column;
            }

            // Create the grids.
            for (int grid = 0; grid < 9; grid++)
            {
                Group g = new Group();
                Grids[grid] = g;
            }

            // Assign tiles to the grids.
            // There's probably an elegant way to do what comes next, but I'm dumb...
            Grids[0].Tiles[0] = Rows[0].Tiles[0];
            Grids[0].Tiles[1] = Rows[0].Tiles[1];
            Grids[0].Tiles[2] = Rows[0].Tiles[2];
            Grids[0].Tiles[3] = Rows[1].Tiles[0];
            Grids[0].Tiles[4] = Rows[1].Tiles[1];
            Grids[0].Tiles[5] = Rows[1].Tiles[2];
            Grids[0].Tiles[6] = Rows[2].Tiles[0];
            Grids[0].Tiles[7] = Rows[2].Tiles[1];
            Grids[0].Tiles[8] = Rows[2].Tiles[2];

            Grids[1].Tiles[0] = Rows[0].Tiles[3];
            Grids[1].Tiles[1] = Rows[0].Tiles[4];
            Grids[1].Tiles[2] = Rows[0].Tiles[5];
            Grids[1].Tiles[3] = Rows[1].Tiles[3];
            Grids[1].Tiles[4] = Rows[1].Tiles[4];
            Grids[1].Tiles[5] = Rows[1].Tiles[5];
            Grids[1].Tiles[6] = Rows[2].Tiles[3];
            Grids[1].Tiles[7] = Rows[2].Tiles[4];
            Grids[1].Tiles[8] = Rows[2].Tiles[5];

            Grids[2].Tiles[0] = Rows[0].Tiles[6];
            Grids[2].Tiles[1] = Rows[0].Tiles[7];
            Grids[2].Tiles[2] = Rows[0].Tiles[8];
            Grids[2].Tiles[3] = Rows[1].Tiles[6];
            Grids[2].Tiles[4] = Rows[1].Tiles[7];
            Grids[2].Tiles[5] = Rows[1].Tiles[8];
            Grids[2].Tiles[6] = Rows[2].Tiles[6];
            Grids[2].Tiles[7] = Rows[2].Tiles[7];
            Grids[2].Tiles[8] = Rows[2].Tiles[8];

            Grids[3].Tiles[0] = Rows[3].Tiles[0];
            Grids[3].Tiles[1] = Rows[3].Tiles[1];
            Grids[3].Tiles[2] = Rows[3].Tiles[2];
            Grids[3].Tiles[3] = Rows[4].Tiles[0];
            Grids[3].Tiles[4] = Rows[4].Tiles[1];
            Grids[3].Tiles[5] = Rows[4].Tiles[2];
            Grids[3].Tiles[6] = Rows[5].Tiles[0];
            Grids[3].Tiles[7] = Rows[5].Tiles[1];
            Grids[3].Tiles[8] = Rows[5].Tiles[2];

            Grids[4].Tiles[0] = Rows[3].Tiles[3];
            Grids[4].Tiles[1] = Rows[3].Tiles[4];
            Grids[4].Tiles[2] = Rows[3].Tiles[5];
            Grids[4].Tiles[3] = Rows[4].Tiles[3];
            Grids[4].Tiles[4] = Rows[4].Tiles[4];
            Grids[4].Tiles[5] = Rows[4].Tiles[5];
            Grids[4].Tiles[6] = Rows[5].Tiles[3];
            Grids[4].Tiles[7] = Rows[5].Tiles[4];
            Grids[4].Tiles[8] = Rows[5].Tiles[5];

            Grids[5].Tiles[0] = Rows[3].Tiles[6];
            Grids[5].Tiles[1] = Rows[3].Tiles[7];
            Grids[5].Tiles[2] = Rows[3].Tiles[8];
            Grids[5].Tiles[3] = Rows[4].Tiles[6];
            Grids[5].Tiles[4] = Rows[4].Tiles[7];
            Grids[5].Tiles[5] = Rows[4].Tiles[8];
            Grids[5].Tiles[6] = Rows[5].Tiles[6];
            Grids[5].Tiles[7] = Rows[5].Tiles[7];
            Grids[5].Tiles[8] = Rows[5].Tiles[8];

            Grids[6].Tiles[0] = Rows[6].Tiles[0];
            Grids[6].Tiles[1] = Rows[6].Tiles[1];
            Grids[6].Tiles[2] = Rows[6].Tiles[2];
            Grids[6].Tiles[3] = Rows[7].Tiles[0];
            Grids[6].Tiles[4] = Rows[7].Tiles[1];
            Grids[6].Tiles[5] = Rows[7].Tiles[2];
            Grids[6].Tiles[6] = Rows[8].Tiles[0];
            Grids[6].Tiles[7] = Rows[8].Tiles[1];
            Grids[6].Tiles[8] = Rows[8].Tiles[2];

            Grids[7].Tiles[0] = Rows[6].Tiles[3];
            Grids[7].Tiles[1] = Rows[6].Tiles[4];
            Grids[7].Tiles[2] = Rows[6].Tiles[5];
            Grids[7].Tiles[3] = Rows[7].Tiles[3];
            Grids[7].Tiles[4] = Rows[7].Tiles[4];
            Grids[7].Tiles[5] = Rows[7].Tiles[5];
            Grids[7].Tiles[6] = Rows[8].Tiles[3];
            Grids[7].Tiles[7] = Rows[8].Tiles[4];
            Grids[7].Tiles[8] = Rows[8].Tiles[5];

            Grids[8].Tiles[0] = Rows[6].Tiles[6];
            Grids[8].Tiles[1] = Rows[6].Tiles[7];
            Grids[8].Tiles[2] = Rows[6].Tiles[8];
            Grids[8].Tiles[3] = Rows[7].Tiles[6];
            Grids[8].Tiles[4] = Rows[7].Tiles[7];
            Grids[8].Tiles[5] = Rows[7].Tiles[8];
            Grids[8].Tiles[6] = Rows[8].Tiles[6];
            Grids[8].Tiles[7] = Rows[8].Tiles[7];
            Grids[8].Tiles[8] = Rows[8].Tiles[8];

            for (int grid = 0; grid < 9; grid++)
            {
                for (int tile = 0; tile < 9; tile++)
                {
                    // Save grid reference for easy access elsewhere.
                    Grids[grid].Tiles[tile].GridIndex = grid;
                }
            }
        }