uni

University stuff
git clone git://git.margiolis.net/uni.git
Log | Files | Refs | README | LICENSE

outputs.c (3050B)


      1 #include "outputs.h"
      2 
      3 void
      4 print_board(const Board *b)
      5 {    
      6 	int i, j, x, y = 1;
      7 	print_grid(b);
      8 	wattron(b->gw, A_BOLD);
      9 	for (i = 0; i < b->rows; i++)
     10 	{
     11 		x = 2; 
     12 		for (j = 0; j < b->cols; j++)
     13 		{
     14 			mvwaddch(b->gw, y, x, b->db[i][j]);
     15 			x += 3;
     16 		}
     17 		y++;
     18 	}
     19 }
     20 
     21 void
     22 print_grid(const Board *b)
     23 {
     24 	int i, j;
     25 	wattroff(b->gw, A_BOLD);
     26 	for (i = 1; i <= b->rows; i++)
     27 	{
     28 		wmove(b->gw, i, 1);
     29 		for (j = 0; j < b->cols; j++)
     30 			wprintw(b->gw, "[ ]");
     31 	}
     32 	wrefresh(b->gw);
     33 }
     34 
     35 #define YMID(x) getmaxy(x)/2
     36 #define XMID(x) getmaxx(x)/2
     37 
     38 void
     39 session_info(const Board *b)
     40 {
     41 	mvprintw(0, 0, "Current position: (%d, %d) ", b->x, b->y);
     42 	mvprintw(0, XMID(stdscr)-strlen("Defused mines: x/x")/2,
     43 			"Defused mines: %d/%d", b->ndefused, b->nmines);
     44 	mvprintw(0, XMAX(stdscr)-strlen("m Controls"), "m Controls");
     45 }
     46 
     47 void
     48 session_write(const Board *b, State state)
     49 {
     50 	int i, j;
     51 	FILE *fsession = fopen(SESSION_PATH, "w");
     52 	if (fsession == NULL)
     53 	{
     54 		mvprintw(0, 0, "Error opening file, exiting...");
     55 		refresh();
     56 		exit(EXIT_FAILURE);
     57 	}
     58 	else
     59 	{
     60 		state == GAME_WON
     61 			? fprintf(fsession, "Mine hit at position (%d, %d)\n\n",
     62 					b->x+1, b->y+1)
     63 			: fprintf(fsession, "Last mine defused at position (%d, %d)\n\n",
     64 					b->x+1, b->y+1);
     65 		fprintf(fsession, "Board overview\n\n");
     66 		for (i = 0; i < b->rows; i++)
     67 		{
     68 			for (j = 0; j < b->cols; j++)
     69 				fprintf(fsession, "%c ", b->mb[i][j]);
     70 			fprintf(fsession, "\n");
     71 		}           
     72 	}
     73 	fclose(fsession);
     74 }
     75 
     76 void
     77 score_write(const Board *b)
     78 {
     79 	FILE *scorelog = fopen(SCORE_LOG_PATH, "a");
     80 	char *playername = get_pname();
     81 	if (scorelog == NULL)
     82 	{
     83 		mvprintw(0, 0, "Error opening file, exiting...");
     84 		refresh();
     85 		exit(EXIT_FAILURE);
     86 	}
     87 	else
     88 	{
     89 		fprintf(scorelog, "%s,%d,%dx%d\n",
     90 				playername, b->ndefused, b->cols, b->rows);
     91 		sort_scorelog(scorelog);
     92 		clrtoeol();
     93 		show_scorelog(scorelog);
     94 		mvprintw(0, 0, "New score written to score log");
     95 		refresh();
     96 		getchar();
     97 	}
     98 	fclose(scorelog);
     99 	free(playername);
    100 }
    101 
    102 char *
    103 get_pname(void)
    104 {   
    105 	char buffer[20];
    106 	char *playername;
    107 	move(0, 0);
    108 	echo();
    109 	clrtoeol();
    110 	printw("Your name: ");
    111 	refresh();
    112 	scanw("%s", buffer);
    113 	noecho();
    114 	refresh();
    115 	playername = (char *)malloc(strlen(buffer) + 1);
    116 	return (strcpy(playername, buffer));
    117 }
    118 
    119 void
    120 sort_scorelog(FILE *scorelog)
    121 {
    122 
    123 }
    124 
    125 void
    126 show_scorelog(FILE *scorelog)
    127 {
    128 
    129 }
    130 
    131 void
    132 parse_data(FILE *scorelog)
    133 {
    134 
    135 }
    136 
    137 void
    138 game_won(const Board *b)
    139 {
    140 	wclear(b->gw);
    141 	wrefresh(b->gw);
    142 	attron(A_BOLD);
    143 	mvprintw(YMID(stdscr)-2, XMID(stdscr)-11, "You defused all the mines!");
    144 	mvprintw(YMID(stdscr)-1, XMID(stdscr)-3, "You won :)");
    145 	mvprintw(YMID(stdscr), XMID(stdscr)-11, "Press any key to continue");
    146 	refresh();
    147 	attroff(A_BOLD);
    148 }
    149 
    150 void
    151 game_over(const Board *b)
    152 {
    153 	wclear(b->gw);
    154 	wrefresh(b->gw);
    155 	attron(A_BOLD);
    156 	mvprintw(YMID(stdscr)-2, XMID(stdscr)-24, "You hit a mine! (or tried to defuse the wrong cell)");
    157 	mvprintw(YMID(stdscr)-1, XMID(stdscr)-4, "Game over :(");
    158 	mvprintw(YMID(stdscr), XMID(stdscr)-11, "Press any key to continue");
    159 	refresh();
    160 	attroff(A_BOLD);
    161 }
    162 
    163 #undef YMID
    164 #undef XMID