Normed
This commit is contained in:
parent
39046d0ef5
commit
8e2c3ab995
|
@ -1,2 +1,3 @@
|
|||
obj/
|
||||
build/
|
||||
*.swp
|
||||
|
|
|
@ -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');
|
||||
}
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* error_check.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: akarami <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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);
|
||||
}
|
|
@ -0,0 +1,85 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* helpers.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: akarami <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/26 18:49:56 by akarami #+# #+# */
|
||||
/* Updated: 2023/03/26 18:50:40 by akarami ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#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;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
*/
|
|
@ -0,0 +1,19 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* backtrack.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: akarami <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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
|
|
@ -0,0 +1,19 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* error_check.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: akarami <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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
|
|
@ -0,0 +1,23 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* helpers.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: akarami <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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
|
|
@ -6,13 +6,13 @@
|
|||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include "include/main.h"
|
||||
#include "include/backtrack.h"
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue