Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* main.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: squiana <[email protected]> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2019/07/14 19:50:13 by squiana #+# #+# */
- /* Updated: 2019/07/14 22:37:21 by squiana ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include <unistd.h>
- int test_symbol(int n, char **tab);
- int sudoky(int place, char **tab);
- void display(char **tab);
- int only_one(char **tab);
- int main(int argc, char **argv)
- {
- if (test_symbol(argc, argv) + only_one(argv) == 2)
- {
- if (sudoky(9, argv))
- display(argv);
- else
- write(1, "Error\n", 6);
- }
- else
- write(1, "Error\n", 6);
- return (0);
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* test.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: squiana <[email protected]> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2019/07/14 20:44:39 by squiana #+# #+# */
- /* Updated: 2019/07/14 22:55:30 by squiana ### ########.fr */
- /* */
- /* ************************************************************************** */
- int test_line(char **tab, char nb, int i)
- {
- int j;
- j = 0;
- while (j < 9)
- {
- if (tab[i][j] == nb)
- return (0);
- j++;
- }
- return (1);
- }
- int test_column(char **tab, char nb, int j)
- {
- int i;
- i = 1;
- while (i <= 9)
- {
- if (tab[i][j] == nb)
- return (0);
- i++;
- }
- return (1);
- }
- int test_block(char **tab, int i, int j, char nb)
- {
- int x;
- int y;
- x = -1;
- if (i <= 3)
- i = 1;
- else if (i <= 6)
- i = 4;
- else if (i <= 9)
- i = 7;
- j = j - (j % 3);
- while (++x < 3)
- {
- y = -1;
- while (++y < 3)
- {
- if (tab[i][j] == nb)
- return (0);
- j++;
- }
- j = j - 3;
- i++;
- }
- return (1);
- }
- int test_symbol(int n, char **tab)
- {
- int i;
- int j;
- i = 0;
- if (n == 10)
- {
- while (++i <= 9)
- {
- j = 0;
- while (tab[i][j])
- {
- if ((tab[i][j] <= 48 || tab[i][j] > 57) && tab[i][j] != 46)
- return (0);
- j++;
- }
- if (j != 9)
- return (0);
- }
- return (1);
- }
- else
- return (0);
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* sudoky.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: squiana <[email protected]> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2019/07/14 20:46:01 by squiana #+# #+# */
- /* Updated: 2019/07/14 22:55:34 by squiana ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include <unistd.h>
- int test_line(char **tab, char nb, int i);
- int test_column(char **tab, char nb, int j);
- int test_block(char **tab, int i, int j, char nb);
- void ft_putchar(char c)
- {
- write(1, &c, 1);
- }
- void display(char **tab)
- {
- int i;
- int j;
- i = 1;
- while (i <= 9)
- {
- j = 0;
- while (tab[i][j] != '\0')
- {
- ft_putchar(tab[i][j]);
- if (j != 8)
- ft_putchar(' ');
- j++;
- }
- ft_putchar('\n');
- i++;
- }
- }
- int sudoky(int place, char **tab)
- {
- int i;
- int j;
- char nb;
- nb = '0';
- i = place / 9;
- j = place % 9;
- if (place == 90)
- return (1);
- if (tab[i][j] != '.')
- return (sudoky(place + 1, tab));
- while (++nb <= '9')
- {
- if (test_line(tab, nb, i) + test_column(tab, nb, j) +
- test_block(tab, i, j, nb) == 3)
- {
- tab[i][j] = nb;
- if (sudoky(place + 1, tab))
- return (1);
- }
- }
- tab[i][j] = '.';
- return (0);
- }
- int only_one(char **tab)
- {
- int l;
- int i;
- int j;
- l = 0;
- i = 1;
- while (i <= 9)
- {
- j = 0;
- while (tab[i][j] != '\0')
- {
- if ((tab[i][j] >= 49 && tab[i][j] <= 57))
- {
- l++;
- j++;
- }
- else
- j++;
- }
- i++;
- }
- if ((l < 17) || (l == 81))
- return (0);
- else
- return (1);
- }
Advertisement
Add Comment
Please, Sign In to add comment