Advertisement
B1KMusic

KTaNE Module Solver: Memory

Jun 26th, 2018
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.92 KB | None | 0 0
  1. /* Version 1.1
  2.  * Implements the rules for the 'Memory' module
  3.  * Warning: contains terse type syntax. cdecl might help.
  4.  * Unless grabbed from user input, numbers are internally counted from 0, so ds0 refers to the handler for stage 1.
  5.  * See <http://www.bombmanual.com/manual/1/html/index.html> for the rules. Subject of Memory.
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <ctype.h>
  10.  
  11. #define MAP(a, b, c, d) \
  12.     ( ((int[4]){(a), (b), (c), (d)})[st->display - 1] )
  13.  
  14. struct state {
  15.     int stage;
  16.     int display;
  17.     int positions[5];
  18.     int labels[5];
  19. };
  20.  
  21. int
  22. get_num(char *prompt)
  23. {
  24.     int ret = 0;
  25.  
  26.     while(ret < '1' || ret > '4'){
  27.         if(!isspace(ret))
  28.             printf("%s >", prompt);
  29.  
  30.         ret = getchar();
  31.     }
  32.  
  33.     return ret - '0';
  34. }
  35.  
  36. static int
  37. ds0(struct state *st)
  38. {
  39.     return MAP(2,2,3,4);
  40. }
  41.  
  42. static int
  43. ds1(struct state *st)
  44. {
  45.     if(st->display == 1)
  46.         puts("Press Label 4");
  47.  
  48.     return MAP(0, st->positions[0], 1, st->positions[0]);
  49. }
  50.  
  51. static int
  52. ds2(struct state *st)
  53. {
  54.     if(st->display == 4)
  55.         puts("Press Label 4");
  56.  
  57.     return MAP(st->labels[1], st->labels[0], 3, 0);
  58. }
  59.  
  60.  
  61. static int
  62. ds3(struct state *st)
  63. {
  64.     return MAP(st->positions[0], 1, st->positions[1], st->positions[1]);
  65. }
  66.  
  67. static int
  68. ds4(struct state *st)
  69. {
  70.     printf("Press Label %i\n", st->labels[ MAP(0,1,3,2) ]);
  71.     return 0;
  72. }
  73.  
  74. int
  75. main(void)
  76. {
  77.     struct state st = {0};
  78.     static int (*stagefn[5])(struct state *) = { ds0, ds1, ds2, ds3, ds4 };
  79.  
  80.     while(st.stage < 5){
  81.         st.display = get_num("Display?");
  82.         st.positions[st.stage] = stagefn[st.stage](&st);
  83.  
  84.         if(st.positions[st.stage])
  85.             printf("Press %i\n", st.positions[st.stage]);
  86.         else
  87.             st.positions[st.stage] = get_num("Position?");
  88.  
  89.         if(st.stage < 4)
  90.             st.labels[st.stage] = get_num("Label?");
  91.  
  92.         st.stage++;
  93.     }
  94.  
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement