Best way to check two numbers in a 3x3 box

Hi, I am making a sudoku game and I have managed to add random numbers, add numbers, and to checklines(if there is 2 numbers on the same line). I am trying to check the 3x3 boxes if the number is twice or more in the same box. I have managed to do it one way, but is this the easiest way? In that case I need to do this 9 more times, or is there a better way?
Sorry for my bad english

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
  bool checkBox(){
	int box1[3][3];

	for (int i = 0; i <= 2; i++){
		for (int j = 0; j <= 2; j++){
			box1[i][j] = board[i][j];
		}
	}

	if (box1[0][0] != 0){
		if (box1[0][0] == box1[0][1] || box1[0][0] == box1[0][2] || box1[0][0] == box1[1][0] || box1[0][0] == box1[1][1]
			|| box1[0][0] == box1[1][2] || box1[0][0] == box1[2][0] || box1[0][0] == box1[2][1] || box1[0][0] == box1[2][2]){
			return true;
		}
	}

	if (box1[0][1] != 0){
		if (box1[0][1] == box1[0][0] || box1[0][1] == box1[0][2] || box1[0][1] == box1[1][0] || box1[0][1] == box1[1][1]
			|| box1[0][1] == box1[1][2] || box1[0][1] == box1[2][0] || box1[0][1] == box1[2][1] || box1[0][1] == box1[2][2]){
			return true;
		}
	}

	if (box1[0][2] != 0){
		if (box1[0][2] == box1[0][0] || box1[0][2] == box1[0][0] || box1[0][2] == box1[1][0] || box1[0][2] == box1[1][1]
			|| box1[0][2] == box1[1][2] || box1[0][2] == box1[2][0] || box1[0][2] == box1[2][1] || box1[0][2] == box1[2][2]){
			return true;
		}
	}

	if (box1[1][0] != 0){
		if (box1[1][0] == box1[0][0] || box1[1][0] == box1[0][2] || box1[1][0] == box1[0][0] || box1[1][0] == box1[1][1]
			|| box1[1][0] == box1[1][2] || box1[1][0] == box1[2][0] || box1[1][0] == box1[2][1] || box1[1][0] == box1[2][2]){
			return true;
		}
	}

	if (box1[1][1] != 0){
		if (box1[1][1] == box1[0][0] || box1[1][1] == box1[0][2] || box1[1][1] == box1[1][0] || box1[1][1] == box1[0][0]
			|| box1[1][1] == box1[1][2] || box1[1][1] == box1[2][0] || box1[1][1] == box1[2][1] || box1[1][1] == box1[2][2]){
			return true;
		}
	}

	if (box1[1][2] != 0){
		if (box1[1][2] == box1[0][0] || box1[1][2] == box1[0][2] || box1[1][2] == box1[1][0] || box1[1][2] == box1[1][1]
			|| box1[1][2] == box1[0][0] || box1[1][2] == box1[2][0] || box1[1][2] == box1[2][1] || box1[1][2] == box1[2][2]){
			return true;
		}
	}

	if (box1[2][0] != 0){
		if (box1[2][0] == box1[0][0] || box1[2][0] == box1[0][2] || box1[2][0] == box1[1][0] || box1[2][0] == box1[1][1]
			|| box1[2][0] == box1[1][2] || box1[2][0] == box1[0][0] || box1[2][0] == box1[2][1] || box1[2][0] == box1[2][2]){
			return true;
		}
	}

	if (box1[2][1] != 0){
		if (box1[2][1] == box1[0][0] || box1[2][1] == box1[0][2] || box1[2][1] == box1[1][0] || box1[2][1] == box1[1][1]
			|| box1[2][1] == box1[1][2] || box1[2][1] == box1[2][0] || box1[2][1] == box1[0][0] || box1[2][1] == box1[2][2]){
			return true;
		}
	}
	
	if (box1[2][2] != 0){
		if (box1[2][2] == box1[0][0] || box1[2][2] == box1[0][2] || box1[2][2] == box1[1][0] || box1[2][2] == box1[1][1]
			|| box1[2][2] == box1[1][2] || box1[2][2] == box1[2][0] || box1[2][2] == box1[2][1] || box1[2][2] == box1[0][0]){
			return true;
		}
	}



	return false;
}
I recommend you actually keep a list of regions, which is just a list of cells. For each region, check to see if any numbers duplicate.
What is a region?
a list of cells.
I would write a function to check if a certain number is in the box and call it for all 9 numbers. If a number is not in the box it means that one number is there twice.

1
2
3
4
5
6
7
8
9
10
11

bool InBox(int box[3][3], int num)
{
   for (int row = 0; row < 3; row++)
      for (int col = 0; col < 3; col++)
         if (box[row][col] == num)
              return true;

   return false;
}
If I write the InBox function how can I would like the num to be another number in the box. I can't figure out how to do it
I am quite new to C++ so what are cells I can't find anything on about cells or regions
A cell is what one number occupies in sudoku. A region is a group of cells that should contain unique numbers. Each row is a region. Each column is a region. And there are 9 regions consisting of 3x3 blocks in a typical sudoku game and it would appear these are the regions we're interested in.


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
/* box_region =
    0 | 1 | 2
   ---+---+---
    3 | 4 | 5
   ---+---+---
    6 | 7 | 8

    where each region refers to a distinct 3x3 block of cells.
*/


bool checkBox(unsigned box_region)
{
    const unsigned region_row = box_region / 3;
    const unsigned region_col = box_region % 3;

    const unsigned row = region_row * 3;
    const unsigned col = region_col * 3;

    bool encountered[10] = {};

    for (unsigned r = row; r < row + 3; ++r)
        for (unsigned c = col; c < col + 3; ++c)
        {
            auto val = board[r][c];
            if (val && encountered[val])
                return false;               // duplicate encountered

            encountered[val] = true;
        }

    return true;
}
It's MUCH faster to use a bitset for this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Find the first duplicate number in the box. Return the duplicate, or 0
// if there are none
int findDupe(int box[3][3])
{
    std::bitset<9> inBox;
    for (int i=0; i<3;++i) {
        for (int j=0; j<3; ++j) {
            if (inBox.test(box[i][j])) {
                return box[i][j];
            } else {
                inBox.set(box[i][j]);
            }
        }
    }
    return 0;
}

Topic archived. No new replies allowed.