Exemple #1
0
        private static IEnumerable <SearchState> CreateSearches(ICubieCube baseCube, DataSource dataSource)
        {
            for (byte rot = 0; rot <= 2; rot++)
            {
                foreach (var inv in new[] { false, true })
                {
                    MutableCubieCube cb;

                    if (rot == 0)
                    {
                        // no rotation
                        cb = baseCube.Clone();
                    }
                    else if (rot == 1)
                    {
                        // conjugation by 120° rotation
                        cb = Symmetries.Basic.Cubes[32].Clone();
                        cb.Multiply(baseCube);
                        cb.Multiply(Symmetries.Basic.Cubes[16]);
                    }
                    else if (rot == 2)
                    {
                        // conjugation by 240° rotation
                        cb = Symmetries.Basic.Cubes[16].Clone();
                        cb.Multiply(baseCube);
                        cb.Multiply(Symmetries.Basic.Cubes[32]);
                    }
                    else
                    {
                        throw new ArgumentException("Rotation should be 1,2, or 3");
                    }


                    if (inv)
                    {
                        cb = cb.Invert(); // invert cube
                    }
                    var coCube = cb.ToCoordinateCube(dataSource);

                    var search = new SearchState(inv, rot, coCube, ImmutableLinkedList <Move> .Empty, false);

                    yield return(search);
                }
            }
        }
        public void MaybeAddSearch(SearchState searchState)
        {
            if (_seenCubes.TryAdd(searchState.Cube, searchState.MovesSoFar.Count))
            {
                DepthCubes.Add(searchState);
                return;
            }

            while (true)
            {
                if (!_seenCubes.TryGetValue(searchState.Cube, out var previousMoves) ||
                    previousMoves <= searchState.MovesSoFar.Count)
                {
                    return;                                                //Previous solution is as good as or better
                }
                if (!_seenCubes.TryUpdate(searchState.Cube, searchState.MovesSoFar.Count, previousMoves))
                {
                    continue;                                                                                       //Failure updating - try again
                }
                DepthCubes.Add(searchState);
                return;
            }
        }