Created
April 13, 2018 23:32
-
-
Save regehr/092a54717fea0e96aa71a691279bae80 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <assert.h> | |
enum dir { N, S, W, E }; | |
static enum dir getcmd(void) { | |
int in; | |
do { | |
in = getchar(); | |
} while (in == '\n'); | |
if (in == EOF) { | |
printf("see you later\n"); | |
exit(0); | |
} | |
switch (in) { | |
case 'n': | |
return N; | |
case 's': | |
return S; | |
case 'w': | |
return W; | |
case 'e': | |
return E; | |
} | |
printf("invalid command -- game over man\n"); | |
exit(0); | |
} | |
static void win(void) { | |
printf("you won\n"); | |
assert(0); | |
} | |
/* 6 7 8 9 a <- finish | |
* 5 | |
* 4 3 2 d | |
* 1 b c | |
* start -> 0 e | |
* f | |
*/ | |
static int room = 0; | |
#ifdef FUNCS | |
static void room0(enum dir d) { | |
if (d == N) | |
room = 1; | |
} | |
static void room1(enum dir d) { | |
if (d == N) | |
room = 2; | |
if (d == S) | |
room = 0; | |
if (d == E) | |
room = 0xb; | |
} | |
static void room2(enum dir d) { | |
if (d == W) | |
room = 3; | |
if (d == S) | |
room = 1; | |
} | |
static void room3(enum dir d) { | |
if (d == W) | |
room = 4; | |
if (d == E) | |
room = 2; | |
} | |
static void room4(enum dir d) { | |
if (d == N) | |
room = 5; | |
if (d == E) | |
room = 3; | |
} | |
static void room5(enum dir d) { | |
if (d == N) | |
room = 6; | |
if (d == S) | |
room = 4; | |
} | |
static void room6(enum dir d) { | |
if (d == S) | |
room = 5; | |
if (d == E) | |
room = 7; | |
} | |
static void room7(enum dir d) { | |
if (d == W) | |
room = 6; | |
if (d == E) | |
room = 8; | |
} | |
static void room8(enum dir d) { | |
if (d == W) | |
room = 7; | |
if (d == E) | |
room = 9; | |
} | |
static void room9(enum dir d) { | |
if (d == W) | |
room = 8; | |
if (d == E) | |
room = 0xa; | |
} | |
static void roomb(enum dir d) { | |
if (d == E) | |
room = 0xc; | |
if (d == W) | |
room = 1; | |
} | |
static void roomc(enum dir d) { | |
if (d == W) | |
room = 0xb; | |
if (d == N) | |
room = 0xd; | |
if (d == S) | |
room = 0xe; | |
} | |
static void roomd(enum dir d) { | |
if (d == S) | |
room = 0xc; | |
} | |
static void roome(enum dir d) { | |
if (d == N) | |
room = 0xc; | |
if (d == S) | |
room = 0xf; | |
} | |
static void roomf(enum dir d) { | |
if (d == N) | |
room = 0xe; | |
} | |
#else | |
static const int map[16][4] = { | |
{1, -1, -1, -1}, // 0 | |
{2, 0, -1, 0xb}, // 1 | |
{-1, 1, 3, -1}, // 2 | |
{-1, -1, 4, 2}, // 3 | |
{5, -1, -1, 3}, // 4 | |
{6, 4, -1, -1}, // 5 | |
{-1, 5, -1, 7}, // 6 | |
{-1, -1, 6, 8}, // 7 | |
{-1, -1, 7, 9}, // 8 | |
{-1, -1, 8, 0xa}, // 9 | |
{-1, -1, -1, -1}, // a | |
{-1, -1, 1, 0xc}, // b | |
{0xd, 0xe, 0xb, -1}, // c | |
{-1, 0xc, -1, -1}, // d | |
{0xc, 0xf, -1, -1}, // e | |
{0xe, -1, -1, -1}, // f | |
}; | |
#endif | |
int main(void) { | |
while (1) { | |
printf("you are in room %x\n", room); | |
enum dir d = getcmd(); | |
#ifdef FUNCS | |
int old_room = room; | |
switch (room) { | |
case 0: | |
room0(d); | |
break; | |
case 1: | |
room1(d); | |
break; | |
case 2: | |
room2(d); | |
break; | |
case 3: | |
room3(d); | |
break; | |
case 4: | |
room4(d); | |
break; | |
case 5: | |
room5(d); | |
break; | |
case 6: | |
room6(d); | |
break; | |
case 7: | |
room7(d); | |
break; | |
case 8: | |
room8(d); | |
break; | |
case 9: | |
room9(d); | |
break; | |
case 0xb: | |
roomb(d); | |
break; | |
case 0xc: | |
roomc(d); | |
break; | |
case 0xd: | |
roomd(d); | |
break; | |
case 0xe: | |
roome(d); | |
break; | |
case 0xf: | |
roomf(d); | |
break; | |
default: | |
assert(0); | |
} | |
if (room == old_room) | |
printf("sorry, can't go that way\n"); | |
#else | |
int next = map[room][d]; | |
if (next == -1) | |
printf("sorry, can't go that way\n"); | |
else | |
room = next; | |
#endif | |
assert(room >= 0 && room <= 0xf); | |
if (room == 0xa) | |
win(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment