help with sudoku game

I need help trying to figure out what is wrong with my code. We are doing a project, and I keep getting an infinite loop when asking the "row and column" to put the number in. Also I keep getting a negative number when it gives me the numbers I got.

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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

void printBoard(int[3][3]);
bool checkBoard(int[3][3], int, int, int);

int main() {
	// Initialize file
	ifstream ins("gameBoard.txt");
	bool win = false;
	int *numbers;
	int winCount = 0, passCount = 0, roundCount = 0;
	int firstNumber, secondNumber, choiceNumber;
	int row, col, bord = 0;

	if (!ins.is_open())
	{
		cout << "Error opening gameBoard.txt ";
		return 0;
	}
	string lineNo;
	getline(ins, lineNo);
	ins.close();
	numbers = new int[lineNo.size()];

	for (int i = 0; i < lineNo.size(); i++)
	{
		numbers[i] = (int)lineNo[i] - 48;
	}

	int boardGame[3][3];
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j<3; j++)
			boardGame[i][j] = 0;
	}

	while (true)
	{
		if (passCount == 4)
		{
			win = false;
			break;
		}
		cout << "Round " << roundCount << endl << endl;

		printBoard(boardGame);
		firstNumber = numbers[roundCount];
		secondNumber = numbers[roundCount + 1];

		while (true)
		{
			cout << " You got numbers " << firstNumber << "," << secondNumber 
				<< endl << endl;
			cout << "Choose number (-1 to pass): ";
			cin >> choiceNumber;

			if (choiceNumber == firstNumber || 
				choiceNumber == secondNumber)
				break;
			else
				cout << "Please select right number";
		}

		if (choiceNumber == -1)
		{
			roundCount++;
			passCount++;
			continue;
		}
		else
		{
			while (true)
			{
				cout << "Enter the row and column number: ";
				cin >> row >> col;
				if (row<0 || row>2 || col<0 || col>2)
					cout << "Invalid row column" << endl;
				else
				{
					if (checkBoard(boardGame, row, col, choiceNumber))
					{
						boardGame[row][col] = choiceNumber;
						bord++;
						break;
					}
				}
			}
		}
		roundCount++;
		cout << endl << endl;

		if (bord == 9)
		{
			win = true;
			break;
		}
	}

	if (win)
	{
		cout << " You win. " << endl;
		printBoard(boardGame);
	}
	else
	{
		cout << " Sorry! You loose.... ";
	}

	return 0;
}
void printBoard(int board[3][3])
{
	cout << "-------------" << endl;
	for (int i = 0; i<3; i++)
	{
		cout << "|";

		for (int j = 0; j < 3; j++)
		{
			if (board[i][j] != 0)
				cout << " " << board[i][j] << "|";
			else
				cout << " |";
		}

		cout << endl;
	}
	cout << "-------------" << endl;
}
bool checkBoard(int board[3][3], int row, int col, int choiceNumber)
{
	if (board[row][col] == 0)
	{
		if (row == 0)
		{
			if (col == 0)
			{
				if (board[row][col + 1] == choiceNumber || 
					board[row + 1][col] == choiceNumber)
				{
					cout << "Value cannot be same to next value" << endl;
					return false;
				}
			}
			else if (col == 2)
			{
				if (board[row][col - 1] == choiceNumber || 
					board[row + 1][col] == choiceNumber)
				{
					cout << "Value cannot be same to next value" << endl;
					return false;
				}
			}
			else
			{
				if (board[row][col - 1] == choiceNumber || 
					board[row + 1][col] == choiceNumber || 
					board[row][col + 1] == choiceNumber)
				{
					cout << "Value cannot be same to next value" << endl;
					return false;
				}
			}
		}
		else if (col == 0)
		{
			if (row == 0)
			{
				if (board[row][col + 1] == choiceNumber || 
					board[row + 1][col] == choiceNumber)
				{
					cout << "Value cannot be same to next value" << endl;
					return false;
				}
			}
			else if (row == 2)
			{
				if (board[row - 1][col] == choiceNumber ||
					board[row][col + 1] == choiceNumber)
				{
					cout << "Value cannot be same to next value" << endl;
					return false;
				}
			}
			else
			{
				if (board[row - 1][col] == choiceNumber ||
					board[row][col + 1] == choiceNumber ||
					board[row + 1][col] == choiceNumber)
				{
					cout << "Value cannot be same to next value" << endl;
					return false;
				}
			}
		}
		else if (row == 2)
		{
			if (col == 0)
			{
				if (board[row - 1][col] == choiceNumber || 
					board[row][col + 1] == choiceNumber)
				{
					cout << "Value cannot be same to next value" << endl;
					return false;
				}
			}
			else if (col == 2)
			{
				if (board[row - 1][col] == choiceNumber || 
					board[row][col - 1] == choiceNumber)
				{
					cout << "Value cannot be same to next value" << endl;
					return false;
				}
			}
			else
			{
				if (board[row - 1][col] == choiceNumber || 
					board[row][col + 1] == choiceNumber || 
					board[row][col - 1] == choiceNumber)
				{
					cout << "Value cannot be same to next value" << endl;
					return false;
				}
			}
		}
		else if (col == 2)
		{
			if (row == 0)
			{
				if (board[row][col - 1] == choiceNumber || 
					board[row + 1][col] == choiceNumber)
				{
					cout << "Value cannot be same to next value" << endl;
					return false;
				}
			}
			else if (row == 2)
			{
				if (board[row - 1][col] == choiceNumber || 
					board[row][col - 1] == choiceNumber)
				{
					cout << "Value cannot be same to next value" << endl;
					return false;
				}
			}
			else
			{
				if (board[row - 1][col] == choiceNumber || 
					board[row][col - 1] == choiceNumber || 
					board[row + 1][col] == choiceNumber)
				{
					cout << "Value cannot be same to next value" << endl;
					return false;
				}
			}
		}
		else
		{
			if (board[row - 1][col] == choiceNumber || 
				board[row + 1][col] == choiceNumber || 
				board[row][col + 1] == choiceNumber || 
				board[row][col + 1] == choiceNumber)
			{
				cout << "Value cannot be same to next value" << endl;
				return false;
			}
			else
				return true;
		}
	}
	else
		return false;
}
Last edited on
> help with sudoku game
Well I call it a big problem, since what you posted was a 3x3 board for tic-tac-toe.

yes I know. Our professor gave us the direction for it to be 3 x 3. He named the project as micro sudoku.
So what's in gameBoard.txt ?

> firstNumber = numbers[roundCount];
> secondNumber = numbers[roundCount + 1];
You have no code to stop you stepping off the end of the array and reading garbage.
@senkovlad16

What does the file "gameBoard.txt", look like
it has these numbers "33424324113113242232"

Your checkBoard is broken.
1
2
3
4
5
6
7
8
9
10
11
12
$ g++ -g -Wall -Wextra foo.cpp
foo.cpp: In function ‘int main()’:
foo.cpp:29:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < lineNo.size(); i++)
                    ^
foo.cpp:15:6: warning: unused variable ‘winCount’ [-Wunused-variable]
  int winCount = 0, passCount = 0, roundCount = 0;
      ^
foo.cpp: In function ‘bool checkBoard(int (*)[3], int, int, int)’:
foo.cpp:278:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^

That last warning means the function will return some garbage value if it finds a route through the code which doesn't match any of the return paths you've implemented.

So debugging...
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
$ gdb -q ./a.out
Reading symbols from ./a.out...done.
(gdb) b checkBoard
Breakpoint 1 at 0x4015da: file foo.cpp, line 136.
(gdb) run
Starting program: /home/sc/Documents/a.out 
Round 0


| | | |
| | | |
| | | |

 You got numbers 3,3

Choose number (-1 to pass): 3
Enter the row and column number: 1 1

Breakpoint 1, checkBoard (board=0x7fffffffdc10, row=1, col=1, choiceNumber=3) at foo.cpp:136
136		if (board[row][col] == 0)
(gdb) c
Continuing.


Round 1


| | | |
| | 3| |
| | | |

 You got numbers 3,4

Choose number (-1 to pass): 4
Enter the row and column number: 0 0

Breakpoint 1, checkBoard (board=0x7fffffffdc10, row=0, col=0, choiceNumber=4) at foo.cpp:136
136		if (board[row][col] == 0)
(gdb) p *(int(*)[3])board@3
$4 = {{0, 0, 0}, {0, 3, 0}, {0, 0, 0}}
(gdb) s
138			if (row == 0)
(gdb) 
140				if (col == 0)
(gdb) 
142					if (board[row][col + 1] == choiceNumber ||
(gdb) 
143						board[row + 1][col] == choiceNumber)
(gdb) 
142					if (board[row][col + 1] == choiceNumber ||
(gdb) 
278	}

