B1KMusic

flippy.c (update 2016-04-07)

Apr 4th, 2016
517
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.53 KB | None | 0 0
  1. /* flippy.c
  2.  * I wrote this quickie because a specific puzzle in Minecraft is extremely difficult.
  3.  * Specifically, the second puzzle in the puzzle branch of a map called "Diversity".
  4.  * The program emulates the puzzle's core logic and prints your solution if you happen to solve it. I have not solved it :V
  5.  *
  6.  * [Update 2016-04-07] I updated the code to be more windows-friendly.
  7.  *   + Added a compiler flag so the user doesn't have to bang around in code they don't understand
  8.  *   * For non-ANSI mode, I replaced "[ ON  ]" and "[ OFF ]" with a more visually obvious "[ +++ ]" and "[     ]"
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13.  
  14. /* Note: this code uses linux-specific terminal features in get_choice() and slot_read_color().
  15.  * If you're on Windows, add -DNOFANCY to your compile line to disable USE_ANSI and USE_STTY.
  16.  */
  17.  
  18. #if defined(NOFANCY)
  19. #    define USE_ANSI 0
  20. #    define USE_STTY 0
  21. #else
  22. #    define USE_ANSI 1
  23. #    define USE_STTY 1
  24. #endif
  25.  
  26. typedef enum SlotMasks {
  27.     sm_1,
  28.     sm_2,
  29.     sm_3,
  30.     sm_4,
  31.     sm_5,
  32.     sm_6,
  33.     sm_7,
  34. } SlotMask;
  35.  
  36. void slot_toggle(int *slots, SlotMask sm);
  37. int slot_read(int *slots, SlotMask sm);
  38. char *slot_read_color(int *slots, SlotMask sm);
  39. void print_slots(int *slots);
  40. void print_choices();
  41. void print_past_choices(int *pastChoices);
  42. void exec_choice(int *slots, int choice);
  43. int get_choice();
  44. int getnum();
  45. void game_loop();
  46.  
  47. void
  48. slot_toggle(int *slots, SlotMask sm)
  49. {/*{{{*/
  50.     *slots ^= (1 << sm);
  51. }/*}}}*/
  52.  
  53. int
  54. slot_read(int *slots, SlotMask sm)
  55. {/*{{{*/
  56.     return !!((*slots) & (1 << sm));
  57. }/*}}}*/
  58.  
  59. char *
  60. slot_read_color(int *slots, SlotMask sm)
  61. {/*{{{*/
  62.     if(slot_read(slots, sm) == 1){
  63.         return USE_ANSI ? "\e[0;32m ON  \e[0m" : " +++ ";
  64.     } else {
  65.         return USE_ANSI ? "\e[0;31m OFF \e[0m" : "     ";
  66.     }
  67. }/*}}}*/
  68.  
  69. void
  70. print_slots(int *slots)
  71. {/*{{{*/
  72.     SlotMask sm = sm_1;
  73.  
  74.     printf("\n");
  75.  
  76.     while(sm <= sm_7){
  77.         printf("   %i    ", sm + 1);
  78.         sm++;
  79.     }
  80.  
  81.     printf("\n");
  82.     sm = sm_1;
  83.  
  84.     while(sm <= sm_7){
  85.         printf("[%s] ", slot_read_color(slots, sm));
  86.         sm++;
  87.     }
  88.  
  89.     printf("\n");
  90. }/*}}}*/
  91.  
  92. void
  93. print_choices()
  94. {/*{{{*/
  95.     printf("Select flipper:\n");
  96.     printf("  1: 1, 4, 7\n");
  97.     printf("  2: 1, 2, 6\n");
  98.     printf("  3: 3, 5, 6\n");
  99.     printf("  4: 2, 4, 7\n");
  100. }/*}}}*/
  101.  
  102. int
  103. getnum()
  104. {/*{{{*/
  105.     int num = fgetc(stdin) - '0';
  106.  
  107.     if(num >= 0 && num <= 9)
  108.         return num;
  109.  
  110.     if(num == '\n' - '0')
  111.         return -1;
  112.  
  113.     return -1;
  114. }/*}}}*/
  115.  
  116. int
  117. get_choice()
  118. {/*{{{*/
  119.     int c = -1;
  120.  
  121.     while(1){
  122.         printf("\n>> ");
  123.         if(USE_STTY)
  124.             system("stty raw");
  125.  
  126.         c = getnum();
  127.  
  128.         if(USE_STTY)
  129.             system("stty sane");
  130.  
  131.         if(c >= 1 && c <= 4){
  132.             return c;
  133.         } else if (c == -1){
  134.             continue;
  135.         } else {
  136.             printf("E: Please enter a number between 1 and 4\n");
  137.         }
  138.     }
  139. }/*}}}*/
  140.  
  141. void
  142. exec_choice(int *slots, int choice)
  143. {/*{{{*/
  144.     switch(choice){
  145.         case 1:
  146.             slot_toggle(slots, sm_1);
  147.             slot_toggle(slots, sm_4);
  148.             slot_toggle(slots, sm_7);
  149.             break;
  150.         case 2:
  151.             slot_toggle(slots, sm_1);
  152.             slot_toggle(slots, sm_2);
  153.             slot_toggle(slots, sm_6);
  154.             break;
  155.         case 3:
  156.             slot_toggle(slots, sm_3);
  157.             slot_toggle(slots, sm_5);
  158.             slot_toggle(slots, sm_6);
  159.             break;
  160.         case 4:
  161.             slot_toggle(slots, sm_2);
  162.             slot_toggle(slots, sm_4);
  163.             slot_toggle(slots, sm_7);
  164.             break;
  165.     }
  166. }/*}}}*/
  167.  
  168. void
  169. print_past_choices(int *pastChoices)
  170. {/*{{{*/
  171.     while(*pastChoices){
  172.         printf("%i, ", *pastChoices);
  173.         pastChoices++;
  174.     }
  175.  
  176.     printf("\n");
  177. }/*}}}*/
  178.  
  179. void
  180. game_loop()
  181. {/*{{{*/
  182.     int slots = 0;
  183.     int choice = 0;
  184.     int pastChoices[51] = {0};
  185.     int pci = 0;
  186.  
  187.     while(1){
  188.         print_slots(&slots);
  189.         print_choices();
  190.         choice = get_choice();
  191.         pastChoices[pci++] = choice;
  192.         exec_choice(&slots, choice);
  193.  
  194.         if(slots == 127){
  195.             break;
  196.         }
  197.  
  198.         if(pci >= 50){
  199.             printf("Solution exceeded 50 moves.\n");
  200.             exit(1);
  201.         }
  202.     }
  203.  
  204.     printf("You win!\n");
  205.     printf("Here are the choices that led to your victory:\n");
  206.     print_past_choices(pastChoices);
  207. }/*}}}*/
  208.  
  209. int
  210. main()
  211. {/*{{{*/
  212.     game_loop();
  213. }/*}}}*/
Advertisement
Add Comment
Please, Sign In to add comment