1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
void Sudoku::nakedPairs()
{
bool pairFound = false;
int pairCounter = 0;
int location;
int skip = 0;
int pairCheck[81][2] = { 0 }; //initalize 2D check array
//check for the row and column of naked 2
//check row and column for duplicate
//if duplicate, fix array
for (int r = 1; r <= 9; r++) //set pairCheck array to number of placements
{
for (int c = 1; c <= 9; c++)
{
location = getLocation(r, c);
int counter = 0;
for (int n = 1; n <= 9; n++)
{
if (myArray[location].canPlaceNumber[n])
counter++;
}
if (counter == 2)
{
pairCounter++;
for (int n = 1; n <= 9; n++)
{
if (myArray[location].canPlaceNumber[n] && pairCheck[location][0] == 0)
pairCheck[getLocation(r, c)][0] = n;
else if (myArray[location].canPlaceNumber[n] && pairCheck[location][1] == 0)
pairCheck[getLocation(r, c)][1] = n;
}
}
}
}
while (pairCounter != 0)
{
for (int r = 1; r <= 9; r++)
{
bool rowDuplicate = false;
int check1 = 0;
int check2 = 0;
int location1 = 0;
int location2 = 0;
for (int c = 1; c <= 9; c++)
{
if (check1 == 0 && pairCheck[getLocation(r, c)][0] != 0)
{
pairCounter--;
check1 = pairCheck[getLocation(r, c)][0];
check2 = pairCheck[getLocation(r, c)][1];
pairCheck[getLocation(r, c)][0] = 0;
pairCheck[getLocation(r, c)][1] = 0;
location1 = getLocation(r, c);
}
else if (pairCheck[getLocation(r, c)][0] == check1 && pairCheck[getLocation(r, c)][1] == check2)
{
rowDuplicate = true;
location2 = getLocation(r, c);
}
}
if (rowDuplicate == true)
{
for (int c = 0; c <= 9; c++)
{
location = getLocation(r, c);
bool temp = true;
if (location != location1 && location != location2)
{
myArray[location].canPlaceNumber[check1] = false;
myArray[location].canPlaceNumber[check2] = false;
}
}
}
}
}
}
|