Esempio n. 1
0
        public KendokuGame()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            this.IsMouseVisible            = true;
            this.Window.AllowUserResizing  = true;
            this.Window.ClientSizeChanged += Window_ClientSizeChanged;

            textureManager = new TextureManager(this.Content);
            gridSolver     = new GridSolver();
            gridRawData1   = new GridRawData();
            gridRawData2   = new GridRawData();
            gridRawData3   = new GridRawData();
            gridRawData1   = gridRawData1.LoadRawData("KendokuData_1.json");
            gridRawData2   = gridRawData2.LoadRawData("KendokuData_2.json");
            gridRawData3   = gridRawData3.LoadRawData("KendokuData_3.json");

            gridData1      = new GridData(gridRawData1);
            solveGridData1 = new GridData(gridRawData1);

            gridData2      = new GridData(gridRawData2);
            solveGridData2 = new GridData(gridRawData2);

            gridData3      = new GridData(gridRawData3);
            solveGridData3 = new GridData(gridRawData3);


            state_ = new GameState();
        }
Esempio n. 2
0
        public static void Run([DefaultValue("Game1.txt")] string gameFile, [DefaultValue("18")] int depthPenalty,
                               [DefaultValue("50000")] int maxMoves)
        {
            string gameId = gameFile.Replace(".txt", "");

            Log.Logger = new LoggerConfiguration()
                         .WriteTo.LiterateConsole()
                         .WriteTo.File($"Log-{gameId}_Depth-{depthPenalty}")
                         .MinimumLevel.Debug()
                         .CreateLogger();

            ImmutableBubbleBurstGrid grid;

            using (var stream = File.OpenRead($"Games//{gameFile}"))
            {
                grid = BubbleGridBuilder.Create(new StreamReader(stream));
            }

            var solutionFileName = $"Solution_{gameId}_Depth-{depthPenalty}.json";

            File.Delete(solutionFileName);
            using (var fileStream = File.OpenWrite(solutionFileName))
                using (var writer = new StreamWriter(fileStream))
                {
                    var solver   = new GridSolver(grid, writer, depthPenalty);
                    var solution = solver.Solve(maxMoves);

                    writer.Write(JsonConvert.SerializeObject(solution.Moves));

                    solution.GridState.Display();
                }
        }
Esempio n. 3
0
        public void AnEmptyGridIsSolvedWithOnlyMovingForward()
        {
            var grid   = Grid.OfSize(3);
            var solver = new GridSolver(grid);

            Assert.That(solver.Solve(), Is.True);
            Console.WriteLine(grid.ToString());
        }
Esempio n. 4
0
        public void P11_TestMockGridLoading()
        {
            var gs = new GridSolver();

            gs.LoadMockGrid(5, _mockGrid);
            Assert.AreEqual(25, gs.MockGridSize);
            Assert.AreEqual(0, gs.GridSize);
        }
Esempio n. 5
0
        public void P11_TestGridLoading()
        {
            var gs = new GridSolver();

            gs.LoadGrid(300, 9995);
            Assert.AreEqual(90000, gs.GridSize);
            Assert.AreEqual(0, gs.MockGridSize);
        }
Esempio n. 6
0
        public void ForceASingleValue()
        {
            var grid = Grid.OfSize(3);

            grid.Cell(2, 1, 5);
            grid.Freeze();
            var solver = new GridSolver(grid);

            Assert.That(solver.Solve(), Is.True);
            Console.WriteLine(grid.ToString());
        }
Esempio n. 7
0
        public void AnUnsolvableGrid()
        {
            var grid = Grid.OfSize(3);

            grid.Cell(1, 1, 5);
            grid.Cell(2, 1, 5);
            grid.Freeze();
            var solver = new GridSolver(grid);

            Assert.That(solver.Solve(), Is.False);
            Console.WriteLine(grid.ToString());
        }
Esempio n. 8
0
        public void P11_TestFindMaxPowerMockGrid()
        {
            var gs = new GridSolver();

            gs.LoadMockGrid(5, _mockGrid);
            gs.BlockSizeLimit = 3;
            gs.FindMaxPower();
            var maxIdx = gs.MaxIndex;

            Assert.AreEqual(2, maxIdx.X);
            Assert.AreEqual(2, maxIdx.Y);
        }
