Exemplo n.º 1
0
 public PuzzleDto Map(PuzzleViewModel source)
 {
     return(new PuzzleDto {
         Name = source.Name,
         Difficulty = (Difficulty)Enum.Parse(typeof(Difficulty), source.Difficulty)
     });
 }
Exemplo n.º 2
0
        private void MainWindow_Activated(object?sender, EventArgs e)
        {
            // this app's light/dark theme setting over rides the OS setting
            // so only check if the title bar and borders setting has changed
            PuzzleViewModel vm = (PuzzleViewModel)DataContext;

            vm.AccentTitleBar = WindowsThemeHelper.ShowAccentColorOnTitleBarsAndWindowBorders();
        }
Exemplo n.º 3
0
        public PartialViewResult SolvePuzzle(PuzzleViewModel puzzleVM)
        {
            if (puzzleVM.PuzzleTileList.Any())
            {
                var puzzleService = new Services.PuzzleService();
                var solvedPuzzle  = puzzleService.SolvePuzzle(puzzleVM);
                return(PartialView(solvedPuzzle));
            }

            return(PartialView());
        }
        public IActionResult Create(PuzzleViewModel vm)
        {
            var       mapper = new PuzzleMapper();
            PuzzleDto entity = mapper.Map(vm);

            using (_context) {
                _context.Puzzles.Add(entity);
                _context.SaveChanges();
            }
            return(RedirectToAction("index"));
        }
        public /*JsonResult*/ PartialViewResult SolvePuzzle(PuzzleViewModel puzzleVM)
        {
            if (puzzleVM.TileIndexList.Any())
            {
                var puzzleService = new Services.PuzzleService();
                var solvedPuzzle  = puzzleService.SolvePuzzle(puzzleVM);
                //return new JsonResult() { Data = solvedPuzzle };
                return(PartialView(solvedPuzzle));
            }

            //return new JsonResult();
            return(PartialView());
        }
Exemplo n.º 6
0
 public void Initialize(PuzzleViewModel puzzleVM)
 {
     this.PuzzleVM                = puzzleVM;
     TileList                     = GetTiles();
     TotalRotationTileList        = GetTotalRotationList();
     SubmittedPuzzleTilesIndices  = GetSubmittedPuzzleTilesIndices();
     SubmittedPuzzleTileCount     = SubmittedPuzzleTilesIndices.Count;
     PuzzleIndexCounter           = GetPuzzleIndexCounter();
     DepthToIndex                 = GetDepthToIndexDictionary();
     CheckedTileDictionary        = GetCheckedTileDictionary();
     DynamicCheckedTileDictionary = GetDynamicCheckedTileDictionary();
     AmountOfTotalSolutions       = GetTotalAmountOfSolutions();
     AmountOfMaximumTriesPerTile  = GetAmountOfMaximumTriesPerTile();
     OriginalDepthCounter         = GetOriginalDepthCounter();
     SimpleConditionsList         = GetSimpleConditionsList();
 }
Exemplo n.º 7
0
        public static PuzzleViewModel FakePuzzleBuilder(string title)
        {
            var puzzleVm = new PuzzleViewModel
            {
                Title = title,
                Words =
                    new List <string>
                {
                    "First",
                    "Second",
                    "Third",
                    "Forth",
                    "Fifth",
                    "Sixth"
                }
            };

            return(puzzleVm);
        }
Exemplo n.º 8
0
        public List <string> ValidatePuzzle(PuzzleViewModel puzzleVM)
        {
            var returnValue = new List <string>();

            if (puzzleVM.PuzzleTileList.Count(s => s.SimpleTileIndexList.Any(t =>
                                                                             t.SpecialCondition.Condition == Condition.Flower ||
                                                                             t.SpecialCondition.Condition == Condition.Gate ||
                                                                             t.SpecialCondition.Condition == Condition.Butterfly ||
                                                                             t.SpecialCondition.Condition == Condition.Tree)) % 2 == 1)
            {
                returnValue.Add(String.Format("Één van de volgende symbolen komt een oneven aantal voor: Flower, Tower, Butterfly, Tree"));
            }

            if (puzzleVM.PuzzleTileList.Count(s => s.SimpleTileIndexList.Any(t =>
                                                                             t.SpecialCondition.Condition == Condition.YinYang ||
                                                                             t.SpecialCondition.Condition == Condition.Pagoda)) > 1)
            {
                returnValue.Add(String.Format("Één van de volgende symbolen komt meer dan één keer voor: YinYang, Pagoda"));
            }

            return(returnValue);
        }
        public DesignPuzzleBoardViewModel()
            : base(0, new FakePuzzlesService(), new FakeUserSevice())
        {
            CurrentUser = "******";

            PuzzleViewModel = new PuzzleViewModel()
            {
                Group = "Science", Title = "Level One"
            };
            Words = base.PuzzlesService.GetOrdereredWordsForPuzzle(0, CurrentUser);

            //SelectedWord = (from word in Words
            //                where word.Word.Equals("india", StringComparison.OrdinalIgnoreCase)
            //                select word).FirstOrDefault();

            GameIsRunning = true;

            //GameCountDown = "00:00:00";

            AcrossAndDownVisible = true;

            AddWordsToBoard();
        }
Exemplo n.º 10
0
        public List <string> ValidatePuzzle(PuzzleViewModel puzzleVM)
        {
            var returnValue           = new List <string>();
            var totalPuzzleConditions = puzzleVM.TileIndexList.SelectMany(s => s.SimpleTileIndexList);

            var IconConditions = new HashSet <Condition>()
            {
                Condition.Butterfly, Condition.Flower, Condition.Gate, Condition.Tree
            };

            foreach (var icon in IconConditions)
            {
                if (totalPuzzleConditions.Count(s => s.Condition == icon) >= 3)
                {
                    returnValue.Add(String.Format("Het volgende symbool komt een te vaak voor: {0}", icon));
                }
                else if (totalPuzzleConditions.Count(s => s.Condition == icon) % 2 != 0)
                {
                    returnValue.Add(String.Format("Het volgende symbool komt een oneven aantal voor: {0}", icon));
                }
            }

            if (totalPuzzleConditions.Count(t =>
                                            t.Condition == Condition.YinYang) > 2)
            {
                returnValue.Add(String.Format("Één van de volgende symbolen komt meer dan twee keer voor: YinYang"));
            }

            if (totalPuzzleConditions.Count(t =>
                                            t.Condition == Condition.Pagoda) > 2)
            {
                returnValue.Add(String.Format("Één van de volgende symbolen komt meer dan twee keer voor: Pagode"));
            }

            return(returnValue);
        }
Exemplo n.º 11
0
        public PuzzleViewModel GetPuzzleVM()
        {
            var puzzleVM = new PuzzleViewModel();

            return(puzzleVM);
        }
Exemplo n.º 12
0
        public SolvedPuzzleViewModel SolvePuzzle(PuzzleViewModel puzzleVM)
        {
            var solvedPuzzleVM = new SolvedPuzzleViewModel();

            solvedPuzzleVM.ErrorList = ValidatePuzzle(puzzleVM);
            if (solvedPuzzleVM.ErrorList.Any())
            {
                return(solvedPuzzleVM);
            }
            Initiator.Initialize(puzzleVM);
            UsedTileList.Clear();
            UsedTileDictionary = TileList.ToDictionary(s => s.TileNumber, s => false);

            var breakCount = 0;
            var tries      = new List <String>();

            var start = DateTime.Now;

            //After the first run, the DepthCounter dictionary should probably be updated to be * 2?
            //while (!solvedPuzzleVM.Solved && CheckedTileDictionary.Values.All(s => s == AmountOfMaximumTriesPerTile))
            while (!solvedPuzzleVM.Solved && AmountOfCheckedSolutions < AmountOfTotalSolutions && breakCount <= AmountOfTotalSolutions)
            {
                breakCount++;
                foreach (var tile in Initiator.TotalRotationTileList)
                {
                    if (solvedPuzzleVM.Solved)
                    {
                        break;
                    }
                    if (UsedTileDictionary[tile.TileNumber])
                    {
                        continue;
                    }

                    if (!UsedTileDictionary[tile.TileNumber])
                    {
                        UsedTileList.Add(tile);
                        UsedTileDictionary[tile.TileNumber] = true;
                    }

                    //var puzzleFieldCount = 0;
                    foreach (var puzzleTile in puzzleVM.TileIndexList.Where(s => !UsedPuzzleTilesIndices.Contains(s.Index)))
                    {
                        //There already is a tile on this puzzlefield
                        if (UsedPuzzleTilesIndices.Contains(puzzleTile.Index))
                        {
                            continue;
                        }

                        if (tile.PuzzleIndex >= 0)
                        {
                            break;
                        }
                        tile.PuzzleDepthCounter = PuzzleIndexCounter[puzzleTile.Index];
                        var tileKey = new UsedTileDictionaryKey(puzzleTile.Index, tile.TileNumber, tile.Degrees);

                        //var usedTileNumbers = new HashSet<int>(UsedTileList.Select(a => a.TileNumber));
                        //var tilesInSameLayer = CheckedTileDictionary.Keys.Where(s => s.PuzzleIndex == tileKey.PuzzleIndex && !usedTileNumbers.Contains(s.TileNumber));
                        //var lessAmount = tilesInSameLayer.Any(a => CheckedTileDictionary[a] < DynamicCheckedTileDictionary[tileKey]);

                        if (CheckedTileDictionary[tileKey] >= DynamicCheckedTileDictionary[tileKey])
                        {
                            RemoveAndResetPuzzleTile(tile);
                            //UsedPuzzleTilesIndices.Remove(tile.PuzzleIndex);
                            //UsedTileList.Remove(tile);
                            //UsedTileDictionary[tile.TileNumber] = false;
                            //tile.PuzzleIndex = -1;
                            //tile.PuzzleDepthCounter = 0;
                            break;
                        }

                        tile.PuzzleIndex = puzzleTile.Index;
                        UsedPuzzleTilesIndices.Add(puzzleTile.Index);

                        if (puzzleVM.UseBailout)
                        {
                            if (UsedTileList.Count >= 1 && UsedTileList.Count < SubmittedPuzzleTileCount)
                            {
                                if (DoEarlyBailOut())
                                {
                                    break;
                                }
                            }
                        }

                        if (UsedTileList.Count == SubmittedPuzzleTileCount)
                        {
                            AmountOfCheckedSolutions++;
                            //var solveString = String.Format("Trying to solve with: {0}", String.Join(" AND ", UsedTileList.OrderBy(s => s.PuzzleIndex).Select(s => s.ToString())));
                            //tries.Add(solveString);

                            FillPuzzleRoads(UsedTileList);
                            if (DoesDefinitiveRoadListSolvePuzzle(Initiator.SimpleConditionsList))
                            {
                                var newList = new List <Tile>();
                                //solvedPuzzleVM.Solved = true;
                                if (solvedPuzzleVM.TileSet.Count <= 2)
                                {
                                    foreach (var ut in UsedTileList)
                                    {
                                        var newTile = new Tile(ut.TileNumber, ut.Degrees);
                                        newTile.PuzzleIndex = ut.PuzzleIndex;
                                        newList.Add(newTile);
                                    }

                                    solvedPuzzleVM.TileSet.Add(newList);
                                }
                            }



                            //var allKeys = UsedTileList
                            //    .Select(s => new UsedTileDictionaryKey(s.PuzzleIndex, s.TileNumber, s.Degrees)).ToList();

                            //foreach (var key in allKeys)
                            //{
                            //    Initiator.CheckedTileDictionary[key]++;
                            //}

                            ////There are still other tile combinations to be checked
                            //foreach (var key in allKeys.OrderByDescending(s => s.PuzzleIndex))
                            //{
                            //    var relevantTile = UsedTileList.FirstOrDefault(s =>
                            //            s.TileNumber == key.TileNumber && s.Degrees == key.Degrees);

                            //    //See if one of the used keys (starting at the end) has been used too much
                            //    if (Initiator.CheckedTileDictionary[key] >= DynamicCheckedTileDictionary[key])
                            //    {
                            //        //UsedPuzzleTilesIndices.Remove(key.PuzzleIndex);
                            //        //UsedTileList.Remove(relevantTile);
                            //        //UsedTileDictionary[key.TileNumber] = false;

                            //        //We should only += the DynamicDepthCounter in the above layers.
                            //        var aboveLayer = OriginalDepthCounter.FirstOrDefault(s => s.Key > relevantTile.PuzzleDepthCounter);
                            //        if (aboveLayer.Key != 0)
                            //        {
                            //            var usedTileNumbersAboveThisLayer = allKeys.Where(s => s.PuzzleIndex < DepthToIndex[aboveLayer.Key]).Select(a => a.TileNumber).ToList();
                            //            var relevantIndex = DepthToIndex[aboveLayer.Key];
                            //            var everyKeyExceptUsedOnes = DynamicCheckedTileDictionary.Where(s =>
                            //            s.Key.PuzzleIndex == relevantIndex &&
                            //            !usedTileNumbersAboveThisLayer.Contains(s.Key.TileNumber)).ToList();

                            //            foreach (var keyEx in everyKeyExceptUsedOnes)
                            //            {
                            //                //Update the count for the next round
                            //                DynamicCheckedTileDictionary[keyEx.Key] += OriginalDepthCounter[aboveLayer.Key];
                            //            }
                            //        }

                            //        RemoveAndResetPuzzleTile(relevantTile);

                            //        //relevantTile.PuzzleDepthCounter = 0;
                            //        //relevantTile.PuzzleIndex = -1;
                            //    }
                            //}
                        }

                        CheckTileCountAndUpIfNeeded(false);
                    }
                }
            }

            var end = DateTime.Now;

            solvedPuzzleVM.SolveDuration            = (end - start);
            solvedPuzzleVM.AmountOfCheckedSolutions = AmountOfCheckedSolutions;
            solvedPuzzleVM.AmountOfTotalSolutions   = AmountOfTotalSolutions;
            solvedPuzzleVM.AmountOfFoundSolutions   = solvedPuzzleVM.TileSet.Count;
            solvedPuzzleVM.TriedSolutions           = tries;
            var nonMax           = Initiator.CheckedTileDictionary.Where(s => s.Value < AmountOfMaximumTriesPerTile).ToList();
            var nonCheckedAmount = nonMax.Sum(s => AmountOfMaximumTriesPerTile - s.Value);

            if (solvedPuzzleVM.AmountOfFoundSolutions == 1)
            {
                //Valid puzzle, lets write to JSON
                if (puzzleVM.Name != "Puzzle ")
                {
                    var jsonPuzzles    = GetJsonPuzzles();
                    var relevantPuzzle = jsonPuzzles.PuzzleList.FirstOrDefault(s => s.Name == puzzleVM.Name);
                    if (relevantPuzzle != null)
                    {
                        if (relevantPuzzle.BestSolveTime.TotalMilliseconds == 0 || solvedPuzzleVM.SolveDuration < relevantPuzzle.BestSolveTime)
                        {
                            solvedPuzzleVM.ErrorList.Add("New solve record!");
                            relevantPuzzle.BestSolveTime = solvedPuzzleVM.SolveDuration;
                            //open file stream
                            using (StreamWriter file = File.CreateText(JsonPuzzlesPath))
                            {
                                string         json       = JsonConvert.SerializeObject(jsonPuzzles);
                                JsonSerializer serializer = new JsonSerializer();
                                //serialize object directly into file stream
                                serializer.Serialize(file, jsonPuzzles);
                                solvedPuzzleVM.SavedPuzzleName = puzzleVM.Name;
                            }
                        }
                        else
                        {
                            solvedPuzzleVM.ErrorList.Add("Puzzle is already saved as: " + puzzleVM.Name + " !");
                        }
                    }
                }
            }

            return(solvedPuzzleVM);
        }