So yes, it actually happens.
The code reaches the end of the function without an explicit return true or return false.
Last edited on
Im sorry I am still new with c++. How can I debug my code so I can make the changes to make my code run.
Well in the absence of a debugger (or the unwillingness to learn to use the one you've got), you pepper the code with cout statements to help you figure it out.

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
bool checkBoard(int board[3][3], int row, int col, int choiceNumber)
{
  if (board[row][col] == 0) {
    if (row == 0) {
      if (col == 0) {
        if (board[row][col + 1] == choiceNumber ||
            board[row + 1][col] == choiceNumber) {
          cout << "Value cannot be same to next value" << endl;
          return false;
        } else {
            cout << "Mabye it's true at 0 0" << endl;
        }
      } else if (col == 2) {
        if (board[row][col - 1] == choiceNumber ||
            board[row + 1][col] == choiceNumber) {
          cout << "Value cannot be same to next value" << endl;
          return false;
        } else {
            cout << "Mabye it's true at 0 2" << endl;
        }
      } else {
        if (board[row][col - 1] == choiceNumber ||
            board[row + 1][col] == choiceNumber ||
            board[row][col + 1] == choiceNumber) {
          cout << "Value cannot be same to next value" << endl;
          return false;
        } else {
            cout << "Mabye it's true at 0 1" << endl;
        }
      }
    } else if (col == 0) {
      if (row == 0) {
        if (board[row][col + 1] == choiceNumber ||
            board[row + 1][col] == choiceNumber) {
          cout << "Value cannot be same to next value" << endl;
          return false;
        }
      } else if (row == 2) {
        if (board[row - 1][col] == choiceNumber ||
            board[row][col + 1] == choiceNumber) {
          cout << "Value cannot be same to next value" << endl;
          return false;
        }
      } else {
        if (board[row - 1][col] == choiceNumber ||
            board[row][col + 1] == choiceNumber ||
            board[row + 1][col] == choiceNumber) {
          cout << "Value cannot be same to next value" << endl;
          return false;
        }
      }
    } else if (row == 2) {
      if (col == 0) {
        if (board[row - 1][col] == choiceNumber ||
            board[row][col + 1] == choiceNumber) {
          cout << "Value cannot be same to next value" << endl;
          return false;
        }
      } else if (col == 2) {
        if (board[row - 1][col] == choiceNumber ||
            board[row][col - 1] == choiceNumber) {
          cout << "Value cannot be same to next value" << endl;
          return false;
        }
      } else {
        if (board[row - 1][col] == choiceNumber ||
            board[row][col + 1] == choiceNumber ||
            board[row][col - 1] == choiceNumber) {
          cout << "Value cannot be same to next value" << endl;
          return false;
        }
      }
    } else if (col == 2) {
      if (row == 0) {
        if (board[row][col - 1] == choiceNumber ||
            board[row + 1][col] == choiceNumber) {
          cout << "Value cannot be same to next value" << endl;
          return false;
        }
      } else if (row == 2) {
        if (board[row - 1][col] == choiceNumber ||
            board[row][col - 1] == choiceNumber) {
          cout << "Value cannot be same to next value" << endl;
          return false;
        }
      } else {
        if (board[row - 1][col] == choiceNumber ||
            board[row][col - 1] == choiceNumber ||
            board[row + 1][col] == choiceNumber) {
          cout << "Value cannot be same to next value" << endl;
          return false;
        }
      }
    } else {
      if (board[row - 1][col] == choiceNumber ||
          board[row + 1][col] == choiceNumber ||
          board[row][col + 1] == choiceNumber ||
          board[row][col + 1] == choiceNumber) {
        cout << "Value cannot be same to next value" << endl;
        return false;
      } else
        return true;
    }
  } else
    return false;
  cout << "Oops - now what?" << endl;
}


I only added extra debug in the row = 0 case, since all the other rows will have the same answer once you figure it out.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$ ./a.out 
Round 0

| | | |
| | | |
| | | |
 You got numbers 3,3

Choose number (-1 to pass): 3 
Enter the row and column number: 1 1 


Round 1

| | | |
| | 3| |
| | | |
 You got numbers 3,4

Choose number (-1 to pass): 4
Enter the row and column number: 0 0
Mabye it's true at 0 0
Oops - now what?
Enter the row and column number:  


An important lesson here is to test OFTEN!

With every line of code you write, there should be in the back of your mind a "How do I test this?" going on as well. If you've no idea how you're going to test it, you need to stop writing code until you figure it out.

Don't write the whole thing, wonder why it doesn't work, and dump the whole thing on a forum.

Topic archived. No new replies allowed.