sudoku naked pairs check

I'm trying to create a sudoku solving algorithm. I have the setup for the initial possibilities and a few functions that fill in what they can for the rows, columns, and boxes. Now I'm trying to implement a naked pairs check.

I am using an array for my puzzle.

This code to check rows for naked pairs is eliminating incorrectly and I'm having trouble finding a solution.

header:
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
#pragma once
#include <iostream>

class Sudoku
{
	public:
		void solve();
		void print();
		void setOriginal(int inputArray[81]);						//generate the table
		void setPossible();						//generate all canPlace variables
		int getLocation(int r, int c);		//function to go to a position on the table
		void setNumber(int r, int c, int n);					//set number at current position
		bool checkRow(int r, int n);			//check row for duplicate n, returns false if no conflict
		bool checkCol(int c, int n);			//check column for duplicates, returns false if no conflict
		bool checkBox(int box, int n);			//check 3x3 box for duplicates, returns false if no conflict
		bool canPlace(int n);					//check if a number can be placed. Returns true if the number can be placed
		bool checkComplete();
		
		int checkIfSingle(int location);		//check if current square has only one possibility, return the number
		void fillSinglesinRow();				//set "n" if only one possible "n" in row
		void fillSinglesinCol();				//set "n" if only one possible "n" in column
		void fillSinglesinBox();				//set "n" if only one possible "n" in 3x3 box
		void fillSinglesRem();					//set "n" if only one possible "n" in a square
		void linedupinBox();					//if number placements line up in a box, then erase extra placements along the row/column
		void nakedPairs();						//if naked pairs exist in a row/column, then erase extra placements along that row/column


		bool hasNumber;
		int number;			//number 1-9
		int box;			//3x3 box 1-9
		bool canPlaceNumber[9];	//bool array makes this easier
};



function:
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;
					}

				}
			}
		}
	}
	
	
}
Last edited on
Topic archived. No new replies allowed.