Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <stdio.h>
- void ft_putchar(char c)
- {
- write(1, &c, 1);
- }
- void display(char **argv)
- {
- int y;
- int x;
- y = 0;
- x = 0;
- while (y < 9)
- {
- while (x < 9)
- {
- ft_putchar(argv[y][x]);
- if (x != 8)
- ft_putchar(' ');
- x++;
- }
- x = 0;
- ft_putchar('\n');
- y++;
- }
- }
- /*Test pour ligne*/
- int checkline(char test, char **argv, int y)
- {
- int x;
- x = 0;
- while (x < 9)
- {
- if (argv[y][x] == test)
- return (0);
- x++;
- }
- return (1);
- }
- /*test pour colonne*/
- int checkcolumn(char test, char **argv, int x)
- {
- int y;
- y = 0;
- while (y < 9)
- {
- if (argv[y][x] == test)
- return (0);
- y++;
- }
- return (1);
- }
- /*test pour block*/
- int checkblock(char test, char **argv, int y, int x)
- {
- int yblock;
- int xblock;
- int i;
- int j;
- ft_putchar(test);
- yblock = y - (y % 3);
- i = 0;
- j = 0;
- printf("x = %d\n", x);
- while (i < 3)
- {
- j = 0;
- xblock = x - (x % 3);
- while (j < 3)
- {
- ft_putchar(argv[yblock][xblock]);
- ft_putchar('\n');
- if (argv[yblock][xblock] == test)
- return (0);
- j++;
- xblock++;
- }
- i++;
- yblock++;
- }
- return (1);
- }
- int put_case(char **argv, int position)
- {
- int y;
- int x;
- char test;
- if (position == 81)
- return (1);
- y = position / 9;
- x = position % 9;
- //printf("converted position p%d to x=%d y =%d\n", position, x, y);
- test = '1';
- if (argv[y][x] != '.')
- {
- // printf("detected number %c\n", argv[y][x]);
- return (put_case(argv, position + 1));
- }
- // printf("detected dot processing test&backtrack\n");
- while (test <= '9')
- {
- if (checkline(test, argv, y) && checkcolumn(test, argv, x) && checkblock(test, argv, y, x))
- {
- // printf("condition line/column/block ok with test = %d\n", test);
- argv[y][x] = test;
- if (put_case(argv, position + 1) == 1)
- return (1);
- }
- test++;
- //printf("condition NOT ok incrementing test = %d\n", test);
- }
- argv[y][x] = '.';
- return (0);
- }
- int main(int argc, char **argv)
- {
- int result;
- if (put_case(&argv[1], 0) == 1)
- display(&argv[1]);
- return (0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement