From d4b2ad59bf571b862621f1f8f6827b46f5fcacf5 Mon Sep 17 00:00:00 2001 From: bionic85 <144353436+bionic85@users.noreply.github.com> Date: Wed, 20 Nov 2024 13:10:42 +0100 Subject: [PATCH] Binary solver is Done --- PuzzlePlayer/Binary.cs | 84 ++++++++++++++++++++++++++---------------- 1 file changed, 53 insertions(+), 31 deletions(-) diff --git a/PuzzlePlayer/Binary.cs b/PuzzlePlayer/Binary.cs index 833053e..f4edeac 100644 --- a/PuzzlePlayer/Binary.cs +++ b/PuzzlePlayer/Binary.cs @@ -105,20 +105,20 @@ namespace PuzzlePlayer_Namespace List<int[,]> result = new List<int[,]>(); for (int i = 0; i < boardToSolve.GetLength(0); i++) - for (int j = 0; j < boardToSolve.GetLength(1); + for (int j = 0; j < boardToSolve.GetLength(1);j++) { int[,] move = CheckMove(i, j, boardToSolve); if (move != null) result.Add(move); } - - return result; } private int[,] CheckMove(int x, int y, int[,] boardToSolve) { + int[,] result = boardToSolve; + bool validForZero = false; bool validForOne = false; @@ -129,65 +129,87 @@ namespace PuzzlePlayer_Namespace // loop two times for checking 0 and 1 for (int i = 0; i <= 1; i++) { - bool valid = false; - - // middle check - valid = MiddleCheck(x, y, boardToSolve, i); - - // side check - - // even 1 and 0 in one row and colum - - + int opposite; + + if (i == 0) + opposite = 1; + else + opposite = 0; + + // check if one of the checks succeded + bool valid = MiddleCheck(x, y, boardToSolve, opposite) || + SideCheck(x, y, boardToSolve, opposite) || + EvenCheck(x, y, boardToSolve, opposite); + + if (i == 0) + validForZero = valid; + else + validForOne = valid; } + // if both 1 and 0 are valid then something went wrong + if (validForZero && validForOne) + return null; + + // change the specifided place in a 0 or 1 if they are valid. if both are not valid then there is no valid move on the checked space and null is returnd. + if (validForZero) + result[x, y] = 0; + else if (validForOne) + result[x, y] = 1; + else + return null; + return result; } // 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) + if (b[x - 1, y] == checkFor && b[x + 1, y] == checkFor) 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) + if (b[x, y - 1] == checkFor && b[x, y + 1] == checkFor) return true; // return false if nothing was found return false; } + // check if the two spaces left, right, up or down of the space are the opposite number. if so return true private bool SideCheck(int x, int y, int[,] b, int checkFor) { - int opposite; - - 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)) + if ((b[x-2,y] == checkFor && b[x-1,y] == checkFor) || (b[x + 2, y] == checkFor && b[x + 1, y] == checkFor)) 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)) + if ((b[x, y - 2] == checkFor && b[x, y - 1] == checkFor) || (b[x, y + 2] == checkFor && b[x, y + 1] == checkFor)) return true; return false; } + // every row and colom should have an even number of 1's and 0's. So if the total number of 1's in a row is equal to half the with of the row a 0 should be filled in. + private bool EvenCheck(int x, int y, int[,] b, int checkFor) + { + // check for a row and colum (provided that the width and height of the board is the same) + int countRow = 0, countCol = 0; + for (int i = 0; i < b.GetLength(0); i++) + { + if(b[i,y] == checkFor) countRow++; + if(b[x,i] == checkFor) countCol++; + } + + // check if the total number of oppisite numbers is equal to half te widht/heigt + if (countRow == b.GetLength(0) / 2 || countCol == b.GetLength(1) / 2) + return true; + + return false; + } } } \ No newline at end of file -- GitLab