commit 67f8660b8df3f52758033f4f9ca6eca92e98145e parent 55f257463e36928fb96b10be1c086b5498edf15f Author: Christos Margiolis <christos@margiolis.net> Date: Tue, 19 May 2020 17:58:47 +0300 Revert "added some constexprs" This reverts commit 1fe9b4ca48d06c85ff01577edcba3f12276239f5. Diffstat:
61 files changed, 532 insertions(+), 286 deletions(-)
diff --git a/assignment-1.5-arrays-pointers-files/minecurses/README.md b/assignment-1.5-arrays-pointers-files/minecurses/README.md @@ -31,7 +31,6 @@ Additional controls * Add colors * Add UTF-8 support * Fix wrong mine spawning bug -* Fix flickering and cursor ## Execution diff --git a/assignment-1.5-arrays-pointers-files/minecurses/bin/minecurses b/assignment-1.5-arrays-pointers-files/minecurses/bin/minecurses Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/minecurses/log/session.txt b/assignment-1.5-arrays-pointers-files/minecurses/log/session.txt @@ -1,9 +1,9 @@ -Last mine defused at position (1, 3) +Mine hit at position (5, 5) Board overview - - - - - -1 1 - - - -* 1 - - - -1 1 - - - - - - - - +- - - - - +- - - 1 1 +- - - 1 D diff --git a/assignment-1.5-arrays-pointers-files/minecurses/obj/audio.o b/assignment-1.5-arrays-pointers-files/minecurses/obj/audio.o Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/minecurses/obj/gameplay.o b/assignment-1.5-arrays-pointers-files/minecurses/obj/gameplay.o Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/minecurses/obj/main.o b/assignment-1.5-arrays-pointers-files/minecurses/obj/main.o Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/minecurses/obj/minesweeper.o b/assignment-1.5-arrays-pointers-files/minecurses/obj/minesweeper.o Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/minecurses/obj/navigation.o b/assignment-1.5-arrays-pointers-files/minecurses/obj/navigation.o Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/minecurses/obj/outputs.o b/assignment-1.5-arrays-pointers-files/minecurses/obj/outputs.o Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/minecurses/src/audio.c b/assignment-1.5-arrays-pointers-files/minecurses/src/audio.c @@ -1,7 +1,7 @@ #include "audio.h" void * -play_audio(void *tid) +play_audio(void *threadid) { /*int tid = (long)threadid;*/ Mix_Music *music = NULL; @@ -13,7 +13,6 @@ play_audio(void *tid) while (Mix_PlayingMusic()) ; Mix_FreeMusic(music); Mix_CloseAudio(); - return NULL; } void diff --git a/assignment-1.5-arrays-pointers-files/minecurses/src/audio.h b/assignment-1.5-arrays-pointers-files/minecurses/src/audio.h @@ -6,7 +6,7 @@ #define AUDIO_PATH "res/detective-8bit.wav" -void *play_audio(void *tid); +void *play_audio(void *threadid); void volume (char); void pause_audio(void); diff --git a/assignment-1.5-arrays-pointers-files/minecurses/src/gameplay.c b/assignment-1.5-arrays-pointers-files/minecurses/src/gameplay.c @@ -1,153 +1,135 @@ #include "gameplay.h" #include "main.h" -#define X b->x -#define Y b->y - void -play(Board *b) +play(WINDOW *gw, Board *b) { - b->x = b->y = 0; - b->gameover = FALSE; - b->ndefused = 0; + int mbx = 0, mby = 0; + int gameover = FALSE; + int ndefused = 0; int move; do { erase(); - delwin(b->gw); + delwin(gw); refresh(); - b->gw = game_win(b->rows, b->cols); - print_board(b); - session_info(b); - navigate(b, &move); + gw = game_win(b->rows, b->cols); + print_board(gw, b); + session_info(mbx, mby, ndefused, b->nmines); + navigate(gw, &move, &mbx, &mby); switch (move) { - case ENTER: /* FALLTHROUGH */ - case OPEN_LOWER: - case OPEN_UPPER: - b->gameover = open_cell(b); + case ENTER: case OPEN_LOWER: case OPEN_UPPER: + gameover = open_cell(gw, b, mby, mbx, gameover); break; - case FLAG_LOWER: /* FALLTHROUGH */ - case FLAG_UPPER: - handle_flags(b); + case FLAG_LOWER: case FLAG_UPPER: + handle_flags(gw, b, mby, mbx); break; - case DEFUSE_LOWER: /* FALLTHROUGH */ - case DEFUSE_UPPER: - if (b->db[Y][X] == FLAG && b->mb[Y][X] == MINE) + case DEFUSE_LOWER: case DEFUSE_UPPER: + if (b->db[mby][mbx] == FLAG && b->mb[mby][mbx] == MINE) { - b->ndefused++; - defuse_mine(b); + ndefused++; + defuse_mine(gw, b, mby, mbx); } - else if (b->db[Y][X] == FLAG && b->mb[Y][X] != MINE) - b->gameover = TRUE; + else if (b->db[mby][mbx] == FLAG && b->mb[mby][mbx] != MINE) gameover = TRUE; break; case PAUSE_AUDIO: pause_audio(); break; - case VOLUME_UP: /* FALLTHROUGH */ - case VOLUME_DOWN: + case VOLUME_UP: case VOLUME_DOWN: volume(move); break; case 'm': - handle_menu(b); + handle_menu(gw, b); break; case 'r': clear_board(b); reset(b); - init_game(b); + init_game(gw, b); break; default: break; } - } while (((Y >= 0 && Y < b->rows) && - (X >= 0 && X < b->cols)) && - b->ndefused < b->nmines && !b->gameover && - move != QUIT); + } while (((mby >= 0 && mby < b->rows) && (mbx >= 0 && mbx < b->cols)) && + ndefused < b->nmines && !gameover && move != QUIT); - if (b->gameover) - handle_gameover(b); - if (b->ndefused == b->nmines) - handle_win(b); + if (gameover) + handle_gameover(gw, b, mby, mbx); + if (ndefused == b->nmines) + handle_win(gw, b, mby, mbx, ndefused); } int -open_cell(Board *b) +open_cell(WINDOW *gw, Board *b, int mby, int mbx, int gameover) { - transfer(b); - reveal(b); - return (b->db[Y][X] == MINE) ? TRUE : FALSE; + transfer(b, mby, mbx); + reveal(gw, b, mby, mbx, mby+1, 3*mbx+2); + if (b->db[mby][mbx] == MINE) gameover = TRUE; + return gameover; } void -handle_flags(Board *b) +handle_flags(WINDOW *gw, Board *b, int mby, int mbx) { - if (b->db[Y][X] == FLAG) - b->db[Y][X] = BLANK; - else if (b->db[Y][X] != FLAG && - b->db[Y][X] != BLANK) - return; - else b->db[Y][X] = FLAG; - reveal(b); + if (b->db[mby][mbx] == FLAG) b->db[mby][mbx] = BLANK; + else if (b->db[mby][mbx] != FLAG && b->db[mby][mbx] != BLANK) return; + else b->db[mby][mbx] = FLAG; + reveal(gw, b, mby, mbx, mby+1, 3*mbx+2); } void -defuse_mine(Board *b) +defuse_mine(WINDOW *gw, Board *b, int mby, int mbx) { refresh(); - b->db[Y][X] = b->mb[Y][X] = DEFUSED; - reveal(b); + b->db[mby][mbx] = b->mb[mby][mbx] = DEFUSED; + reveal(gw, b, mby, mbx, mby+1, 3*mbx+2); } void -transfer(Board *b) +transfer(Board *b, int mby, int mbx) { - b->db[Y][X] = b->mb[Y][X]; + b->db[mby][mbx] = b->mb[mby][mbx]; } void -reveal(const Board *b) +reveal(WINDOW *gw, Board *b, int mby, int mbx, int y, int x) { - int y = b->y + 1; - int x = 3 * b->x + 2; - mvwaddch(b->gw, y, x, b->db[Y][X]); - wrefresh(b->gw); + mvwaddch(gw, y, x, b->db[mby][mbx]); + wrefresh(gw); } int -is_defused(const Board *b) +is_defused(Board *b, int mby, int mbx) { - return (b->db[Y][X] == DEFUSED) ? TRUE : FALSE; + return (b->db[mby][mbx] == DEFUSED) ? TRUE : FALSE; } void -handle_menu(const Board *b) +handle_menu(WINDOW *gw, Board *b) { options_menu(); - box(b->gw, 0, 0); - print_board(b); + box(gw, 0, 0); + print_board(gw, b); } void -handle_gameover(const Board *b) +handle_gameover(WINDOW *gw, Board *b, int mby, int mbx) { - game_over(b); + game_over(gw); getchar(); erase(); refresh(); - box(b->gw, 0, 0); - print_board(b); - wrefresh(b->gw); - session_write(b, GAME_LOST); + box(gw, 0, 0); + print_board(gw, b); + wrefresh(gw); + session_write(b, mbx, mby, GAME_LOST); } void -handle_win(const Board *b) +handle_win(WINDOW *gw, Board *b, int mby, int mbx, int ndefused) { - game_won(b); + game_won(gw); getchar(); - session_write(b, GAME_WON); - score_write(b); + session_write(b, mbx, mby, GAME_WON); + score_write(ndefused, b->rows, b->cols); } - -#undef Y -#undef X diff --git a/assignment-1.5-arrays-pointers-files/minecurses/src/gameplay.h b/assignment-1.5-arrays-pointers-files/minecurses/src/gameplay.h @@ -23,15 +23,15 @@ #define VOLUME_DOWN '-' #define QUIT 'q' -void play (Board *b); -int open_cell (Board *b); -void handle_flags (Board *b); -void defuse_mine (Board *b); -void transfer (Board *b); -void reveal (const Board *b); -int is_defused (const Board *b); -void handle_menu (const Board *b); -void handle_gameover(const Board *b); -void handle_win (const Board *b); +void play (struct _win_st* gw, Board *b); +int open_cell (struct _win_st* gw, Board *b, int mby, int mbx, int gameover); +void handle_flags (struct _win_st* gw, Board *b, int mby, int mbx); +void defuse_mine (struct _win_st* gw, Board *b, int mby, int mbx); +void transfer (Board *b, int mby, int mbx); +void reveal (struct _win_st* gw, Board *b, int mby, int mbx, int y, int x); +int is_defused (Board *b, int mby, int mbx); +void handle_menu (struct _win_st*, Board *b); +void handle_gameover(struct _win_st* gw, Board *b, int mby, int mbx); +void handle_win (struct _win_st* gw, Board *b, int mby, int mbx, int ndefused); #endif /* GAMEPLAY_H */ diff --git a/assignment-1.5-arrays-pointers-files/minecurses/src/main.c b/assignment-1.5-arrays-pointers-files/minecurses/src/main.c @@ -9,37 +9,32 @@ int main(int argc, char **argv) { -#ifndef NCURSES_VERSION - fprintf(stderr, "ncurses is needed in order to run this program.\n"); - return EXIT_FAILURE; -#endif /* NCURSES_VERSION */ init_curses(); Board b; reset(&b); - b.gw = game_win(b.rows, b.cols); - init_game(&b); + WINDOW *gw = game_win(b.rows, b.cols); + init_game(gw, &b); pthread_t audiothread; - long tid = 1; - pthread_create(&audiothread, NULL, play_audio, (void *)tid); - play(&b); + long threadid = 1; + pthread_create(&audiothread, NULL, play_audio, (void *)threadid); + play(gw, &b); pthread_cancel(audiothread); clear_board(&b); - delwin(b.gw); + delwin(gw); endwin(); - return EXIT_SUCCESS; + + return 0; } void reset(Board *b) { echo(); - b->cols = set_cols(); - b->rows = set_rows(); - b->nmines = set_nmines(b->rows * b->cols); - b->ndefused = b->x = b->y = 0; - b->gameover = FALSE; + b->cols = set_cols(); + b->rows = set_rows(); + b->nmines = set_nmines(b->rows * b->cols); noecho(); options_menu(); erase(); @@ -47,10 +42,10 @@ reset(Board *b) } void -init_game(Board *b) +init_game(WINDOW *gw, Board *b) { - init_db(b); - init_mb(b); + init_db(gw, b); + init_mb(gw, b); } void diff --git a/assignment-1.5-arrays-pointers-files/minecurses/src/main.h b/assignment-1.5-arrays-pointers-files/minecurses/src/main.h @@ -12,17 +12,13 @@ typedef struct { char **db; char **mb; - int rows, cols; - int nmines, ndefused; - int gameover; - int x, y; - WINDOW *gw; + int rows, cols, nmines; } Board; extern Board b; -void reset (Board *b); -void init_game (Board *b); +void reset(Board *b); +void init_game(WINDOW *gw, Board *b); void clear_board(Board *b); #endif /* MAIN_H */ diff --git a/assignment-1.5-arrays-pointers-files/minecurses/src/minesweeper.c b/assignment-1.5-arrays-pointers-files/minecurses/src/minesweeper.c @@ -1,14 +1,19 @@ #include "minesweeper.h" void -init_db(Board *b) +init_db(WINDOW *gw, Board *b) { int i; b->db = (char **)malloc(b->rows * sizeof(char *)); for (i = 0; i < b->rows; i++) b->db[i] = (char *)malloc(b->cols); - if (b->mb == NULL) die(); + if (b->db == NULL) + { + mvprintw(0, 0, "Error, not enough memory, exiting..."); + refresh(); + exit(EXIT_FAILURE); + } else fill_db(b); } @@ -22,14 +27,19 @@ fill_db(Board *b) } void -init_mb(Board *b) +init_mb(WINDOW *gw, Board *b) { int i; b->mb = (char **)malloc(b->rows * sizeof(char *)); for (i = 0; i < b->rows; i++) b->mb[i] = (char *)malloc(b->cols); - if (b->mb == NULL) die(); + if (b->mb == NULL) + { + mvprintw(0, 0, "Error, not enough memory, exiting..."); + refresh(); + exit(EXIT_FAILURE); + } else { place_mines(b); @@ -62,30 +72,30 @@ add_adj(Board *b) } int -is_mine(const Board *b, int r, int c) +is_mine(Board *b, int row, int col) { - return (b->mb[r][c] == MINE) ? TRUE : FALSE; + return (b->mb[row][col] == MINE) ? TRUE : FALSE; } int -outof_bounds(const Board *b, int r, int c) +outof_bounds(Board *b, int row, int col) { - return (r < 0 || r > b->rows-1 || c < 0 || c > b->cols-1) ? TRUE : FALSE; + return (row < 0 || row > b->rows-1 || col < 0 || col > b->cols-1) ? TRUE : FALSE; } uint8_t -adj_mines(const Board *b, int r, int c) +adj_mines(Board *b, int row, int col) { uint8_t nadj = 0; - if (!outof_bounds(b, r, c-1) && b->mb[r][c-1] == MINE) nadj++; // North - if (!outof_bounds(b, r, c+1) && b->mb[r][c+1] == MINE) nadj++; // South - if (!outof_bounds(b, r+1, c) && b->mb[r+1][c] == MINE) nadj++; // East - if (!outof_bounds(b, r-1, c) && b->mb[r-1][c] == MINE) nadj++; // West - if (!outof_bounds(b, r+1, c-1) && b->mb[r+1][c-1] == MINE) nadj++; // North-East - if (!outof_bounds(b, r-1, c-1) && b->mb[r-1][c-1] == MINE) nadj++; // North-West - if (!outof_bounds(b, r+1, c+1) && b->mb[r+1][c+1] == MINE) nadj++; // South-East - if (!outof_bounds(b, r-1, c+1) && b->mb[r-1][c+1] == MINE) nadj++; // South-West + if (!outof_bounds(b, row, col-1) && b->mb[row][col-1] == MINE) nadj++; // north + if (!outof_bounds(b, row, col+1) && b->mb[row][col+1] == MINE) nadj++; // south + if (!outof_bounds(b, row+1, col) && b->mb[row+1][col] == MINE) nadj++; // east + if (!outof_bounds(b, row-1, col) && b->mb[row-1][col] == MINE) nadj++; // west + if (!outof_bounds(b, row+1, col-1) && b->mb[row+1][col-1] == MINE) nadj++; // north-east + if (!outof_bounds(b, row-1, col-1) && b->mb[row-1][col-1] == MINE) nadj++; // north-west + if (!outof_bounds(b, row+1, col+1) && b->mb[row+1][col+1] == MINE) nadj++; // south-east + if (!outof_bounds(b, row-1, col+1) && b->mb[row-1][col+1] == MINE) nadj++; // south-west return nadj; } @@ -99,11 +109,3 @@ fill_spaces(Board *b) if (b->mb[i][j] != MINE && b->mb[i][j] == '0') b->mb[i][j] = '-'; } - -void -die(void) -{ - mvprintw(0, 0, "Error, cannot allocate memory, exiting..."); - refresh(); - exit(EXIT_FAILURE); -} diff --git a/assignment-1.5-arrays-pointers-files/minecurses/src/minesweeper.h b/assignment-1.5-arrays-pointers-files/minecurses/src/minesweeper.h @@ -4,20 +4,20 @@ #include <stdlib.h> #include <string.h> #include <time.h> + #include "wins.h" #define BLANK ' ' #define MINE '*' -void init_db (Board *b); +void init_db (struct _win_st* gw, Board *b); void fill_db (Board *b); -void init_mb (Board *b); +void init_mb (struct _win_st* gw, Board *b); void place_mines (Board *b); void add_adj (Board *b); -int is_mine (const Board *b, int r, int c); -int outof_bounds (const Board *b, int r, int c); -uint8_t adj_mines(const Board *b, int r, int c); +int is_mine (Board *b, int row, int col); +int outof_bounds (Board *b, int row, int col); +uint8_t adj_mines(Board *b, int row, int col); void fill_spaces (Board *b); -void die (void); #endif /* MINESWEEPER_H */ diff --git a/assignment-1.5-arrays-pointers-files/minecurses/src/navigation.c b/assignment-1.5-arrays-pointers-files/minecurses/src/navigation.c @@ -1,37 +1,37 @@ #include "navigation.h" void -navigate(Board *b, int *mv) +navigate(WINDOW *gw, int *mv, int *mbx, int *mby) { static int y = 1, x = 2; - update_curs(b, y, x); - b->x = (x-2)/3; - b->y = y-1; + update_curs(gw, y, x); + *mbx = (x-2)/3; + *mby = y-1; refresh(); - getmv(b, mv, &y, &x); + getmv(gw, mv, &y, &x); } void -getmv(const Board *b, int *mv, int *y, int *x) +getmv(WINDOW *gw, int *mv, int *y, int *x) { - *mv = wgetch(b->gw); - switch (*mv) + *mv = wgetch(gw); + switch (*mv) // vim keys support!! { - case 'k': case 'K': /* FALLTHROUGH */ + case 'k': case 'K': case 'w': case 'W': mvup(y); break; - case 'j': case 'J': /* FALLTHROUGH */ + case 'j': case 'J': case 's': case 'S': - mvdown(y, YMAX(b->gw)); + mvdown(y, YMAX(gw)); break; - case 'h': case 'H': /* FALLTHROUGH */ + case 'h': case 'H': case 'a': case 'A': mvleft(x); break; - case 'l': case 'L': /* FALLTHROUGH */ + case 'l': case 'L': case 'd': case 'D': - mvright(x, XMAX(b->gw)); + mvright(x, XMAX(gw)); break; } } @@ -65,7 +65,7 @@ mvright(int *x, int xmax) } void -update_curs(const Board *b, int y, int x) +update_curs(WINDOW *gw, int y, int x) { - wmove(b->gw, y, x); + wmove(gw, y, x); } diff --git a/assignment-1.5-arrays-pointers-files/minecurses/src/navigation.h b/assignment-1.5-arrays-pointers-files/minecurses/src/navigation.h @@ -4,12 +4,12 @@ #include "main.h" #include "minesweeper.h" -void navigate (Board *b, int *mv); -void getmv (const Board *b, int *mv, int *y, int *x); +void navigate (struct _win_st* gw, int *mv, int *mbx, int *mby); +void getmv (struct _win_st* gw, int *mv, int *y, int *x); void mvup (int *y); void mvdown (int *y, int xmax); void mvleft (int *x); void mvright (int *x, int xmax); -void update_curs(const Board *b, int y, int x); +void update_curs(struct _win_st* gw, int y, int x); #endif /* NAVIGATION_H */ diff --git a/assignment-1.5-arrays-pointers-files/minecurses/src/outputs.c b/assignment-1.5-arrays-pointers-files/minecurses/src/outputs.c @@ -1,17 +1,17 @@ #include "outputs.h" void -print_board(const Board *b) +print_board(WINDOW *gw, Board *b) { int i, j, x, y = 1; - print_grid(b); - wattron(b->gw, A_BOLD); + print_grid(gw, b->rows, b->cols); + wattron(gw, A_BOLD); for (i = 0; i < b->rows; i++) { x = 2; for (j = 0; j < b->cols; j++) { - mvwaddch(b->gw, y, x, b->db[i][j]); + mvwaddch(gw, y, x, b->db[i][j]); x += 3; } y++; @@ -19,33 +19,32 @@ print_board(const Board *b) } void -print_grid(const Board *b) +print_grid(WINDOW *gw, int rows, int cols) { int i, j; - wattroff(b->gw, A_BOLD); - for (i = 1; i <= b->rows; i++) + wattroff(gw, A_BOLD); + for (i = 1; i <= rows; i++) { - wmove(b->gw, i, 1); - for (j = 0; j < b->cols; j++) - wprintw(b->gw, "[ ]"); + wmove(gw, i, 1); + for (j = 0; j < cols; j++) + wprintw(gw, "[ ]"); } - wrefresh(b->gw); + wrefresh(gw); } #define YMID(x) getmaxy(x)/2 #define XMID(x) getmaxx(x)/2 void -session_info(const Board *b) +session_info(int mbx, int mby, int ndefused, int nmines) { - mvprintw(0, 0, "Current position: (%d, %d) ", b->x, b->y); - mvprintw(0, XMID(stdscr)-strlen("Defused mines: x/x")/2, - "Defused mines: %d/%d", b->ndefused, b->nmines); + mvprintw(0, 0, "Current position: (%d, %d) ", mbx, mby); + mvprintw(0, XMID(stdscr)-strlen("Defused mines: x/x")/2, "Defused mines: %d/%d", ndefused, nmines); mvprintw(0, XMAX(stdscr)-strlen("m Controls"), "m Controls"); } void -session_write(const Board *b, State state) +session_write(Board *b, int hitrow, int hitcol, State state) { int i, j; FILE *fsession = fopen(SESSION_PATH, "w"); @@ -58,10 +57,8 @@ session_write(const Board *b, State state) else { state == GAME_WON - ? fprintf(fsession, "Mine hit at position (%d, %d)\n\n", - b->x+1, b->y+1) - : fprintf(fsession, "Last mine defused at position (%d, %d)\n\n", - b->x+1, b->y+1); + ? fprintf(fsession, "Mine hit at position (%d, %d)\n\n", hitrow+1, hitcol+1) + : fprintf(fsession, "Last mine defused at position (%d, %d)\n\n", hitrow+1, hitcol+1); fprintf(fsession, "Board overview\n\n"); for (i = 0; i < b->rows; i++) { @@ -74,7 +71,7 @@ session_write(const Board *b, State state) } void -score_write(const Board *b) +score_write(int ndefused, int cols, int rows) { FILE *scorelog = fopen(SCORE_LOG_PATH, "a"); char *playername = get_pname(); @@ -86,8 +83,7 @@ score_write(const Board *b) } else { - fprintf(scorelog, "%s,%d,%dx%d\n", - playername, b->ndefused, b->cols, b->rows); + fprintf(scorelog, "%s,%d,%dx%d\n", playername, ndefused, cols, rows); sort_scorelog(scorelog); clrtoeol(); show_scorelog(scorelog); @@ -135,10 +131,10 @@ parse_data(FILE *scorelog) } void -game_won(const Board *b) +game_won(WINDOW *gw) { - wclear(b->gw); - wrefresh(b->gw); + wclear(gw); + wrefresh(gw); attron(A_BOLD); mvprintw(YMID(stdscr)-2, XMID(stdscr)-11, "You defused all the mines!"); mvprintw(YMID(stdscr)-1, XMID(stdscr)-3, "You won :)"); @@ -148,10 +144,10 @@ game_won(const Board *b) } void -game_over(const Board *b) +game_over(WINDOW *gw) { - wclear(b->gw); - wrefresh(b->gw); + wclear(gw); + wrefresh(gw); attron(A_BOLD); mvprintw(YMID(stdscr)-2, XMID(stdscr)-24, "You hit a mine! (or tried to defuse the wrong cell)"); mvprintw(YMID(stdscr)-1, XMID(stdscr)-4, "Game over :("); diff --git a/assignment-1.5-arrays-pointers-files/minecurses/src/outputs.h b/assignment-1.5-arrays-pointers-files/minecurses/src/outputs.h @@ -12,16 +12,16 @@ typedef enum { GAME_LOST } State; -void print_board (const Board *b); -void print_grid (const Board *b); -void session_info (const Board *b); -void session_write(const Board *b, State state); -void score_write (const Board *b); +void print_board (struct _win_st* gw, Board *b); +void print_grid (struct _win_st* gw, int rows, int cols); +void session_info (int mbx, int mby, int ndefused, int nmines); +void session_write(Board *b, int hitrow, int hitcol, State state); +void score_write (int ndefused, int cols, int rows); char *get_pname (void); void sort_scorelog(FILE *scorelog); void show_scorelog(FILE *scorelog); void parse_data (FILE *scorelog); -void game_won (const Board *b); -void game_over (const Board *b); +void game_won (struct _win_st* gw); +void game_over (struct _win_st* gw); #endif /* OUTPUTS_H */ diff --git a/assignment-2.2-classes/classes.cpp b/assignment-2.2-classes/classes.cpp @@ -22,11 +22,11 @@ class Student Student(const Student& s); ~Student(); + const char *get_AM() const; const std::string& get_name() const; - constexpr const char *get_AM() const {return this->AM;} - constexpr unsigned int get_semester() const {return this->semester;} - constexpr unsigned int get_pcourses() const {return this->pcourses;} - constexpr float *get_grades() const {return this->grades;} + unsigned int get_semester() const; + unsigned int get_pcourses() const; + float *get_grades() const; void set_AM(const char *AM); void set_name(const std::string& name); @@ -77,12 +77,36 @@ Student::~Student() } } +const char * +Student::get_AM() const +{ + return this->AM; +} + const std::string& Student::get_name() const { return this->name; } +unsigned int +Student::get_semester() const +{ + return this->semester; +} + +unsigned int +Student::get_pcourses() const +{ + return this->pcourses; +} + +float * +Student::get_grades() const +{ + return this->grades; +} + void Student::set_AM(const char *AM) { diff --git a/assignment-2.3-operoverloading/bin/operoverloading b/assignment-2.3-operoverloading/bin/operoverloading Binary files differ. diff --git a/assignment-2.3-operoverloading/obj/course.o b/assignment-2.3-operoverloading/obj/course.o Binary files differ. diff --git a/assignment-2.3-operoverloading/obj/main.o b/assignment-2.3-operoverloading/obj/main.o Binary files differ. diff --git a/assignment-2.3-operoverloading/obj/student.o b/assignment-2.3-operoverloading/obj/student.o Binary files differ. diff --git a/assignment-2.3-operoverloading/src/course.cpp b/assignment-2.3-operoverloading/src/course.cpp @@ -21,6 +21,12 @@ Course::get_cname() const return this->cname; } +unsigned int +Course::get_csemester() const +{ + return this->csemester; +} + void Course::set_code(const std::string& code) { diff --git a/assignment-2.3-operoverloading/src/course.h b/assignment-2.3-operoverloading/src/course.h @@ -18,7 +18,7 @@ class Course const std::string& get_code() const; const std::string& get_cname() const; - constexpr unsigned int get_csemester() const {return csemester;} + unsigned int get_csemester() const; void set_code(const std::string& code); void set_cname(const std::string& cname); diff --git a/assignment-2.3-operoverloading/src/student.cpp b/assignment-2.3-operoverloading/src/student.cpp @@ -93,12 +93,84 @@ Student::operator= (const Student& s) return *this; } +bool +Student::operator== (const Student& s) const +{ + return this->semester == s.semester; +} + +bool +Student::operator!= (const Student& s) const +{ + return this->semester != s.semester; +} + +bool +Student::operator< (const Student& s) const +{ + return this->semester < s.semester; +} + +bool +Student::operator<= (const Student& s) const +{ + return this->semester <= s.semester; +} + +bool +Student::operator> (const Student& s) const +{ + return this->semester > s.semester; +} + +bool +Student::operator>= (const Student& s) const +{ + return this->semester >= s.semester; +} + +const char * +Student::get_AM() const +{ + return this->AM; +} + const std::string& Student::get_name() const { return this->name; } +unsigned int +Student::get_semester() const +{ + return this->semester; +} + +unsigned int +Student::get_pcourses() const +{ + return this->pcourses; +} + +float * +Student::get_grades() const +{ + return (this->pcourses > 0) ? this->grades : nullptr; +} + +Course ** +Student::get_submitted_courses() const +{ + return this->sc; +} + +unsigned int +Student::get_num_submitted_courses() const +{ + return this->nsc; +} + void Student::set_AM(const char *AM) { diff --git a/assignment-2.3-operoverloading/src/student.h b/assignment-2.3-operoverloading/src/student.h @@ -31,20 +31,20 @@ class Student void operator+= (Course *c); Student operator= (const Student& s); - bool operator== (const Student& s) const {return this->semester == s.semester;} - bool operator!= (const Student& s) const {return this->semester != s.semester;} - bool operator< (const Student& s) const {return this->semester < s.semester;} - bool operator<= (const Student& s) const {return this->semester <= s.semester;} - bool operator> (const Student& s) const {return this->semester > s.semester;} - bool operator>= (const Student& s) const {return this->semester >= s.semester;} + bool operator== (const Student& s) const; + bool operator!= (const Student& s) const; + bool operator< (const Student& s) const; + bool operator<= (const Student& s) const; + bool operator> (const Student& s) const; + bool operator>= (const Student& s) const; + const char *get_AM() const; const std::string& get_name() const; - constexpr const char *get_AM() const {return this->AM;} - constexpr unsigned int get_semester() const {return this->semester;} - constexpr unsigned int get_pcourses() const {return this->pcourses;} - constexpr float *get_grades() const {return (this->pcourses > 0) ? this->grades : nullptr;} - constexpr Course **get_submitted_courses() const {return this->sc;} - constexpr unsigned int get_num_submitted_courses() const {return this->nsc;} + unsigned int get_semester() const; + unsigned int get_pcourses() const; + float *get_grades() const; + Course **get_submitted_courses() const; + unsigned int get_num_submitted_courses() const; void set_AM(const char *AM); void set_name(const std::string& name); diff --git a/assignment-2.4-inheritance/bin/inheritance b/assignment-2.4-inheritance/bin/inheritance Binary files differ. diff --git a/assignment-2.4-inheritance/obj/app.o b/assignment-2.4-inheritance/obj/app.o Binary files differ. diff --git a/assignment-2.4-inheritance/obj/appsystem.o b/assignment-2.4-inheritance/obj/appsystem.o Binary files differ. diff --git a/assignment-2.4-inheritance/obj/game.o b/assignment-2.4-inheritance/obj/game.o Binary files differ. diff --git a/assignment-2.4-inheritance/obj/main.o b/assignment-2.4-inheritance/obj/main.o Binary files differ. diff --git a/assignment-2.4-inheritance/obj/manufacturer.o b/assignment-2.4-inheritance/obj/manufacturer.o Binary files differ. diff --git a/assignment-2.4-inheritance/obj/office.o b/assignment-2.4-inheritance/obj/office.o Binary files differ. diff --git a/assignment-2.4-inheritance/obj/review.o b/assignment-2.4-inheritance/obj/review.o Binary files differ. diff --git a/assignment-2.4-inheritance/src/app.cpp b/assignment-2.4-inheritance/src/app.cpp @@ -64,6 +64,12 @@ App::addrevs(const std::vector<Review *> revs) reviews.insert(reviews.end(), revs.begin(), revs.end()); } +const char * +App::get_serialnum() const +{ + return serialnum; +} + const std::string& App::get_name() const { @@ -82,6 +88,18 @@ App::get_manf() const return Manufacturer(*manf); } +const std::vector<Review *>& +App::get_revs() const +{ + return reviews; +} + +int +App::get_price() const +{ + return price; +} + void App::set_serialnum(const char *serialnum) { diff --git a/assignment-2.4-inheritance/src/app.h b/assignment-2.4-inheritance/src/app.h @@ -31,12 +31,12 @@ class App void addrev(Review *rev); void addrevs(const std::vector<Review *> revs); + const char *get_serialnum() const; const std::string& get_name() const; const std::string& get_os() const; const Manufacturer get_manf() const; - constexpr const char *get_serialnum() const {return serialnum;} - constexpr const std::vector<Review *>& get_revs() const {return reviews;} - constexpr int get_price() const {return price;} + const std::vector<Review *>& get_revs() const; + int get_price() const; void set_serialnum(const char *serialnum); void set_name(const std::string& name); diff --git a/assignment-2.4-inheritance/src/appsystem.cpp b/assignment-2.4-inheritance/src/appsystem.cpp @@ -183,6 +183,18 @@ AppSystem::get_goodgames() const return ggames; } +const std::vector<App *>& +AppSystem::get_apps() const +{ + return apps; +} + +const std::vector<Manufacturer *>& +AppSystem::get_manfs() const +{ + return manfs; +} + bool AppSystem::valid_path(const std::string& strpath) { @@ -192,17 +204,18 @@ AppSystem::valid_path(const std::string& strpath) const std::string AppSystem::err_csv(const std::string& strpath) { - return "Error. File must be of format \'.csv\'. (" + strpath + ")."; + return "Error. File must be of format \'.csv\'. (" + + strpath + ")."; } const std::string -AppSystem::err_read(const std::string& strpath) +AppSystem::err_read(const char *fpath) { - return "Error reading file \'" + strpath + "\'."; + return "Error reading file \'" + std::string(fpath) + "\'."; } const std::string -AppSystem::err_write(const std::string& strpath) +AppSystem::err_write(const char *fpath) { - return "Error writing to file \'" + strpath + "\'."; + return "Error writing to file \'" + std::string(fpath) + "\'."; } diff --git a/assignment-2.4-inheritance/src/appsystem.h b/assignment-2.4-inheritance/src/appsystem.h @@ -37,10 +37,10 @@ class AppSystem void removebad (Manufacturer *man); void removebad (const char *manfname); - constexpr const std::vector<App *>& get_apps() const {return apps;} - constexpr const std::vector<Manufacturer *>& get_manfs() const {return manfs;} const std::vector<Office *> get_freeapps() const; const std::vector<Game *> get_goodgames() const; + const std::vector<App *>& get_apps() const; + const std::vector<Manufacturer *>& get_manfs() const; private: template<typename T> bool parse(std::ifstream& f); @@ -49,9 +49,9 @@ class AppSystem parse_reviews(const std::string& appname, const char *rpath); void write_office_exts(Office *of, std::ofstream& f); bool valid_path(const std::string& strpath); - const std::string err_csv(const std::string& strpath); - const std::string err_read(const std::string& strpath); - const std::string err_write(const std::string& strpath); + const std::string err_csv(const std::string& fpath); + const std::string err_read(const char *fpath); + const std::string err_write(const char *fpath); template<typename T> void dealloc(std::vector<T *>& vec); }; @@ -182,7 +182,7 @@ AppSystem::import_data(const char *fpath) } catch (const std::ifstream::failure& e) { - std::cerr << err_read(std::string(fpath)) << std::endl << e.what() << std::endl; + std::cerr << err_read(fpath) << std::endl << e.what() << std::endl; return false; } return true; @@ -252,7 +252,7 @@ AppSystem::export_data(const char *fpath) } catch (const std::ofstream::failure& e) { - std::cerr << err_write(std::string(fpath)) << std::endl << e.what() << std::endl; + std::cerr << err_read(fpath) << std::endl << e.what() << std::endl; return false; } return true; diff --git a/assignment-2.4-inheritance/src/game.cpp b/assignment-2.4-inheritance/src/game.cpp @@ -22,6 +22,12 @@ Game::get_genre() const return genre; } +bool +Game::get_online() const +{ + return online; +} + void Game::set_genre(const std::string& genre) { diff --git a/assignment-2.4-inheritance/src/game.h b/assignment-2.4-inheritance/src/game.h @@ -17,7 +17,7 @@ class Game: public App Game(const Game& g); const std::string get_genre() const; - constexpr bool get_online() const {return online;} + bool get_online() const; void set_genre(const std::string& genre); void set_online(bool online); diff --git a/assignment-2.4-inheritance/src/main.cpp b/assignment-2.4-inheritance/src/main.cpp @@ -63,7 +63,7 @@ operator<< (std::ostream& stream, const AppSystem& sys) return stream; } -void +static void cont(const char *s) { std::cout << std::endl; @@ -72,7 +72,7 @@ cont(const char *s) if (strlen(s) > 0) std::cout << s << std::endl << std::endl; } -void +static void pluseqs(AppSystem& sys) { Manufacturer *comp = new Manufacturer("0004", "Company", "comp@comp.com"); @@ -84,7 +84,7 @@ pluseqs(AppSystem& sys) sys += new Game("0005", "minecurses", "NiceOS 0.5", chris, 0, "Puzzle", false); } -void +static void edit(AppSystem& sys) { sys.newrev("minecurses", new Review(5, "gamer", "Good game")); @@ -102,13 +102,13 @@ edit(AppSystem& sys) sys.chexts("zathura", {".exe", ".bin", ".dat"}); } -void +static void remove(AppSystem& sys) { sys.removebad("GNU"); } -void +static void getapps(const AppSystem& sys) { const std::vector<Office *>& fapps = sys.get_freeapps(); diff --git a/assignment-2.4-inheritance/src/manufacturer.cpp b/assignment-2.4-inheritance/src/manufacturer.cpp @@ -36,6 +36,18 @@ Manufacturer::convstr(const char *str) return tmp; } +const char * +Manufacturer::get_serialnum() const +{ + return serialnum; +} + +const char * +Manufacturer::get_name() const +{ + return name; +} + const std::string& Manufacturer::get_email() const { diff --git a/assignment-2.4-inheritance/src/manufacturer.h b/assignment-2.4-inheritance/src/manufacturer.h @@ -19,9 +19,9 @@ class Manufacturer Manufacturer(const Manufacturer& m); ~Manufacturer(); + const char *get_serialnum() const; + const char *get_name() const; const std::string& get_email() const; - constexpr const char *get_serialnum() const {return serialnum;} - constexpr const char *get_name() const {return name;} void set_serialnum(const char *serialnum); void set_name(const char *name); diff --git a/assignment-2.4-inheritance/src/review.cpp b/assignment-2.4-inheritance/src/review.cpp @@ -13,6 +13,12 @@ Review::Review(int stars, const std::string& username, const std::string& commen Review::Review(const Review& r) :stars(r.stars), username(r.username), comment(r.comment) {} +int +Review::get_stars() const +{ + return stars; +} + const std::string& Review::get_username() const { diff --git a/assignment-2.4-inheritance/src/review.h b/assignment-2.4-inheritance/src/review.h @@ -16,9 +16,9 @@ class Review Review(int stars, const std::string& username, const std::string& comment); Review(const Review& r); + int get_stars() const; const std::string& get_username() const; const std::string& get_comment() const; - constexpr int get_stars() const {return stars;} void set_stars(int stars); void set_username(const std::string& username); diff --git a/assignment-2.5-spreadsheets/bin/spreadsheets b/assignment-2.5-spreadsheets/bin/spreadsheets Binary files differ. diff --git a/assignment-2.5-spreadsheets/obj/course.o b/assignment-2.5-spreadsheets/obj/course.o Binary files differ. diff --git a/assignment-2.5-spreadsheets/obj/datahandler.o b/assignment-2.5-spreadsheets/obj/datahandler.o Binary files differ. diff --git a/assignment-2.5-spreadsheets/obj/main.o b/assignment-2.5-spreadsheets/obj/main.o Binary files differ. diff --git a/assignment-2.5-spreadsheets/obj/student.o b/assignment-2.5-spreadsheets/obj/student.o Binary files differ. diff --git a/assignment-2.5-spreadsheets/obj/xstring.o b/assignment-2.5-spreadsheets/obj/xstring.o Binary files differ. diff --git a/assignment-2.5-spreadsheets/src/course.cpp b/assignment-2.5-spreadsheets/src/course.cpp @@ -2,3 +2,15 @@ Course::Course(const lab::xstring& code, const lab::xstring& name) :code(code), name(name) {} + +const lab::xstring& +Course::get_code() const +{ + return code; +} + +const lab::xstring& +Course::get_name() const +{ + return name; +} diff --git a/assignment-2.5-spreadsheets/src/course.h b/assignment-2.5-spreadsheets/src/course.h @@ -12,8 +12,8 @@ class Course public: Course(const lab::xstring& code, const lab::xstring& name); - constexpr const lab::xstring& get_code() const {return code;} - constexpr const lab::xstring& get_name() const {return name;} + const lab::xstring& get_code() const; + const lab::xstring& get_name() const; }; #endif /* COURSE_H */ diff --git a/assignment-2.5-spreadsheets/src/student.cpp b/assignment-2.5-spreadsheets/src/student.cpp @@ -4,3 +4,21 @@ Student::Student(const lab::xstring& AM, const lab::xstring& lname, const lab::xstring& fname) :AM(AM), lname(lname), fname(fname) {} + +const lab::xstring& +Student::get_AM() const +{ + return AM; +} + +const lab::xstring& +Student::get_lname() const +{ + return lname; +} + +const lab::xstring& +Student::get_fname() const +{ + return fname; +} diff --git a/assignment-2.5-spreadsheets/src/student.h b/assignment-2.5-spreadsheets/src/student.h @@ -15,9 +15,9 @@ class Student const lab::xstring& lname, const lab::xstring& fname); - constexpr const lab::xstring& get_AM() const {return AM;} - constexpr const lab::xstring& get_lname() const {return lname;} - constexpr const lab::xstring& get_fname() const {return fname;} + const lab::xstring& get_AM() const; + const lab::xstring& get_lname() const; + const lab::xstring& get_fname() const; }; #endif /* STUDENT_H */ diff --git a/assignment-2.5-spreadsheets/src/xstring.cpp b/assignment-2.5-spreadsheets/src/xstring.cpp @@ -88,6 +88,78 @@ xstring::operator+= (char c) return *this; } +bool +xstring::operator== (const xstring& s) const +{ + return strcmp(this->str, s.str) == 0; +} + +bool +xstring::operator== (const char *s) const +{ + return strcmp(this->str, s) == 0; +} + +bool +xstring::operator!= (const xstring& s) const +{ + return strcmp(this->str, s.str) != 0; +} + +bool +xstring::operator!= (const char *s) const +{ + return strcmp(this->str, s) != 0; +} + +bool +xstring::operator< (const xstring& s) const +{ + return strcmp(str, s.str) < 0; +} + +bool +xstring::operator< (const char *s) const +{ + return strcmp(str, s) < 0; +} + +bool +xstring::operator<= (const xstring& s) const +{ + return strcmp(str, s.str) <= 0; +} + +bool +xstring::operator<= (const char *s) const +{ + return strcmp(str, s) <= 0; +} + +bool +xstring::operator> (const xstring& s) const +{ + return strcmp(str, s.str) > 0; +} + +bool +xstring::operator> (const char *s) const +{ + return strcmp(str, s) > 0; +} + +bool +xstring::operator>= (const xstring& s) const +{ + return strcmp(str, s.str) >= 0; +} + +bool +xstring::operator>= (const char *s) const +{ + return strcmp(str, s) >= 0; +} + char& xstring::operator[] (std::size_t i) const { @@ -222,6 +294,18 @@ xstring::find(const char *s) const return (strstr(this->cstr(), s) != nullptr); } +char * +xstring::cstr() const +{ + return str; +} + +char& +xstring::front() const +{ + return str[0]; +} + char& xstring::back() const { @@ -229,6 +313,18 @@ xstring::back() const else return str[0]; } +std::size_t +xstring::length() const +{ + return strlen(str); +} + +bool +xstring::empty() const +{ + return len == 0; +} + void xstring::clear() { @@ -263,6 +359,12 @@ xstring::conv(const char *s) const return tmp; } +bool +xstring::strempty(const char *s) const +{ + return strlen(s) == 0; +} + std::istream& getline(std::istream& stream, xstring& s, char delim) { diff --git a/assignment-2.5-spreadsheets/src/xstring.h b/assignment-2.5-spreadsheets/src/xstring.h @@ -27,31 +27,19 @@ class xstring xstring& operator+= (const xstring& s); xstring& operator+= (const char *s); xstring& operator+= (char c); - constexpr bool operator== (const xstring& s) const - {return strcmp(this->str, s.str) == 0;} - constexpr bool operator== (const char *s) const - {return strcmp(this->str, s) == 0;} - constexpr bool operator!= (const xstring& s) const - {return strcmp(this->str, s.str) != 0;} - constexpr bool operator!= (const char *s) const - {return strcmp(this->str, s) != 0;} - constexpr bool operator< (const xstring& s) const - {return strcmp(this->str, s.str) < 0;} - constexpr bool operator< (const char *s) const - {return strcmp(this->str, s) < 0;} - constexpr bool operator<= (const xstring& s) const - {return strcmp(this->str, s.str) <= 0;} - constexpr bool operator<= (const char *s) const - {return strcmp(this->str, s) <= 0;} - constexpr bool operator> (const xstring& s) const - {return strcmp(this->str, s.str) > 0;} - constexpr bool operator> (const char *s) const - {return strcmp(this->str, s) > 0;} - constexpr bool operator>= (const xstring& s) const - {return strcmp(this->str, s.str) >= 0;} - constexpr bool operator>= (const char *s) const - {return strcmp(this->str, s) >= 0;} - char& operator[] (std::size_t i) const; + bool operator== (const xstring& s) const; + bool operator== (const char *s) const; + bool operator!= (const xstring& s) const; + bool operator!= (const char *s) const; + bool operator< (const xstring& s) const; + bool operator< (const char *s) const; + bool operator<= (const xstring& s) const; + bool operator<= (const char *s) const; + bool operator> (const xstring& s) const; + bool operator> (const char *s) const; + bool operator>= (const xstring& s) const; + bool operator>= (const char *s) const; + char& operator[] (std::size_t i) const; friend std::ostream& operator<< (std::ostream& stream, const xstring& s); friend std::istream& operator>> (std::istream& stream, const xstring& s); @@ -66,23 +54,23 @@ class xstring void replace(std::size_t i, char c); bool find(const xstring& s) const; bool find(const char *s) const; - constexpr char *cstr() const {return str;} - constexpr char& front() const {return str[0];} + char *cstr() const; + char& front() const; char& back() const; - constexpr std::size_t length() const {return strlen(str);} - constexpr bool empty() const {return len == 0;} + std::size_t length() const; + bool empty() const; void clear(); private: void resize(std::size_t n); char *conv(const char *s) const; - constexpr bool strempty(const char *s) const {return strlen(s) == 0;} + bool strempty(const char *s) const; }; std::istream& getline(std::istream& stream, xstring& s, char delim = '\n'); template<typename T> xstring to_xstr(T val); template<typename T> xstring to_xstr(const char *fs, T val); -template<typename T> constexpr const char *getformat(); +template<typename T> const char *getformat(); template<typename T> xstring to_xstr(T val) @@ -100,7 +88,7 @@ to_xstr(const char *fs, T val) return xstring(buf); } -template<typename T> constexpr const char * +template<typename T> const char * getformat() { if (std::is_same<T, short>::value) return "%hi";