Skip to content
Snippets Groups Projects
Commit d4b2ad59 authored by bionic85's avatar bionic85
Browse files

Binary solver is Done

parent 4503ca8c
No related branches found
No related tags found
No related merge requests found
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment