Agus_Darmawan

temp_unblock_me_solver

Jan 21st, 2020
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.99 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <conio.h>
  5. #include <dirent.h>
  6.  
  7. void next_moves();
  8. void add(char move[][6], unsigned short i, unsigned short j);
  9. int equals(char a[][6], char b[][6]);
  10. void printm(char move[][6]);
  11.  
  12. char data_puzzle[6][6];
  13.  
  14. // parameter type adalah type kesulitan
  15. int cari_banyak_level(char type[20]) {
  16.  
  17. char directory[200] = "";
  18.  
  19. getcwd(directory, sizeof(directory));
  20. strcat(directory, type);
  21.  
  22. DIR *dir;
  23. struct dirent *ent;
  24. int banyak_level = 0;
  25.  
  26. if((dir = opendir(directory)) != NULL) {
  27.  
  28. while((ent = readdir(dir)) != NULL)
  29. if((strcmp(ent->d_name, ".") != 0) && (strcmp(ent->d_name, "..") != 0))
  30. banyak_level++;
  31.  
  32. closedir(dir);
  33. }
  34.  
  35. return banyak_level;
  36. }
  37.  
  38. /*
  39. dir contoh \\intermediate
  40. kesulitan contoh intermediate_level_
  41. */
  42. char * cari_nama_file(char * dir, char * kesulitan) {
  43.  
  44. int pilih_level;
  45. char level[10] = "";
  46. char nama_file[50] = "";
  47.  
  48. int banyak_level = cari_banyak_level(dir);
  49. do {
  50. printf("\n\n"
  51. " ======================================= \n"
  52. "|| INTERMEDIATE PUZZLE ||\n"
  53. " ======================================= \n");
  54. for(int i = 1; i <= banyak_level; i++)
  55. printf("|| %i. Puzzle %d ||\n", i, i);
  56.  
  57. printf(" ======================================= \n"
  58. " *****************\n"
  59. "|| Pilih: "
  60. );
  61. scanf("%d", &pilih_level);
  62. printf(" *****************\n");
  63. } while (pilih_level < 1 || pilih_level > banyak_level);
  64.  
  65. itoa(pilih_level, level, 10);
  66. // intermediate
  67. strcpy(nama_file, kesulitan);
  68. // intermediate/
  69. strcat(nama_file, "\\");
  70. // intermediate//intermediate
  71. strcat(nama_file, kesulitan);
  72. // intermediate//intermediate_level_
  73. strcat(nama_file, "_level_");
  74. // intermediate//intermediate_level_1
  75. strcat(nama_file, level);
  76. // intermediate//intermediate_level_1.txt
  77. strcat(nama_file, ".txt");
  78. printf("nama file %s\n", nama_file);
  79.  
  80. return nama_file;
  81. }
  82.  
  83. void tampilan_awal() {
  84.  
  85. printf("\n\n"
  86. " ================================================ \n"
  87. "|| || \n"
  88. "|| PROGRAM TUGAS BESAR ANALGO ||\n"
  89. "|| KELOMPOK GAME \"UNBLOCK ME\" ||\"\n"
  90. "|| || \n"
  91. " ================================================ \n"
  92. "|| || \n"
  93. "|| AGUS DARMAWAN - 10115358 ||\n"
  94. "|| HAMDANI - 10115146 ||\n"
  95. "|| Roni Nurlana - 10115375 ||\n"
  96. "|| Agung Nugraha - 10116412 ||\n"
  97. "|| || \n"
  98. " ================================================ \n"
  99. "Tekan Sembarang Untuk Melanjutkan\n"
  100. );
  101. _getch();
  102. }
  103. /* ========================================================================= */
  104.  
  105. struct node {
  106. int depth;
  107. struct node *parent;
  108. struct node *prev;
  109. struct node *next;
  110. char move[6][6];
  111. unsigned short piece_moved_i, piece_moved_j;
  112. };
  113.  
  114. struct node *first_node = NULL;
  115. struct node *current_node = NULL;
  116. struct node *last_node = NULL;
  117.  
  118. struct node *solution_node = NULL;
  119.  
  120. unsigned int new_moves = 0;
  121. unsigned int fifo_size = 0;
  122. unsigned int prev_depth = 0;
  123.  
  124. char puzzle_kosong[6][6] = {
  125. {' ', ' ', '-', '-', ' ', ' '},
  126. {' ', ' ', ' ', ' ', ' ', ' '},
  127. {' ', ' ', ' ', ' ', ' ', ' '},
  128. {' ', ' ', ' ', ' ', ' ', ' '},
  129. {' ', ' ', ' ', ' ', ' ', ' '},
  130. {' ', ' ', ' ', ' ', ' ', ' '}
  131. };
  132. /* ========================================================================= */
  133.  
  134. int main() {
  135.  
  136. tampilan_awal();
  137.  
  138. char pilih_type;
  139. char nama_file[50] = "";
  140. do {
  141. system("cls");
  142. printf("\n\n"
  143. " ========================================= \n"
  144. "|| UNBLOCK ME SOLVER ||\n"
  145. "|| 1. Puzzle Kosong ||\n"
  146. "|| 2. Test Puzzle ||\n"
  147. "|| 3. Beginner Puzzle (Pemula) ||\n"
  148. "|| 4. Intemediate Puzzle (Pertengahan) ||\n"
  149. "|| 5. Advance Puzzle (Lanjutan) ||\n"
  150. " ========================================= \n"
  151. " *****************\n"
  152. "|| Pilih : "
  153. );
  154. pilih_type = (char) getchar();
  155. printf(" *****************\n");
  156.  
  157. system("cls");
  158. memset(&data_puzzle, 0, sizeof(data_puzzle));
  159.  
  160. switch (pilih_type) {
  161. case '1': {
  162. printf(" ======================================= \n"
  163. "|| PUZZLE KOSONG ||\n"
  164. " ======================================= \n");
  165. memcpy(data_puzzle, puzzle_kosong, sizeof(char) * 36);
  166. }
  167. break;
  168.  
  169. case '2':
  170. strcpy(nama_file, cari_nama_file("\\test", "test"));
  171. break;
  172.  
  173. case '3':
  174. strcpy(nama_file, cari_nama_file("\\beginner", "beginner"));
  175. break;
  176.  
  177. case '4':
  178. strcpy(nama_file, cari_nama_file("\\intermediate", "intermediate"));
  179. break;
  180.  
  181. case '5':
  182. strcpy(nama_file, cari_nama_file("\\advanced", "advanced"));
  183. break;
  184.  
  185. default:
  186. printf("Anda salah memilih type kesulitan\n");
  187. _getch();
  188. }
  189. } while(pilih_type < '1' || pilih_type > '5');
  190.  
  191. /* jika yang dipilih bukan 1 (puzzle kosong) */
  192. if(pilih_type != 1) {
  193. FILE *f;
  194. if ((f = fopen(nama_file, "r")) == NULL) {
  195. perror("fopen");
  196. return 1;
  197. }
  198. char c;
  199. int i = 0;
  200. int j = 0;
  201. while ((c = (char) getc(f)) != -1) {
  202. if (j == 6) {
  203. j = 0;
  204. i++;
  205. }
  206. if (c != ',' && c != '\n') {
  207. data_puzzle[i][j] = c;
  208. j++;
  209. }
  210. }
  211. fclose(f);
  212. }
  213.  
  214. struct node root;
  215.  
  216. root.depth = 0;
  217. root.parent = NULL;
  218. root.prev = NULL;
  219. root.next = NULL;
  220.  
  221. memcpy(root.move, data_puzzle, sizeof(char) * 36);
  222.  
  223. printm(root.move);
  224. printf("\nTekan sembarang\n");
  225. _getch();
  226.  
  227. first_node = current_node = last_node = &root;
  228. fifo_size = 1;
  229.  
  230. next_moves();
  231. new_moves = 0;
  232.  
  233. system("cls");
  234. printf("\n\n"
  235. " ===========================================================\n"
  236. " || Ketika aplikasi berjalan, silahkan tunggu beberapa saat ||\n"
  237. " || Biarkan program mencari solusi terbaik ||\n"
  238. " || Waktu yang dibutuhkan bisa sampe beberapa menit ||\n"
  239. " || Tergantung kesulitan dari puzzle yang dipilih ||\n"
  240. " ===========================================================\n"
  241. "\n\n"
  242. );
  243.  
  244. while (current_node->next != NULL && solution_node == NULL) {
  245.  
  246. current_node = current_node->next;
  247. next_moves();
  248. new_moves = 0;
  249. }
  250.  
  251. if (solution_node != NULL) {
  252.  
  253. printf("Perpindahan ke %d:\n", solution_node->depth);
  254. printm(solution_node->move);
  255.  
  256. current_node = solution_node;
  257. while (current_node->parent != NULL) {
  258.  
  259. current_node = current_node->parent;
  260. printf("Pindah %d:\n", current_node->depth);
  261. printm(current_node->move);
  262. }
  263. printf("Solusi ditemukan, %u kali pindah (%u percobaan).\n", solution_node->depth, fifo_size);
  264. }
  265.  
  266. else
  267. printf("Solusi tidak ditemukan.\n");
  268.  
  269. return 0;
  270. }
  271.  
  272. int equals(char a[][6], char b[][6]) {
  273.  
  274. for (int j = 0; j < 6; j++)
  275. for (int i = 0; i < 6; i++)
  276. if (a[i][j] != b[i][j])
  277. return 0;
  278.  
  279. return 1;
  280. }
  281.  
  282. void add(char move[][6], unsigned short i, unsigned short j) {
  283.  
  284. struct node *n = last_node;
  285. do {
  286. if (equals(n->move, move))
  287. return;
  288. } while ((n = n->prev) != NULL);
  289.  
  290. struct node *new_node = malloc(sizeof(struct node));
  291. memcpy(new_node->move, move, sizeof(char) * 36);
  292.  
  293. new_node->depth = current_node->depth + 1;
  294. new_node->parent = current_node;
  295. new_node->prev = last_node;
  296. new_node->next = NULL;
  297. new_node->piece_moved_i = i;
  298. new_node->piece_moved_j = j;
  299.  
  300. last_node->next = new_node;
  301. last_node = new_node;
  302.  
  303. new_moves++;
  304. fifo_size++;
  305. }
  306.  
  307. void next_moves() {
  308. char working_move[6][6];
  309. char next_move[6][6];
  310. char piece;
  311.  
  312. memcpy(working_move, current_node->move, sizeof(char) * 36);
  313.  
  314. for (int j = 0; j < 6; j++) {
  315. for (int i = 0; i < 6; i++) {
  316.  
  317. piece = working_move[i][j];
  318. if (piece == ' ')
  319. continue;
  320.  
  321. // Horizontal pieces of size 2
  322. if (piece == '-' || piece == '*') {
  323.  
  324. if (! (current_node->piece_moved_i == i && current_node->piece_moved_j == j)) {
  325.  
  326. // geser kanan
  327. memcpy(next_move, current_node->move, sizeof(char) * 36);
  328. for (int k = j + 2; ((k < 6) && (current_node->move[i][k] == ' ')); k++) {
  329. if (k == 5 && piece == '*') {
  330. solution_node = last_node;
  331. return;
  332. }
  333. next_move[i][k] = piece;
  334. next_move[i][k - 2] = ' ';
  335. add(next_move, i, k - 1);
  336. }
  337.  
  338. // geser kiri
  339. memcpy(next_move, current_node->move, sizeof(char) * 36);
  340. for (int k = j - 1; ((k >= 0) && (current_node->move[i][k] == ' ')); k--) {
  341. next_move[i][k] = piece;
  342. next_move[i][k + 2] = ' ';
  343. add(next_move, i, k);
  344. }
  345. }
  346. // Mark the piece as dealt
  347. working_move[i][j + 1] = 'x';
  348.  
  349. }
  350. // Vertical pieces of size 2
  351. else if (piece == '|') {
  352.  
  353. if (! ((current_node->piece_moved_i == i) && (current_node->piece_moved_j == j))) {
  354.  
  355. // geser atas
  356. memcpy(next_move, current_node->move, sizeof(char) * 36);
  357. for (int k = i - 1; ((k >= 0) && (current_node->move[k][j] == ' ')); k--) {
  358. next_move[k][j] = '|';
  359. next_move[k + 2][j] = ' ';
  360. add(next_move, k, j);
  361. }
  362.  
  363. // geser bawah
  364. memcpy(next_move, current_node->move, sizeof(char) * 36);
  365. for (int k = i + 2; ((k < 6) && (current_node->move[k][j] == ' ')); k++) {
  366. next_move[k - 2][j] = ' ';
  367. next_move[k][j] = '|';
  368. add(next_move, k - 1, j);
  369. }
  370. }
  371.  
  372. // Mark the piece as dealt
  373. working_move[i + 1][j] = 'x';
  374.  
  375. }
  376. }
  377. }
  378.  
  379. } // end next_moves();
  380.  
  381. void printm(char move[][6]) {
  382.  
  383. printf("++++++++\n");
  384. for (int i = 0; i < 6; i++) {
  385. printf("+");
  386. for (int j = 0; j < 6; j++)
  387. printf("%c", move[i][j]);
  388.  
  389. if (i != 2)
  390. printf("+");
  391. printf("\n");
  392. }
  393. printf("++++++++\n");
  394. }
Add Comment
Please, Sign In to add comment