Advertisement
Guest User

garbage group

a guest
Jul 23rd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.90 KB | None | 0 0
  1. /* ************************************************************************** */
  2. /* */
  3. /* ::: :::::::: */
  4. /* endgame.c :+: :+: :+: */
  5. /* +:+ +:+ +:+ */
  6. /* By: hschou <hschou@student.42.us.org> +#+ +:+ +#+ */
  7. /* +#+#+#+#+#+ +#+ */
  8. /* Created: 2019/07/20 16:25:55 by hschou #+# #+# */
  9. /* Updated: 2019/07/21 23:22:17 by ssonu ### ########.fr */
  10. /* */
  11. /* ************************************************************************** */
  12.  
  13. void ft_putchar(char c);
  14.  
  15. void endgame(int **tb)
  16. {
  17. short i;
  18. short j;
  19.  
  20. i = 0;
  21. j = 0;
  22. while (0 <= i && i <= 8)
  23. {
  24. j = 0;
  25. while (0 <= j && j <= 8)
  26. {
  27. ft_putchar(tb[i][j] + '0');
  28. ft_putchar(' ');
  29. j++;
  30. }
  31. ft_putchar('\n');
  32. i++;
  33. }
  34. return ;
  35. }
  36. /* ************************************************************************** */
  37. /* */
  38. /* ::: :::::::: */
  39. /* error.c :+: :+: :+: */
  40. /* +:+ +:+ +:+ */
  41. /* By: hschou <hschou@student.42.us.org> +#+ +:+ +#+ */
  42. /* +#+#+#+#+#+ +#+ */
  43. /* Created: 2019/07/20 16:02:15 by hschou #+# #+# */
  44. /* Updated: 2019/07/21 23:22:08 by ssonu ### ########.fr */
  45. /* */
  46. /* ************************************************************************** */
  47.  
  48. short error(int argc, char **argv)
  49. {
  50. int i;
  51. int j;
  52.  
  53. i = 1;
  54. j = 0;
  55. if (argc != 10)
  56. return (1);
  57. while (i != 10)
  58. {
  59. while (argv[i][j] != '\0')
  60. {
  61. if (argv[i][j] == '.' || ('0' <= argv[i][j] && argv[i][j] <= '9'))
  62. j++;
  63. else
  64. return (1);
  65. }
  66. if (j != 9)
  67. return (1);
  68. j = 0;
  69. i++;
  70. }
  71. return (0);
  72. }
  73. /* ************************************************************************** */
  74. /* */
  75. /* ::: :::::::: */
  76. /* ft_putchar.c :+: :+: :+: */
  77. /* +:+ +:+ +:+ */
  78. /* By: hschou <hschou@student.42.us.org> +#+ +:+ +#+ */
  79. /* +#+#+#+#+#+ +#+ */
  80. /* Created: 2019/07/15 10:43:56 by hschou #+# #+# */
  81. /* Updated: 2019/07/21 20:53:55 by ssonu ### ########.fr */
  82. /* */
  83. /* ************************************************************************** */
  84.  
  85. #include <unistd.h>
  86.  
  87. void ft_putchar(char c)
  88. {
  89. write(1, &c, 1);
  90. }
  91. /* ************************************************************************** */
  92. /* */
  93. /* ::: :::::::: */
  94. /* ft_putstr.c :+: :+: :+: */
  95. /* +:+ +:+ +:+ */
  96. /* By: hschou <hschou@student.42.us.org> +#+ +:+ +#+ */
  97. /* +#+#+#+#+#+ +#+ */
  98. /* Created: 2019/07/15 13:07:39 by hschou #+# #+# */
  99. /* Updated: 2019/07/21 20:57:05 by ssonu ### ########.fr */
  100. /* */
  101. /* ************************************************************************** */
  102.  
  103. void ft_putchar(char c);
  104.  
  105. void ft_putstr(char *str)
  106. {
  107. int i;
  108.  
  109. i = 0;
  110. while (str[i] != '\0')
  111. ft_putchar(str[i++]);
  112. }
  113. /* ************************************************************************** */
  114. /* */
  115. /* ::: :::::::: */
  116. /* ft_sudoku.c :+: :+: :+: */
  117. /* +:+ +:+ +:+ */
  118. /* By: ssonu <marvin@42.fr> +#+ +:+ +#+ */
  119. /* +#+#+#+#+#+ +#+ */
  120. /* Created: 2019/07/21 17:02:49 by ssonu #+# #+# */
  121. /* Updated: 2019/07/21 23:28:46 by ssonu ### ########.fr */
  122. /* */
  123. /* ************************************************************************** */
  124.  
  125. int check(int y, int x, int z, int **tb)
  126. {
  127. int i;
  128. int j;
  129. int a;
  130. int b;
  131.  
  132. i = 0;
  133. j = 0;
  134. while (j >= 0 && j <= 8)
  135. if (tb[y][j++] == z)
  136. return (0);
  137. while (i >= 0 && i <= 8)
  138. if (tb[i++][x] == z)
  139. return (0);
  140. i = 0;
  141. j = 0;
  142. a = (y / 3) * 3;
  143. b = (x / 3) * 3;
  144. while (i < 3)
  145. {
  146. while (j < 3)
  147. if (tb[a + i][b + j++] == z)
  148. return (0);
  149. i++;
  150. }
  151. return (1);
  152. }
  153.  
  154. int ft_sudoku(int **tb, int num)
  155. {
  156. int y;
  157. int x;
  158. int z;
  159. int flag;
  160.  
  161. y = num / 9;
  162. x = num % 9;
  163. if (num >= 81)
  164. return (1);
  165. z = 1;
  166. if (tb[y][x] != 0)
  167. return (ft_sudoku(tb, num + 1));
  168. while (z <= 9)
  169. {
  170. flag = check(y, x, z, tb);
  171. if (flag == 1)
  172. {
  173. tb[y][x] = z;
  174. if (ft_sudoku(tb, num + 1))
  175. return (1);
  176. tb[y][x] = 0;
  177. }
  178. z++;
  179. }
  180. return (0);
  181. }
  182. /* ************************************************************************** */
  183. /* */
  184. /* ::: :::::::: */
  185. /* main.c :+: :+: :+: */
  186. /* +:+ +:+ +:+ */
  187. /* By: hschou <hschou@student.42.us.org> +#+ +:+ +#+ */
  188. /* +#+#+#+#+#+ +#+ */
  189. /* Created: 2019/07/20 20:36:52 by hschou #+# #+# */
  190. /* Updated: 2019/07/21 23:27:58 by ssonu ### ########.fr */
  191. /* */
  192. /* ************************************************************************** */
  193.  
  194. #include <unistd.h>
  195. #include <stdlib.h>
  196.  
  197. short error(int argc, char **argv);
  198. void ft_putchar(char c);
  199. void ft_putstr(char *str);
  200. int **startgame(char **argv);
  201. int ft_sudoku(int **tb, int num);
  202. void endgame(int **tb);
  203.  
  204. int main(int argc, char **argv)
  205. {
  206. int **tb;
  207. int a;
  208.  
  209. if (error(argc, argv) == 1)
  210. {
  211. ft_putstr("Error\n");
  212. return (0);
  213. }
  214. tb = startgame(argv);
  215. a = ft_sudoku(tb, 0);
  216. if (a == 1)
  217. {
  218. endgame(tb);
  219. }
  220. else
  221. ft_putstr("Error\n");
  222. return (0);
  223. }
  224. /* ************************************************************************** */
  225. /* */
  226. /* ::: :::::::: */
  227. /* startgame.c :+: :+: :+: */
  228. /* +:+ +:+ +:+ */
  229. /* By: hschou <hschou@student.42.us.org> +#+ +:+ +#+ */
  230. /* +#+#+#+#+#+ +#+ */
  231. /* Created: 2019/07/20 17:40:19 by hschou #+# #+# */
  232. /* Updated: 2019/07/21 23:23:28 by ssonu ### ########.fr */
  233. /* */
  234. /* ************************************************************************** */
  235.  
  236. #include <stdlib.h>
  237.  
  238. int str_len(char *str)
  239. {
  240. int i;
  241.  
  242. i = 0;
  243. while (str[i] != '\0')
  244. {
  245. i++;
  246. }
  247. return (i);
  248. }
  249.  
  250. int **startgame(char **argv)
  251. {
  252. short i;
  253. short j;
  254. int **tb;
  255.  
  256. tb = (int **)malloc(sizeof(int *) * 9);
  257. i = 0;
  258. while (i < 9)
  259. {
  260. if (str_len(argv[i + 1]) != 9)
  261. return (NULL);
  262. tb[i] = (int *)malloc(sizeof(int) * 9);
  263. j = 0;
  264. while (j < 9)
  265. {
  266. if (argv[i + 1][j] == '.')
  267. tb[i][j] = 0;
  268. else if (argv[i + 1][j] >= '1' && argv[i + 1][j] <= '9')
  269. tb[i][j] = argv[i + 1][j] - '0';
  270. else
  271. return (NULL);
  272. j++;
  273. }
  274. i++;
  275. }
  276. return (tb);
  277. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement