Creating sudoku in C++

Creating sudoku in C++

In order to make a sudoku, you need to insult the following steps one by one:

1 Create a grid and partially fill it according to an algorithm

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 solutions

 

The algorithm you need is below:

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;
}

 

The algorithm was taken from http://www.cplusplus.com

Good luck!

Σχολιάστε

Top