eg0rmaffin

sudoku GOTY Edition

Jul 14th, 2019
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.20 KB | None | 0 0
  1. /* ************************************************************************** */
  2. /*                                                                            */
  3. /*                                                        :::      ::::::::   */
  4. /*   main.c                                             :+:      :+:    :+:   */
  5. /*                                                    +:+ +:+         +:+     */
  6. /*   By: squiana <[email protected]>                     +#+  +:+       +#+        */
  7. /*                                                +#+#+#+#+#+   +#+           */
  8. /*   Created: 2019/07/14 19:50:13 by squiana           #+#    #+#             */
  9. /*   Updated: 2019/07/14 22:37:21 by squiana          ###   ########.fr       */
  10. /*                                                                            */
  11. /* ************************************************************************** */
  12.  
  13. #include <unistd.h>
  14.  
  15. int     test_symbol(int n, char **tab);
  16. int     sudoky(int place, char **tab);
  17. void    display(char **tab);
  18. int     only_one(char **tab);
  19.  
  20. int     main(int argc, char **argv)
  21. {
  22.     if (test_symbol(argc, argv) + only_one(argv) == 2)
  23.     {
  24.         if (sudoky(9, argv))
  25.             display(argv);
  26.         else
  27.             write(1, "Error\n", 6);
  28.     }
  29.     else
  30.         write(1, "Error\n", 6);
  31.     return (0);
  32. }
  33. /* ************************************************************************** */
  34. /*                                                                            */
  35. /*                                                        :::      ::::::::   */
  36. /*   test.c                                             :+:      :+:    :+:   */
  37. /*                                                    +:+ +:+         +:+     */
  38. /*   By: squiana <[email protected]>                     +#+  +:+       +#+        */
  39. /*                                                +#+#+#+#+#+   +#+           */
  40. /*   Created: 2019/07/14 20:44:39 by squiana           #+#    #+#             */
  41. /*   Updated: 2019/07/14 22:55:30 by squiana          ###   ########.fr       */
  42. /*                                                                            */
  43. /* ************************************************************************** */
  44.  
  45. int     test_line(char **tab, char nb, int i)
  46. {
  47.     int j;
  48.  
  49.     j = 0;
  50.     while (j < 9)
  51.     {
  52.         if (tab[i][j] == nb)
  53.             return (0);
  54.         j++;
  55.     }
  56.     return (1);
  57. }
  58.  
  59. int     test_column(char **tab, char nb, int j)
  60. {
  61.     int i;
  62.  
  63.     i = 1;
  64.     while (i <= 9)
  65.     {
  66.         if (tab[i][j] == nb)
  67.             return (0);
  68.         i++;
  69.     }
  70.     return (1);
  71. }
  72.  
  73. int     test_block(char **tab, int i, int j, char nb)
  74. {
  75.     int x;
  76.     int y;
  77.  
  78.     x = -1;
  79.     if (i <= 3)
  80.         i = 1;
  81.     else if (i <= 6)
  82.         i = 4;
  83.     else if (i <= 9)
  84.         i = 7;
  85.     j = j - (j % 3);
  86.     while (++x < 3)
  87.     {
  88.         y = -1;
  89.         while (++y < 3)
  90.         {
  91.             if (tab[i][j] == nb)
  92.                 return (0);
  93.             j++;
  94.         }
  95.         j = j - 3;
  96.         i++;
  97.     }
  98.     return (1);
  99. }
  100.  
  101. int     test_symbol(int n, char **tab)
  102. {
  103.     int i;
  104.     int j;
  105.  
  106.     i = 0;
  107.     if (n == 10)
  108.     {
  109.         while (++i <= 9)
  110.         {
  111.             j = 0;
  112.             while (tab[i][j])
  113.             {
  114.                 if ((tab[i][j] <= 48 || tab[i][j] > 57) && tab[i][j] != 46)
  115.                     return (0);
  116.                 j++;
  117.             }
  118.             if (j != 9)
  119.                 return (0);
  120.         }
  121.         return (1);
  122.     }
  123.     else
  124.         return (0);
  125. }
  126. /* ************************************************************************** */
  127. /*                                                                            */
  128. /*                                                        :::      ::::::::   */
  129. /*   sudoky.c                                           :+:      :+:    :+:   */
  130. /*                                                    +:+ +:+         +:+     */
  131. /*   By: squiana <[email protected]>                     +#+  +:+       +#+        */
  132. /*                                                +#+#+#+#+#+   +#+           */
  133. /*   Created: 2019/07/14 20:46:01 by squiana           #+#    #+#             */
  134. /*   Updated: 2019/07/14 22:55:34 by squiana          ###   ########.fr       */
  135. /*                                                                            */
  136. /* ************************************************************************** */
  137.  
  138. #include <unistd.h>
  139.  
  140. int     test_line(char **tab, char nb, int i);
  141. int     test_column(char **tab, char nb, int j);
  142. int     test_block(char **tab, int i, int j, char nb);
  143.  
  144. void    ft_putchar(char c)
  145. {
  146.     write(1, &c, 1);
  147. }
  148.  
  149. void    display(char **tab)
  150. {
  151.     int i;
  152.     int j;
  153.  
  154.     i = 1;
  155.     while (i <= 9)
  156.     {
  157.         j = 0;
  158.         while (tab[i][j] != '\0')
  159.         {
  160.             ft_putchar(tab[i][j]);
  161.             if (j != 8)
  162.                 ft_putchar(' ');
  163.             j++;
  164.         }
  165.         ft_putchar('\n');
  166.         i++;
  167.     }
  168. }
  169.  
  170. int     sudoky(int place, char **tab)
  171. {
  172.     int     i;
  173.     int     j;
  174.     char    nb;
  175.  
  176.     nb = '0';
  177.     i = place / 9;
  178.     j = place % 9;
  179.     if (place == 90)
  180.         return (1);
  181.     if (tab[i][j] != '.')
  182.         return (sudoky(place + 1, tab));
  183.     while (++nb <= '9')
  184.     {
  185.         if (test_line(tab, nb, i) + test_column(tab, nb, j) +
  186.         test_block(tab, i, j, nb) == 3)
  187.         {
  188.             tab[i][j] = nb;
  189.             if (sudoky(place + 1, tab))
  190.                 return (1);
  191.         }
  192.     }
  193.     tab[i][j] = '.';
  194.     return (0);
  195. }
  196.  
  197. int     only_one(char **tab)
  198. {
  199.     int l;
  200.     int i;
  201.     int j;
  202.  
  203.     l = 0;
  204.     i = 1;
  205.     while (i <= 9)
  206.     {
  207.         j = 0;
  208.         while (tab[i][j] != '\0')
  209.         {
  210.             if ((tab[i][j] >= 49 && tab[i][j] <= 57))
  211.             {
  212.                 l++;
  213.                 j++;
  214.             }
  215.             else
  216.                 j++;
  217.         }
  218.         i++;
  219.     }
  220.     if ((l < 17) || (l == 81))
  221.         return (0);
  222.     else
  223.         return (1);
  224. }
Advertisement
Add Comment
Please, Sign In to add comment