예제 #1
0
        private bool TryFind(List <Tile> tiles, out int index)
        {
            BlankTileIndexFinder blankTileIndexFinder = new BlankTileIndexFinder();
            bool found = blankTileIndexFinder.TryFind(tiles, out index);

            return(found);
        }
예제 #2
0
        public virtual Board Build(int size)
        {
            if (size < 1)
            {
                throw new ArgumentException($"Board size must be bigger then 0, got {size}");
            }
            List <Tile> tiles                  = GetLegalBoardTiles(size);
            int         blankIndex             = blankTileIndexFinder.TryFind(tiles, out int index) ? index: throw new Exception("Illegal number of tiles!");
            int         numberOfMisplacedTiles = misplacedTilesCounter.Count(tiles);
            Board       board                  = new Board
            {
                Tiles               = tiles,
                BlankIndex          = blankIndex,
                Size                = size,
                TotalMisplacedTiles = numberOfMisplacedTiles,
                IsSolved            = numberOfMisplacedTiles == 0
            };

            return(board);
        }
예제 #3
0
        public virtual bool Validate(List <Tile> tiles, int size)
        {
            bool isValid = blankTileIndexFinder.TryFind(tiles, out int blankIndex);

            if (isValid)
            {
                int  numberOfInversions       = inversionsCounter.Count(tiles);
                bool isNumberOfInversionsEven = numberOfInversions % 2 == 0;
                bool isSizeEven = size % 2 == 0;

                if (isSizeEven)
                {
                    int  blankIndexRow      = blankIndex / size;
                    bool isBlankOnAnEvenRow = blankIndexRow % 2 == 0;

                    isValid = isBlankOnAnEvenRow ^ isNumberOfInversionsEven;
                }
                else
                {
                    isValid = isNumberOfInversionsEven;
                }
            }
            return(isValid);
        }