-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoku.c
127 lines (112 loc) · 2.75 KB
/
sudoku.c
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sudoku.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bwebb <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/18 12:28:54 by bwebb #+# #+# */
/* Updated: 2020/01/18 15:17:49 by bwebb ### ########.fr */
/* */
/* ************************************************************************** */
#include "./sudoku.h"
int popgrid(int grid[9][9], char **in)
{
int i;
int k;
i = 0;
while (i <= 8)
{
k = -1;
while (++k <= 8)
grid[i][k] = (ft_isdigit(in[i][k])) ? in[i][k] - '0' : 0;
i++;
}
i = 0;
while (i <= 8)
{
k = -1;
while (++k <= 8)
if (grid[i][k] && !check(grid, i, k))
return (0);
i++;
}
return (1);
}
int validin(int argc, char **argv)
{
int i;
int j;
if (argc != 10)
return (0);
i = 0;
while (i <= 8)
{
j = 0;
while(argv[i][j])
{
if (!ft_isdigit(argv[i][j]) && !(argv[i][j] == '.'))
return (0);
j++;
}
if (j != 9)
return (0);
i++;
}
return (1);
}
int putgrid(int grid[9][9])
{
int i;
int k;
ft_putstr("\e[1;1H\e[2J╔═══════╦═══════╦═══════╗\n");
i = 0;
while (i <= 8)
{
k = 0;
ft_putstr("║ ");
while (k <= 8)
{
(grid[i][k]) ? ft_putchar(grid[i][k] + '0') : ft_putchar(' ');
ft_putchar(' ');
if ((k == 2) || (k == 5))
ft_putstr("║ ");
k++;
}
ft_putstr("║\n");
if ((i == 2) || (i == 5))
ft_putstr("╠═══════╬═══════╬═══════╣\n");
i++;
}
ft_putstr("╚═══════╩═══════╩═══════╝\n");
return (usleep(1000));
}
int algo(int grid[9][9], int x, int y);
int next_cell(int grid[9][9], int x, int y) {
if (x == 8) {
if (y == 8)
return (1);
return (algo(grid, 0, y + 1));
}
return (algo(grid, x + 1, y));
}
int algo(int grid[9][9], int x, int y) {
if (grid[x][y] != 0)
return (next_cell(grid, x, y));
while (grid[x][y] < 9) {
grid[x][y]++;
if (check(grid, x, y))
if (next_cell(grid, x, y))
return (1);
}
return (grid[x][y] = 0);
}
int main(int argc, char **argv)
{
int grid[9][9];
if (validin(argc, ++argv) && popgrid(grid, argv) && algo(grid, 0, 0))
putgrid(grid);
else
ft_putendl("Error");
return (1);
}