Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* endgame.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: hschou <hschou@student.42.us.org> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2019/07/20 16:25:55 by hschou #+# #+# */
- /* Updated: 2019/07/21 23:22:17 by ssonu ### ########.fr */
- /* */
- /* ************************************************************************** */
- void ft_putchar(char c);
- void endgame(int **tb)
- {
- short i;
- short j;
- i = 0;
- j = 0;
- while (0 <= i && i <= 8)
- {
- j = 0;
- while (0 <= j && j <= 8)
- {
- ft_putchar(tb[i][j] + '0');
- ft_putchar(' ');
- j++;
- }
- ft_putchar('\n');
- i++;
- }
- return ;
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* error.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: hschou <hschou@student.42.us.org> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2019/07/20 16:02:15 by hschou #+# #+# */
- /* Updated: 2019/07/21 23:22:08 by ssonu ### ########.fr */
- /* */
- /* ************************************************************************** */
- short error(int argc, char **argv)
- {
- int i;
- int j;
- i = 1;
- j = 0;
- if (argc != 10)
- return (1);
- while (i != 10)
- {
- while (argv[i][j] != '\0')
- {
- if (argv[i][j] == '.' || ('0' <= argv[i][j] && argv[i][j] <= '9'))
- j++;
- else
- return (1);
- }
- if (j != 9)
- return (1);
- j = 0;
- i++;
- }
- return (0);
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_putchar.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: hschou <hschou@student.42.us.org> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2019/07/15 10:43:56 by hschou #+# #+# */
- /* Updated: 2019/07/21 20:53:55 by ssonu ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include <unistd.h>
- void ft_putchar(char c)
- {
- write(1, &c, 1);
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_putstr.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: hschou <hschou@student.42.us.org> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2019/07/15 13:07:39 by hschou #+# #+# */
- /* Updated: 2019/07/21 20:57:05 by ssonu ### ########.fr */
- /* */
- /* ************************************************************************** */
- void ft_putchar(char c);
- void ft_putstr(char *str)
- {
- int i;
- i = 0;
- while (str[i] != '\0')
- ft_putchar(str[i++]);
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_sudoku.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: ssonu <marvin@42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2019/07/21 17:02:49 by ssonu #+# #+# */
- /* Updated: 2019/07/21 23:28:46 by ssonu ### ########.fr */
- /* */
- /* ************************************************************************** */
- int check(int y, int x, int z, int **tb)
- {
- int i;
- int j;
- int a;
- int b;
- i = 0;
- j = 0;
- while (j >= 0 && j <= 8)
- if (tb[y][j++] == z)
- return (0);
- while (i >= 0 && i <= 8)
- if (tb[i++][x] == z)
- return (0);
- i = 0;
- j = 0;
- a = (y / 3) * 3;
- b = (x / 3) * 3;
- while (i < 3)
- {
- while (j < 3)
- if (tb[a + i][b + j++] == z)
- return (0);
- i++;
- }
- return (1);
- }
- int ft_sudoku(int **tb, int num)
- {
- int y;
- int x;
- int z;
- int flag;
- y = num / 9;
- x = num % 9;
- if (num >= 81)
- return (1);
- z = 1;
- if (tb[y][x] != 0)
- return (ft_sudoku(tb, num + 1));
- while (z <= 9)
- {
- flag = check(y, x, z, tb);
- if (flag == 1)
- {
- tb[y][x] = z;
- if (ft_sudoku(tb, num + 1))
- return (1);
- tb[y][x] = 0;
- }
- z++;
- }
- return (0);
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* main.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: hschou <hschou@student.42.us.org> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2019/07/20 20:36:52 by hschou #+# #+# */
- /* Updated: 2019/07/21 23:27:58 by ssonu ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include <unistd.h>
- #include <stdlib.h>
- short error(int argc, char **argv);
- void ft_putchar(char c);
- void ft_putstr(char *str);
- int **startgame(char **argv);
- int ft_sudoku(int **tb, int num);
- void endgame(int **tb);
- int main(int argc, char **argv)
- {
- int **tb;
- int a;
- if (error(argc, argv) == 1)
- {
- ft_putstr("Error\n");
- return (0);
- }
- tb = startgame(argv);
- a = ft_sudoku(tb, 0);
- if (a == 1)
- {
- endgame(tb);
- }
- else
- ft_putstr("Error\n");
- return (0);
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* startgame.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: hschou <hschou@student.42.us.org> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2019/07/20 17:40:19 by hschou #+# #+# */
- /* Updated: 2019/07/21 23:23:28 by ssonu ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include <stdlib.h>
- int str_len(char *str)
- {
- int i;
- i = 0;
- while (str[i] != '\0')
- {
- i++;
- }
- return (i);
- }
- int **startgame(char **argv)
- {
- short i;
- short j;
- int **tb;
- tb = (int **)malloc(sizeof(int *) * 9);
- i = 0;
- while (i < 9)
- {
- if (str_len(argv[i + 1]) != 9)
- return (NULL);
- tb[i] = (int *)malloc(sizeof(int) * 9);
- j = 0;
- while (j < 9)
- {
- if (argv[i + 1][j] == '.')
- tb[i][j] = 0;
- else if (argv[i + 1][j] >= '1' && argv[i + 1][j] <= '9')
- tb[i][j] = argv[i + 1][j] - '0';
- else
- return (NULL);
- j++;
- }
- i++;
- }
- return (tb);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement