Advertisement
Guest User

Untitled

a guest
Jul 15th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <stdio.h>
  3.  
  4. void ft_putchar(char c)
  5. {
  6. write(1, &c, 1);
  7. }
  8.  
  9. void display(char **argv)
  10. {
  11. int y;
  12. int x;
  13.  
  14. y = 0;
  15. x = 0;
  16. while (y < 9)
  17. {
  18. while (x < 9)
  19. {
  20. ft_putchar(argv[y][x]);
  21. if (x != 8)
  22. ft_putchar(' ');
  23. x++;
  24. }
  25. x = 0;
  26. ft_putchar('\n');
  27. y++;
  28. }
  29. }
  30.  
  31. /*Test pour ligne*/
  32. int checkline(char test, char **argv, int y)
  33. {
  34. int x;
  35.  
  36. x = 0;
  37. while (x < 9)
  38. {
  39. if (argv[y][x] == test)
  40. return (0);
  41. x++;
  42. }
  43. return (1);
  44. }
  45. /*test pour colonne*/
  46. int checkcolumn(char test, char **argv, int x)
  47. {
  48. int y;
  49.  
  50. y = 0;
  51. while (y < 9)
  52. {
  53. if (argv[y][x] == test)
  54. return (0);
  55. y++;
  56. }
  57. return (1);
  58. }
  59. /*test pour block*/
  60. int checkblock(char test, char **argv, int y, int x)
  61. {
  62. int yblock;
  63. int xblock;
  64. int i;
  65. int j;
  66. ft_putchar(test);
  67. yblock = y - (y % 3);
  68. i = 0;
  69. j = 0;
  70. printf("x = %d\n", x);
  71. while (i < 3)
  72. {
  73. j = 0;
  74. xblock = x - (x % 3);
  75. while (j < 3)
  76. {
  77. ft_putchar(argv[yblock][xblock]);
  78. ft_putchar('\n');
  79. if (argv[yblock][xblock] == test)
  80. return (0);
  81. j++;
  82. xblock++;
  83. }
  84. i++;
  85. yblock++;
  86. }
  87. return (1);
  88. }
  89.  
  90. int put_case(char **argv, int position)
  91. {
  92. int y;
  93. int x;
  94. char test;
  95.  
  96. if (position == 81)
  97. return (1);
  98. y = position / 9;
  99. x = position % 9;
  100. //printf("converted position p%d to x=%d y =%d\n", position, x, y);
  101. test = '1';
  102. if (argv[y][x] != '.')
  103. {
  104. // printf("detected number %c\n", argv[y][x]);
  105. return (put_case(argv, position + 1));
  106. }
  107. // printf("detected dot processing test&backtrack\n");
  108. while (test <= '9')
  109. {
  110. if (checkline(test, argv, y) && checkcolumn(test, argv, x) && checkblock(test, argv, y, x))
  111. {
  112. // printf("condition line/column/block ok with test = %d\n", test);
  113. argv[y][x] = test;
  114. if (put_case(argv, position + 1) == 1)
  115. return (1);
  116. }
  117. test++;
  118. //printf("condition NOT ok incrementing test = %d\n", test);
  119. }
  120. argv[y][x] = '.';
  121. return (0);
  122. }
  123.  
  124. int main(int argc, char **argv)
  125. {
  126. int result;
  127. if (put_case(&argv[1], 0) == 1)
  128. display(&argv[1]);
  129. return (0);
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement