|
CS256 - Fundamentals of Structured Programming
| Displaying exercises/e9/solution/c4.c
#include "c.h"
/**
* 1) Open the map.txt file for reading. If you cannot open it, print that you
* are unable to and waitforkeypress() and then exit the program.
* 2) Read the map one character at a time, process the map in such a way that
* each character is put on the curses screen like so:
* '#' -> a space w/ blue background
* '.' -> a space with white background
* '&' -> a '&' that is yellow on white and bold
* '+' -> a '+' that is yellow on white
* '%' -> a '%' that is red on white and bold
* 's' -> a 's' that is white on blue
* anything else should be printed as is.
* 3) Keep track of the number of lines printed (look for '\n' characters)
* and stop when you are at the last line of the screen.
*/
int main(void)
{
init_curses();
FILE *fp = fopen("map.txt", "r");
if (fp == NULL) {
mvprintw(10,10, "Unable to open map.txt");
mvprintw(LINES-1, 0, "Press any key to exit");
getch();
leave_curses();
return 0;
}
clear();
int ch, line;
move(0,0);
while((ch = fgetc(fp)) != EOF) {
if (ch == '\n') {
line++;
if (line == LINES-1) break;
}
if (ch == '#') addch(' '| COLOR_PAIR(COLOR_BLUE));
else if (ch == '.') addch(' '| COLOR_PAIR(COLOR_WHITE));
else if (ch == '&') addch('&'| COLOR_PAIR(COLOR_YELLOW*8+COLOR_WHITE)|A_BOLD);
else if (ch == '+') addch('+'| COLOR_PAIR(COLOR_YELLOW*8+COLOR_WHITE));
else if (ch == '%') addch('%'| COLOR_PAIR(COLOR_RED*8+COLOR_WHITE)|A_BOLD);
else if (ch == 's') addch('s'| COLOR_PAIR(COLOR_WHITE*8+COLOR_BLUE));
else addch(ch);
}
fclose(fp);
mvprintw(LINES-1, 0, "Press any key to exit");
getch();
leave_curses();
return 0;
}
|