Exemplo n.º 13
0
        public SolvedPuzzleViewModel SolvePuzzle(PuzzleViewModel puzzleVM)
        {
            var solvedPuzzleVM = new SolvedPuzzleViewModel();

            solvedPuzzleVM.ErrorList = ValidatePuzzle(puzzleVM);
            if (solvedPuzzleVM.ErrorList.Any())
            {
                return(solvedPuzzleVM);
            }
            UsedPuzzleTilesIndices = new HashSet <int>(puzzleVM.PuzzleTileList.Select(s => s.Index));
            var simpleConditionsList = puzzleVM.PuzzleTileList.SelectMany(s => s.SimpleTileIndexList).ToList();

            FillCheckedTileDictionary();
            FillTotalAmountOfSolutions();
            //usedTileList = new List<Tile>();

            var start = DateTime.Now;

            while (!solvedPuzzleVM.Solved && AmountOfCheckedSolutions <= AmountOfTotalSolutions)
            {
                foreach (var tile in Initiator.TileList)
                {
                    if (UsedTileList.Count == puzzleVM.PuzzleTileList.Count)
                    {
                        AmountOfCheckedSolutions++;
                        FillPuzzleRoads(UsedTileList);
                        if (DoesDefinitiveRoadListSolvePuzzle(simpleConditionsList))
                        {
                            solvedPuzzleVM.Solved = true;
                            break;
                        }
                        else
                        {
                            UsedTileList.Clear();
                        }
                        //break;
                    }

                    var tileIsUsed = false;

                    var tempTileRotationList = new List <Tile> {
                        tile
                    };
                    tempTileRotationList.AddRange(tile.TileRotationList);

                    foreach (var rotatedTile in tempTileRotationList)
                    {
                        if (UsedTileList.Count == puzzleVM.PuzzleTileList.Count)
                        {
                            FillPuzzleRoads(UsedTileList);
                            if (DoesDefinitiveRoadListSolvePuzzle(simpleConditionsList))
                            {
                                solvedPuzzleVM.Solved = true;
                                break;
                            }
                        }

                        foreach (var puzzleTile in puzzleVM.PuzzleTileList)
                        {
                            if (UsedTileList.Any(s => s.PuzzleIndex == puzzleTile.Index))
                            {
                                continue;
                            }
                            if (UsedTileList.Count == puzzleVM.PuzzleTileList.Count)
                            {
                                FillPuzzleRoads(UsedTileList);
                                if (DoesDefinitiveRoadListSolvePuzzle(simpleConditionsList))
                                {
                                    solvedPuzzleVM.Solved = true;
                                    break;
                                }
                            }

                            rotatedTile.PuzzleIndex = puzzleTile.Index;
                            UsedTileList.Add(rotatedTile);
                            var tileKey = new UsedTileDictionaryKey(puzzleTile.Index, rotatedTile.TileNumber, rotatedTile.Degrees);
                            CheckedTileDictionary[tileKey]++;
                            tileIsUsed = true;
                            break;
                        }
                        if (tileIsUsed)
                        {
                            break;
                        }
                    }

                    if (tileIsUsed)
                    {
                        continue;
                    }
                }
            }

            var end = DateTime.Now;

            solvedPuzzleVM.SolveDuration            = (end - start);
            solvedPuzzleVM.AmountOfCheckedSolutions = AmountOfCheckedSolutions;
            solvedPuzzleVM.AmountOfTotalSolutions   = AmountOfTotalSolutions;
            solvedPuzzleVM.TileSet = UsedTileList.ToDictionary(s => s.PuzzleIndex);

            return(solvedPuzzleVM);
        }
Exemplo n.º 14
0
 public PuzzleScreen(Navigator navigator, Puzzle puzzle) : base(navigator)
 {
     this.GoToMenu        = new EnabledCommand(() => SwitchTo(new MenuScreen(navigator)));
     this.PuzzleViewModel = new PuzzleViewModel(puzzle);
     PuzzleViewModel.IsSolved.ValueChanged += IsSolved_ValueChanged;
 }