diff --git a/PuzzlePlayer/Binary.cs b/PuzzlePlayer/Binary.cs index 7ac9deaaf3b775ac088f635f691679b8735973e5..833053ea16951dbac0f07230e83f54de2cc7dc44 100644 --- a/PuzzlePlayer/Binary.cs +++ b/PuzzlePlayer/Binary.cs @@ -129,8 +129,10 @@ namespace PuzzlePlayer_Namespace // loop two times for checking 0 and 1 for (int i = 0; i <= 1; i++) { + bool valid = false; + // middle check - MiddleCheck(x, y, boardToSolve, i); + valid = MiddleCheck(x, y, boardToSolve, i); // side check @@ -145,10 +147,46 @@ namespace PuzzlePlayer_Namespace // check if the space is surrounded on both sides by the same number. If it is, the checked space should be the opposite number private bool MiddleCheck(int x, int y, int[,] b, int checkFor) { + int opposite; + + if (checkFor == 0) + opposite = 1; + else + opposite = 0; + + // first check if x-1 and x+1 aren't out of bounds + // after that do the check if the move is valid + if(!(x-1 < 0 || x+1 > b.GetLength(0))) + if (b[x - 1, y] == opposite && b[x + 1, y] == opposite) + return true; + + // same for y + if (!(y - 1 < 0 || y + 1 > b.GetLength(1))) + if (b[x, y - 1] == opposite && b[x, y + 1] == opposite) + return true; + + // return false if nothing was found + return false; + } - if (b[x - 1, y]) + private bool SideCheck(int x, int y, int[,] b, int checkFor) + { + int opposite; - return false; + if (checkFor == 0) + opposite = 1; + else + opposite = 0; + + if (!(x - 2 < 0 || x + 2 > b.GetLength(0))) + if ((b[x-2,y] == opposite && b[x-1,y] == opposite) || (b[x + 2, y] == opposite && b[x + 1, y] == opposite)) + return true; + + if (!(y - 2 < 0 || y + 2 > b.GetLength(1))) + if ((b[x, y - 2] == opposite && b[x, y - 1] == opposite) || (b[x, y + 2] == opposite && b[x, y + 1] == opposite)) + return true; + + return false; } }