sudoku c++

hey guys so i have this sudoku solver. the code is right and it works very well. my problem is the how do i insert a function that will output a partially filled grid and will ask the user to fill the partially filled grid. please help guys.............

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
//This program solves the sudoku puzzle.
#include <iostream>
#include <cmath>
using namespace std;
void loadinput();
void printgrid();
bool checkrow(int row, int column);
bool checkcolumn(int row, int column);
bool checksquare(int row, int column);
bool solve(int row, int column);
int inputgrid[9][9]; 
struct Playinggrid {
	int number;
	bool fixed;
} 
	grid[9][9];
void loadinput(){
	for (int i = 0; i < 9; i++){
		for (int j = 0; j < 9; j++){
			if (inputgrid[i][j] == 0){
				
				grid[i][j].fixed = false;
				grid[i][j].number = 0;
			}
			
			else{
				grid[i][j].fixed = true;
				grid[i][j].number = inputgrid[i][j];
			}
		}
	}
}
				
void printgrid() {
	
	for (int i = 0; i < 9; i++)
	{
		
		for (int j = 0; j < 9; j++)
		{
			cout << grid[i][j].number << " ";
		}
		
		cout << endl;
	}
}
bool checkrow(int row, int column){
	for (int i = 0; i < 9; i++){
		if (i != column){
			if (grid[row][i].number == grid[row][column].number ){
				return false;
			}
		}
	}
		
	return true;
	
} 
bool checkcolumn(int row, int column){

	for (int i = 0; i < 9; i++){
		if (i != row){
			if (grid[i][column].number == grid[row][column].number ){
				return false;
			}
		}
	}
	return true;	
}
bool checksquare(int row, int column){
	int vsquare = row/3;
	int hsquare = column/3;
	
           	      for (int i = vsquare * 3; i < (vsquare*3 + 3); i++){
		for (int j = hsquare * 3; j < (hsquare*3 + 3); j++){
			if (!(i == row && j == column)){
				if (grid[ row ][ column ].number == grid[i][j].number){
				return false;
				}	
			}
		}	
	}
	return true;
}
bool solve(int row, int column){
		while (grid[row][column].fixed == true)
{
		column++;
		
		if(column > 8){
			column = 0;
			row++;
		}
		if (row > 8){
			return true;
		}
	}
	
	for (int n = 1; n < 10; n++){
		int nextrow, nextcolumn;
		
		grid[row][column].number = n;
		
		if ( checkcolumn(row, column) 
		  && checkrow(row, column) 
		  && checksquare(row, column)){
			nextrow = row;
			nextcolumn = column;
			
			nextcolumn++;
			if(nextcolumn > 8){
			nextcolumn = 0;
			nextrow++;
			}
			if (nextcolumn == 0 && nextrow == 9){
				return true;
			}
			if(solve(nextrow, nextcolumn)){
				return true;
			}
		}
	}
	grid[row][column].number = 0;
	return false;
	
}
int main(){
	
	solve(0, 0);
	
	printgrid();
	
	return 0;
}
closed account (o3hC5Di1)
Hi there,

So basically you are asking how to make a sudoku game?

You will have to:

1 Create a grid and partially fill it according to an algorithm: http://www.google.be/search?q=sudoku%20creation%20algorithm
2 Print the grid to the user
3 Ask the user for input
4 repeat step 2 and 3 until the sudoku is completed
5 check solution

That should get you started, let us know which of these steps you require assistance with and try to be specific about what you need help on, then you're most likely to get a response.

All the best,
NwN
Last edited on
hi NwN thanks for the help my code works gud but i want it to output a partially filled grid where the user can enter number instead my code outputs already solved puzzle directly. so basically its no.1,3 and 4.
thanks
You might want to change the solve function to creating a separate grid altogether. Then in main, you can ask the user to enter whatever numbers they want, and if they want a "hint" you can display the answer at that grid location. If they just want it solved, you can show them the separate grid that solved came up with so they know.
thanks guys
Topic archived. No new replies allowed.