Problem with Sudoku game.

Hey guys,

I'm trying to make a 6x6 Sudoku game, and i did it but with a small problem. The app is asking user if he/she wants manually to make a puzzle or she/he wants program to do it by itself. There is my problem. I made the program to check if there is no same numbers in the same row and same column and then put the random number in. But my problem is that i can't figure out how to make program to check if there is duplicate numbers in the same box. There are six 2x3 boxes because it's a 6x6 Sudoku. How do i check only that box where program wants to put the number??

Thanks in advance.
Last edited on
One idea is to use an array of bool for each box.If a number is in the box you set the value to true.

For example:
bool numbers[10]; // first index unused
if you find the number 3 in the box you set numbers[3] = true;
Thomas1965 yes, but my problem is that i don't know how to separate 6 2x3 boxes from an 6x6 array. Do you get what i mean ? Sorry my english is not so good so i can't explain really good :)
Last edited on
This is my code but it still sometimes give out same numbers in the same box...

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
bool checkBox(int row, int column, int num)
{
    for(int i = 0; i < 6; i++)
    {
        if(i == row)
        {
            if(column == 0 || column == 1 || column == 2)
            {
                for(column = 0; column < 3; column++)
                {
                    for(int k = 0; k < 6; k++)
                    {
                        if(num==puzzle[k][column]) return false;
                    }
                }
            }
            else if(column == 3 || column == 4 || column == 5)
            {
                for(column = 3; column < 6; column++)
                {
                    for(int k = 0; k < 6; k++)
                    {
                        if(num==puzzle[k][column]) return false;
                    }
                }
            }
        }
        return true;
    }

    for(int i = 0; i < 6; i++)
    {
        if(i == column)
        {
            if(row == 0 || row == 1 || row == 2)
            {
                for(row = 0; row < 3; row++)
                {
                    for(int k = 0; k < 6; k++)
                    {
                        if(num==puzzle[row][k]) return false;
                    }
                }
            }
            else  if(row == 3 || row == 4 || row == 5)
            {
                for(row = 3; row < 6; row++)
                {
                    for(int k = 0; k < 6; k++)
                    {
                        if(num==puzzle[row][k]) return false;
                    }
                }
            }
        }
        return true;
    }
}
Last edited on
How is the box organized? Is it 3 cols and 6 rows (vertically) or 6 cols and 3 rows (horizontally) ?

Could you maybe post all the code?
Of course, here it is:
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int puzzle[6][6];
int maxPuzzle = 16;


bool check(int row, int column, int num)
{
    for(int i=0; i<6; i++)
    {
        if(i==row)continue;
        if(num==puzzle[i][column]) return false;
    }
    for(int j=0; j<6; j++)
    {
        if(j==column) continue;
        if(num==puzzle[row][j]) return false;
    }
    return true;
}

bool checkBox(int row, int column, int num)
{
    if(column < 6)
    {
        if(row == 0 || row == 1)
        {
            for(int i = 0; i < 2; i++)
            {
                for(int j = 0; j < 3; j++)
                {
                    if(num == puzzle[i][j]) return false;
                }
            }
        } else return true;
        if(row == 2 || row == 3)
        {
            for(int i = 2; i < 4; i++)
            {
                for(int j = 0; j < 3; j++)
                {
                    if(num == puzzle[i][j]) return false;
                }
            }
        } else return true;
        if(row == 4 || row == 5)
        {
            for(int i = 4; i < 6; i++)
            {
                for(int j = 0; j < 3; j++)
                {
                    if(num == puzzle[i][j]) return false;
                }
            }
        } else return true;
    }
    else return 0;
}

void Show()
{
    cout << "-----------------" << endl;
     for(int i = 0; i < 6; i++)
     {
         for(int j = 0; j < 6; j++)
         {
             if(j == 0) cout << "|";
             cout << " " << puzzle[i][j];
             if(j == 2 || j == 5)
             {
                 cout << " |";
             }
         }
         if(i == 1 || i == 3)
         {
             cout << endl;
             cout << "-----------------";
         }
         cout << endl;
     }
     cout << "-----------------";
     cout << endl;
}

