commit 55f257463e36928fb96b10be1c086b5498edf15f parent c9893fb058c96765263420a9b8e133aed477506d Author: Christos Margiolis <christos@margiolis.net> Date: Tue, 19 May 2020 16:42:38 +0300 added some constexprs Diffstat:
61 files changed, 286 insertions(+), 532 deletions(-)
diff --git a/assignment-1.5-arrays-pointers-files/minecurses/README.md b/assignment-1.5-arrays-pointers-files/minecurses/README.md @@ -31,6 +31,7 @@ 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 @@ -Mine hit at position (5, 5) +Last mine defused at position (1, 3) 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 *threadid) +play_audio(void *tid) { /*int tid = (long)threadid;*/ Mix_Music *music = NULL; @@ -13,6 +13,7 @@ play_audio(void *threadid) 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 *threadid); +void *play_audio(void *tid); 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,135 +1,153 @@ #include "gameplay.h" #include "main.h" +#define X b->x +#define Y b->y + void -play(WINDOW *gw, Board *b) +play(Board *b) { - int mbx = 0, mby = 0; - int gameover = FALSE; - int ndefused = 0; + b->x = b->y = 0; + b->gameover = FALSE; + b->ndefused = 0; int move; do { erase(); - delwin(gw); + delwin(b->gw); refresh(); - gw = game_win(b->rows, b->cols); - print_board(gw, b); - session_info(mbx, mby, ndefused, b->nmines); - navigate(gw, &move, &mbx, &mby); + b->gw = game_win(b->rows, b->cols); + print_board(b); + session_info(b); + navigate(b, &move); switch (move) { - case ENTER: case OPEN_LOWER: case OPEN_UPPER: - gameover = open_cell(gw, b, mby, mbx, gameover); + case ENTER: /* FALLTHROUGH */ + case OPEN_LOWER: + case OPEN_UPPER: + b->gameover = open_cell(b); break; - case FLAG_LOWER: case FLAG_UPPER: - handle_flags(gw, b, mby, mbx); + case FLAG_LOWER: /* FALLTHROUGH */ + case FLAG_UPPER: + handle_flags(b); break; - case DEFUSE_LOWER: case DEFUSE_UPPER: - if (b->db[mby][mbx] == FLAG && b->mb[mby][mbx] == MINE) + case DEFUSE_LOWER: /* FALLTHROUGH */ + case DEFUSE_UPPER: + if (b->db[Y][X] == FLAG && b->mb[Y][X] == MINE) { - ndefused++; - defuse_mine(gw, b, mby, mbx); + b->ndefused++; + defuse_mine(b); } - else if (b->db[mby][mbx] == FLAG && b->mb[mby][mbx] != MINE) gameover = TRUE; + else if (b->db[Y][X] == FLAG && b->mb[Y][X] != MINE) + b->gameover = TRUE; break; case PAUSE_AUDIO: pause_audio(); break; - case VOLUME_UP: case VOLUME_DOWN: + case VOLUME_UP: /* FALLTHROUGH */ + case VOLUME_DOWN: volume(move); break; case 'm': - handle_menu(gw, b); + handle_menu(b); break; case 'r': clear_board(b); reset(b); - init_game(gw, b); + init_game(b); break; default: break; } - } while (((mby >= 0 && mby < b->rows) && (mbx >= 0 && mbx < b->cols)) && - ndefused < b->nmines && !gameover && move != QUIT); + } while (((Y >= 0 && Y < b->rows) && + (X >= 0 && X < b->cols)) && + b->ndefused < b->nmines && !b->gameover && + move != QUIT); - if (gameover) - handle_gameover(gw, b, mby, mbx); - if (ndefused == b->nmines) - handle_win(gw, b, mby, mbx, ndefused); + if (b->gameover) + handle_gameover(b); + if (b->ndefused == b->nmines) + handle_win(b); } int -open_cell(WINDOW *gw, Board *b, int mby, int mbx, int gameover) +open_cell(Board *b) { - transfer(b, mby, mbx); - reveal(gw, b, mby, mbx, mby+1, 3*mbx+2); - if (b->db[mby][mbx] == MINE) gameover = TRUE; - return gameover; + transfer(b); + reveal(b); + return (b->db[Y][X] == MINE) ? TRUE : FALSE; } void -handle_flags(WINDOW *gw, Board *b, int mby, int mbx) +handle_flags(Board *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); + 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); } void -defuse_mine(WINDOW *gw, Board *b, int mby, int mbx) +defuse_mine(Board *b) { refresh(); - b->db[mby][mbx] = b->mb[mby][mbx] = DEFUSED; - reveal(gw, b, mby, mbx, mby+1, 3*mbx+2); + b->db[Y][X] = b->mb[Y][X] = DEFUSED; + reveal(b); } void -transfer(Board *b, int mby, int mbx) +transfer(Board *b) { - b->db[mby][mbx] = b->mb[mby][mbx]; + b->db[Y][X] = b->mb[Y][X]; } void -reveal(WINDOW *gw, Board *b, int mby, int mbx, int y, int x) +reveal(const Board *b) { - mvwaddch(gw, y, x, b->db[mby][mbx]); - wrefresh(gw); + int y = b->y + 1; + int x = 3 * b->x + 2; + mvwaddch(b->gw, y, x, b->db[Y][X]); + wrefresh(b->gw); } int -is_defused(Board *b, int mby, int mbx) +is_defused(const Board *b) { - return (b->db[mby][mbx] == DEFUSED) ? TRUE : FALSE; + return (b->db[Y][X] == DEFUSED) ? TRUE : FALSE; } void -handle_menu(WINDOW *gw, Board *b) +handle_menu(const Board *b) { options_menu(); - box(gw, 0, 0); - print_board(gw, b); + box(b->gw, 0, 0); + print_board(b); } void -handle_gameover(WINDOW *gw, Board *b, int mby, int mbx) +handle_gameover(const Board *b) { - game_over(gw); + game_over(b); getchar(); erase(); refresh(); - box(gw, 0, 0); - print_board(gw, b); - wrefresh(gw); - session_write(b, mbx, mby, GAME_LOST); + box(b->gw, 0, 0); + print_board(b); + wrefresh(b->gw); + session_write(b, GAME_LOST); } void -handle_win(WINDOW *gw, Board *b, int mby, int mbx, int ndefused) +handle_win(const Board *b) { - game_won(gw); + game_won(b); getchar(); - session_write(b, mbx, mby, GAME_WON); - score_write(ndefused, b->rows, b->cols); + session_write(b, GAME_WON); + score_write(b); } + +#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 (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); +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); #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,32 +9,37 @@ 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); - WINDOW *gw = game_win(b.rows, b.cols); - init_game(gw, &b); + b.gw = game_win(b.rows, b.cols); + init_game(&b); pthread_t audiothread; - long threadid = 1; - pthread_create(&audiothread, NULL, play_audio, (void *)threadid); - play(gw, &b); + long tid = 1; + pthread_create(&audiothread, NULL, play_audio, (void *)tid); + play(&b); pthread_cancel(audiothread); clear_board(&b); - delwin(gw); + delwin(b.gw); endwin(); - - return 0; + return EXIT_SUCCESS; } void reset(Board *b) { echo(); - b->cols = set_cols(); - b->rows = set_rows(); - b->nmines = set_nmines(b->rows * b->cols); + 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; noecho(); options_menu(); erase(); @@ -42,10 +47,10 @@ reset(Board *b) } void -init_game(WINDOW *gw, Board *b) +init_game(Board *b) { - init_db(gw, b); - init_mb(gw, b); + init_db(b); + init_mb(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,13 +12,17 @@ typedef struct { char **db; char **mb; - int rows, cols, nmines; + int rows, cols; + int nmines, ndefused; + int gameover; + int x, y; + WINDOW *gw; } Board; extern Board b; -void reset(Board *b); -void init_game(WINDOW *gw, Board *b); +void reset (Board *b); +void init_game (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,19 +1,14 @@ #include "minesweeper.h" void -init_db(WINDOW *gw, Board *b) +init_db(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->db == NULL) - { - mvprintw(0, 0, "Error, not enough memory, exiting..."); - refresh(); - exit(EXIT_FAILURE); - } + if (b->mb == NULL) die(); else fill_db(b); } @@ -27,19 +22,14 @@ fill_db(Board *b) } void -init_mb(WINDOW *gw, Board *b) +init_mb(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) - { - mvprintw(0, 0, "Error, not enough memory, exiting..."); - refresh(); - exit(EXIT_FAILURE); - } + if (b->mb == NULL) die(); else { place_mines(b); @@ -72,30 +62,30 @@ add_adj(Board *b) } int -is_mine(Board *b, int row, int col) +is_mine(const Board *b, int r, int c) { - return (b->mb[row][col] == MINE) ? TRUE : FALSE; + return (b->mb[r][c] == MINE) ? TRUE : FALSE; } int -outof_bounds(Board *b, int row, int col) +outof_bounds(const Board *b, int r, int c) { - return (row < 0 || row > b->rows-1 || col < 0 || col > b->cols-1) ? TRUE : FALSE; + return (r < 0 || r > b->rows-1 || c < 0 || c > b->cols-1) ? TRUE : FALSE; } uint8_t -adj_mines(Board *b, int row, int col) +adj_mines(const Board *b, int r, int c) { uint8_t nadj = 0; - 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 + 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 return nadj; } @@ -109,3 +99,11 @@ 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 (struct _win_st* gw, Board *b); +void init_db (Board *b); void fill_db (Board *b); -void init_mb (struct _win_st* gw, Board *b); +void init_mb (Board *b); void place_mines (Board *b); void add_adj (Board *b); -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); +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); 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(WINDOW *gw, int *mv, int *mbx, int *mby) +navigate(Board *b, int *mv) { static int y = 1, x = 2; - update_curs(gw, y, x); - *mbx = (x-2)/3; - *mby = y-1; + update_curs(b, y, x); + b->x = (x-2)/3; + b->y = y-1; refresh(); - getmv(gw, mv, &y, &x); + getmv(b, mv, &y, &x); } void -getmv(WINDOW *gw, int *mv, int *y, int *x) +getmv(const Board *b, int *mv, int *y, int *x) { - *mv = wgetch(gw); - switch (*mv) // vim keys support!! + *mv = wgetch(b->gw); + switch (*mv) { - case 'k': case 'K': + case 'k': case 'K': /* FALLTHROUGH */ case 'w': case 'W': mvup(y); break; - case 'j': case 'J': + case 'j': case 'J': /* FALLTHROUGH */ case 's': case 'S': - mvdown(y, YMAX(gw)); + mvdown(y, YMAX(b->gw)); break; - case 'h': case 'H': + case 'h': case 'H': /* FALLTHROUGH */ case 'a': case 'A': mvleft(x); break; - case 'l': case 'L': + case 'l': case 'L': /* FALLTHROUGH */ case 'd': case 'D': - mvright(x, XMAX(gw)); + mvright(x, XMAX(b->gw)); break; } } @@ -65,7 +65,7 @@ mvright(int *x, int xmax) } void -update_curs(WINDOW *gw, int y, int x) +update_curs(const Board *b, int y, int x) { - wmove(gw, y, x); + wmove(b->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 (struct _win_st* gw, int *mv, int *mbx, int *mby); -void getmv (struct _win_st* gw, int *mv, int *y, int *x); +void navigate (Board *b, int *mv); +void getmv (const Board *b, 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(struct _win_st* gw, int y, int x); +void update_curs(const Board *b, 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(WINDOW *gw, Board *b) +print_board(const Board *b) { int i, j, x, y = 1; - print_grid(gw, b->rows, b->cols); - wattron(gw, A_BOLD); + print_grid(b); + wattron(b->gw, A_BOLD); for (i = 0; i < b->rows; i++) { x = 2; for (j = 0; j < b->cols; j++) { - mvwaddch(gw, y, x, b->db[i][j]); + mvwaddch(b->gw, y, x, b->db[i][j]); x += 3; } y++; @@ -19,32 +19,33 @@ print_board(WINDOW *gw, Board *b) } void -print_grid(WINDOW *gw, int rows, int cols) +print_grid(const Board *b) { int i, j; - wattroff(gw, A_BOLD); - for (i = 1; i <= rows; i++) + wattroff(b->gw, A_BOLD); + for (i = 1; i <= b->rows; i++) { - wmove(gw, i, 1); - for (j = 0; j < cols; j++) - wprintw(gw, "[ ]"); + wmove(b->gw, i, 1); + for (j = 0; j < b->cols; j++) + wprintw(b->gw, "[ ]"); } - wrefresh(gw); + wrefresh(b->gw); } #define YMID(x) getmaxy(x)/2 #define XMID(x) getmaxx(x)/2 void -session_info(int mbx, int mby, int ndefused, int nmines) +session_info(const Board *b) { - 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, 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, XMAX(stdscr)-strlen("m Controls"), "m Controls"); } void -session_write(Board *b, int hitrow, int hitcol, State state) +session_write(const Board *b, State state) { int i, j; FILE *fsession = fopen(SESSION_PATH, "w"); @@ -57,8 +58,10 @@ session_write(Board *b, int hitrow, int hitcol, State state) else { state == GAME_WON - ? 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, "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, "Board overview\n\n"); for (i = 0; i < b->rows; i++) { @@ -71,7 +74,7 @@ session_write(Board *b, int hitrow, int hitcol, State state) } void -score_write(int ndefused, int cols, int rows) +score_write(const Board *b) { FILE *scorelog = fopen(SCORE_LOG_PATH, "a"); char *playername = get_pname(); @@ -83,7 +86,8 @@ score_write(int ndefused, int cols, int rows) } else { - fprintf(scorelog, "%s,%d,%dx%d\n", playername, ndefused, cols, rows); + fprintf(scorelog, "%s,%d,%dx%d\n", + playername, b->ndefused, b->cols, b->rows); sort_scorelog(scorelog); clrtoeol(); show_scorelog(scorelog); @@ -131,10 +135,10 @@ parse_data(FILE *scorelog) } void -game_won(WINDOW *gw) +game_won(const Board *b) { - wclear(gw); - wrefresh(gw); + wclear(b->gw); + wrefresh(b->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 :)"); @@ -144,10 +148,10 @@ game_won(WINDOW *gw) } void -game_over(WINDOW *gw) +game_over(const Board *b) { - wclear(gw); - wrefresh(gw); + wclear(b->gw); + wrefresh(b->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 (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); +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); char *get_pname (void); void sort_scorelog(FILE *scorelog); void show_scorelog(FILE *scorelog); void parse_data (FILE *scorelog); -void game_won (struct _win_st* gw); -void game_over (struct _win_st* gw); +void game_won (const Board *b); +void game_over (const Board *b); #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; - unsigned int get_semester() const; - unsigned int get_pcourses() const; - float *get_grades() 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;} void set_AM(const char *AM); void set_name(const std::string& name); @@ -77,36 +77,12 @@ 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,12 +21,6 @@ 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; - unsigned int get_csemester() const; + constexpr unsigned int get_csemester() const {return csemester;} 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,84 +93,12 @@ 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; - 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 {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;} - const char *get_AM() const; const std::string& get_name() const; - 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; + 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;} 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,12 +64,6 @@ 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 { @@ -88,18 +82,6 @@ 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; - const std::vector<Review *>& get_revs() const; - int get_price() 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;} 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,18 +183,6 @@ 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) { @@ -204,18 +192,17 @@ 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 char *fpath) +AppSystem::err_read(const std::string& strpath) { - return "Error reading file \'" + std::string(fpath) + "\'."; + return "Error reading file \'" + strpath + "\'."; } const std::string -AppSystem::err_write(const char *fpath) +AppSystem::err_write(const std::string& strpath) { - return "Error writing to file \'" + std::string(fpath) + "\'."; + return "Error writing to file \'" + strpath + "\'."; } 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& fpath); - const std::string err_read(const char *fpath); - const std::string err_write(const char *fpath); + 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); 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(fpath) << std::endl << e.what() << std::endl; + std::cerr << err_read(std::string(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_read(fpath) << std::endl << e.what() << std::endl; + std::cerr << err_write(std::string(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,12 +22,6 @@ 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; - bool get_online() const; + constexpr bool get_online() const {return online;} 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; } -static void +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; } -static void +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); } -static void +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"}); } -static void +void remove(AppSystem& sys) { sys.removebad("GNU"); } -static void +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,18 +36,6 @@ 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,12 +13,6 @@ 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,15 +2,3 @@ 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); - const lab::xstring& get_code() const; - const lab::xstring& get_name() const; + constexpr const lab::xstring& get_code() const {return code;} + constexpr const lab::xstring& get_name() const {return name;} }; #endif /* COURSE_H */ diff --git a/assignment-2.5-spreadsheets/src/student.cpp b/assignment-2.5-spreadsheets/src/student.cpp @@ -4,21 +4,3 @@ 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); - const lab::xstring& get_AM() const; - const lab::xstring& get_lname() const; - const lab::xstring& get_fname() const; + 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;} }; #endif /* STUDENT_H */ diff --git a/assignment-2.5-spreadsheets/src/xstring.cpp b/assignment-2.5-spreadsheets/src/xstring.cpp @@ -88,78 +88,6 @@ 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 { @@ -294,18 +222,6 @@ 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 { @@ -313,18 +229,6 @@ 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() { @@ -359,12 +263,6 @@ 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,19 +27,31 @@ class xstring xstring& operator+= (const xstring& s); xstring& operator+= (const char *s); xstring& operator+= (char c); - 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; + 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; friend std::ostream& operator<< (std::ostream& stream, const xstring& s); friend std::istream& operator>> (std::istream& stream, const xstring& s); @@ -54,23 +66,23 @@ class xstring void replace(std::size_t i, char c); bool find(const xstring& s) const; bool find(const char *s) const; - char *cstr() const; - char& front() const; + constexpr char *cstr() const {return str;} + constexpr char& front() const {return str[0];} char& back() const; - std::size_t length() const; - bool empty() const; + constexpr std::size_t length() const {return strlen(str);} + constexpr bool empty() const {return len == 0;} void clear(); private: void resize(std::size_t n); char *conv(const char *s) const; - bool strempty(const char *s) const; + constexpr bool strempty(const char *s) const {return strlen(s) == 0;} }; 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> const char *getformat(); +template<typename T> constexpr const char *getformat(); template<typename T> xstring to_xstr(T val) @@ -88,7 +100,7 @@ to_xstr(const char *fs, T val) return xstring(buf); } -template<typename T> const char * +template<typename T> constexpr const char * getformat() { if (std::is_same<T, short>::value) return "%hi";