#include <unistd.h>
int ft_strlen(char *str); //ft_strlen
int ft_check_av_entree(char **a)
{
int i;
i = 0;
while (a[1][i] != '\0')
{
if (a[1][i] >= '1' && a[1][i] <= '4' && (i % 2 == 0))
{
i++;
}
else if (a[1][i] == ' ' && (i % 2 == 1))
{
i++;
}
else
{
return (0);
}
if (ft_strlen(a[1]) == 31)
{
return (1);
}
}
return (1); // possible erreur
}
int ft_check_doublons_lig_col_sortie(int **tab_sortie, int etage, int x, int y)
{
int i;
i = 0;
while (i < 4)
{
if (tab_sortie[y][i] == etage)
{
return (0);
}
i++;
}
i = 0;
while (i < 4)
{
if (tab_sortie[i][x] == etage)
{
return (0);
}
i++;
}
return (1);
}
void ft_line(int l[4])
{
int i;
char value;
i = 0;
while (i < 4)
{
value = l[i] + '0';
write(1, &value, 1);
if (i < 3)
write(1, " ", 1);
i++;
}
write(1, "\n", 1);
}
void ft_print(int tab_sortie[4][4])
{
ft_line(tab_sortie[0]);
ft_line(tab_sortie[1]);
ft_line(tab_sortie[2]);
ft_line(tab_sortie[3]);
}
/*
* --> je modifie pour avoir un tab_sortie[4][4] en entree
void ft_print(int l1[4], int l2[4], int l3[4], int l4[4])
{
ft_line(l1);
ft_line(l2);
ft_line(l3);
ft_line(l4);
}
*/
/*#include <stdio.h>
#include <stdlib.h>
int main(int ac, char **av)
{
int a = 1;
int b = 1;
int tab_sortie[4][4];
int tab_paramettres[16] = {1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4};
while(a<=4)
{
b = 0;
while(b<=4)
{
tab_sortie[a - 1][b - 1] = tab_paramettres[(a * b) - 1];
b++;
}
a++;
}
if (ac != 17)
printf("erreur, pas le bon nombre d'entrees");
else
ft_print(tab_sortie);
return (0);
}*/
int check_for_4(int x, int y, int **tab_entree, int *tab_solutions)
{
if (tab_entree[0][x - 1] == 4)
{
tab_solutions[0] = y;
return (1);
}
if (tab_entree[1][x - 1] == 4)
{
tab_solutions[0] = (5 - y);
return (1);
}
if (tab_entree[2][y - 1] == 4)
{
tab_solutions[0] = x;
return (1);
}
if (tab_entree[1][y - 1] == 4)
{
tab_solutions[0] = (5 - x);
return (1);
}
return (0);
}
int check_for_1(int x, int y, int **tab_entree, int *tab_solutions)
{
int flag_found;
flag_found = 0;
if (y == 1)
if (tab_entree[0][x] == 1)
flag_found = 1;
if (y == 4)
if (tab_entree[1][x] == 1)
flag_found = 1;
if (x == 1)
if (tab_entree[2][y] == 1)
flag_found = 1;
if (x == 4)
if (tab_entree[3][y] == 1)
flag_found = 1;
if (flag_found)
{
tab_solutions[0] = 4;
return (1);
}
return (0);
}
int check_for_3_and_2(int x, int y, int **tab_entree, int *tab_solutions)
{
int flag_found;
flag_found = 0;
if (y == 2)
if (tab_entree[0][x] == 2 && tab_entree[1][x] == 3)
flag_found = 1;
if (y == 3)
if (tab_entree[0][x] == 3 && tab_entree[1][x] == 2)
flag_found = 1;
if (x == 2)
if (tab_entree[2][y] == 2 && tab_entree[3][y] == 3)
flag_found = 1;
if (x == 3)
if (tab_entree[2][y] == 3 && tab_entree[3][y] == 2)
flag_found = 1;
if (flag_found)
{
tab_solutions[0] = 4;
return (1);
}
return (0);
}
int ft_sortie_possible_x_y(int x, int y, int **tab_entree, int *tab_solutions)
{
int solutions;
int i;
i = 1;
while (i <= 4)
{
tab_solutions[i] = i;
i++;
}
solutions = 4;
solutions = check_for_3_and_2(x, y, tab_entree, tab_solutions);
solutions = check_for_1(x, y, tab_entree, tab_solutions);
solutions = check_for_4(x, y, tab_entree, tab_solutions);
return (solutions);
}
/* --------------------------------------------
* tab_entree [up, down, left, right][position]
*
* --------------------------------------------
* *tab_solutions -> tous les chiffres qu'on
* pourrait mettre a la case x y de la sortie
*
* --------------------------------------------
* ' o ' est aux positions
* x -> 3
* y -> 2
*
* 1 2 3 4
* -----------------
* 1 | | | | |
* -----------------
* 2 | | | o | |
* -----------------
* 3 | | | | |
* -----------------
* 4 | | | | |
* -----------------
*
* --------------------------------------------
* */
void ft_string_to_tab(char **av, int **tab_bord)
{
int i;
int j;
int k;
k = 0;
j = 0;
i = 0;
while (av[1][i] != '\0')
{
if (k > 3)
{
j++;
k = 0;
}
if (av[1][i] >= '1' && av[1][i] <= '4')
{
tab_bord[j][k] = av[1][i] - 48;
k++;
}
i++;
}
}
int ft_strlen(char *str)
{
int len;
len = 0;
while (str[len])
len++;
return (len);
}
int ft_check_av_entree(char **a);
int ft_check_doublons_lig_col_sortie(int **tab_sortie, int etage, int x, int y);
void ft_print(int tab_sortie[4][4]);
int ft_sortie_possible_x_y(int x, int y, int **tab_entree, int *tab_solutions);
void ft_string_to_tab(char **a, int **tab_bord);
int ft_strlen(char *str);
int main(int ac, char **av)
{
if (!ft_check_av_entree(av) || ac > 2)
{
write(1, "Error\n", 6);
}
return (0);
}
To embed this project on your website, copy the following code and paste it into your website's HTML: