Esempio n. 1
0
        /// <summary>
        /// Exhaustively try all possible values for the tiles, using constraints to
        /// reduce the search space
        /// </summary>
        /// <param name="cpb"></param>
        /// <param name="depth"></param> Parameter debugging and curiousity
        /// <returns></returns>
        static private SearchResult search(CPBoard cpb, int depth)
        {
            var freeTile = cpb.GetFreeTile();

            // No free tile available
            if (freeTile.Value == null)
            {
                return(new SearchResult(cpb, IsSolved(cpb)));
            }
            var possibleValues = freeTile.Value;

            foreach (var c in possibleValues)
            {
                // Make a copy in case we reach contradiction and need to revert back
                var  copy      = cpb.Copy();
                bool didAssign = Assign(copy, freeTile.Key, c.ToString());
                CDebug.Assert(copy.Get(freeTile.Key) != cpb.Get(freeTile.Key), "Original CPB mutated!");
                if (didAssign)
                {
                    SearchResult sr = search(copy, depth + 1);
                    if (sr.DidSolve)
                    {
                        return(sr);
                    }
                }
            }
            return(new SearchResult(cpb, false));
        }
Esempio n. 2
0
        public void TestCopy()
        {
            var     cpb  = new CPBoard(bSolvableByCP);
            CPBoard copy = cpb.Copy();

            // Changing original does not mutate copy
            Assert.NotEqual("4", copy.Get(0));
            cpb.Set(0, "4");
            Assert.NotEqual("4", copy.Get(0));

            // Chaning copy does not change original
            Assert.NotEqual("2", cpb.Get(1));
            copy.Set(1, "2");
            Assert.NotEqual("2", cpb.Get(1));
        }