Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* flippy.c
- * I wrote this quickie because a specific puzzle in Minecraft is extremely difficult.
- * Specifically, the second puzzle in the puzzle branch of a map called "Diversity".
- * The program emulates the puzzle's core logic and prints your solution if you happen to solve it. I have not solved it :V
- *
- * [Update 2016-04-07] I updated the code to be more windows-friendly.
- * + Added a compiler flag so the user doesn't have to bang around in code they don't understand
- * * For non-ANSI mode, I replaced "[ ON ]" and "[ OFF ]" with a more visually obvious "[ +++ ]" and "[ ]"
- */
- #include <stdio.h>
- #include <stdlib.h>
- /* Note: this code uses linux-specific terminal features in get_choice() and slot_read_color().
- * If you're on Windows, add -DNOFANCY to your compile line to disable USE_ANSI and USE_STTY.
- */
- #if defined(NOFANCY)
- # define USE_ANSI 0
- # define USE_STTY 0
- #else
- # define USE_ANSI 1
- # define USE_STTY 1
- #endif
- typedef enum SlotMasks {
- sm_1,
- sm_2,
- sm_3,
- sm_4,
- sm_5,
- sm_6,
- sm_7,
- } SlotMask;
- void slot_toggle(int *slots, SlotMask sm);
- int slot_read(int *slots, SlotMask sm);
- char *slot_read_color(int *slots, SlotMask sm);
- void print_slots(int *slots);
- void print_choices();
- void print_past_choices(int *pastChoices);
- void exec_choice(int *slots, int choice);
- int get_choice();
- int getnum();
- void game_loop();
- void
- slot_toggle(int *slots, SlotMask sm)
- {/*{{{*/
- *slots ^= (1 << sm);
- }/*}}}*/
- int
- slot_read(int *slots, SlotMask sm)
- {/*{{{*/
- return !!((*slots) & (1 << sm));
- }/*}}}*/
- char *
- slot_read_color(int *slots, SlotMask sm)
- {/*{{{*/
- if(slot_read(slots, sm) == 1){
- return USE_ANSI ? "\e[0;32m ON \e[0m" : " +++ ";
- } else {
- return USE_ANSI ? "\e[0;31m OFF \e[0m" : " ";
- }
- }/*}}}*/
- void
- print_slots(int *slots)
- {/*{{{*/
- SlotMask sm = sm_1;
- printf("\n");
- while(sm <= sm_7){
- printf(" %i ", sm + 1);
- sm++;
- }
- printf("\n");
- sm = sm_1;
- while(sm <= sm_7){
- printf("[%s] ", slot_read_color(slots, sm));
- sm++;
- }
- printf("\n");
- }/*}}}*/
- void
- print_choices()
- {/*{{{*/
- printf("Select flipper:\n");
- printf(" 1: 1, 4, 7\n");
- printf(" 2: 1, 2, 6\n");
- printf(" 3: 3, 5, 6\n");
- printf(" 4: 2, 4, 7\n");
- }/*}}}*/
- int
- getnum()
- {/*{{{*/
- int num = fgetc(stdin) - '0';
- if(num >= 0 && num <= 9)
- return num;
- if(num == '\n' - '0')
- return -1;
- return -1;
- }/*}}}*/
- int
- get_choice()
- {/*{{{*/
- int c = -1;
- while(1){
- printf("\n>> ");
- if(USE_STTY)
- system("stty raw");
- c = getnum();
- if(USE_STTY)
- system("stty sane");
- if(c >= 1 && c <= 4){
- return c;
- } else if (c == -1){
- continue;
- } else {
- printf("E: Please enter a number between 1 and 4\n");
- }
- }
- }/*}}}*/
- void
- exec_choice(int *slots, int choice)
- {/*{{{*/
- switch(choice){
- case 1:
- slot_toggle(slots, sm_1);
- slot_toggle(slots, sm_4);
- slot_toggle(slots, sm_7);
- break;
- case 2:
- slot_toggle(slots, sm_1);
- slot_toggle(slots, sm_2);
- slot_toggle(slots, sm_6);
- break;
- case 3:
- slot_toggle(slots, sm_3);
- slot_toggle(slots, sm_5);
- slot_toggle(slots, sm_6);
- break;
- case 4:
- slot_toggle(slots, sm_2);
- slot_toggle(slots, sm_4);
- slot_toggle(slots, sm_7);
- break;
- }
- }/*}}}*/
- void
- print_past_choices(int *pastChoices)
- {/*{{{*/
- while(*pastChoices){
- printf("%i, ", *pastChoices);
- pastChoices++;
- }
- printf("\n");
- }/*}}}*/
- void
- game_loop()
- {/*{{{*/
- int slots = 0;
- int choice = 0;
- int pastChoices[51] = {0};
- int pci = 0;
- while(1){
- print_slots(&slots);
- print_choices();
- choice = get_choice();
- pastChoices[pci++] = choice;
- exec_choice(&slots, choice);
- if(slots == 127){
- break;
- }
- if(pci >= 50){
- printf("Solution exceeded 50 moves.\n");
- exit(1);
- }
- }
- printf("You win!\n");
- printf("Here are the choices that led to your victory:\n");
- print_past_choices(pastChoices);
- }/*}}}*/
- int
- main()
- {/*{{{*/
- game_loop();
- }/*}}}*/
Advertisement
Add Comment
Please, Sign In to add comment