/// <summary> /// Solves Image using BruteForce /// </summary> public SplittedImage Solve() { Console.WriteLine($"--Start {DateTime.Now}"); int move = 0; do { if (_splittedImage.CheckImageCorectness()) { break; } int[] actualImagePositions = new int[9]; GetActualImagePosition(); void GetActualImagePosition() { for (int a = 0; a < 9; a++) { actualImagePositions[a] = _splittedImage.GetImagePart((SplittedImage.ImagePosittion)a).Id; } } for (int a = 0; a < 9; a++) { if (actualImagePositions[a] != nextImagePosition[a]) { int index; for (index = 0; index < 9; index++) { if (nextImagePosition[a] == actualImagePositions[index]) { break; } } _splittedImage.MoveImagePart((SplittedImage.ImagePosittion)index, (SplittedImage.ImagePosittion)a); GetActualImagePosition(); } } move++; do { if (_splittedImage.CheckImageCorectness()) { break; } } while (NextRotate(_splittedImage)); } while (NextPermutation(nextImagePosition)); Console.WriteLine($"--End {DateTime.Now}"); return(_splittedImage); }
static void ThreadJob(object param) { object[] paramArray = (object[])param; SplittedImage splittedImage = (SplittedImage)paramArray[0]; int[] nextImagePosition = (int[])paramArray[1]; int skips = (int)paramArray[2]; object zamek = paramArray[3]; MyRef <bool> solved = (MyRef <bool>)paramArray[4]; MyRef <SplittedImage> result = (MyRef <SplittedImage>)paramArray[5]; int move = (int)paramArray[6]; int modulator = 0; int[] internalnextImagePosition; lock (zamek) { internalnextImagePosition = nextImagePosition; } int[] actualImagePositions = new int[9]; void GetActualImagePosition(SplittedImage splittedImag, int[] imagePositionArray) { for (int a = 0; a < 9; a++) { imagePositionArray[a] = splittedImag.GetImagePart((SplittedImage.ImagePosittion)a).Id; } } do { lock (zamek) { move++; if (splittedImage.CheckImageCorectness() && solved.Value == false) { solved.Value = true; GetActualImagePosition(result.Value, actualImagePositions); GetActualImagePosition(splittedImage, internalnextImagePosition); for (int a = 0; a < 9; a++) { if (actualImagePositions[a] != internalnextImagePosition[a]) { int index; for (index = 0; index < 9; index++) { if (internalnextImagePosition[a] == actualImagePositions[index]) { break; } } result.Value.MoveImagePart((SplittedImage.ImagePosittion)index, (SplittedImage.ImagePosittion)a); GetActualImagePosition(result.Value, actualImagePositions); } result.Value.GetImagePart((SplittedImage.ImagePosittion)a).Rotation = splittedImage .GetImagePart((SplittedImage.ImagePosittion)a) .Rotation; if (result.Value.CheckImageCorectness()) { break; } } break; } else if (solved.Value == true) { break; } } modulator++; if (modulator % 10 == 0) { Console.WriteLine($"{Thread.CurrentThread.Name} -- {move}"); } GetActualImagePosition(splittedImage, actualImagePositions); for (int a = 0; a < 9; a++) { if (actualImagePositions[a] != internalnextImagePosition[a]) { int index; for (index = 0; index < 9; index++) { if (internalnextImagePosition[a] == actualImagePositions[index]) { break; } } splittedImage.MoveImagePart((SplittedImage.ImagePosittion)index, (SplittedImage.ImagePosittion)a); GetActualImagePosition(splittedImage, actualImagePositions); } if (splittedImage.CheckImageCorectness()) { break; } } do { if (splittedImage.CheckImageCorectness()) { break; } } while (NextRotate(splittedImage)); } while (NextPermutation(internalnextImagePosition)); }