Beispiel #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));
        }
        public void TestFreeTile()
        {
            var cpb = new CPBoard(bSolvableByCP);
            var ft  = cpb.GetFreeTile();

            Assert.Equal(0, ft.Key);
            Assert.Equal("123456789", ft.Value);  // No elimination has occured yet

            cpb = new CPBoard(bSolved);
            ft  = cpb.GetFreeTile();
            Assert.Equal(0, ft.Key);  // TODO: Really need maybe type, 0 is not a good default
            Assert.Null(ft.Value);
        }