Esempio n. 9
0
        public ActionResult <string> Post([FromBody] string body, [FromQuery(Name = "rd")] string rowdelimiter)
        {
            if (String.IsNullOrWhiteSpace(body))
            {
                return(this.BadRequest("you must provide a body containing the 9x9 puzzle to be solved"));
            }

            Grid grid = null;

            try
            {
                var puzzle = Converter.From(body, rowdelimiter);
                grid = Grid.From(puzzle);
            }
            catch (FormatException fe)
            {
                return(this.BadRequest("the supplied puzzle contained non-numeric values"));
            }
            catch (ArgumentException ae)
            {
                return(this.BadRequest(ae.Message));
            }
            catch (Exception ex)
            {
                this._logger.LogError(ex.ToString());
                return(this.Problem("an error occurred while parsing the supplied puzzle", statusCode: 500));
            }

            try
            {
                var solver = new GridSolver(grid);
                var result = solver.Solve();
                if (result)
                {
                    return(this.Ok(grid.ToString()));
                }
                else
                {
                    return(this.UnprocessableEntity("the puzzle was unsolvable"));
                }
            }
            catch (ArgumentException ae)
            {
                return(this.BadRequest(ae.Message));
            }
            catch (Exception ex)
            {
                this._logger.LogError(ex.ToString());
                return(this.Problem("an error occurred while solving the supplied puzzle", statusCode: 500));
            }
        }
Esempio n. 10
0
        public void SolveAFullPuzzle()
        {
            var values = new int[][]
            {
                new int[] { 0, 9, 0, 0, 0, 8, 6, 0, 4 },
                new int[] { 6, 0, 0, 0, 1, 9, 0, 0, 0 },
                new int[] { 0, 4, 8, 6, 2, 5, 0, 0, 0 },
                new int[] { 0, 2, 0, 0, 8, 0, 7, 1, 6 },
                new int[] { 0, 7, 0, 0, 0, 0, 0, 8, 0 },
                new int[] { 3, 8, 5, 0, 7, 0, 0, 2, 0 },
                new int[] { 0, 0, 0, 8, 6, 1, 5, 4, 0 },
                new int[] { 0, 0, 0, 3, 5, 0, 0, 0, 2 },
                new int[] { 5, 0, 7, 2, 0, 0, 0, 3, 0 },
            };

            var grid   = Grid.From(values);
            var solver = new GridSolver(grid);

            Assert.That(solver.Solve(), Is.True);
            Console.WriteLine(grid.ToString());
        }
Esempio n. 11
0
        public bool IsRuleValid()
        {
            int errorCount = 0;

            int[] levelDataArray = levelData.Select(x => Int32.Parse(x.ToString())).ToArray();
            if (levelDataArray.Length != 81)
            {
                //Debug.LogError($"Rule id: {Id} length is invalid ({levelDataArray.Length})");
                Debug.LogError($"Rule id: {Id} length is invalid ({levelDataArray.Length})");
                errorCount += 1;
            }

            if (targetIndexes.Length != 3)
            {
                Debug.LogError($"Rule id: {Id} target length is invalid");
                errorCount += 1;
            }
            GameObject gameObject = new GameObject();

            for (int i = 0; i < levelDataArray.Length; i++)
            {
                gameObject.AddComponent <GridSquareScript>();
            }
            GridSquareScript[] gridSquares = gameObject.GetComponentsInChildren <GridSquareScript>();

            for (int i = 0; i < gridSquares.Length; i++)
            {
                bool             isTarget   = targetIndexes.Contains(i);
                GridSquareScript gridSquare = gridSquares[i];
                gridSquare.SetupTestGridSquare(i, levelDataArray[i], isTarget);
            }
            Debug.LogWarning($"************* RULE {Id} *************");
            targetSolutions  = new GridSolutionType[targetIndexes.Length];
            difficultyRating = 0;
            for (int i = 0; i < targetIndexes.Length; i++)
            {
                int targetCount         = 0;
                GridSquareScript target = gridSquares[targetIndexes[i]];
                Debug.LogWarning($"---------- TARGET {target.Index} ---------");
                for (int j = 1; j <= 9; j++)
                {
                    target.UpdateTestGridNumber(j);
                    Debug.LogWarning($"---------- Number {target.Number} ---------");
                    GridSolutionType solution = GridSolver.SolveGridAtIndex(gridSquares, target);
                    if (solution != GridSolutionType.None)
                    {
                        targetCount += 1;
                        Debug.LogWarning($"Rule id: {Id} target {targetIndexes[i]} has solution with {j} ({solution})");
                        targetSolutions[i] = solution;
                    }
                }

                //RESET TARGET AFTER TEST SO DOESN"T EFFECT NEXT TARGET!!!
                target.UpdateTestGridNumber(0);

                if (targetCount != 1)
                {
                    Debug.LogError($"Rule id: {Id} target {targetIndexes[i]} has {targetCount} solutions");
                    errorCount += 1;
                }
                else
                {
                    difficultyRating += (int)targetSolutions[i];
                }
            }
            DestroyImmediate(gameObject);
#if UNITY_EDITOR
            EditorUtility.SetDirty(this);
#endif
            return(errorCount == 0);
        }
Esempio n. 12
0
        public void UpdateGamePlay(MouseState mouseState, Vector2 mouseInWorld, int position, int size, ref GridCell selectedCell, ref GridData gridData, ref GridData solveGridData, GridSolver gridSolver, GameGrid gameGrid)
        {
            if (mouseState.LeftButton == ButtonState.Pressed &
                mouseInWorld.Y <position + size&
                                mouseInWorld.Y> position & mouseInWorld.X <position + size&
                                                                           mouseInWorld.X> position)
            {
                int tempMouseInWorldX = (int)mouseInWorld.X / 100;
                tempMouseInWorldX *= 100;

                int tempMouseInWorldY = (int)mouseInWorld.Y / 100;
                tempMouseInWorldY *= 100;
                selectedCell       = new GridCell((tempMouseInWorldY - position - (gridData.gridCount / size)) / 100, (tempMouseInWorldX - position - (gridData.gridCount / size)) / 100, gridData.sections[0], gridData);
            }
            if (mouseState.LeftButton == ButtonState.Pressed & mouseInWorld.Y <= 210 & mouseInWorld.Y > 110 & mouseInWorld.X <= position + 100 & mouseInWorld.X >= position & selectedCell != null)
            {
                selectedCell.value = 1;
                gridData.gridCells[selectedCell.rowIndex, selectedCell.columnIndex].value = selectedCell.value;
                updatePossibilities(gridData, gridData.gridCells[selectedCell.rowIndex, selectedCell.columnIndex]);
                selectedCell = null;
            }
            else if (mouseState.LeftButton == ButtonState.Pressed & mouseInWorld.Y <= 210 & mouseInWorld.Y > 110 & mouseInWorld.X <= position + 100 + (1 * 110) & mouseInWorld.X >= position + (1 * 110) & selectedCell != null)
            {
                selectedCell.value = 2;
                gridData.gridCells[selectedCell.rowIndex, selectedCell.columnIndex].value = selectedCell.value;
                updatePossibilities(gridData, gridData.gridCells[selectedCell.rowIndex, selectedCell.columnIndex]);
                selectedCell = null;
            }
            else if (mouseState.LeftButton == ButtonState.Pressed & mouseInWorld.Y <= 210 & mouseInWorld.Y > 110 & mouseInWorld.X <= position + 100 + (2 * 110) & mouseInWorld.X >= position + (2 * 110) & selectedCell != null)
            {
                selectedCell.value = 3;
                gridData.gridCells[selectedCell.rowIndex, selectedCell.columnIndex].value = selectedCell.value;
                updatePossibilities(gridData, gridData.gridCells[selectedCell.rowIndex, selectedCell.columnIndex]);
                selectedCell = null;
            }
            else if (mouseState.LeftButton == ButtonState.Pressed & mouseInWorld.Y <= 210 & mouseInWorld.Y > 110 & mouseInWorld.X <= position + 100 + (3 * 110) & mouseInWorld.X >= position + (3 * 110) & selectedCell != null)
            {
                selectedCell.value = 4;
                gridData.gridCells[selectedCell.rowIndex, selectedCell.columnIndex].value = selectedCell.value;
                updatePossibilities(gridData, gridData.gridCells[selectedCell.rowIndex, selectedCell.columnIndex]);
                selectedCell = null;
            }
            else if (mouseState.LeftButton == ButtonState.Pressed & mouseInWorld.Y <= 210 & mouseInWorld.Y > 110 & mouseInWorld.X <= position + 100 + (4 * 110) & mouseInWorld.X >= position + (4 * 110) & selectedCell != null)
            {
                selectedCell.value = 5;
                gridData.gridCells[selectedCell.rowIndex, selectedCell.columnIndex].value = selectedCell.value;
                updatePossibilities(gridData, gridData.gridCells[selectedCell.rowIndex, selectedCell.columnIndex]);
                selectedCell = null;
            }
            else if (mouseState.LeftButton == ButtonState.Pressed & mouseInWorld.Y <= 210 & mouseInWorld.Y > 110 & mouseInWorld.X <= position + 100 + (5 * 110) & mouseInWorld.X >= position + (5 * 110) & selectedCell != null)
            {
                selectedCell.value = 6;
                gridData.gridCells[selectedCell.rowIndex, selectedCell.columnIndex].value = selectedCell.value;
                updatePossibilities(gridData, gridData.gridCells[selectedCell.rowIndex, selectedCell.columnIndex]);
                selectedCell = null;
            }
            else if (mouseState.LeftButton == ButtonState.Pressed & mouseInWorld.Y <= 210 & mouseInWorld.Y > 110 & mouseInWorld.X <= position + 100 + (7 * 110) & mouseInWorld.X >= position + (7 * 110))
            {
                solveGridData     = gridSolver.SolveAll(solveGridData);
                this.currentState = State.Answer;
            }


            if (mouseState.LeftButton == ButtonState.Pressed & (mouseInWorld.Y > position + size | mouseInWorld.Y <position | mouseInWorld.X> position + size | mouseInWorld.X < position))
            {
                selectedCell = null;
            }
            gameGrid.Update();
        }