void MakePuzzle()
{
    int s = 0, x = 0, y = 0, num = 0;

    while(s < maxPuzzle)
    {
        cout << "Type in the position you want to put the number on: ";
        cin >> x >> y;
        cout << "Type in the number you want to put in: ";
        cin >> num;

        if(check(x,y,num) == true)
        {
            if(puzzle[x][y] == 0)
            {
                s++;
                puzzle[x][y] = num;
                Show();
                cout << endl;
            }
            else
            {
                cout << "You already have a number here." << endl;
            }
        }
        else cout << "There is that same number in this row/column! " << endl;
    }
}

void AutoMakePuzzle()
{
    int s = 0;

    while(s < maxPuzzle)
    {
        int x = rand() % 6 + 0;
        int y = rand() % 6 + 0;
        int num = rand() % 6 + 0;
        if(check(x,y,num) == true && checkBox(x,y,num) == true)
        {
            if(puzzle[x][y] == 0)
            {
                puzzle[x][y] = num;
                s++;
            }
        }
    }
}
void Clear()
{
    int x = 0, y = 0;
    cout << "What position do you want to clear: ";
    cin >> x >> y;
    puzzle[x][y] = 0;
    Show();
}

void PutNum()
{
    int x = 0, y = 0, num = 0, s = 0;
    char a,b;
    while(s < 20)
    {
        cout << "Type in the position you want to put the number on: ";
        cin >> x >> y;
        cout << "Type in the number you want to put in: ";
        cin >> num;
        if(num < 7)
        {
            if(check(x,y,num) == true)
            {
                if(puzzle[x][y] == 0)
                {
                    s++;
                    puzzle[x][y] = num;
                    Show();
                    cout << endl;
                }
                else
                {
                    cout << "You already have a number here." << endl;
                }
            }
            else cout << "There is that same number in this row/column! " << endl;
        }
        else cout << "You must type either 0 or 1 or 2 or 3 or 4 or 5 or 6..." << endl;


        cout << "Do you want to clear some field ?(y/n)" << endl;
        cin >> a;

        if(a == 'y') Clear();

        else
        {
            cout << "Do you want to stop trying?" << endl;
            cin >> b;
        }
        if(b == 'y')
        {
            cout << endl;
            cout << endl;
            cout << "Too bad :( Good luck next time!" << endl;
            return;
        }
    }

}

int main()
{
    srand(time(NULL));
    string a;

    cout << "Do you want puzzle to be made manually or auto ?" << endl;
    cin >> a;

    if(a == "auto") AutoMakePuzzle();
    else if(a == "manually") MakePuzzle();

    Show();

    cout << "The game now begins!" << endl;

    PutNum();
    return 0;
}
Last edited on
How do i check only that box where program wants to put the number??


You could write a function that only works on a box. Just copy the two rows and 3 columns.
1
2
void GetBox(int index, int box[2][3])
{}

the index of the top left box would be 0 top right would be 1 and so on.

Then you can write functions like bool ContainsNumber( int box[2][3], int num)
I hope it makes a little sense.
the index of the top left box would be 0 top right would be 1 and so on.

Sorry, i didn't get how you want to use GetBox, and is the function ContainsNumber in GetBox function ?
Basically what i understood is that first you find a box with GetBox(but i actually don't get how is it finding box) and then check if the number is in that box with ContainsNumber. Is that correct ?
I managed to make it work, but the code is kinda long. It works, but i'm sure your idea is better solution for the problem.

Here is the code :

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
bool checkBox(int row, int column, int num)
{
    bool found = false;

    if(row == 0 || row == 1 || row == 2)
    {
        if(column == 0 || column == 1 || column == 2)
        {
            for(int i = 0; i < 3; i++)
            {
                for(int j = 0; j < 3; j++)
                {
                    if(num == puzzle[i][j]) return false;
                    else found = true;
                }
            }
        }
        if(column == 3 || column == 4 || column == 5)
        {
            for(int i = 0; i < 3; i++)
            {
                for(int j = 3; j < 6; j++)
                {
                    if(num == puzzle[i][j]) return false;
                    else found = true;
                }
            }
        }
        if(column == 6 || column == 7 || column == 8)
        {
            for(int i = 0; i < 3; i++)
            {
                for(int j = 6; j < 9; j++)
                {
                    if(num == puzzle[i][j]) return false;
                    else found = true;
                }
            }
        }
    }
    if(row == 3 || row == 4 || row == 5)
    {
        if(column == 0 || column == 1 || column == 2)
        {
            for(int i = 3; i < 6; i++)
            {
                for(int j = 0; j < 3; j++)
                {
                    if(num == puzzle[i][j]) return false;
                    else found = true;
                }
            }
        }
        if(column == 3 || column == 4 || column == 5)
        {
            for(int i = 3; i < 6; i++)
            {
                for(int j = 3; j < 6; j++)
                {
                    if(num == puzzle[i][j]) return false;
                    else found = true;
                }
            }
        }
        if(column == 6 || column == 7 || column == 8)
        {
            for(int i = 3; i < 6; i++)
            {
                for(int j = 6; j < 9; j++)
                {
                    if(num == puzzle[i][j]) return false;
                    else found = true;
                }
            }
        }
    }
    if(row == 6 || row == 7 || row == 8)
    {
        if(column == 0 || column == 1 || column == 2)
        {
            for(int i = 6; i < 9; i++)
            {
                for(int j = 0; j < 3; j++)
                {
                    if(num == puzzle[i][j]) return false;
                    else found = true;
                }
            }
        }
        if(column == 3 || column == 4 || column == 5)
        {
            for(int i = 6; i < 9; i++)
            {
                for(int j = 3; j < 6; j++)
                {
                    if(num == puzzle[i][j]) return false;
                    else found = true;
                }
            }
        }
        if(column == 6 || column == 7 || column == 8)
        {
            for(int i = 6; i < 9; i++)
            {
                for(int j = 6; j < 9; j++)
                {
                    if(num == puzzle[i][j]) return false;
                    else found = true;
                }
            }
        }
    }

    if(found == true) return true;
}
Good that you got it working.

Something I don't understand. The puzzle has 6 rows and 6 columns. Why then
1
2
if(column == 6 || column == 7 || column == 8) and
if(row == 6 || row == 7 || row == 8)

From the display of the puzzle I assumed that it is divided into 6 fields each 2 rows high and 3 columns wide.
In your code you deal with 3 rows and 3 columns.
Oh sorry, i forgot to say that i changed it to 9x9 sudoku. I can give you the rest of the new code if you want to.
Do you have an idea how i could make the code smaller? For checkBox function only, the rest is fine i guess :)
Yes please post the complete code and I will have a look.
Do you have an idea how i could make the code smaller?

The only way I think of is with a kind of minigrid.
I gues you will need some code to check if it is solved or maybe solve it.
I saw this technique years ago in a book about soduko.
Do you have an idea how i could make the code smaller?


Given the appropriate constants, something like the following (untested) code:

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
const unsigned PUZZLE_WIDTH = 9;
const unsigned PUZZLE_HEIGHT = 9;

int puzzle[PUZZLE_HEIGHT][PUZZLE_WIDTH] = {};

const unsigned REGIONS_PER = 3;     // regions per column/row


bool checkBox(int row, int column, int num)
{
    const int rows_per_region = PUZZLE_HEIGHT / REGIONS_PER;
    const int cols_per_region = PUZZLE_WIDTH / REGIONS_PER;

    const int start_row = (row / REGIONS_PER) * rows_per_region;
    const int end_row = start_row + rows_per_region;

    const int start_col = (column / REGIONS_PER) * cols_per_region;
    const int end_col = start_col + cols_per_region;

    for (int r = start_row; r < end_row; ++r)
        for (int c = start_col; c < end_col; ++c)
            if (num == puzzle[r][c])
                return false;

    return true; 
}
Yes please post the complete code and I will have a look.

Hey cire helped with a great code so now i'll try to make the puzzle solve itself too :) So every solution is included in the program. I'll start some research now, and then post if i found a solution. The new code with cires's solution is on the bottom of the post :)


Given the appropriate constants, something like the following (untested) code:

Dude, just wow... :D That code is brilliant :) Good job and thanks :)


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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int maxPuzzle = 32;
int filled = 0;

const unsigned PUZZLE_WIDTH = 9;
const unsigned PUZZLE_HEIGHT = 9;

int puzzle[PUZZLE_HEIGHT][PUZZLE_WIDTH] = {};

const unsigned REGIONS_PER = 3;     // regions per column/row


bool checkBox(int row, int column, int num)
{
    const int rows_per_region = PUZZLE_HEIGHT / REGIONS_PER;
    const int cols_per_region = PUZZLE_WIDTH / REGIONS_PER;

    const int start_row = (row / REGIONS_PER) * rows_per_region;
    const int end_row = start_row + rows_per_region;

    const int start_col = (column / REGIONS_PER) * cols_per_region;
    const int end_col = start_col + cols_per_region;

    for (int r = start_row; r < end_row; ++r)
        for (int c = start_col; c < end_col; ++c)
            if (num == puzzle[r][c])
                return false;

    return true;
}

bool check(int row, int column, int num)
{
    for(int i = 0; i < 9 ; i++)
    {
        if(num == puzzle[i][column]) return false;
    }
    for(int j=0; j<9; j++)
    {
        if(num==puzzle[row][j]) return false;
    }
    return true;
}

void Show()
{
    cout << "-------------------------" << endl;
     for(int i = 0; i < 9; i++)
     {
         for(int j = 0; j < 9; j++)
         {
             if(j == 0) cout << "|";
             cout << " " << puzzle[i][j];
             if(j == 2 || j == 5 || j == 8)
             {
                 cout << " |";
             }
         }
         if(i == 2 || i == 5)
         {
             cout << endl;
             cout << "-------------------------";
         }
         cout << endl;
     }
     cout << "-------------------------";
     cout << endl;
}

void MakePuzzle()
{
    int s = 0, x = 0, y = 0, num = 0;

    while(s < maxPuzzle)
    {
        cout << "Type in the position you want to put the number on: ";
        cin >> x >> y;
        cout << "Type in the number you want to put in: ";
        cin >> num;
        if(num < 10)
        {
            if(check(x,y,num) == true && checkBox(x,y,num) == true)
            {
                if(puzzle[x][y] == 0)
                {
                    s++;
                    filled ++;
                    puzzle[x][y] = num;
                    Show();
                    cout << endl;
                }
                else
                {
                    cout << "You already have a number here." << endl;
                }
            } else cout << "There is that same number in this row/column/box! " << endl;
        } else cout << "You must type in numbers from 0-10" << endl;
    }
}

void AutoMakePuzzle()
{
    int s = 0;

    while(s < maxPuzzle)
    {
        int x = rand() % 9 + 0;
        int y = rand() % 9 + 0;
        int num = rand() % 9 + 1;

        if(check(x,y,num) == true && checkBox(x,y,num) == true)
        {
            if(puzzle[x][y] == 0)
            {
                puzzle[x][y] = num;
                filled++;
                s++;
            }
        }
    }
}
void Clear()
{
    int x = 0, y = 0;
    cout << "Don't cheat! :) Use it as undo option only :)" << endl;
    cout << "What position do you want to clear: ";
    cin >> x >> y;
    puzzle[x][y] = 0;
    filled--;
    Show();
}

void PutNum()
{
    int x = 0, y = 0, num = 0, s = 0;
    char a,b;
    while(b != 'y')
    {
        cout << "Type in the position you want to put the number on: ";
        cin >> x >> y;
        cout << "Type in the number you want to put in: ";
        cin >> num;
        if(num < 10)
        {
            if(check(x,y,num) == true && checkBox(x,y,num) == true)
            {
                if(puzzle[x][y] == 0)
                {
                    s++;
                    filled++;
                    puzzle[x][y] = num;
                    Show();
                    cout << endl;
                }
                else
                {
                    cout << "You already have a number here." << endl;
                }
            }
            else cout << "There is that same number in this row/column/box! " << endl;
        }
        else cout << "You must type in numbers from 0-10" << endl;


        cout << "Do you want to clear some field ?(y/n)" << endl;
        cin >> a;

        if(a == 'y') Clear();

        else
        {
            cout << "Do you want to stop trying?" << endl;
            cin >> b;
        }
        if(filled == 81)
        {
            cout << "Good job!!" << endl;
        }
        if(b == 'y')
        {
            cout << endl;
            cout << "Too bad :( Good luck next time!" << endl;
        }
    }
}

int main()
{
    srand(time(NULL));
    string a;

    cout << "Do you want puzzle to be made manually or auto ?" << endl;
    cin >> a;

    if(a == "auto") AutoMakePuzzle();
    else if(a == "manually") MakePuzzle();

    Show();

    cout << "The game now begins!" << endl;

    PutNum();
    return 0;
}
Last edited on
Topic archived. No new replies allowed.