This commit is contained in:
Timo Schmidt 2023-03-26 18:30:20 +02:00
parent 97d992c651
commit 6ceda4e900
7 changed files with 206 additions and 0 deletions

70
tmp/backtrack.c Normal file
View File

@ -0,0 +1,70 @@
#include "helpers.h"
#include "backtrack.h"
#include <stdio.h>
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');
}
}

7
tmp/backtrack.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef BACKTRACK_H
# define BACKTRACK_H
int backtrack(int board[SIZE][SIZE], int clues[SIZE]);
void print_board(int board[SIZE][SIZE]);
#endif

25
tmp/error_check.c Normal file
View File

@ -0,0 +1,25 @@
#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);
}

7
tmp/error_check.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef ERROR_CHECK_H
# define ERROR_CHECK_H
# include "helpers.h"
int is_valid_input(int argc, char **argv);
#endif

67
tmp/helpers.c Normal file
View File

@ -0,0 +1,67 @@
#include <unistd.h>
#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;
// }
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;
}
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;
}
}

12
tmp/helpers.h Normal file
View File

@ -0,0 +1,12 @@
#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

18
tmp/main.c Normal file
View File

@ -0,0 +1,18 @@
#include "helpers.h"
#include "error_check.h"
#include "backtrack.h"
int main(int argc, char **argv)
{
int board[SIZE][SIZE];
int clues[SIZE * 4];
if (!is_valid_input(argc, argv))
return (ft_puterr(1));
get_clues(argv[1], clues);
init_board(board);
if (!backtrack(board, clues))
return (ft_puterr(2));
print_board(board);
return (0);
}