public void EmbedSecretData(byte[] containerBytes, byte[] secretBytes, SudokuMatrix <byte> sudokuKey) { if (containerBytes == null || containerBytes.Length == 0 || secretBytes == null || secretBytes.Length == 0) { throw new ArgumentException(); } if (secretBytes.Length * 2 >= containerBytes.Length) { throw new InvalidOperationException("Cannot encrypt secret data because container image is too small"); } ValidateSudoku(sudokuKey); for (int i = 0, secretDataIterator = 0; i + 1 < containerBytes.Length && secretDataIterator < secretBytes.Length; i += 2, secretDataIterator++) { byte currentByte = secretBytes[secretDataIterator]; SudokuCoordinates initialCoordinates = new SudokuCoordinates(containerBytes[i], containerBytes[i + 1]); SudokuCoordinates nearestCoordinates = sudokuKey.FindNearestCoordinates(currentByte, initialCoordinates); if (initialCoordinates != nearestCoordinates) { containerBytes[i] = nearestCoordinates.X; containerBytes[i + 1] = nearestCoordinates.Y; } } }
internal static void Run() { string result; var continueLoop = true; do { var matrix = new SudokuMatrix(); matrix.SetDifficulty(new Difficulty() { Name = "Test", DifficultyLevel = DifficultyLevel.TEST }); matrix.GenerateSolution(); DisplayScreens.DisplayMatix(matrix); Console.Write("\n\nWould you like to generate another solution (yes/no): "); result = Console.ReadLine(); if (result.ToLower().Equals("no") || result.ToLower().Equals("n")) { continueLoop = false; } } while (continueLoop); }
public void Initialize(ref SudokuMatrix <T> matrix, string passwordString) { var password = GetHash(passwordString); int transformationsCount = 0; for (int i = 0; i < password.Length; i++) { var transformation = _invariantTransforations[password[i] % _invariantTransforations.Count]; int[] transformationParams = new int[transformation.IndexesLength]; #if DEBUG System.Console.Write($"Transformation: {password[i] % _invariantTransforations.Count} -"); #endif for (int j = 0; j < transformationParams.Length && i + j < password.Length; j++, ++i) { transformationParams[j] = password[i]; #if DEBUG System.Console.Write($" {transformationParams[j]}"); #endif } #if DEBUG System.Console.WriteLine(); #endif transformation.Transform(ref matrix, transformationParams); transformationsCount++; } #if DEBUG System.Console.WriteLine("-", 20); System.Console.WriteLine($"Transf. count = {transformationsCount}"); System.Console.WriteLine("-", 20); #endif }
public void Setup() { populatedTestMatrix = new SudokuMatrix(); populatedTestMatrix.GenerateSolution(); intList = populatedTestMatrix.ToIntList(); firstInt = intList[0]; populatedTestMatrix = new SudokuMatrix(intList); }
public void HaveADefaultValueOfZero() { // Arrange and Act var testMatrix = new SudokuMatrix(); sut = testMatrix.SudokuCells[0]; // Assert Assert.That(sut.Value, Is.EqualTo(0)); }
public void HaveAvailableValuesNineCountIfValueIsZero() { // Arrange and Act var testMatrix = new SudokuMatrix(); sut = testMatrix.SudokuCells[0]; // Assert Assert.That(sut.AvailableValues.Count, Is.EqualTo(9)); }
public async Task <ISolutionResult> Generate() { var result = new SolutionResult(); var continueLoop = true; do { var matrix = new SudokuMatrix(); matrix.GenerateSolution(); var response = (await solutionsRepository.GetSolvedSolutions()); var matrixNotInDB = true; if (response.Success) { foreach (var solution in response.Objects.ConvertAll(s => (SudokuSolution)s)) { if (solution.SolutionList.Count > 0 && solution.ToString().Equals(matrix)) { matrixNotInDB = false; } } } if (matrixNotInDB) { result.Solution = new SudokuSolution( matrix.ToIntList()); continueLoop = false; } } while (continueLoop); var solutionResponse = await solutionsRepository.Create((SudokuSolution)result.Solution); if (solutionResponse.Success) { result.Success = solutionResponse.Success; result.Message = SolutionsMessages.SolutionGeneratedMessage; return(result); } else { result.Success = solutionResponse.Success; result.Message = SolutionsMessages.SolutionNotGeneratedMessage; return(result); } }
public void HaveAnAssociatedMatrix() { // Arrange and Act var testMatrix = new SudokuMatrix(); sut = testMatrix.SudokuCells[0]; // Assert Assert.That(sut.SudokuMatrixId, Is.TypeOf <int>()); Assert.That(testMatrix.Id, Is.EqualTo(sut.SudokuMatrixId)); Assert.That(testMatrix, Is.TypeOf <SudokuMatrix>()); }
public void Setup() { populatedTestMatrix = new SudokuMatrix(); populatedTestMatrix.GenerateSolution(); var sb = new StringBuilder(); foreach (var i in populatedTestMatrix.ToIntList()) { sb.Append(i); } stringList = sb.ToString(); }
/// <summary> /// 1 - vertical region index /// 2 - column index 1 inside vertical region /// 3 - column index 2 inside vertical region /// </summary> /// <param name="matrix"></param> /// <param name="indexes"></param> public void Transform(ref SudokuMatrix <T> matrix, params int[] indexes) { if (matrix == null || indexes.Length != IndexesLength) { throw new ArgumentException(); } int regionIndex = indexes[0] < matrix.RegionsCount ? indexes[0] : indexes[0] % matrix.RegionsCount; int regionLineOffset = regionIndex * matrix.BlockSize; int index1 = indexes[1] < matrix.BlockSize ? indexes[1] : indexes[1] % matrix.BlockSize; int index2 = indexes[2] < matrix.BlockSize ? indexes[2] : indexes[2] % matrix.BlockSize; matrix.SwapColumns(index1 + regionLineOffset, index2 + regionLineOffset); }
private void ValidateSudoku(SudokuMatrix <byte> sudokuKey) { if (sudokuKey == null) { throw new ArgumentException(); } if (sudokuKey.SudokuSize != GetExpectedSudokuSize()) { throw new ArgumentException($"This steganography method works only with matrix {GetExpectedSudokuSize()}x{GetExpectedSudokuSize()}."); } if (!sudokuKey.IsMatrixValid) { throw new ArgumentException("Invalid sudoku matrix"); } }
/// <summary> /// 1 - horizpontal region 1 index /// 2 - horizpontal region 2 index /// </summary> /// <param name="matrix"></param> /// <param name="indexes"></param> public void Transform(ref SudokuMatrix <T> matrix, params int[] indexes) { if (matrix == null || indexes.Length != IndexesLength) { throw new ArgumentException(); } int index1 = indexes[0] < matrix.RegionsCount ? indexes[0] : indexes[0] % matrix.RegionsCount; int index2 = indexes[1] < matrix.RegionsCount ? indexes[1] : indexes[1] % matrix.RegionsCount; int block1LineOffet = index1 * matrix.BlockSize; int block2LineOffet = index2 * matrix.BlockSize; for (int i = 0; i < matrix.BlockSize; i++) { matrix.SwapColumns(block1LineOffet + i, block2LineOffet + i); } }
public async Task Setup() { context = await TestDatabase.GetDatabaseContext(); sut = new GamesRepository <Game>(context); user = context .Users .FirstOrDefault(u => u.Id == 1); matrix = new SudokuMatrix(); difficulty = context .Difficulties .FirstOrDefault(d => d.DifficultyLevel == DifficultyLevel.MEDIUM); newGame = new Game(user, matrix, difficulty, 1); }
private void EmbedSecret(Bitmap containerBitmap, byte[] secretData, IKey <TKey> key) { var containerBitmapParts = GetBitmapParts(containerBitmap, ImageLockMode.ReadWrite, StegoConstraints.ContainerFileConstraints, "container"); SudokuMatrix <T> sudokuKey = _sudokuMatrixFactory.Create(_sudokuStegoMethod.GetExpectedSudokuSize(), key); try { _sudokuStegoMethod.EmbedSecretData(containerBitmapParts.PayloadBytes, secretData, sudokuKey); containerBitmap.UpdateBitmapPayloadBytes(containerBitmapParts.PayloadBytes, containerBitmapParts.Bitmap); } catch (InvalidOperationException) { throw; } catch (Exception e) { throw new Exception("Something went wrong. Try again", e); } }
private SecretFile ExtractSecretFile(byte[] stegoBytes, SudokuMatrix <T> sudokuKey) { int offset = 0; //extract secret file length (stored in 4 bytes) var secretFilePayloadLengthInByteArray = _sudokuStegoMethod.ExtractSecretData(4, ref offset, stegoBytes, sudokuKey); int secretFilePayloadLength = BitConverter.ToInt32(secretFilePayloadLengthInByteArray, 0); if (secretFilePayloadLength <= 0 || secretFilePayloadLength >= stegoBytes.Length) { throw new InvalidOperationException("Unable to extract secret data"); } //extract secret file name (in bytes) length (number is stored in a 4 bytes) var secretFileNameLengthInByteArray = _sudokuStegoMethod.ExtractSecretData(4, ref offset, stegoBytes, sudokuKey); int secretFileNameLength = BitConverter.ToInt32(secretFileNameLengthInByteArray, 0); if (secretFileNameLength <= 0 || secretFileNameLength >= stegoBytes.Length) { throw new InvalidOperationException("Unable to extract secret data"); } try { //extract secret file name var secretFileNameInByteArray = _sudokuStegoMethod.ExtractSecretData(secretFileNameLength, ref offset, stegoBytes, sudokuKey); string secretFileName = Encoding.UTF8.GetString(secretFileNameInByteArray, 0, secretFileNameInByteArray.Length); //extract secret file payload var secretFilePayloadBytes = _sudokuStegoMethod.ExtractSecretData(secretFilePayloadLength, ref offset, stegoBytes, sudokuKey); return(new SecretFile(secretFileName, secretFilePayloadBytes)); } catch { throw new InvalidOperationException("Unable to extract secret data"); } }
private byte[] ExtractSecretBytes(byte[] stegoBytes, SudokuMatrix <T> sudokuKey) { int offset = 0; //extract secret bytes length (stored in 4 bytes) var secretBytesLengthInByteArray = _sudokuStegoMethod.ExtractSecretData(4, ref offset, stegoBytes, sudokuKey); int secretBytesLength = BitConverter.ToInt32(secretBytesLengthInByteArray, 0); if (secretBytesLength <= 0 || secretBytesLength >= stegoBytes.Length) { throw new InvalidOperationException("Unable to extract secret data"); } try { //extract secret bytes (payload) return(_sudokuStegoMethod.ExtractSecretData(secretBytesLength, ref offset, stegoBytes, sudokuKey)); } catch { throw new InvalidOperationException("Unable to extract secret data"); } }
public static Tuple <double, int> CalculateMatrixesDifference <T>(SudokuMatrix <T> m1, SudokuMatrix <T> m2) { int identicalElementsCount = 0; if (m1.SudokuSize != m2.SudokuSize || m1.SudokuSize == 0) { throw new ArgumentException(); } for (int i = 0; i < m1.SudokuSize; i++) { for (int j = 0; j < m1.SudokuSize; j++) { if (m1[i, j].Equals(m2[i, j])) { identicalElementsCount++; } } } var similarityPercentage = 100.0 * identicalElementsCount / (m1.SudokuSize * m1.SudokuSize); return(new Tuple <double, int>(similarityPercentage, identicalElementsCount)); }
public byte[] ExtractSecretData(int bytesAmountToExtract, ref int stegocontainerOffset, byte[] stegocontainerBytes, SudokuMatrix <byte> sudokuKey) { if (bytesAmountToExtract <= 0 || stegocontainerOffset < 0 || stegocontainerBytes == null || stegocontainerBytes.Length == 0) { throw new ArgumentException(); } ValidateSudoku(sudokuKey); int stegoIterator = stegocontainerOffset; byte[] secretData = new byte[bytesAmountToExtract]; for (int i = 0; i < bytesAmountToExtract; stegoIterator += 2, i++) { secretData[i] = sudokuKey[stegocontainerBytes[stegoIterator], stegocontainerBytes[stegoIterator + 1]]; } stegocontainerOffset = stegoIterator; return(secretData); }
internal static void Run() { Console.WriteLine("\nPlease enter the sudoku puzzle you wish to solve."); Console.WriteLine("You will be entering the nine values for each row."); Console.WriteLine("Just enter the values with no spaces, for unknown"); Console.WriteLine("values enter 0. Once you're done the solver will"); Console.WriteLine("produce an answer. The solver will notify you if"); Console.WriteLine("the sodoku puzzle cannot be solved.\n"); Console.WriteLine("Press enter to continue!"); Console.ReadLine(); var continueLoop = true; do { var response = new StringBuilder(); Console.Write("Enter the first row: "); response.Append(Console.ReadLine()); Console.Write("Enter the second row: "); response.Append(Console.ReadLine()); Console.Write("Enter the third row: "); response.Append(Console.ReadLine()); Console.Write("Enter the fourth row: "); response.Append(Console.ReadLine()); Console.Write("Enter the fifth row: "); response.Append(Console.ReadLine()); Console.Write("Enter the sixth row: "); response.Append(Console.ReadLine()); Console.Write("Enter the seventh row: "); response.Append(Console.ReadLine()); Console.Write("Enter the eighth row: "); response.Append(Console.ReadLine()); Console.Write("Enter the ninth row: "); response.Append(Console.ReadLine()); Console.Write("\nPress enter to continue... "); Console.ReadLine(); Console.WriteLine(); var matrix = new SudokuMatrix(response.ToString()); Task solver = matrix.Solve(); ConsoleSpiner spin = new ConsoleSpiner(); while (!solver.IsCompleted) { spin.Turn(); } Console.WriteLine(); Console.Beep(); if (matrix.IsValid()) { var displayMatrix = new SudokuMatrix(matrix.ToIntList()); displayMatrix.SetDifficulty( new Difficulty { Name = "Test", DifficultyLevel = DifficultyLevel.TEST }); DisplayScreens.DisplayMatix(displayMatrix); // Format and display the TimeSpan value. string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", matrix.Stopwatch.Elapsed.Hours, matrix.Stopwatch.Elapsed.Minutes, matrix.Stopwatch.Elapsed.Seconds, matrix.Stopwatch.Elapsed.Milliseconds / 10); Console.Write("\n\nTime to generate solution: " + elapsedTime + "\n\n"); } else { Console.WriteLine("\nNeed more values in order to deduce a solution.\n"); } Console.Write("Would you like to solve another solution (yes/no): "); var result = Console.ReadLine(); if (result.ToLower().Equals("no") || result.ToLower().Equals("n")) { continueLoop = false; } else { Console.WriteLine(); } } while (continueLoop); }
internal static List <int> IsolateIntersectingValues(ISudokuMatrix sudokuMatrix, List <int> paramList) { var MissingFirstColumn = MissingSudokuValues(sudokuMatrix.FirstColumnValues); var MissingSecondColumn = MissingSudokuValues(sudokuMatrix.SecondColumnValues); var MissingThirdColumn = MissingSudokuValues(sudokuMatrix.ThirdColumnValues); var MissingFourthColumn = MissingSudokuValues(sudokuMatrix.FourthColumnValues); var MissingFifthColumn = MissingSudokuValues(sudokuMatrix.FifthColumnValues); var MissingSixthColumn = MissingSudokuValues(sudokuMatrix.SixthColumnValues); var MissingSeventhColumn = MissingSudokuValues(sudokuMatrix.SeventhColumnValues); var MissingEighthColumn = MissingSudokuValues(sudokuMatrix.EighthColumnValues); var MissingNinthColumn = MissingSudokuValues(sudokuMatrix.NinthColumnValues); var MissingFirstRegion = MissingSudokuValues(sudokuMatrix.FirstRegionValues); var MissingSecondRegion = MissingSudokuValues(sudokuMatrix.SecondRegionValues); var MissingThirdRegion = MissingSudokuValues(sudokuMatrix.ThirdRegionValues); var MissingFourthRegion = MissingSudokuValues(sudokuMatrix.FourthRegionValues); var MissingFifthRegion = MissingSudokuValues(sudokuMatrix.FifthRegionValues); var MissingSixthRegion = MissingSudokuValues(sudokuMatrix.SixthRegionValues); var MissingSeventhRegion = MissingSudokuValues(sudokuMatrix.SeventhRegionValues); var MissingEighthRegion = MissingSudokuValues(sudokuMatrix.EighthRegionValues); var MissingNinthRegion = MissingSudokuValues(sudokuMatrix.NinthRegionValues); var MissingFirstRow = MissingSudokuValues(sudokuMatrix.FirstRowValues); var MissingSecondRow = MissingSudokuValues(sudokuMatrix.SecondRowValues); var MissingThirdRow = MissingSudokuValues(sudokuMatrix.ThirdRowValues); var MissingFourthRow = MissingSudokuValues(sudokuMatrix.FourthRowValues); var MissingFifthRow = MissingSudokuValues(sudokuMatrix.FifthRowValues); var MissingSixthRow = MissingSudokuValues(sudokuMatrix.SixthRowValues); var MissingSeventhRow = MissingSudokuValues(sudokuMatrix.SeventhRowValues); var MissingEighthRow = MissingSudokuValues(sudokuMatrix.EighthRowValues); var MissingNinthRow = MissingSudokuValues(sudokuMatrix.NinthRowValues); var tmp = new SudokuMatrix(paramList); var count = 0; do { for (var i = 0; i < tmp.SudokuCells.Count; i++) { if (i == 0) { ReviewSudokuCharacterForPossibleUpdate(ref MissingFirstColumn, ref MissingFirstRegion, ref MissingFirstRow, tmp.SudokuCells[i]); } else if (i == 1) { ReviewSudokuCharacterForPossibleUpdate(ref MissingSecondColumn, ref MissingFirstRegion, ref MissingFirstRow, tmp.SudokuCells[i]); } else if (i == 2) { ReviewSudokuCharacterForPossibleUpdate(ref MissingThirdColumn, ref MissingFirstRegion, ref MissingFirstRow, tmp.SudokuCells[i]); } else if (i == 3) { ReviewSudokuCharacterForPossibleUpdate(ref MissingFourthColumn, ref MissingSecondRegion, ref MissingFirstRow, tmp.SudokuCells[i]); } else if (i == 4) { ReviewSudokuCharacterForPossibleUpdate(ref MissingFifthColumn, ref MissingSecondRegion, ref MissingFirstRow, tmp.SudokuCells[i]); } else if (i == 5) { ReviewSudokuCharacterForPossibleUpdate(ref MissingSixthColumn, ref MissingSecondRegion, ref MissingFirstRow, tmp.SudokuCells[i]); } else if (i == 6) { ReviewSudokuCharacterForPossibleUpdate(ref MissingSeventhColumn, ref MissingThirdRegion, ref MissingFirstRow, tmp.SudokuCells[i]); } else if (i == 7) { ReviewSudokuCharacterForPossibleUpdate(ref MissingEighthColumn, ref MissingThirdRegion, ref MissingFirstRow, tmp.SudokuCells[i]); } else if (i == 8) { ReviewSudokuCharacterForPossibleUpdate(ref MissingNinthColumn, ref MissingThirdRegion, ref MissingFirstRow, tmp.SudokuCells[i]); } else if (i == 9) { ReviewSudokuCharacterForPossibleUpdate(ref MissingFirstColumn, ref MissingFirstRegion, ref MissingSecondRow, tmp.SudokuCells[i]); } else if (i == 10) { ReviewSudokuCharacterForPossibleUpdate(ref MissingSecondColumn, ref MissingFirstRegion, ref MissingSecondRow, tmp.SudokuCells[i]); } else if (i == 11) { ReviewSudokuCharacterForPossibleUpdate(ref MissingThirdColumn, ref MissingFirstRegion, ref MissingSecondRow, tmp.SudokuCells[i]); } else if (i == 12) { ReviewSudokuCharacterForPossibleUpdate(ref MissingFourthColumn, ref MissingSecondRegion, ref MissingSecondRow, tmp.SudokuCells[i]); } else if (i == 13) { ReviewSudokuCharacterForPossibleUpdate(ref MissingFifthColumn, ref MissingSecondRegion, ref MissingSecondRow, tmp.SudokuCells[i]); } else if (i == 14) { ReviewSudokuCharacterForPossibleUpdate(ref MissingSixthColumn, ref MissingSecondRegion, ref MissingSecondRow, tmp.SudokuCells[i]); } else if (i == 15) { ReviewSudokuCharacterForPossibleUpdate(ref MissingSeventhColumn, ref MissingThirdRegion, ref MissingSecondRow, tmp.SudokuCells[i]); } else if (i == 16) { ReviewSudokuCharacterForPossibleUpdate(ref MissingEighthColumn, ref MissingThirdRegion, ref MissingSecondRow, tmp.SudokuCells[i]); } else if (i == 17) { ReviewSudokuCharacterForPossibleUpdate(ref MissingNinthColumn, ref MissingThirdRegion, ref MissingSecondRow, tmp.SudokuCells[i]); } else if (i == 18) { ReviewSudokuCharacterForPossibleUpdate(ref MissingFirstColumn, ref MissingFirstRegion, ref MissingThirdRow, tmp.SudokuCells[i]); } else if (i == 19) { ReviewSudokuCharacterForPossibleUpdate(ref MissingSecondColumn, ref MissingFirstRegion, ref MissingThirdRow, tmp.SudokuCells[i]); } else if (i == 20) { ReviewSudokuCharacterForPossibleUpdate(ref MissingThirdColumn, ref MissingFirstRegion, ref MissingThirdRow, tmp.SudokuCells[i]); } else if (i == 21) { ReviewSudokuCharacterForPossibleUpdate(ref MissingFourthColumn, ref MissingSecondRegion, ref MissingThirdRow, tmp.SudokuCells[i]); } else if (i == 22) { ReviewSudokuCharacterForPossibleUpdate(ref MissingFifthColumn, ref MissingSecondRegion, ref MissingThirdRow, tmp.SudokuCells[i]); } else if (i == 23) { ReviewSudokuCharacterForPossibleUpdate(ref MissingSixthColumn, ref MissingSecondRegion, ref MissingThirdRow, tmp.SudokuCells[i]); } else if (i == 24) { ReviewSudokuCharacterForPossibleUpdate(ref MissingSeventhColumn, ref MissingThirdRegion, ref MissingThirdRow, tmp.SudokuCells[i]); } else if (i == 25) { ReviewSudokuCharacterForPossibleUpdate(ref MissingEighthColumn, ref MissingThirdRegion, ref MissingThirdRow, tmp.SudokuCells[i]); } else if (i == 26) { ReviewSudokuCharacterForPossibleUpdate(ref MissingNinthColumn, ref MissingThirdRegion, ref MissingThirdRow, tmp.SudokuCells[i]); } else if (i == 27) { ReviewSudokuCharacterForPossibleUpdate(ref MissingFirstColumn, ref MissingFourthRegion, ref MissingFourthRow, tmp.SudokuCells[i]); } else if (i == 28) { ReviewSudokuCharacterForPossibleUpdate(ref MissingSecondColumn, ref MissingFourthRegion, ref MissingFourthRow, tmp.SudokuCells[i]); } else if (i == 29) { ReviewSudokuCharacterForPossibleUpdate(ref MissingThirdColumn, ref MissingFourthRegion, ref MissingFourthRow, tmp.SudokuCells[i]); } else if (i == 30) { ReviewSudokuCharacterForPossibleUpdate(ref MissingFourthColumn, ref MissingFifthRegion, ref MissingFourthRow, tmp.SudokuCells[i]); } else if (i == 31) { ReviewSudokuCharacterForPossibleUpdate(ref MissingFifthColumn, ref MissingFifthRegion, ref MissingFourthRow, tmp.SudokuCells[i]); } else if (i == 32) { ReviewSudokuCharacterForPossibleUpdate(ref MissingSixthColumn, ref MissingFifthRegion, ref MissingFourthRow, tmp.SudokuCells[i]); } else if (i == 33) { ReviewSudokuCharacterForPossibleUpdate(ref MissingSeventhColumn, ref MissingSixthRegion, ref MissingFourthRow, tmp.SudokuCells[i]); } else if (i == 34) { ReviewSudokuCharacterForPossibleUpdate(ref MissingEighthColumn, ref MissingSixthRegion, ref MissingFourthRow, tmp.SudokuCells[i]); } else if (i == 35) { ReviewSudokuCharacterForPossibleUpdate(ref MissingNinthColumn, ref MissingSixthRegion, ref MissingFourthRow, tmp.SudokuCells[i]); } else if (i == 36) { ReviewSudokuCharacterForPossibleUpdate(ref MissingFirstColumn, ref MissingFourthRegion, ref MissingFifthRow, tmp.SudokuCells[i]); } else if (i == 37) { ReviewSudokuCharacterForPossibleUpdate(ref MissingSecondColumn, ref MissingFourthRegion, ref MissingFifthRow, tmp.SudokuCells[i]); } else if (i == 38) { ReviewSudokuCharacterForPossibleUpdate(ref MissingThirdColumn, ref MissingFourthRegion, ref MissingFifthRow, tmp.SudokuCells[i]); } else if (i == 39) { ReviewSudokuCharacterForPossibleUpdate(ref MissingFourthColumn, ref MissingFifthRegion, ref MissingFifthRow, tmp.SudokuCells[i]); } else if (i == 40) { ReviewSudokuCharacterForPossibleUpdate(ref MissingFifthColumn, ref MissingFifthRegion, ref MissingFifthRow, tmp.SudokuCells[i]); } else if (i == 41) { ReviewSudokuCharacterForPossibleUpdate(ref MissingSixthColumn, ref MissingFifthRegion, ref MissingFifthRow, tmp.SudokuCells[i]); } else if (i == 42) { ReviewSudokuCharacterForPossibleUpdate(ref MissingSeventhColumn, ref MissingSixthRegion, ref MissingFifthRow, tmp.SudokuCells[i]); } else if (i == 43) { ReviewSudokuCharacterForPossibleUpdate(ref MissingEighthColumn, ref MissingSixthRegion, ref MissingFifthRow, tmp.SudokuCells[i]); } else if (i == 44) { ReviewSudokuCharacterForPossibleUpdate(ref MissingNinthColumn, ref MissingSixthRegion, ref MissingFifthRow, tmp.SudokuCells[i]); } else if (i == 45) { ReviewSudokuCharacterForPossibleUpdate(ref MissingFirstColumn, ref MissingFourthRegion, ref MissingSixthRow, tmp.SudokuCells[i]); } else if (i == 46) { ReviewSudokuCharacterForPossibleUpdate(ref MissingSecondColumn, ref MissingFourthRegion, ref MissingSixthRow, tmp.SudokuCells[i]); } else if (i == 47) { ReviewSudokuCharacterForPossibleUpdate(ref MissingThirdColumn, ref MissingFourthRegion, ref MissingSixthRow, tmp.SudokuCells[i]); } else if (i == 48) { ReviewSudokuCharacterForPossibleUpdate(ref MissingFourthColumn, ref MissingFifthRegion, ref MissingSixthRow, tmp.SudokuCells[i]); } else if (i == 49) { ReviewSudokuCharacterForPossibleUpdate(ref MissingFifthColumn, ref MissingFifthRegion, ref MissingSixthRow, tmp.SudokuCells[i]); } else if (i == 50) { ReviewSudokuCharacterForPossibleUpdate(ref MissingSixthColumn, ref MissingFifthRegion, ref MissingSixthRow, tmp.SudokuCells[i]); } else if (i == 51) { ReviewSudokuCharacterForPossibleUpdate(ref MissingSeventhColumn, ref MissingSixthRegion, ref MissingSixthRow, tmp.SudokuCells[i]); } else if (i == 52) { ReviewSudokuCharacterForPossibleUpdate(ref MissingEighthColumn, ref MissingSixthRegion, ref MissingSixthRow, tmp.SudokuCells[i]); } else if (i == 53) { ReviewSudokuCharacterForPossibleUpdate(ref MissingNinthColumn, ref MissingSixthRegion, ref MissingSixthRow, tmp.SudokuCells[i]); } else if (i == 54) { ReviewSudokuCharacterForPossibleUpdate(ref MissingFirstColumn, ref MissingSeventhRegion, ref MissingSeventhRow, tmp.SudokuCells[i]); } else if (i == 55) { ReviewSudokuCharacterForPossibleUpdate(ref MissingSecondColumn, ref MissingSeventhRegion, ref MissingSeventhRow, tmp.SudokuCells[i]); } else if (i == 56) { ReviewSudokuCharacterForPossibleUpdate(ref MissingThirdColumn, ref MissingSeventhRegion, ref MissingSeventhRow, tmp.SudokuCells[i]); } else if (i == 57) { ReviewSudokuCharacterForPossibleUpdate(ref MissingFourthColumn, ref MissingEighthRegion, ref MissingSeventhRow, tmp.SudokuCells[i]); } else if (i == 58) { ReviewSudokuCharacterForPossibleUpdate(ref MissingFifthColumn, ref MissingEighthRegion, ref MissingSeventhRow, tmp.SudokuCells[i]); } else if (i == 59) { ReviewSudokuCharacterForPossibleUpdate(ref MissingSixthColumn, ref MissingEighthRegion, ref MissingSeventhRow, tmp.SudokuCells[i]); } else if (i == 60) { ReviewSudokuCharacterForPossibleUpdate(ref MissingSeventhColumn, ref MissingNinthRegion, ref MissingSeventhRow, tmp.SudokuCells[i]); } else if (i == 61) { ReviewSudokuCharacterForPossibleUpdate(ref MissingEighthColumn, ref MissingNinthRegion, ref MissingSeventhRow, tmp.SudokuCells[i]); } else if (i == 62) { ReviewSudokuCharacterForPossibleUpdate(ref MissingNinthColumn, ref MissingNinthRegion, ref MissingSeventhRow, tmp.SudokuCells[i]); } else if (i == 63) { ReviewSudokuCharacterForPossibleUpdate(ref MissingFirstColumn, ref MissingSeventhRegion, ref MissingEighthRow, tmp.SudokuCells[i]); } else if (i == 64) { ReviewSudokuCharacterForPossibleUpdate(ref MissingSecondColumn, ref MissingSeventhRegion, ref MissingEighthRow, tmp.SudokuCells[i]); } else if (i == 65) { ReviewSudokuCharacterForPossibleUpdate(ref MissingThirdColumn, ref MissingSeventhRegion, ref MissingEighthRow, tmp.SudokuCells[i]); } else if (i == 66) { ReviewSudokuCharacterForPossibleUpdate(ref MissingFourthColumn, ref MissingEighthRegion, ref MissingEighthRow, tmp.SudokuCells[i]); } else if (i == 67) { ReviewSudokuCharacterForPossibleUpdate(ref MissingFifthColumn, ref MissingEighthRegion, ref MissingEighthRow, tmp.SudokuCells[i]); } else if (i == 68) { ReviewSudokuCharacterForPossibleUpdate(ref MissingSixthColumn, ref MissingEighthRegion, ref MissingEighthRow, tmp.SudokuCells[i]); } else if (i == 69) { ReviewSudokuCharacterForPossibleUpdate(ref MissingSeventhColumn, ref MissingNinthRegion, ref MissingEighthRow, tmp.SudokuCells[i]); } else if (i == 70) { ReviewSudokuCharacterForPossibleUpdate(ref MissingEighthColumn, ref MissingNinthRegion, ref MissingEighthRow, tmp.SudokuCells[i]); } else if (i == 71) { ReviewSudokuCharacterForPossibleUpdate(ref MissingNinthColumn, ref MissingNinthRegion, ref MissingEighthRow, tmp.SudokuCells[i]); } else if (i == 72) { ReviewSudokuCharacterForPossibleUpdate(ref MissingFirstColumn, ref MissingSeventhRegion, ref MissingNinthRow, tmp.SudokuCells[i]); } else if (i == 73) { ReviewSudokuCharacterForPossibleUpdate(ref MissingSecondColumn, ref MissingSeventhRegion, ref MissingNinthRow, tmp.SudokuCells[i]); } else if (i == 74) { ReviewSudokuCharacterForPossibleUpdate(ref MissingThirdColumn, ref MissingSeventhRegion, ref MissingNinthRow, tmp.SudokuCells[i]); } else if (i == 75) { ReviewSudokuCharacterForPossibleUpdate(ref MissingFourthColumn, ref MissingEighthRegion, ref MissingNinthRow, tmp.SudokuCells[i]); } else if (i == 76) { ReviewSudokuCharacterForPossibleUpdate(ref MissingFifthColumn, ref MissingEighthRegion, ref MissingNinthRow, tmp.SudokuCells[i]); } else if (i == 77) { ReviewSudokuCharacterForPossibleUpdate(ref MissingSixthColumn, ref MissingEighthRegion, ref MissingNinthRow, tmp.SudokuCells[i]); } else if (i == 78) { ReviewSudokuCharacterForPossibleUpdate(ref MissingSeventhColumn, ref MissingNinthRegion, ref MissingNinthRow, tmp.SudokuCells[i]); } else if (i == 79) { ReviewSudokuCharacterForPossibleUpdate(ref MissingEighthColumn, ref MissingNinthRegion, ref MissingNinthRow, tmp.SudokuCells[i]); } else { ReviewSudokuCharacterForPossibleUpdate(ref MissingNinthColumn, ref MissingNinthRegion, ref MissingNinthRow, tmp.SudokuCells[i]); } } count++; } while (count < 10); var result = new SudokuMatrix(tmp.ToIntList()); return(result.ToIntList()); }
public async Task <IBaseResult> AddSolutions(int limitArg) { var result = new BaseResult(); if (limitArg == 0) { result.Success = false; result.Message = SolutionsMessages.SolutionsNotAddedMessage; return(result); } var limit = 1000; if (limitArg <= limit) { limit = limitArg; } var reduceLimitBy = 0; var solutionsInDB = new List <List <int> >(); try { var solutions = (await solutionsRepository.GetSolvedSolutions()) .Objects.ConvertAll(s => (SudokuSolution)s); foreach (var solution in solutions) { solutionsInDB.Add(solution.SolutionList); } } catch (Exception exp) { result.Success = false; result.Message = exp.Message; return(result); } var matrix = new SudokuMatrix(); try { List <List <int> > solutionsList = new List <List <int> >(); List <SudokuSolution> newSolutions = new List <SudokuSolution>(); var continueLoop = true; do { for (var i = 0; i < limit - reduceLimitBy; i++) { matrix.GenerateSolution(); if (!solutionsInDB.Contains(matrix.ToIntList())) { solutionsList.Add(matrix.ToIntList()); } } solutionsList = solutionsList .Distinct() .ToList(); if (limit == solutionsList.Count) { continueLoop = false; } else { reduceLimitBy = limit - solutionsList.Count; } } while (continueLoop); foreach (var solutionList in solutionsList) { newSolutions.Add(new SudokuSolution(solutionList)); } var solutionsResponse = await solutionsRepository .AddSolutions(newSolutions.ConvertAll(s => (ISudokuSolution)s)); if (solutionsResponse.Success) { result.Success = solutionsResponse.Success; result.Message = SolutionsMessages.SolutionsAddedMessage; return(result); } else if (!solutionsResponse.Success && solutionsResponse.Exception != null) { result.Success = solutionsResponse.Success; result.Message = solutionsResponse.Exception.Message; return(result); } else { result.Success = false; result.Message = SolutionsMessages.SolutionsNotAddedMessage; return(result); } } catch (Exception exp) { result.Success = false; result.Message = exp.Message; return(result); } }
public async Task <ISolutionResult> Solve( ISolutionRequest request) { if (request == null) { throw new ArgumentNullException(nameof(request)); } var result = new SolutionResult(); try { var solvedSolutions = ((await solutionsRepository.GetSolvedSolutions()).Objects) .ConvertAll(s => (SudokuSolution)s); var intList = new List <int>(); intList.AddRange(request.FirstRow); intList.AddRange(request.SecondRow); intList.AddRange(request.ThirdRow); intList.AddRange(request.FourthRow); intList.AddRange(request.FifthRow); intList.AddRange(request.SixthRow); intList.AddRange(request.SeventhRow); intList.AddRange(request.EighthRow); intList.AddRange(request.NinthRow); var sudokuSolver = new SudokuMatrix(intList); await sudokuSolver.Solve(); if (sudokuSolver.IsValid()) { var solution = new SudokuSolution(sudokuSolver.ToIntList()); var addResultToDataContext = true; if (solvedSolutions.Count > 0) { foreach (var solvedSolution in solvedSolutions) { if (solvedSolution.ToString().Equals(solution.ToString())) { addResultToDataContext = false; } } } if (addResultToDataContext) { solution = (SudokuSolution)(await solutionsRepository.Create(solution)).Object; } else { solution = solvedSolutions.Where(s => s.SolutionList.IsThisListEqual(solution.SolutionList)).FirstOrDefault(); } result.Success = true; result.Solution = solution; result.Message = SolutionsMessages.SudokuSolutionFoundMessage; } else { intList = sudokuSolver.ToIntList(); if (solvedSolutions.Count > 0) { var solutonInDB = false; foreach (var solution in solvedSolutions) { var possibleSolution = true; for (var i = 0; i < intList.Count - 1; i++) { if (intList[i] != 0 && intList[i] != solution.SolutionList[i]) { possibleSolution = false; break; } } if (possibleSolution) { solutonInDB = possibleSolution; result.Success = possibleSolution; result.Solution = solution; result.Message = SolutionsMessages.SudokuSolutionFoundMessage; break; } } if (!solutonInDB) { result.Success = false; result.Solution = null; result.Message = SolutionsMessages.SudokuSolutionNotFoundMessage; } } else { result.Success = false; result.Solution = null; result.Message = SolutionsMessages.SudokuSolutionNotFoundMessage; } } return(result); } catch (Exception exp) { result.Success = false; result.Message = exp.Message; return(result); } }
internal static void Run() { Console.Write("\nPlease enter your nickname: "); var nickName = new string(Console.ReadLine()); var user = new User() { NickName = nickName }; Console.WriteLine("\nSet a difficulty level:\n"); Console.WriteLine("Enter 1 for Steady Sloth (EASY)"); Console.WriteLine("Enter 2 for Leaping Lemur (MEDIUM)"); Console.WriteLine("Enter 3 for Mighty Mountain Lion (HARD)"); Console.WriteLine("Enter 4 for Sneaky Shark (EVIL)\n"); Console.Write(string.Format("{0}, please make your selection: ", user.NickName)); var difficultyResponse = Console.ReadLine(); IDifficulty difficulty = new Difficulty(); if (Int32.TryParse(difficultyResponse, out var difficultyNumber)) { if (difficultyNumber == 1 || difficultyNumber == 2 || difficultyNumber == 3 || difficultyNumber == 4) { if (difficultyNumber == 1) { difficulty = new Difficulty() { Name = "Easy", DifficultyLevel = DifficultyLevel.EASY }; } else if (difficultyNumber == 2) { difficulty = new Difficulty() { Name = "Medium", DifficultyLevel = DifficultyLevel.MEDIUM }; } else if (difficultyNumber == 3) { difficulty = new Difficulty() { Name = "Hard", DifficultyLevel = DifficultyLevel.HARD }; } else if (difficultyNumber == 4) { difficulty = new Difficulty() { Name = "Evil", DifficultyLevel = DifficultyLevel.EVIL }; } } } ISudokuMatrix matrix = new SudokuMatrix(); matrix.GenerateSolution(); matrix.SetDifficulty(difficulty); IGame game = new Game( user, (SudokuMatrix)matrix, (Difficulty)difficulty); game.KeepScore = true; game.SudokuMatrix.Stopwatch.Start(); do { DisplayScreens.GameScreen(game); var command = Console.ReadLine(); command = command.ToUpper().Trim(); if (command.Equals("1") || command.Equals("ENTER") || command.Equals("2") || command.Equals("DELETE")) { var continueX = true; do { Console.Write("\nEnter the column: "); var xValue = Console.ReadLine(); if (Int32.TryParse(xValue, out var xNumber)) { if (xNumber > 0 && xNumber < 10) { var continueY = true; do { Console.Write("\nEnter the row: "); var yValue = Console.ReadLine(); if (Int32.TryParse(yValue, out var yNumber)) { if (yNumber > 0 && yNumber < 10) { var cell = game.SudokuMatrix.SudokuCells .Where(c => c.Column == xNumber && c.Row == yNumber).FirstOrDefault(); if (cell.Hidden) { bool userEntryInvalid = true; do { if (command.Equals("1") || command.Equals("ENTER")) { Console.Write("\nEnter a number from 1 through 9: "); string userEntry = Console.ReadLine(); if (Int32.TryParse(userEntry, out var userNumber)) { if (userNumber > 0 && userNumber < 10) { cell.DisplayedValue = userNumber; continueX = false; continueY = false; userEntryInvalid = false; } else { DisplayScreens.InvalidCoordinate(); } } else { DisplayScreens.InvalidCoordinate(); } } else { cell.DisplayedValue = 0; continueX = false; continueY = false; userEntryInvalid = false; } } while (userEntryInvalid); } else { Console.WriteLine("\nThis value is a hint provided by the system and cannot be changed."); Console.WriteLine("Please try again.\n\n\t (Press Enter to Continue)"); Console.ReadLine(); break; } } else { DisplayScreens.InvalidCoordinate(); } } } while (continueY); } else { DisplayScreens.InvalidCoordinate(); } } else { DisplayScreens.InvalidCommand(); } } while (continueX); } else if (command.Equals("3") || command.Equals("CHECK")) { if (game.IsSolved()) { Console.WriteLine(string.Format("\n{0}, you win!\n", user.NickName)); game.ContinueGame = false; // Format and display the TimeSpan value. string elapsedTime = String.Format("{0:00}{1:00}:{2:00}:{3:00}.{4:00}\n", game.TimeToSolve.Days, game.TimeToSolve.Hours, game.TimeToSolve.Minutes, game.TimeToSolve.Seconds, game.TimeToSolve.Milliseconds / 10); Console.Write("Time to solve: " + elapsedTime + "\n"); Console.Write("Score: " + game.Score + "\n"); } else { Console.WriteLine("\nNOPE... TRY AGAIN!"); } } else if (command.Equals("4") || command.Equals("EXIT")) { Console.Write("\n{0}, are you sure you want to exit to the main menu (yes/no): ", user.NickName); var exitCommand = Console.ReadLine(); if (exitCommand.ToLower().Equals("yes") || exitCommand.ToLower().Equals("y")) { game.ContinueGame = false; } } } while (game.ContinueGame); Console.WriteLine("\nPress enter to return to the main menu."); Console.ReadLine(); }