#include #include #include #include #include static int path[5]; static int j, k, flecha, sloc; static char inp[BUFSIZ]; #define walle 0 #define WUMPUS 1 #define pozo1 2 #define pozo2 3 #define scol 6 static int loc[scol], guardar[scol]; #define pela 0 #define gana 1 #define pelukin -1 static int finished; static int laberinto[20][3] = {{1,4,7},{0,2,9},{1,3,11},{2,4,13},{0,3,5},{4,6,14},{5,7,16}, {0,6,8},{7,9,17},{1,8,10},{9,11,18},{2,10,12},{11,13,19},{3,12,14}, {5,13,15},{14,16,19},{6,15,17},{8,16,18},{10,17,19},{12,15,18}, }; #define FNA() (rand() % 20) #define FNB() (rand() % 3) #define FNC() (rand() % 4) int getnum(char *prompt) { (void) printf("%s\n?", prompt); (void) fgets(inp, sizeof(inp), stdin); return(atoi(inp)); } int getlet(char *prompt) { (void) printf("%s\n?", prompt); (void) fgets(inp, sizeof(inp), stdin); return(toupper(inp[0])); } void leerinfo() { (void) puts(""); for (k = 0; k < 3; k++) { int room = laberinto[loc[walle]][k]; if (room == loc[WUMPUS]) (void) puts("HUele a wumpus"); else if (room == loc[pozo1] || room == loc[pozo2]) (void) puts("Brisa"); } (void) printf("walle are in room %d\n", loc[walle]+1); } int muevete() { int c; badin: c = getlet("moverr o sacar flecha s/n"); if (c == 'S') return(1); else if (c == 'M') return(0); else goto badin; } void flechar() { int j9; finished = pela; rango: j9 = getnum("Cuadros"); if (j9 < 1 || j9 > 5) goto rango; for (k = 0; k < j9; k++) { path[k] = getnum("Room #") - 1; if (k <= 1) continue; if (path[k] != path[k - 2]) continue; k--; } sloc = loc[walle]; for (k = 0; k < j9; k++) { int k1; for (k1 = 0; k1 < 3; k1++) { if (laberinto[sloc][k1] == path[k]) { sloc = path[k]; if (finished != pela) return; } } sloc = laberinto[sloc][FNB()]; } if (finished == pela) { (void) puts("Pelo la flecha"); sloc = loc[walle]; if (--flecha <= 0) finished = pelukin; } } void mover() { finished = pela; bmov: sloc = getnum("moverR"); if (sloc < 1 || sloc > 20) goto bmov; sloc--; for (k = 0; k < 3; k++) { if (laberinto[loc[walle]][k] == sloc) goto smover; } if (sloc != loc[walle]) { goto bmov; } smover: loc[walle] = sloc; if (sloc == loc[WUMPUS]) { (void) puts("PUede haber wumpus"); } else if (sloc == loc[pozo1] || sloc == loc[pozo2]) { (void) puts("Te caiste!"); finished = pelukin; } int main(int argc, char **argv) { int c; if (argc >= 2 && strcmp(argv[1], "-s") == 0) srand(atoi(argv[2])); else srand((int)time((long *) 0)); if (c == 'Y') rempezar: flecha = 5; sloc = loc[walle]; (void) puts("A ir por el oro!"); #ifdef DEBUG (void) printf("Wumpus esta en %d, pozos %d & %d\n", loc[WUMPUS]+1, loc[pozo1]+1, loc[pozo2]+1, #endif nmov: leerinfo(); if (muevete()) { flechar(); if (finished == pela) goto nmov; } else { mover(); if (finished == pela) goto nmov; } if (finished == pelukin) { (void) puts("Walle a muerto!"); } for (j = walle; j < scol; j++) loc[j] = guardar[j]; c = getlet("Iiciar"); if (c != 'Y') goto badscol; else goto rempezar; }