#include #include #include void setBool(); void notePoss(); void getPonse(); void printMaze(); /* Master maze. Layout: maze[column][row][nsew passable] */ int maze[3][3][4]={ {{0,1,1,0}, {1,1,0,0}, {1,0,0,0}}, {{0,0,1,1}, {0,1,0,0,}, {1,0,1,0}}, {{0,1,0,1}, {1,1,0,0}, {1,0,0,1}} }; int north=0, south=0, east=0, west=0; /*initializes booleans(well, faked ones) for use later*/ int again=1; int col=0, row=2; /* Player's starting point in maze. */ /* main() : intro; while loop->set nsew passability, let user know where they can go, parse response. */ void main(void) { printf("Moza 0.1: Parsing a Maze Game\tby Katie Rivard, July 2000\n\nWelcome to an exercise in game programming in C. It's not a great attempt, but it does some limited text parsing and navigation. Type '?' for instructions. Enjoy.\n\n"); while(again) { setBool(); notePoss(); getPonse(); } } /* setBool() : Looks at the values in the master maze, and determines whether the player can go north, south, east or west, which are much easier to type than maze[col][row][{0,1,2,3}] */ void setBool() { north=0; south=0; east=0; west=0; if (maze[col][row][0]) north=1; if (maze[col][row][1]) south=1; if (maze[col][row][2]) east=1; if (maze[col][row][3]) west=1; } /* notePoss() : Prints the directions you can go from your current position, based on the values set by setBool() */ void notePoss() { char cnorth[8], csouth[8], ceast[7], cwest[7]; /* coordinate info for debugging: printf("You are at %d,%d.\n", col, row); printf("North=%d. South=%d. East=%d. West=%d.\n", north, south, east, west); */ /*1,1 is the location of the final point in the maze.*/ if(col==1 && row==1) { printf("You've won!! (What an accomplishment...)\n"); again=0; } else { if(north) {strcpy(cnorth, "north\t"); } else { strcpy(cnorth, ""); } printf("You may go: \t%s", cnorth); if(south) {strcpy(csouth, "south\t");} else { strcpy(csouth, ""); } printf("%s", csouth); if(east) {strcpy(ceast, "east\t"); } else { strcpy(ceast, ""); } printf("%s", ceast); if(west) {strcpy(cwest, "west\t"); } else {strcpy(cwest, "");} printf("%s\n", cwest); } } /* getPonse() : Parses submitted text. This particular game only supports single-character commands. */ void getPonse() { char ponse[2]; printf(">"); scanf("%c%c", &ponse[0], &ponse[1]); /*scans 2 characters, the command and the \n. getc() doesn't work on my machine, otherwise I'd have used that instead.*/ switch(ponse[0]) { case 'n': if(north) { row+= -1; break; }else{ printf("You slam into a wall. Wanna try again?\n"); break; } case 's':if(south) { row+= 1; break; }else{ printf("You slam into a wall. Wanna try again?\n"); break; } case 'e':if(east) { col+= 1; break; }else{ printf("You slam into a wall. Wanna try again?\n"); break; } case 'w':if(west) { col+= -1; break; }else{ printf("You slam into a wall. Wanna try again?\n"); break; } case 'q': again=0; break; case '?': printf("HELP:\nn\tnorth\ns\tsouth\ne\teast\nw\twest\nq\tquit\n?\thelp"); break; default: printf("'scuse me?\n"); break; } } /* printMaze() : Prints out a copy of the maze, grid-style. Each row of the grid is a column, each column of the grid is a row. The four-digit number in each grid position is the passability of the four cardinal direcitons(n, s, e, w). Confusing, yes, but operable, and only took 5 minutes to grind out. */ void printMaze() { int i, n, q; for(i=0; i<3; i++) { col=i; for(n=0; n<3; n++) { row=n; for(q=0; q<4; q++) { printf("%d",maze[col][row][q]); } printf("\t"); } printf("\n"); } }