Пример #1
0
        public ActionResult <PuzzleResponse> Solution([FromBody] Puzzle sudokuPuzzle)
        {
            PuzzleResponse response;

            try
            {
                if (sudokuPuzzle != null)
                {
                    int[][] result = _sudokuService.GetSolvedSudoku6x6(sudokuPuzzle.arrSudoku);
                    if (result != null)
                    {
                        response = new PuzzleResponse(Constants.Code.OK, Utility.Constants.Message.Success, result);
                    }
                    else
                    {
                        response = new PuzzleResponse(Constants.Code.OK, Utility.Constants.Message.SolutionNotFound, result);
                    }
                    return(response);
                }
                else
                {
                    throw new PuzzleException(Constants.Message.IncorrectInput, Constants.Code.Error);
                }
            }
            catch (PuzzleException ex)
            {
                _logger.LogInformation("ERROR " + ex.Message);
                response = new PuzzleResponse(ex.Code, ex.Message, null);
                return(response);
            }
        }
Пример #2
0
    private static PuzzleResponse GetDiagonalDownRight(string word, int x, int y, char[,] array)
    {
        List <Breakdown> coords = new List <Breakdown>();
        int            length   = word.Length;
        bool           isFound  = false;
        PuzzleResponse response = new PuzzleResponse();
        StringBuilder  sBuilder = new StringBuilder();

        if (!isFound)
        {
            for (int i = 0; i < length; i++)
            {
                int yy = y + i;
                if (yy > 0 &&
                    yy < array.GetLength(1) &&
                    (x + i) < array.GetLength(0))
                {
                    coords.Add(new Breakdown()
                    {
                        Character = array[x + i, y + i],
                        Row       = x + i,
                        Column    = y + i
                    });
                    sBuilder.Append(array[x + i, y + i]);
                }
            }
            if (sBuilder.ToString().Equals(word))
            {
                isFound            = true;
                response.Word      = sBuilder.ToString();
                response.Breakdown = coords;
            }
        }
        return(response);
    }
Пример #3
0
        public void PostPuzzletest()
        {
            var settingsMoq = new Mock <IOptions <AppSettings> >();

            settingsMoq.Setup(x => x.Value).Returns(testSettings);
            var controller = new PuzzleController(settingsMoq.Object, testPuzzle);
            var result     = controller.Post(postRequestCorrect);

            ActionResult   testResult     = (ActionResult)result.Result;
            OkObjectResult okObjectResult = Assert.IsType <OkObjectResult>(testResult);
            PuzzleResponse response       = (PuzzleResponse)okObjectResult.Value;

            Assert.True(response.IsCorrect);
        }
Пример #4
0
    private static PuzzleResponse GetVerticalUp(string word, int x, int y, char[,] array)
    {
        List <Breakdown> coords = new List <Breakdown>();
        int            length   = word.Length;
        bool           isFound  = false;
        PuzzleResponse response = new PuzzleResponse();
        StringBuilder  sBuilder = new StringBuilder();

        if (!isFound)
        {
            for (int i = 0; i < length; i++)
            {
                int  height = array.GetLength(1);
                int  width  = array.GetLength(0);
                char cTemp  = word.ToCharArray()[i];

                if (y > 0 &&
                    x - 1 > 0 &&
                    y < height &&
                    (x - i) < width)
                {
                    if (array[x - i, y] == cTemp)
                    {
                        coords.Add(new Breakdown()
                        {
                            Character = array[x - i, y],
                            Row       = x - i,
                            Column    = y
                        });
                        sBuilder.Append(array[i, y]);
                    }
                    else
                    {
                        return(null);
                    }
                }
            }
            if (sBuilder.ToString().Equals(word))
            {
                isFound            = true;
                response.Word      = sBuilder.ToString();
                response.Breakdown = coords;
            }
        }
        return(response);
    }
Пример #5
0
        public void CreatePuzzle_ShouldReturnPuzzleForId0()
        {
            mockLogger     = new Mock <ILogger <SudokuController> >();
            logger         = mockLogger.Object;
            mockService    = new Mock <IPuzzleService>();
            service        = mockService.Object;
            mockRepository = new Mock <IPuzzleRepository>();
            mockRepository.Setup(x => x.GetPuzzleById(0))
            .Returns(new Puzzle(testData.GetValidPuzzle0()
                                ));
            repository = mockRepository.Object;
            sut        = new SudokuController(logger, service, repository);
            var actualResult   = sut.CreatePuzzle(0);
            var expectedResult = new PuzzleResponse(Constants.Code.OK, Constants.Message.Success, testData.GetValidPuzzle0());

            Assert.AreEqual(expectedResult.Equals(actualResult.Value), true);
        }
Пример #6
0
        public void Solution_ShouldReturnNoSolutionForInvalidPuzzle()
        {
            mockLogger = new Mock <ILogger <SudokuController> >();
            logger     = mockLogger.Object;
            var puzzle = testData.GetInvalidPuzzle();

            mockService = new Mock <IPuzzleService>();
            mockService.Setup(x => x.GetSolvedSudoku6x6(puzzle))
            .Returns((int[][])null);
            service        = mockService.Object;
            mockRepository = new Mock <IPuzzleRepository>();
            repository     = mockRepository.Object;
            sut            = new SudokuController(logger, service, repository);
            var actualResult   = sut.Solution(new Puzzle(puzzle));
            var expectedResult = new PuzzleResponse(Constants.Code.OK, Constants.Message.SolutionNotFound, null);

            Assert.AreEqual(expectedResult.Equals(actualResult.Value), true);
        }
        public ActionResult <PuzzleResponse> Post(PuzzleRequest request)
        {
            PuzzleResponse response = new PuzzleResponse
            {
                IsCorrect = false
            };

            String compareTo = "";

            switch (request.Round)
            {
            case 1:
                compareTo = _appSettings.Round1;
                break;

            case 2:
                compareTo = _appSettings.Round2;
                break;

            case 3:
                compareTo = _appSettings.Round3;
                break;

            case 4:
                compareTo = _appSettings.Round4;
                break;
            }

            response.ReturnMsg = StringUtilities.StringComparePercentange(compareTo.ToLower(), request.Answer.ToLower());
            if (compareTo.ToUpper() == request.Answer.ToUpper())
            {
                response.IsCorrect = true;
            }

            return(Ok(response));
        }
Пример #8
0
        public ActionResult <PuzzleResponse> CreatePuzzle(int id)
        {
            PuzzleResponse response;

            try
            {
                Puzzle puzzle = _puzzlerepository.GetPuzzleById(id);
                if (puzzle != null)
                {
                    response = new PuzzleResponse(Constants.Code.OK, Constants.Message.Success, puzzle.arrSudoku);
                    return(response);
                }
                else
                {
                    throw new PuzzleException(Constants.Message.InternalServerError, Constants.Code.Error);
                }
            }
            catch (PuzzleException ex)
            {
                _logger.LogError("ERROR " + ex.Message);
                response = new PuzzleResponse(ex.Code, ex.Message, null);
                return(response);
            }
        }
Пример #9
0
    public static PuzzleResponse SolvePuzzle(char[,] matrix, string word)
    {
        string         wordResponse   = string.Empty;
        PuzzleResponse puzzleResponse = new PuzzleResponse();
        bool           isSolved       = false;
        PuzzleResponse pr             = new PuzzleResponse();

        if (!isSolved)
        {
            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(1); j++)
                {
                    char c = word.ToCharArray()[0];
                    if (c == matrix[i, j])
                    {
                        //Search on Left to Right on Horizontal
                        if ((pr = GetHorizontalRight(word, i, j, matrix)) != null)
                        {
                            if (!string.IsNullOrWhiteSpace(pr.Word))
                            {
                                isSolved       = true;
                                puzzleResponse = pr;
                                //Console.WriteLine(pr.Word);
                                //foreach (var item in pr.Breakdown)
                                //{
                                //    Console.WriteLine(item.Row + "," + item.Column);
                                // }
                                break;
                            }
                        }
                        //Search on Left to Right on Horizontal
                        if ((pr = GetHorizontalLeft(word, i, j, matrix)) != null)
                        {
                            if (!string.IsNullOrWhiteSpace(pr.Word))
                            {
                                isSolved       = true;
                                puzzleResponse = pr;
                                //Console.WriteLine(pr.Word);
                                //foreach (var item in pr.Breakdown)
                                //{
                                //    Console.WriteLine(item.Row + "," + item.Column);
                                //}
                                break;
                            }
                        }
                        //Search on the Vertical way, from top to down
                        if ((pr = GetVerticalDown(word, i, j, matrix)) != null)
                        {
                            if (!string.IsNullOrWhiteSpace(pr.Word))
                            {
                                isSolved       = true;
                                puzzleResponse = pr;
                                //Console.WriteLine(pr.Word);
                                //foreach (var item in pr.Breakdown)
                                //{
                                //    Console.WriteLine(item.Row + "," + item.Column);
                                //}
                                break;
                            }
                        }
                        //Search on the Vertical way, from top to down
                        if ((pr = GetVerticalUp(word, i, j, matrix)) != null)
                        {
                            if (!string.IsNullOrWhiteSpace(pr.Word))
                            {
                                isSolved       = true;
                                puzzleResponse = pr;
                                //Console.WriteLine(pr.Word);
                                //foreach (var item in pr.Breakdown)
                                //{
                                //    Console.WriteLine(item.Row + "," + item.Column);
                                //}
                                break;
                            }
                        }
                        //Search on the left diagonal on down direction
                        if ((pr = GetDiagonalDownLeft(word, i, j, matrix)) != null)
                        {
                            if (!string.IsNullOrWhiteSpace(pr.Word))
                            {
                                isSolved       = true;
                                puzzleResponse = pr;

                                //Console.WriteLine(pr.Word);
                                //foreach (var item in pr.Breakdown)
                                //{
                                //    Console.WriteLine(item.Row + "," + item.Column);
                                //}
                                break;
                            }
                        }
                        //Search on the left diagonal on down direction
                        if ((pr = GetDiagonalUpLeft(word, i, j, matrix)) != null)
                        {
                            if (!string.IsNullOrWhiteSpace(pr.Word))
                            {
                                isSolved       = true;
                                puzzleResponse = pr;
                                //Console.WriteLine(pr.Word);
                                //foreach (var item in pr.Breakdown)
                                //{
                                //    Console.WriteLine(item.Row + "," + item.Column);
                                //}
                                break;
                            }
                        }

                        //Search on the right diagonal on down direction
                        if ((pr = GetDiagonalUpRight(word, i, j, matrix)) != null)
                        {
                            if (!string.IsNullOrWhiteSpace(pr.Word))
                            {
                                isSolved       = true;
                                puzzleResponse = pr;
                                //Console.WriteLine(pr.Word);
                                //foreach (var item in pr.Breakdown)
                                //{
                                //    Console.WriteLine(item.Row + "," + item.Column);
                                //}
                                break;
                            }
                        }
                        //Search on the right diagonal on down direction
                        if ((pr = GetDiagonalDownRight(word, i, j, matrix)) != null)
                        {
                            if (!string.IsNullOrWhiteSpace(pr.Word))
                            {
                                isSolved       = true;
                                puzzleResponse = pr;
                                //Console.WriteLine(pr.Word);
                                //foreach (var item in pr.Breakdown)
                                //{
                                //    Console.WriteLine(item.Row + "," + item.Column);
                                //}
                                break;
                            }
                        }
                    }
                }
                if (isSolved)
                {
                    break;
                }
            }
        }
        return(puzzleResponse);
    }
