diff --git a/ex00/.gitignore b/ex00/.gitignore index 8ce9af9..35f50e7 100644 --- a/ex00/.gitignore +++ b/ex00/.gitignore @@ -1,2 +1,3 @@ obj/ build/ +*.swp diff --git a/ex00/backtrack.c b/ex00/backtrack.c new file mode 100644 index 0000000..9101a4d --- /dev/null +++ b/ex00/backtrack.c @@ -0,0 +1,70 @@ +#include "helpers.h" +#include "backtrack.h" +#include + +int is_solved(int board[SIZE][SIZE]) +{ + int i; + int j; + + i = 0; + while (i < SIZE) + { + j = 0; + while (j < SIZE) + { + if (!board[i][j]) + return (0); + j++; + } + i++; + } + return (1); +} + +int next_cell_pos(int board[SIZE][SIZE]) +{ + int pos; + + pos = 0; + while (board[pos / SIZE][pos % SIZE]) + pos++; + return (pos); +} + +void *get_candidates(int board[SIZE][SIZE], int next_cell) +{ + return (0); +} + +int backtrack(int board[SIZE][SIZE], int clues[SIZE * 4]) +{ + int next_cell; + void *candidates; + + if (is_solved(board)) + return (1); + next_cell = next_cell_pos(board); + candidates = get_candidates(board, next_cell); + printf("%d\n", candidates[0]; + return (0); +} + +void print_board(int board[SIZE][SIZE]) +{ + int i; + int j; + + i = -1; + while (++i < SIZE) + { + j = -1; + while (++j < SIZE) + { + ft_putchar(board[i][j] + '0'); + ft_putchar(' '); + } + ft_putchar('\b'); + ft_putchar('\n'); + } +} diff --git a/ex00/error_check.c b/ex00/error_check.c new file mode 100644 index 0000000..deae3cd --- /dev/null +++ b/ex00/error_check.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* error_check.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: akarami +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/03/26 18:49:07 by akarami #+# #+# */ +/* Updated: 2023/03/26 18:49:08 by akarami ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "helpers.h" + +int is_valid_input(int argc, char **argv) +{ + char *input; + int valid_input_len; + int i; + + if (argc != 2) + return (0); + input = argv[1]; + valid_input_len = SIZE * 4 * 2 - 1; + if (ft_strlen(input) != valid_input_len) + return (0); + i = 0; + while (i < valid_input_len) + { + if (i % 2 == 0 && (input[i] < '1' || input[i] > '4')) + return (0); + if (i % 2 == 1 && input[i] != ' ') + return (0); + i++; + } + return (1); +} diff --git a/ex00/helpers.c b/ex00/helpers.c new file mode 100644 index 0000000..25f3a96 --- /dev/null +++ b/ex00/helpers.c @@ -0,0 +1,85 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* helpers.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: akarami +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/03/26 18:49:56 by akarami #+# #+# */ +/* Updated: 2023/03/26 18:50:40 by akarami ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "helpers.h" + +void ft_putchar(char c) +{ + write(1, &c, 1); +} + +void init_board(int board[SIZE][SIZE]) +{ + int i; + int j; + + i = -1; + while (++i < SIZE) + { + j = -1; + while (++j < SIZE) + board[i][j] = 0; + } +} + +int ft_puterr(int exit_code) +{ + write(1, "Error\n", 6); + return (exit_code); +} + +int ft_strlen(char *str) +{ + int i; + + i = 0; + while (str[i]) + i++; + return (i); +} + +void get_clues(char *input, int *border) +{ + int i; + int len; + + len = ft_strlen(input); + i = 0; + while (i < len) + { + *border++ = input[i] - '0'; + i += 2; + } +} + +/* + + board[0][0] = 1; + board[0][1] = 2; + board[0][2] = 3; + board[0][3] = 4; + board[1][0] = 2; + board[1][1] = 3; + board[1][2] = 4; + board[1][3] = 1; + board[2][0] = 3; + board[2][1] = 4; + board[2][2] = 1; + board[2][3] = 2; + board[3][0] = 4; + board[3][1] = 1; + board[3][2] = 2; + board[3][3] = 3; + board[3][3] = 0; + +*/ diff --git a/ex00/include/backtrack.h b/ex00/include/backtrack.h new file mode 100644 index 0000000..15acef2 --- /dev/null +++ b/ex00/include/backtrack.h @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* backtrack.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: akarami +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/03/26 18:48:28 by akarami #+# #+# */ +/* Updated: 2023/03/26 18:48:35 by akarami ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef BACKTRACK_H +# define BACKTRACK_H + +int backtrack(int board[SIZE][SIZE], int clues[SIZE]); +void print_board(int board[SIZE][SIZE]); + +#endif diff --git a/ex00/include/error_check.h b/ex00/include/error_check.h new file mode 100644 index 0000000..371c6d2 --- /dev/null +++ b/ex00/include/error_check.h @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* error_check.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: akarami +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/03/26 18:48:43 by akarami #+# #+# */ +/* Updated: 2023/03/26 18:48:44 by akarami ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ERROR_CHECK_H +# define ERROR_CHECK_H +# include "helpers.h" + +int is_valid_input(int argc, char **argv); + +#endif diff --git a/ex00/include/helpers.h b/ex00/include/helpers.h new file mode 100644 index 0000000..5781f31 --- /dev/null +++ b/ex00/include/helpers.h @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* helpers.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: akarami +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/03/26 18:50:47 by akarami #+# #+# */ +/* Updated: 2023/03/26 18:50:55 by akarami ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef HELPERS_H +# define HELPERS_H +# define SIZE 4 + +void ft_putchar(char c); +int ft_strlen(char *str); +int ft_puterr(int exit_code); +void init_board(int board[SIZE][SIZE]); +void get_clues(char *input, int *border); + +#endif diff --git a/ex00/main.c b/ex00/main.c index d50a1f7..8117f12 100644 --- a/ex00/main.c +++ b/ex00/main.c @@ -6,13 +6,13 @@ /* By: tischmid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/26 06:13:07 by tischmid #+# #+# */ -/* Updated: 2023/03/26 07:57:54 by tischmid ### ########.fr */ +/* Updated: 2023/03/26 18:51:24 by akarami ### ########.fr */ /* */ /* ************************************************************************** */ -#include #include #include "include/main.h" +#include "include/backtrack.h" int main(int argc, char **argv) {