home *** CD-ROM | disk | FTP | other *** search
- /* *
- * M A G I C W A L L S *
- * *
- * mwalls.c *
- * Heikki Suopanki 1991 *
- * email: suopanki@phoenix.oulu.fi *
- * *
- * You are a ghost, you have to go through *
- * every wall once and only once. *
- * */
-
- #include <stdio.h>
- #include <curses.h>
-
- #define XMAX 35
- #define XMIN 5
- #define YMAX 22
- #define YMIN 2
- #define THICK 1
- #define THIN 0
- #define START_PLACES 6
- #define THICK_WALL "#"
- #define THIN_WALL "+"
- #define GHOST "%"
- #define WALL_NUM 16
- #define NORMAL_MOVE -1
- #define CANT_MOVE 100
-
-
- int xp, xpo, yp, ypo;
- int walls;
- int moves;
-
- struct wall_pos {
- int x_start;
- int y_start;
- int x_end;
- int y_end;
- int wall_state;
- } ;
-
- struct wall_pos wall_map[WALL_NUM] =
- {
- {10,7,15,7,THIN},
- {15,7,25,7,THIN},
- {25,7,30,7,THIN},
- {15,12,20,12,THIN},
- {20,12,25,12,THIN},
- {25,12,30,12,THIN},
- {10,17,15,17,THIN},
- {15,17,20,17,THIN},
- {20,17,30,17,THIN},
- {10,7,10,17,THIN},
- {15,7,15,12,THIN},
- {15,12,15,17,THIN},
- {20,12,20,17,THIN},
- {25,7,25,12,THIN},
- {30,7,30,12,THIN},
- {30,12,30,17,THIN}
- };
-
- int start_place[START_PLACES][2] =
- { 20, 5, 12, 12, 20, 10, 27, 10, 17, 15, 25, 15};
-
- main(argc,argv)
- int argc;
- char *argv[];
- {
- char mv;
- if (argc>1) show_instructions();
- init_game();
- while(walls > 0) {
- mv = getch();
- if (mv == 'q' || mv == 'Q') end_game();
- if (mv == 'r'){
- redraw_screen();
- move_player();
- }
- if (mv == 'n') init_game();
- if (mv == 'h' || mv == 'j' || mv == 'k' || mv == 'l') {
- xpo = xp;
- ypo = yp;
- moves++;
- if (mv == 'h') {
- if (--xp < XMIN) xp = XMIN; }
- if (mv == 'j') {
- if (++yp > YMAX) yp = YMAX; }
- if (mv == 'k') {
- if (--yp < YMIN) yp = YMIN; }
- if (mv == 'l') {
- if (++xp > XMAX) xp = XMAX; }
- update_game();
- move_player();
- }
- }
- clear();
- refresh();
- endwin();
- printf("%s", "Congratulations!");
- printf("%s%d%s", "You solved the game using ", moves, " moves");
- exit();
-
- }
-
- init_game()
- {
- int place;
- int i;
- for(i=0;i < WALL_NUM; i++){
- wall_map[i].wall_state = THIN;
- }
- moves = 0;
- walls = WALL_NUM;
- initscr();
- crmode();
- noecho();
- xp = 10;
- xpo = xp;
- yp = 3;
- ypo = yp;
- redraw_screen();
- for(i=0; i < START_PLACES; i++){
- mvprintw(start_place[i][1], start_place[i][0],"%1d", i+1);
- }
- mvaddstr(20, 40, "Starting place? Give number (1-6)");
- refresh();
- place = 0;
- while (place < 1 || place > START_PLACES){
- place = getch() - 48;
- }
- mvaddstr(20, 40, " ");
- for(i=0; i < START_PLACES; i++){
- mvaddstr(start_place[i][1], start_place[i][0], " ");
- }
- xp = start_place[--place][0];
- yp = start_place[place][1];
- move_player();
- }
-
- update_game()
- {
- int check_res;
- check_res = check_wall();
- if (check_res == CANT_MOVE) {
- xp = xpo;
- yp = ypo;
- }
- else {
- if (check_res != NORMAL_MOVE) {
- xp = 2*xp - xpo;
- yp = 2*yp - ypo;
- move_player();
- block_wall(check_res);
- }
- }
- update_stats();
- }
-
- redraw_screen()
- {
- int i,x1,y1,x2,y2;
- char *wall_type;
- clear();
- for(i=0; i < WALL_NUM; i++) {
- x1 = wall_map[i].x_start;
- y1 = wall_map[i].y_start;
- x2 = wall_map[i].x_end;
- y2 = wall_map[i].y_end;
- if (wall_map[i].wall_state == THICK){
- wall_type = THICK_WALL;
- }
- else {
- wall_type = THIN_WALL;
- }
- if (x1 == x2) {
- while (y1 <= y2) {
- mvaddstr(y1++,x1, wall_type);
- }
- }
- else {
- while (x1 <= x2) {
- mvaddstr(y1,x1++, wall_type);
- }
- }
- }
- mvaddstr(1,6, " M A G I C W A L L S v1.0 , Heikki Suopanki 1991.");
- mvaddstr(3, 50, "Solve the puzzle....");
- mvaddstr(4, 50, "Go through each wall!");
- mvprintw(8, 50, "%s%3d", "Moves: ", moves);
- mvprintw(9, 50, "%s%3d", "Walls left: ", walls);
- mvaddstr(12, 55, "k");
- mvaddstr(13, 53, "h l");
- mvaddstr(14, 55, "j");
- mvaddstr(15, 50, "r = redisplay");
- mvaddstr(16, 50, "n = new game");
- mvaddstr(17, 50, "q = quit");
- move(yp, xp);
- refresh();
- }
-
- check_wall()
- {
- int i;
- int sum = 0;
- int result = NORMAL_MOVE;
- for(i=0; i < WALL_NUM;i++) {
- if (wall_map[i].x_start == wall_map[i].x_end) {
- if (xp == wall_map[i].x_start && yp >= wall_map[i].y_start && yp <= wall_map[i].y_end) {
- if (wall_map[i].wall_state == THIN){
- result = i; }
- else { result = CANT_MOVE;}
- sum++;
- }
- }
- else {
- if (yp == wall_map[i].y_start && xp >= wall_map[i].x_start && xp <= wall_map[i].x_end) {
- if (wall_map[i].wall_state == THIN){
- result = i; }
- else { result = CANT_MOVE;}
- sum++;
- }
- }
-
- }
- if (sum > 1) { result = CANT_MOVE;}
- return result;
- }
-
- move_player()
- {
- mvaddstr(ypo,xpo, " ");
- mvaddstr(yp,xp, GHOST);
- move(yp,xp);
- refresh();
- }
-
- update_stats()
- {
- mvprintw(8, 62, "%3d", moves);
- mvprintw(9, 62, "%3d", walls);
- }
-
- show_instructions()
- {
- puts("You are a ghost and you have to go through every wall.");
- puts("You can go only once through each wall.");
- puts("The walls are marked either ++++ or ###, depending on");
- puts("whether you have go through them or not.");
- puts("You can choose the room where you want to start.");
- exit();
- }
-
- block_wall(i)
- int i;
- {
- int x1,y1,x2,y2;
- wall_map[i].wall_state = THICK;
- walls--;
- x1 = wall_map[i].x_start;
- y1 = wall_map[i].y_start;
- x2 = wall_map[i].x_end;
- y2 = wall_map[i].y_end;
- if (x1 == x2) {
- while (y1 <= y2) {
- mvaddstr(y1++,x1, "#");
- }
- }
- else {
- while (x1 <= x2){
- mvaddstr(y1,x1++, "#");
- }
- }
- }
-
- end_game()
- {
- clear();
- mvaddstr(20,1, "");
- refresh();
- endwin();
- printf("\n");
- exit();
- }
-