Sudoku solver problem.

I posted this on the general c++ board but it hasn't gotten any replies so I have also posted it here. :)
Ok when I try and run it it seg faults and I can't seem to find out where. Also if you see any places in my code that would cause a problem please let me know.

solve function: I have to use this function because that is what my teacher will call when he test it. So that is why I call solve_sudoku2, it makes more sence for me to solve it.
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
void solve_sudoku(int board[])
{
	int newBoard[9][9];
	bool writeable[9][9];
	int posx = 0, posy = 0, boardIndex = 0;
	for(int i = 0; i < 9; i++)
	{
		for(int j = 0; j < 9; j++)
		{
			newBoard[i][j] = board[boardIndex];
			if(board[boardIndex] != 0)
				writeable[i][j] = false;
			else
				writeable[i][j] = true;
			boardIndex++;
		}
	}
	solve_sudoku2(newBoard, writeable, posx, posy);
	boardIndex = 0;
	for(int i = 0; i < 9; i++)
	{
		for(int j = 0; j < 9; j++)
		{
			board[boardIndex] = newBoard[i][j];
			boardIndex++;
		}
	}
}


solve_sudoku2
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
void solve_sudoku2(int newBoard[9][9], bool writeable[9][9], int posx, int posy)
{
	bool canGetInt = true;
	int number = 0;
	for(posy; posy < 9; posy++)
	{
		for(posx; posx < 9; posx++)
		{
			if(writeable[posy][posx])
			{
				number = newBoard[posy][posx];
				canGetInt = get_int(newBoard, number, posx, posy);
			}
			if(!canGetInt)
			{
				while(!writeable[posy][posx])
				{
					newBoard[posy][posx] = 0;
					if(posx == 0)
					{
						posy--;
						posx = 8;
					}
					else
						posx--;
				}
				solve_sudoku2(newBoard, writeable, posx, posy);
			}
			else
			{
				newBoard[posy][posx] = number;
			}
		}
		posx = 0;
	}
}


get_int 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
bool get_int(int newBoard[9][9], int& number, int posx, int posy) 
{
	bool works = false;
	while(!works)
	{
		if(number == 9)
			return false;
		number++;
		for(int i = 0; i < 9; i++)
		{
			if(number == newBoard[posy][i])
			{
				works = false;
				break;
			}
			else
				works = true;
		}
		for(int i = 0; i < 9; i++)
		{
			if(!works)
				break;
			if(number == newBoard[i][posx])
			{
				works = false;
				break;
			}
			else
				works = true;
		}
		for(int i = (posy/3)*3; i < i+3; i++)
		{
			if(!works)
				break;
			for(int j = (posx/3)*3; j < j+3; j++)
			{
				if(number == newBoard[i][j])
				{
					works = false;
					break;
				}
				else
					works = true;
			}
		}
	}
	return true;
}


The puzzle I am trying to solve in case you need to know is:
1
2
3
4
5
6
7
8
9
0, 1, 9, 0, 0, 2, 0, 0, 0,
8, 0, 0, 6, 9, 0, 0, 0, 0,
0, 5, 7, 4, 0, 0, 0, 0, 0,
0, 0, 3, 0, 0, 0, 0, 0, 7,
0, 0, 0, 3, 2, 5, 0, 0, 0,
4, 0, 0, 0, 0, 0, 8, 0, 0,
0, 0, 0, 0, 0, 8, 4, 5, 0,
0, 0, 0, 0, 5, 3, 0, 0, 8,
0, 0, 0, 9, 0, 0, 6, 1, 0


Any help will be greatly appretiated. Thanks
Last edited on
If we could have main() as well, we could run it through our debuggers and find the error quickly (have you tried doing this yet?).

-Albatross
Heres the .h file, my main wouldn't help because I am doing cpp unit test but you have every function in the program.

1
2
3
4
5
6
7
8
#ifndef SUDOKU_H
#define SUDOKU_H

void solve_sudoku(int board[]);
void solve_sudoku2(int newBoard[9][9], bool writeable[9][9], int posx, int posy);
bool get_int(int newBoard[9][9], int &number, int posx, int posy);

#endif 
Last edited on
Ok I figured out the seg fault but now it gets stuck in an endless loop and I don't have the ability to step through with what I am using to program this, and if I try to do it with pencil and paper it would take forever. I will edit my original code so that it does not have the seg fault.
Topic archived. No new replies allowed.