Exemple #1
0
        /// <summary>
        /// Recursively solves the puzzle using a brute-force backtracking algorithm.
        /// </summary>
        /// <param name="row"></param>
        /// <param name="col"></param>
        /// <returns></returns>
        private async Task <bool> PerformSolve(int row, int col)
        {
            if (IsPuzzleSolved())
            {
                // Puzzle has been solved - return
                return(true);
            }
            else if (Puzzle.Tiles[row, col].Value != 0)
            {
                // Tile is already solved - proceed to next
                return(await NextSolve(row, col));
            }
            else
            {
                // Attempt each possible value 1-9 in this cell
                for (var value = 1; value <= Puzzle.Height; value++)
                {
                    // This is a valid placement of this value, causing no issues with constraints
                    if (ValidateValue(row, col, value))
                    {
                        // Set the tile value and attempt to recursively solve the rest of the board.
                        Puzzle.Tiles[row, col].Value = value;
                        TileUpdated?.Invoke(this, new TileUpdatedEventArgs {
                            Tile = Puzzle.Tiles[row, col]
                        });

                        // Intentional delay inbetween solving each tile
                        if (WaitFor > 0)
                        {
                            await Task.Delay(WaitFor);
                        }

                        if (await NextSolve(row, col))
                        {
                            return(true);
                        }

                        // If any position comes back with an invalid move, rollback this value and try the next
                        Puzzle.Tiles[row, col].Value = 0;
                        TileUpdated?.Invoke(this, new TileUpdatedEventArgs {
                            Tile = Puzzle.Tiles[row, col]
                        });
                    }
                }
            }

            return(false);
        }
        public void AddTileMap(Image image)
        {
            Bitmap bitmap = new Bitmap(image);

            //int currentImageID = TileSetImages.Count + StartIndex;
            //TileSetImages.Add(bitmap);
            for (int i = 0; i <= image.Height - TileSize; i += TileSize)
            {
                for (int j = 0; j <= image.Width - TileSize; j += TileSize)
                {
                    Rectangle target = new Rectangle(j, i, TileSize, TileSize);
                    Bitmap    tmp    = bitmap.Clone(target, System.Drawing.Imaging.PixelFormat.DontCare);
                    Tiles.Add(tmp);
                    TilesData.Add(new TileData(this.StartIndex + Tiles.Count - 1, i / TileSize, j / TileSize));
                }
            }
            bitmap.Dispose();
            if (TileUpdated != null)
            {
                TileUpdated.Invoke(this, new EventArgs());
            }
        }
Exemple #3
0
 protected void OnTileUpdated(Tile tile)
 {
     TileUpdated.Raise(this, new TileUpdatedEventArgs(tile));
 }