Пример #10
0
        static void SolvePuzzle()
        {
            var rules  = MarkovDecoder.ReadRules();
            var values = MarkovDecoder.ReadValues();

            Console.WriteLine("***********************Decoding Characters using Markov Algorithm***************");
            char[,] array = MarkovDecoder.Decypher(rules, values);
            Console.WriteLine("***********************Generating Puzzle****************************************");
            string[] wordsToCheck = MarkovDecoder.ReadWords().ToArray();

            Console.WriteLine("***********************List of possible Words: *********************************");
            foreach (var word in wordsToCheck)
            {
                Console.Write(word + " - ");
            }
            Console.WriteLine();

            List <PuzzleResponse> puzzleResponseList = new List <PuzzleResponse>();
            List <Coordinate>     coordinatesToPaint = new List <Coordinate>();
            StringBuilder         sBuilderFoundWords = new StringBuilder();

            foreach (var word in wordsToCheck)
            {
                var            coordinatesPerWord = Puzzle.CheckAdjacentsC(word, 0, 0, array);
                PuzzleResponse puzzleResponseTemp = new PuzzleResponse();
                puzzleResponseTemp.Breakdown = new List <Breakdown>();
                //We need to check whether or not the query returned values.
                if (coordinatesPerWord.Count > 0)
                {
                    puzzleResponseTemp.Word = word;

                    coordinatesToPaint.AddRange(coordinatesPerWord);
                    foreach (var coord in coordinatesPerWord)
                    {
                        Breakdown brkDown = new Breakdown()
                        {
                            Character = array[coord.X, coord.Y],
                            Row       = coord.X,
                            Column    = coord.Y
                        };
                        puzzleResponseTemp.Breakdown.Add(brkDown);
                    }
                    sBuilderFoundWords.Append(word + "-");
                }
                puzzleResponseList.Add(puzzleResponseTemp);
            }
            foreach (var word in wordsToCheck)
            {
                var solution = PuzzleSolver.SolvePuzzle(array, word);
                if (solution.Word != null)
                {
                    puzzleResponseList.Add(solution);
                }
            }

            foreach (var puzzleSolution in puzzleResponseList)
            {
                foreach (var item in puzzleSolution.Breakdown)
                {
                    Coordinate c = new Coordinate()
                    {
                        X = item.Row,
                        Y = item.Column
                    };
                    coordinatesToPaint.Add(c);
                }
                if (!String.IsNullOrWhiteSpace(puzzleSolution.Word))
                {
                    if (!sBuilderFoundWords.ToString().Contains(puzzleSolution.Word))
                    {
                        sBuilderFoundWords.Append(puzzleSolution.Word + "-");
                    }
                }
            }

            var output = JsonConvert.SerializeObject(puzzleResponseList.Where(p => p.Word != null), Formatting.Indented);

            Console.WriteLine("JSON Output Generated: ");
            Console.WriteLine(output);

            Console.WriteLine(string.Format("WORDS FOUND IN PUZZLE: {0} ", sBuilderFoundWords.ToString()));
            Console.WriteLine();
            MarkovDecoder.PrintSolvedGrid(array, coordinatesToPaint);
            Console.WriteLine();
        }