commit c9893fb058c96765263420a9b8e133aed477506d parent ac1b34ed49e991c3aa2d8537b9e1d6dd30796de0 Author: Christos Margiolis <christos@margiolis.net> Date: Mon, 18 May 2020 01:55:46 +0300 updated submodule Diffstat:
26 files changed, 313 insertions(+), 257 deletions(-)
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 (4, 2) +Mine hit at position (5, 5) Board overview -- - 1 1 1 -- - 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/obj/settings.o b/assignment-1.5-arrays-pointers-files/minecurses/obj/settings.o Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/minecurses/obj/wins.o b/assignment-1.5-arrays-pointers-files/minecurses/obj/wins.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,6 +1,7 @@ #include "audio.h" -void *play_audio(void *threadid) +void * +play_audio(void *threadid) { /*int tid = (long)threadid;*/ Mix_Music *music = NULL; @@ -14,7 +15,8 @@ void *play_audio(void *threadid) Mix_CloseAudio(); } -void volume(char option) +void +volume(char option) { static int volume = MIX_MAX_VOLUME; @@ -31,7 +33,8 @@ void volume(char option) } } -void pause_audio(void) +void +pause_audio(void) { (Mix_PausedMusic() == 1) ? Mix_ResumeMusic() : Mix_PauseMusic(); } 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,40 +1,38 @@ #include "gameplay.h" #include "main.h" -void play(WINDOW *gamew, Board *brd) +void +play(WINDOW *gw, Board *b) { int mbx = 0, mby = 0; int gameover = FALSE; int ndefused = 0; - int ymid, xmid; int move; - ymid = YMAX/2; - xmid = XMAX/2; do { erase(); - delwin(gamew); + delwin(gw); refresh(); - gamew = game_win(brd->rows, brd->cols); - print_board(gamew, brd); - session_info(mbx, mby, xmid, ndefused, brd->nmines); - navigate(gamew, &move, &mbx, &mby); + 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: case OPEN_LOWER: case OPEN_UPPER: - gameover = open_cell(gamew, brd, mby, mbx, gameover); + gameover = open_cell(gw, b, mby, mbx, gameover); break; case FLAG_LOWER: case FLAG_UPPER: - handle_flags(gamew, brd, mby, mbx); + handle_flags(gw, b, mby, mbx); break; case DEFUSE_LOWER: case DEFUSE_UPPER: - if (brd->db[mby][mbx] == FLAG && brd->mb[mby][mbx] == MINE) + if (b->db[mby][mbx] == FLAG && b->mb[mby][mbx] == MINE) { ndefused++; - defuse_mine(gamew, brd, mby, mbx); + defuse_mine(gw, b, mby, mbx); } - else if (brd->db[mby][mbx] == FLAG && brd->mb[mby][mbx] != MINE) gameover = TRUE; + else if (b->db[mby][mbx] == FLAG && b->mb[mby][mbx] != MINE) gameover = TRUE; break; case PAUSE_AUDIO: pause_audio(); @@ -43,85 +41,95 @@ void play(WINDOW *gamew, Board *brd) volume(move); break; case 'm': - handle_menu(gamew, brd); + handle_menu(gw, b); break; case 'r': - reset(brd); - init_game(gamew, brd); + clear_board(b); + reset(b); + init_game(gw, b); break; default: break; } - } while (((mby >= 0 && mby < brd->rows) && (mbx >= 0 && mbx < brd->cols)) && - ndefused < brd->nmines && !gameover && move != QUIT); + } while (((mby >= 0 && mby < b->rows) && (mbx >= 0 && mbx < b->cols)) && + ndefused < b->nmines && !gameover && move != QUIT); if (gameover) - handle_gameover(gamew, brd, ymid, xmid, mby, mbx); - if (ndefused == brd->nmines) - handle_win(gamew, brd, ymid, xmid, mby, mbx, ndefused); + handle_gameover(gw, b, mby, mbx); + if (ndefused == b->nmines) + handle_win(gw, b, mby, mbx, ndefused); } -int open_cell(WINDOW *gamew, Board *brd, int mby, int mbx, int gameover) +int +open_cell(WINDOW *gw, Board *b, int mby, int mbx, int gameover) { - transfer(brd, mby, mbx); - reveal(gamew, brd, mby, mbx, mby+1, 3*mbx+2); - if (brd->db[mby][mbx] == MINE) gameover = TRUE; + 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(WINDOW *gamew, Board *brd, int mby, int mbx) +void +handle_flags(WINDOW *gw, Board *b, int mby, int mbx) { - if (brd->db[mby][mbx] == FLAG) brd->db[mby][mbx] = BLANK; - else if (brd->db[mby][mbx] != FLAG && brd->db[mby][mbx] != BLANK) return; - else brd->db[mby][mbx] = FLAG; - reveal(gamew, brd, mby, mbx, mby+1, 3*mbx+2); + 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(WINDOW *gamew, Board *brd, int mby, int mbx) +void +defuse_mine(WINDOW *gw, Board *b, int mby, int mbx) { refresh(); - brd->db[mby][mbx] = brd->mb[mby][mbx] = DEFUSED; - reveal(gamew, brd, mby, mbx, mby+1, 3*mbx+2); + b->db[mby][mbx] = b->mb[mby][mbx] = DEFUSED; + reveal(gw, b, mby, mbx, mby+1, 3*mbx+2); } -void transfer(Board *brd, int mby, int mbx) +void +transfer(Board *b, int mby, int mbx) { - brd->db[mby][mbx] = brd->mb[mby][mbx]; + b->db[mby][mbx] = b->mb[mby][mbx]; } -void reveal(WINDOW *gamew, Board *brd, int mby, int mbx, int y, int x) +void +reveal(WINDOW *gw, Board *b, int mby, int mbx, int y, int x) { - mvwaddch(gamew, y, x, brd->db[mby][mbx]); - wrefresh(gamew); + mvwaddch(gw, y, x, b->db[mby][mbx]); + wrefresh(gw); } -int is_defused(Board *brd, int mby, int mbx) +int +is_defused(Board *b, int mby, int mbx) { - return (brd->db[mby][mbx] == DEFUSED) ? TRUE : FALSE; + return (b->db[mby][mbx] == DEFUSED) ? TRUE : FALSE; } -void handle_menu(WINDOW *gamew, Board *brd) +void +handle_menu(WINDOW *gw, Board *b) { options_menu(); - box(gamew, 0, 0); - print_board(gamew, brd); + box(gw, 0, 0); + print_board(gw, b); } -void handle_gameover(WINDOW *gamew, Board *brd, int ymid, int xmid, int mby, int mbx) +void +handle_gameover(WINDOW *gw, Board *b, int mby, int mbx) { - game_over(gamew, ymid, xmid); + game_over(gw); getchar(); erase(); refresh(); - box(gamew, 0, 0); - print_board(gamew, brd); - wrefresh(gamew); - session_write(brd, mbx, mby, GAME_LOST); + box(gw, 0, 0); + print_board(gw, b); + wrefresh(gw); + session_write(b, mbx, mby, GAME_LOST); } -void handle_win(WINDOW *gamew, Board *brd, int ymid, int xmid, int mby, int mbx, int ndefused) +void +handle_win(WINDOW *gw, Board *b, int mby, int mbx, int ndefused) { - game_won(gamew, ymid, xmid); + game_won(gw); getchar(); - session_write(brd, mbx, mby, GAME_WON); - score_write(ndefused, brd->rows, brd->cols); + session_write(b, mbx, mby, GAME_WON); + score_write(ndefused, b->rows, b->cols); } 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* gamew, Board *brd); -int open_cell (struct _win_st* gamew, Board *brd, int mby, int mbx, int gameover); -void handle_flags (struct _win_st* gamew, Board *brd, int mby, int mbx); -void defuse_mine (struct _win_st* gamew, Board *brd, int mby, int mbx); -void transfer (Board *brd, int mby, int mbx); -void reveal (struct _win_st* gamew, Board *brd, int mby, int mbx, int y, int x); -int is_defused (Board *brd, int mby, int mbx); -void handle_menu (struct _win_st*, Board *brd); -void handle_gameover (struct _win_st* gamew, Board *brd, int ymid, int xmid, int mby, int mbx); -void handle_win (struct _win_st* gamew, Board *brd, int ymid, int xmid, int mby, int mbx, int ndefused); +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 @@ -6,52 +6,57 @@ #include "wins.h" #include <pthread.h> -int main(int argc, char **argv) +int +main(int argc, char **argv) { - Board brd; init_curses(); - reset(&brd); - WINDOW *gamew = game_win(brd.rows, brd.cols); - init_game(gamew, &brd); + Board b; + reset(&b); + WINDOW *gw = game_win(b.rows, b.cols); + init_game(gw, &b); + pthread_t audiothread; long threadid = 1; pthread_create(&audiothread, NULL, play_audio, (void *)threadid); - play(gamew, &brd); + play(gw, &b); pthread_cancel(audiothread); - clear_board(&brd); - delwin(gamew); + clear_board(&b); + delwin(gw); endwin(); return 0; } -void reset(Board *brd) +void +reset(Board *b) { echo(); - brd->cols = set_cols(); - brd->rows = set_rows(); - brd->nmines = set_nmines(brd->rows * brd->cols); + b->cols = set_cols(); + b->rows = set_rows(); + b->nmines = set_nmines(b->rows * b->cols); noecho(); options_menu(); erase(); refresh(); } -void init_game(WINDOW *gamew, Board *brd) +void +init_game(WINDOW *gw, Board *b) { - brd->db = init_db(gamew, brd->cols, brd->rows); - brd->mb = init_mb(gamew, brd->cols, brd->rows, brd->nmines); + init_db(gw, b); + init_mb(gw, b); } -void clear_board(Board *brd) +void +clear_board(Board *b) { int i; - for (i = 0; i < brd->rows; i++) + for (i = 0; i < b->rows; i++) { - free(brd->db[i]); - free(brd->mb[i]); + free(b->db[i]); + free(b->mb[i]); } - free(brd->db); - free(brd->mb); + free(b->db); + free(b->mb); } diff --git a/assignment-1.5-arrays-pointers-files/minecurses/src/main.h b/assignment-1.5-arrays-pointers-files/minecurses/src/main.h @@ -3,22 +3,22 @@ #if defined linux || defined __unix__ || defined __APPLE__ #include <ncurses.h> -#endif - +#endif /* defined linux || defined __unix__ || defined __APPLE__ */ #include <stdlib.h> -#define YMAX getmaxy(stdscr) -#define XMAX getmaxx(stdscr) +#define YMAX(x) getmaxy(x) +#define XMAX(x) getmaxx(x) -typedef struct -{ +typedef struct { char **db; char **mb; int rows, cols, nmines; } Board; -void reset(Board *brd); -void init_game(WINDOW *gamew, Board *brd); -void clear_board(Board *brd); +extern 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,39 +1,40 @@ #include "minesweeper.h" -char **init_db(WINDOW *gamew, int cols, int rows) +void +init_db(WINDOW *gw, Board *b) { int i; - char **db = (char **)malloc(rows * sizeof(char *)); - for (i = 0; i < rows; i++) - db[i] = (char *)malloc(cols); + b->db = (char **)malloc(b->rows * sizeof(char *)); + for (i = 0; i < b->rows; i++) + b->db[i] = (char *)malloc(b->cols); - if (db == NULL) + if (b->db == NULL) { mvprintw(0, 0, "Error, not enough memory, exiting..."); refresh(); exit(EXIT_FAILURE); } - else fill_db(db, cols, rows); - - return db; + else fill_db(b); } -void fill_db(char **db, int cols, int rows) +void +fill_db(Board *b) { int i, j; - for (i = 0; i < rows; i++) - for (j = 0; j < cols; j++) - db[i][j] = BLANK; + for (i = 0; i < b->rows; i++) + for (j = 0; j < b->cols; j++) + b->db[i][j] = BLANK; } -char **init_mb(WINDOW *gamew, int cols, int rows, int nmines) +void +init_mb(WINDOW *gw, Board *b) { int i; - char **mb = (char **)malloc(rows * sizeof(char *)); - for (i = 0; i < rows; i++) - mb[i] = (char *)malloc(cols); + b->mb = (char **)malloc(b->rows * sizeof(char *)); + for (i = 0; i < b->rows; i++) + b->mb[i] = (char *)malloc(b->cols); - if (mb == NULL) + if (b->mb == NULL) { mvprintw(0, 0, "Error, not enough memory, exiting..."); refresh(); @@ -41,66 +42,70 @@ char **init_mb(WINDOW *gamew, int cols, int rows, int nmines) } else { - place_mines(mb, cols, rows, nmines); - add_adj(mb, cols, rows); - fill_spaces(mb, cols, rows, nmines); + place_mines(b); + add_adj(b); + fill_spaces(b); } - - return mb; } -void place_mines(char **mb, int cols, int rows, int nmines) +void +place_mines(Board *b) { - int i, wrand, hrand; + int i, r, c; srand(time(NULL)); - for (i = 0; i < nmines; i++) + for (i = 0; i < b->nmines; i++) { - wrand = rand() % rows; - hrand = rand() % cols; - mb[wrand][hrand] = MINE; + r = rand() % b->rows; + c = rand() % b->cols; + b->mb[r][c] = MINE; } } -void add_adj(char **mb, int cols, int rows) +void +add_adj(Board *b) { int i, j; - for (i = 0; i < rows; i++) - for (j = 0; j < cols; j++) - if (!is_mine(mb, i, j)) - mb[i][j] = adj_mines(mb, i, j, cols, rows) + '0'; + for (i = 0; i < b->rows; i++) + for (j = 0; j < b->cols; j++) + if (!is_mine(b, i, j)) + b->mb[i][j] = adj_mines(b, i, j) + '0'; } -int is_mine(char **mb, int row, int col) +int +is_mine(Board *b, int row, int col) { - return (mb[row][col] == MINE) ? TRUE : FALSE; + return (b->mb[row][col] == MINE) ? TRUE : FALSE; } -int outof_bounds(int row, int col, int cols, int rows) +int +outof_bounds(Board *b, int row, int col) { - return (row < 0 || row > rows-1 || col < 0 || col > cols-1) ? TRUE : FALSE; + return (row < 0 || row > b->rows-1 || col < 0 || col > b->cols-1) ? TRUE : FALSE; } -uint8_t adj_mines(char **mb, int row, int col, int cols, int rows) +uint8_t +adj_mines(Board *b, int row, int col) { uint8_t nadj = 0; - if (!outof_bounds(row, col-1, cols, rows) && mb[row][col-1] == MINE) nadj++; // north - if (!outof_bounds(row, col+1, cols, rows) && mb[row][col+1] == MINE) nadj++; // south - if (!outof_bounds(row+1, col, cols, rows) && mb[row+1][col] == MINE) nadj++; // east - if (!outof_bounds(row-1, col, cols, rows) && mb[row-1][col] == MINE) nadj++; // west - if (!outof_bounds(row+1, col-1, cols, rows) && mb[row+1][col-1] == MINE) nadj++; // north-east - if (!outof_bounds(row-1, col-1, cols, rows) && mb[row-1][col-1] == MINE) nadj++; // north-west - if (!outof_bounds(row+1, col+1, cols, rows) && mb[row+1][col+1] == MINE) nadj++; // south-east - if (!outof_bounds(row-1, col+1, cols, rows) && mb[row-1][col+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; } -void fill_spaces(char **mb, int cols, int rows, int nmines) +void +fill_spaces(Board *b) { int i, j; - for (i = 0; i < rows; i++) - for (j = 0; j < cols; j++) - if (mb[i][j] != MINE && mb[i][j] == '0') - mb[i][j] = '-'; + for (i = 0; i < b->rows; i++) + for (j = 0; j < b->cols; j++) + if (b->mb[i][j] != MINE && b->mb[i][j] == '0') + b->mb[i][j] = '-'; } diff --git a/assignment-1.5-arrays-pointers-files/minecurses/src/minesweeper.h b/assignment-1.5-arrays-pointers-files/minecurses/src/minesweeper.h @@ -10,14 +10,14 @@ #define BLANK ' ' #define MINE '*' -char **init_db (struct _win_st* gamew, int cols, int rows); -void fill_db (char **db, int cols, int rows); -char **init_mb (struct _win_st* gamew, int cols, int rows, int nmines); -void place_mines (char **mb, int cols, int rows, int nmines); -void add_adj (char **mb, int cols, int rows); -int is_mine (char **mb, int row, int col); -int outof_bounds (int row, int col, int rows, int cols); -uint8_t adj_mines (char **mb, int row, int col, int cols, int rows); -void fill_spaces (char **mb, int cols, int rows, int nmines); +void init_db (struct _win_st* gw, Board *b); +void fill_db (Board *b); +void init_mb (struct _win_st* gw, 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); +void fill_spaces (Board *b); #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,21 +1,21 @@ #include "navigation.h" -void navigate(WINDOW *gamew, int *move, int *mbx, int *mby) +void +navigate(WINDOW *gw, int *mv, int *mbx, int *mby) { - int ymax, xmax; - getmaxyx(gamew, ymax, xmax); static int y = 1, x = 2; - update_curs(gamew, y, x); + update_curs(gw, y, x); *mbx = (x-2)/3; *mby = y-1; refresh(); - getmv(gamew, move, &y, &x, ymax, xmax); + getmv(gw, mv, &y, &x); } -void getmv(WINDOW *gamew, int *move, int *y, int *x, int ymax, int xmax) +void +getmv(WINDOW *gw, int *mv, int *y, int *x) { - *move = wgetch(gamew); - switch (*move) // vim keys support!! + *mv = wgetch(gw); + switch (*mv) // vim keys support!! { case 'k': case 'K': case 'w': case 'W': @@ -23,7 +23,7 @@ void getmv(WINDOW *gamew, int *move, int *y, int *x, int ymax, int xmax) break; case 'j': case 'J': case 's': case 'S': - mvdown(y, ymax); + mvdown(y, YMAX(gw)); break; case 'h': case 'H': case 'a': case 'A': @@ -31,36 +31,41 @@ void getmv(WINDOW *gamew, int *move, int *y, int *x, int ymax, int xmax) break; case 'l': case 'L': case 'd': case 'D': - mvright(x, xmax); + mvright(x, XMAX(gw)); break; } } -void mvup(int *y) +void +mvup(int *y) { (*y)--; if (*y < 1) *y = 1; } -void mvdown(int *y, int ymax) +void +mvdown(int *y, int ymax) { (*y)++; if (*y > ymax-2) *y = ymax-2; } -void mvleft(int *x) +void +mvleft(int *x) { *x -= 3; if (*x < 2) *x = 2; } -void mvright(int *x, int xmax) +void +mvright(int *x, int xmax) { *x += 3; if (*x > xmax-3) *x = xmax-3; } -void update_curs(WINDOW *gamew, int y, int x) +void +update_curs(WINDOW *gw, int y, int x) { - wmove(gamew, 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 @@ -1,14 +1,15 @@ #ifndef NAVIGATION_H #define NAVIGATION_H +#include "main.h" #include "minesweeper.h" -void navigate (struct _win_st* gamew, int *move, int *mbx, int *mby); -void getmv (struct _win_st* gamew, int *move, int *y, int *x, int ymax, int xmax); +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 ymax); +void mvdown (int *y, int xmax); void mvleft (int *x); void mvright (int *x, int xmax); -void update_curs(struct _win_st* gamew, 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,43 +1,50 @@ #include "outputs.h" -void print_board(WINDOW *gamew, Board *brd) +void +print_board(WINDOW *gw, Board *b) { int i, j, x, y = 1; - print_grid(gamew, brd->rows, brd->cols); - wattron(gamew, A_BOLD); - for (i = 0; i < brd->rows; i++) + print_grid(gw, b->rows, b->cols); + wattron(gw, A_BOLD); + for (i = 0; i < b->rows; i++) { x = 2; - for (j = 0; j < brd->cols; j++) + for (j = 0; j < b->cols; j++) { - mvwaddch(gamew, y, x, brd->db[i][j]); + mvwaddch(gw, y, x, b->db[i][j]); x += 3; } y++; } } -void print_grid(WINDOW *gamew, int rows, int cols) +void +print_grid(WINDOW *gw, int rows, int cols) { int i, j; - wattroff(gamew, A_BOLD); + wattroff(gw, A_BOLD); for (i = 1; i <= rows; i++) { - wmove(gamew, i, 1); + wmove(gw, i, 1); for (j = 0; j < cols; j++) - wprintw(gamew, "[ ]"); + wprintw(gw, "[ ]"); } - wrefresh(gamew); + wrefresh(gw); } -void session_info(int mbx, int mby, int xmid, int ndefused, int nmines) +#define YMID(x) getmaxy(x)/2 +#define XMID(x) getmaxx(x)/2 + +void +session_info(int mbx, int mby, int ndefused, int nmines) { mvprintw(0, 0, "Current position: (%d, %d) ", mbx, mby); - mvprintw(0, xmid-strlen("Defused mines: x/x")/2, "Defused mines: %d/%d", ndefused, nmines); - mvprintw(0, XMAX-strlen("m Controls"), "m Controls"); + 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(Board *brd, int hitrow, int hitcol, State state) +void +session_write(Board *b, int hitrow, int hitcol, State state) { int i, j; FILE *fsession = fopen(SESSION_PATH, "w"); @@ -53,17 +60,18 @@ void session_write(Board *brd, int hitrow, int hitcol, State state) ? 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 < brd->rows; i++) + for (i = 0; i < b->rows; i++) { - for (j = 0; j < brd->cols; j++) - fprintf(fsession, "%c ", brd->mb[i][j]); + for (j = 0; j < b->cols; j++) + fprintf(fsession, "%c ", b->mb[i][j]); fprintf(fsession, "\n"); } } fclose(fsession); } -void score_write(int ndefused, int cols, int rows) +void +score_write(int ndefused, int cols, int rows) { FILE *scorelog = fopen(SCORE_LOG_PATH, "a"); char *playername = get_pname(); @@ -87,7 +95,8 @@ void score_write(int ndefused, int cols, int rows) free(playername); } -char *get_pname(void) +char * +get_pname(void) { char buffer[20]; char *playername; @@ -103,41 +112,49 @@ char *get_pname(void) return (strcpy(playername, buffer)); } -void sort_scorelog(FILE *scorelog) +void +sort_scorelog(FILE *scorelog) { } -void show_scorelog(FILE *scorelog) +void +show_scorelog(FILE *scorelog) { } -void parse_data(FILE *scorelog) +void +parse_data(FILE *scorelog) { } -void game_won(WINDOW *gamew, int ymid, int xmid) +void +game_won(WINDOW *gw) { - wclear(gamew); - wrefresh(gamew); + wclear(gw); + wrefresh(gw); attron(A_BOLD); - mvprintw(ymid-2, xmid-11, "You defused all the mines!"); - mvprintw(ymid-1, xmid-3, "You won :)"); - mvprintw(ymid, xmid-11, "Press any key to continue"); + mvprintw(YMID(stdscr)-2, XMID(stdscr)-11, "You defused all the mines!"); + mvprintw(YMID(stdscr)-1, XMID(stdscr)-3, "You won :)"); + mvprintw(YMID(stdscr), XMID(stdscr)-11, "Press any key to continue"); refresh(); attroff(A_BOLD); } -void game_over(WINDOW *gamew, int ymid, int xmid) +void +game_over(WINDOW *gw) { - wclear(gamew); - wrefresh(gamew); + wclear(gw); + wrefresh(gw); attron(A_BOLD); - mvprintw(ymid-2, xmid-24, "You hit a mine! (or tried to defuse the wrong cell)"); - mvprintw(ymid-1, xmid-4, "Game over :("); - mvprintw(ymid, xmid-11, "Press any key to continue"); + 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 :("); + mvprintw(YMID(stdscr), XMID(stdscr)-11, "Press any key to continue"); refresh(); attroff(A_BOLD); } + +#undef YMID +#undef XMID diff --git a/assignment-1.5-arrays-pointers-files/minecurses/src/outputs.h b/assignment-1.5-arrays-pointers-files/minecurses/src/outputs.h @@ -7,18 +7,21 @@ #define SESSION_PATH "log/session.txt" #define SCORE_LOG_PATH "log/scorelog.csv" -typedef enum {GAME_WON, GAME_LOST} State; +typedef enum { + GAME_WON, + GAME_LOST +} State; -void print_board (struct _win_st* gamew, Board *brd); -void print_grid (struct _win_st* gamew, int rows, int cols); -void session_info (int mbx, int mby, int xmid, int ndefused, int nmines); -void session_write (Board *brd, 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 (struct _win_st* gamew, int ymid, int xmid); -void game_over (struct _win_st* gamew, int ymid, int xmid); +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 (struct _win_st* gw); +void game_over (struct _win_st* gw); #endif /* OUTPUTS_H */ diff --git a/assignment-1.5-arrays-pointers-files/minecurses/src/settings.c b/assignment-1.5-arrays-pointers-files/minecurses/src/settings.c @@ -1,37 +1,42 @@ #include "settings.h" -int set_cols(void) +int +set_cols(void) { int cols; do { - mvprintw(YMAX-4, 1, "Columns (Min = 5, Max = %d): ", (XMAX-2)/3-2); + mvprintw(YMAX(stdscr)-4, 1, "Columns (Min = 5, Max = %d): ", + (XMAX(stdscr)-2)/3-2); refresh(); scanw("%d", &cols); refresh(); - } while (cols < 5 || cols > (XMAX-2)/3-2); + } while (cols < 5 || cols > (XMAX(stdscr)-2)/3-2); return cols; } -int set_rows(void) +int +set_rows(void) { int rows; do { - mvprintw(YMAX-3, 1, "Rows (Min = 5, Max = %d): ", YMAX-4); + mvprintw(YMAX(stdscr)-3, 1, "Rows (Min = 5, Max = %d): ", + YMAX(stdscr)-4); refresh(); scanw("%d", &rows); refresh(); - } while (rows < 5 || rows > YMAX-4); + } while (rows < 5 || rows > YMAX(stdscr)-4); return rows; } -int set_nmines(int dimensions) +int +set_nmines(int dimensions) { int nmines; do { - mvprintw(YMAX-2, 1, "Mines (Max = %d): ", dimensions-15); + mvprintw(YMAX(stdscr)-2, 1, "Mines (Max = %d): ", dimensions-15); refresh(); scanw("%d", &nmines); refresh(); diff --git a/assignment-1.5-arrays-pointers-files/minecurses/src/settings.h b/assignment-1.5-arrays-pointers-files/minecurses/src/settings.h @@ -3,8 +3,8 @@ #include "minesweeper.h" -int set_cols (void); -int set_rows (void); -int set_nmines (int dimensions); +int set_cols (void); +int set_rows (void); +int set_nmines(int dimensions); #endif /* SETTINGS_H */ diff --git a/assignment-1.5-arrays-pointers-files/minecurses/src/wins.c b/assignment-1.5-arrays-pointers-files/minecurses/src/wins.c @@ -1,31 +1,34 @@ #include "wins.h" -void init_curses(void) +void +init_curses(void) { initscr(); noecho(); cbreak(); } -WINDOW *game_win(int rows, int cols) +WINDOW * +game_win(int rows, int cols) { int wrows = rows+2; int wcols = cols*3+2; - int wy = YMAX/2 - wrows/2; - int wx = XMAX/2 - wcols/2; - WINDOW *gamew = newwin(wrows, wcols, wy, wx); - wattron(gamew, A_BOLD); - box(gamew, 0, 0); - wrefresh(gamew); - wattroff(gamew, A_BOLD); - return gamew; + int wy = YMAX(stdscr)/2 - wrows/2; + int wx = XMAX(stdscr)/2 - wcols/2; + WINDOW *gw = newwin(wrows, wcols, wy, wx); + wattron(gw, A_BOLD); + box(gw, 0, 0); + wrefresh(gw); + wattroff(gw, A_BOLD); + return gw; } -void options_menu(void) +void +options_menu(void) { int w = 33, h = 15; - int wy = YMAX/2 - h/2; - int wx = XMAX/2 - w/2; + int wy = YMAX(stdscr)/2 - h/2; + int wx = XMAX(stdscr)/2 - w/2; WINDOW *opts = newwin(h, w, wy, wx); werase(opts); box(opts, 0, 0); @@ -37,7 +40,8 @@ void options_menu(void) delwin(opts); } -void fill_menu(WINDOW *opts) +void +fill_menu(WINDOW *opts) { mvwprintw(opts, 1, 1, "q Quit"); mvwprintw(opts, 2, 1, "w/k Move up"); diff --git a/assignment-1.5-arrays-pointers-files/minecurses/src/wins.h b/assignment-1.5-arrays-pointers-files/minecurses/src/wins.h @@ -3,9 +3,9 @@ #include "main.h" -void init_curses (void); -WINDOW *game_win (int rows, int cols); -void options_menu (void); -void fill_menu (struct _win_st*); +void init_curses (void); +WINDOW *game_win (int rows, int cols); +void options_menu(void); +void fill_menu (struct _win_st* opts); #endif /* WINS_H */ diff --git a/assignment3.tar.gz b/assignment3.tar.gz Binary files differ.