Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Version 1.1
- * Implements the rules for the 'Memory' module
- * Warning: contains terse type syntax. cdecl might help.
- * Unless grabbed from user input, numbers are internally counted from 0, so ds0 refers to the handler for stage 1.
- * See <http://www.bombmanual.com/manual/1/html/index.html> for the rules. Subject of Memory.
- */
- #include <stdio.h>
- #include <ctype.h>
- #define MAP(a, b, c, d) \
- ( ((int[4]){(a), (b), (c), (d)})[st->display - 1] )
- struct state {
- int stage;
- int display;
- int positions[5];
- int labels[5];
- };
- int
- get_num(char *prompt)
- {
- int ret = 0;
- while(ret < '1' || ret > '4'){
- if(!isspace(ret))
- printf("%s >", prompt);
- ret = getchar();
- }
- return ret - '0';
- }
- static int
- ds0(struct state *st)
- {
- return MAP(2,2,3,4);
- }
- static int
- ds1(struct state *st)
- {
- if(st->display == 1)
- puts("Press Label 4");
- return MAP(0, st->positions[0], 1, st->positions[0]);
- }
- static int
- ds2(struct state *st)
- {
- if(st->display == 4)
- puts("Press Label 4");
- return MAP(st->labels[1], st->labels[0], 3, 0);
- }
- static int
- ds3(struct state *st)
- {
- return MAP(st->positions[0], 1, st->positions[1], st->positions[1]);
- }
- static int
- ds4(struct state *st)
- {
- printf("Press Label %i\n", st->labels[ MAP(0,1,3,2) ]);
- return 0;
- }
- int
- main(void)
- {
- struct state st = {0};
- static int (*stagefn[5])(struct state *) = { ds0, ds1, ds2, ds3, ds4 };
- while(st.stage < 5){
- st.display = get_num("Display?");
- st.positions[st.stage] = stagefn[st.stage](&st);
- if(st.positions[st.stage])
- printf("Press %i\n", st.positions[st.stage]);
- else
- st.positions[st.stage] = get_num("Position?");
- if(st.stage < 4)
- st.labels[st.stage] = get_num("Label?");
- st.stage++;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement