uni

University stuff
git clone git://git.christosmarg.xyz/uni-assignments.git
Log | Files | Refs | README | LICENSE

commit 97863587d3aaa43b0190e1a33acdfc3574df5fd6
parent 44d915d312bcf723e298e792af771a19bb48ca4c
Author: Christos Margiolis <christos@margiolis.net>
Date:   Sun,  5 Apr 2020 00:34:08 +0300

updated submodule

Diffstat:
Massignment-1.5-arrays-pointers-files/minecurses/README.md | 10+++++++---
Massignment-1.5-arrays-pointers-files/minecurses/bin/minecurses | 0
Dassignment-1.5-arrays-pointers-files/minecurses/bin/minesweeper | 0
Massignment-1.5-arrays-pointers-files/minecurses/include/gameplay.h | 5+++--
Massignment-1.5-arrays-pointers-files/minecurses/include/minesweeper.h | 10+++++-----
Massignment-1.5-arrays-pointers-files/minecurses/include/outputs.h | 1+
Massignment-1.5-arrays-pointers-files/minecurses/include/settings.h | 10+++-------
Massignment-1.5-arrays-pointers-files/minecurses/include/wins.h | 5++---
Massignment-1.5-arrays-pointers-files/minecurses/obj/audio.o | 0
Massignment-1.5-arrays-pointers-files/minecurses/obj/gameplay.o | 0
Massignment-1.5-arrays-pointers-files/minecurses/obj/main.o | 0
Massignment-1.5-arrays-pointers-files/minecurses/obj/minesweeper.o | 0
Massignment-1.5-arrays-pointers-files/minecurses/obj/navigation.o | 0
Massignment-1.5-arrays-pointers-files/minecurses/obj/outputs.o | 0
Massignment-1.5-arrays-pointers-files/minecurses/obj/settings.o | 0
Massignment-1.5-arrays-pointers-files/minecurses/obj/wins.o | 0
Massignment-1.5-arrays-pointers-files/minecurses/src/gameplay.c | 44++++++++++++++++++++++++++++----------------
Massignment-1.5-arrays-pointers-files/minecurses/src/main.c | 17++++++++++-------
Massignment-1.5-arrays-pointers-files/minecurses/src/minesweeper.c | 17++++++-----------
Massignment-1.5-arrays-pointers-files/minecurses/src/navigation.c | 2--
Massignment-1.5-arrays-pointers-files/minecurses/src/outputs.c | 47++++++++++++++++++++++++-----------------------
Massignment-1.5-arrays-pointers-files/minecurses/src/settings.c | 35+++++++++++++----------------------
Massignment-1.5-arrays-pointers-files/minecurses/src/wins.c | 71+++++++++++++++++++++++++++++++++++++++++------------------------------
Massignment-1.5-arrays-pointers-files/minecurses/txt/mnsout.txt | 47+++++++++++++++++++++++++++++++++++++++++------
24 files changed, 184 insertions(+), 137 deletions(-)

diff --git a/assignment-1.5-arrays-pointers-files/minecurses/README.md b/assignment-1.5-arrays-pointers-files/minecurses/README.md @@ -4,6 +4,8 @@ A minesweeper game using the `ncurses` and `SDL2` libraries. The game was origin ## How to play +The objective is to simply find and defuse all the mines. + * Enter number of columns * Enter number of rows * Enter number of mines @@ -16,6 +18,7 @@ A minesweeper game using the `ncurses` and `SDL2` libraries. The game was origin Additional controls +* `m`: Open control menu * `p`: Pause audio * `+`: Volume up * `-`: Volume down @@ -30,8 +33,6 @@ Additional controls * Improve score log * Add macros for the scaled board dimensions * Add colors -* Fix curs_set -* Add echo during setup * Add unicode support ## Execution @@ -53,4 +54,7 @@ $ make clean # optional ## Preview -![MinesweeperScr](https://user-images.githubusercontent.com/54286563/73931645-4d4db080-48e1-11ea-981b-e9b028d34c41.png) +### Start screen +![startmenu](https://user-images.githubusercontent.com/54286563/78460772-52a25e00-76cc-11ea-976b-10212e228ca0.png) +### Gameplay +![gameplay](https://user-images.githubusercontent.com/54286563/78460709-bc6e3800-76cb-11ea-92f4-ab58e141e9be.png) 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/bin/minesweeper b/assignment-1.5-arrays-pointers-files/minecurses/bin/minesweeper Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/minecurses/include/gameplay.h b/assignment-1.5-arrays-pointers-files/minecurses/include/gameplay.h @@ -4,12 +4,13 @@ #include "minesweeper.h" void play_minesweeper (struct _win_st* gamew, char **dboard, char **mboard, int COLS, int ROWS, int NMINES); -bool open_cell (struct _win_st* gamew, char **dboard, char **mboard, int mby, int mbx, bool gameover); +int open_cell (struct _win_st* gamew, char **dboard, char **mboard, int mby, int mbx, int gameover); void handle_flags (struct _win_st* gamew, char **dboard, char **mboard, int mby, int mbx); void defuse_mine (struct _win_st* gamew, char **dboard, char **mboard, int mby, int mbx); void transfer (char **dboard, char **mboard, int mby, int mbx); void reveal (struct _win_st* gamew, char **dboard, int mby, int mbx, int y, int x); -bool is_defused (char **dboard, char **mboard, int mby, int mbx); +int is_defused (char **dboard, char **mboard, int mby, int mbx); +void handle_menu (struct _win_st*, char **dboard, int COLS, int ROWS); void handle_gameover (struct _win_st* gamew, char **mboard, int ymid, int xmid, int COLS, int ROWS, int mby, int mbx); void handle_win (struct _win_st* gamew, char **mboard, int ymid, int xmid, int COLS, int ROWS, int mby, int mbx, int ndefused); diff --git a/assignment-1.5-arrays-pointers-files/minecurses/include/minesweeper.h b/assignment-1.5-arrays-pointers-files/minecurses/include/minesweeper.h @@ -9,12 +9,12 @@ #include <string.h> #include <time.h> -#include "settings.h" +#include "audio.h" #include "gameplay.h" #include "navigation.h" #include "outputs.h" +#include "settings.h" #include "wins.h" -#include "audio.h" #define BLANK ' ' #define MINE '*' @@ -38,9 +38,9 @@ void fill_dboard (char **dboard, int COLS, int ROWS); char **init_mboard (struct _win_st* gamew, int COLS, int ROWS, int NMINES); void place_mines (char **mboard, int COLS, int ROWS, int NMINES); void add_adj (char **mboard, int COLS, int ROWS); -bool is_mine (char **mboard, int row, int col); -bool outof_bounds (int row, int col, int ROWS, int COLS); -int8_t adj_mines (char **mboard, int row, int col, int COLS, int ROWS); +int is_mine (char **mboard, int row, int col); +int outof_bounds (int row, int col, int ROWS, int COLS); +uint8_t adj_mines (char **mboard, int row, int col, int COLS, int ROWS); void fill_spaces (char **mboard, int COLS, int ROWS, int NMINES); #endif /* MINESWEEPER_H */ diff --git a/assignment-1.5-arrays-pointers-files/minecurses/include/outputs.h b/assignment-1.5-arrays-pointers-files/minecurses/include/outputs.h @@ -8,6 +8,7 @@ void print_board (struct _win_st* gamew, char **dboard, int COLS, int ROWS); void print_grid (struct _win_st* gamew, int ROWS, int COLS); +void session_info (int mbx, int mby, int xmax, int xmid, int ndefused, int NMINES); void session_write (char **mboard, int COLS, int ROWS, int hitrow, int hitcol, const char *status); void score_write (int ndefused, int COLS, int ROWS); char *get_pname (void); diff --git a/assignment-1.5-arrays-pointers-files/minecurses/include/settings.h b/assignment-1.5-arrays-pointers-files/minecurses/include/settings.h @@ -3,12 +3,8 @@ #include "minesweeper.h" -#define COLS_CHAR_LENGTH strlen("Columns (Min = 5, Max = XXX): ") + 1 -#define ROWS_CHAR_LENGTH strlen("Rows (Min = 5, Max = YYY): ") + 1 -#define NMINES_CHAR_LENGTH strlen("Mines (Max = MMM): ") + 1 - -int set_cols (struct _win_st* menuw, int xmax); -int set_rows (struct _win_st* menuw, int ymax); -int set_nmines (struct _win_st* menuw, int DIMENSIONS); +int set_cols (int ymax, int xmax); +int set_rows (int ymax); +int set_nmines (int ymax, int DIMENSIONS); #endif /* SETTINGS_H */ diff --git a/assignment-1.5-arrays-pointers-files/minecurses/include/wins.h b/assignment-1.5-arrays-pointers-files/minecurses/include/wins.h @@ -3,10 +3,9 @@ #include "minesweeper.h" -void main_win (void); -WINDOW *menu_win (int *ymax, int *xmax); +void init_curses (void); WINDOW *game_win (int COLS, int ROWS, int NMINES); void options_menu (void); -void options_win (void); +void fill_menu (struct _win_st*); #endif /* WINS_H */ 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/gameplay.c b/assignment-1.5-arrays-pointers-files/minecurses/src/gameplay.c @@ -3,7 +3,7 @@ void play_minesweeper(WINDOW *gamew, char **dboard, char **mboard, int COLS, int ROWS, int NMINES) { int mbx = 0, mby = 0; - bool gameover = false; + int gameover = FALSE; int ndefused = 0; int ymax, xmax, ymid, xmid; char move; @@ -12,51 +12,52 @@ void play_minesweeper(WINDOW *gamew, char **dboard, char **mboard, int COLS, int xmid = xmax/2; print_board(gamew, dboard, COLS, ROWS); - + do { + session_info(mbx, mby, xmax, xmid, ndefused, NMINES); navigate(gamew, dboard, &move, &mbx, &mby); - if (move == ENTER || move == OPEN_LOWER || move == OPEN_UPPER) // handle cell opening + if (move == ENTER || move == OPEN_LOWER || move == OPEN_UPPER) gameover = open_cell(gamew, dboard, mboard, mby, mbx, gameover); - else if (move == FLAG_LOWER || move == FLAG_UPPER) // handle falgs + else if (move == FLAG_LOWER || move == FLAG_UPPER) handle_flags(gamew, dboard, mboard, mby, mbx); - else if (move == DEFUSE_LOWER || move == DEFUSE_UPPER) // check for defuse + else if (move == DEFUSE_LOWER || move == DEFUSE_UPPER) { if (dboard[mby][mbx] == FLAG && mboard[mby][mbx] == MINE) { ndefused++; defuse_mine(gamew, dboard, mboard, mby, mbx); } - else if (dboard[mby][mbx] == FLAG && mboard[mby][mbx] != MINE) gameover = true; + else if (dboard[mby][mbx] == FLAG && mboard[mby][mbx] != MINE) gameover = TRUE; } - else if (move == PAUSE_AUDIO) pause_audio(); // handle audio + else if (move == PAUSE_AUDIO) pause_audio(); else if (move == VOLUME_UP || move == VOLUME_DOWN) volume(move); + else if (move == 'm') handle_menu(gamew, dboard, COLS, ROWS); - mvprintw(1, xmid-8, "Defused mines: %d/%d", ndefused, NMINES); } while (((mby >= 0 && mby < ROWS) && (mbx >= 0 && mbx < COLS)) && ndefused < NMINES && !gameover && move != QUIT); - if (gameover == true) + if (gameover) handle_gameover(gamew, mboard, ymid, xmid, COLS, ROWS, mby, mbx); if (ndefused == NMINES) handle_win(gamew, mboard, ymid, xmid, COLS, ROWS, mby, mbx, ndefused); } -bool open_cell(WINDOW *gamew, char **dboard, char **mboard, int mby, int mbx, bool gameover) +int open_cell(WINDOW *gamew, char **dboard, char **mboard, int mby, int mbx, int gameover) { transfer(dboard, mboard, mby, mbx); reveal(gamew, dboard, mby, mbx, mby+1, 3*mbx+2); - if (dboard[mby][mbx] == MINE) gameover = true; + if (dboard[mby][mbx] == MINE) gameover = TRUE; return gameover; } void handle_flags(WINDOW *gamew, char **dboard, char **mboard, int mby, int mbx) { - if (dboard[mby][mbx] == FLAG) dboard[mby][mbx] = BLANK; // undo flag - else if (dboard[mby][mbx] != FLAG && dboard[mby][mbx] != BLANK) return; // dont flag an already opened mine - else dboard[mby][mbx] = FLAG; // flag if not flagged already + if (dboard[mby][mbx] == FLAG) dboard[mby][mbx] = BLANK; + else if (dboard[mby][mbx] != FLAG && dboard[mby][mbx] != BLANK) return; + else dboard[mby][mbx] = FLAG; reveal(gamew, dboard, mby, mbx, mby+1, 3*mbx+2); } @@ -78,16 +79,27 @@ void reveal(WINDOW *gamew, char **dboard, int mby, int mbx, int y, int x) wrefresh(gamew); } -bool is_defused(char **dboard, char **mboard, int mby, int mbx) +int is_defused(char **dboard, char **mboard, int mby, int mbx) +{ + return ((dboard[mby][mbx] == DEFUSED)) ? TRUE : FALSE; +} + +void handle_menu(WINDOW *gamew, char **dboard, int COLS, int ROWS) { - return ((dboard[mby][mbx] == DEFUSED)) ? true : false; + options_menu(); + box(gamew, 0, 0); + print_board(gamew, dboard, COLS, ROWS); } void handle_gameover(WINDOW *gamew, char **mboard, int ymid, int xmid, int COLS, int ROWS, int mby, int mbx) { game_over(gamew, mboard, ymid, xmid); getchar(); + erase(); + refresh(); + box(gamew, 0, 0); print_board(gamew, mboard, COLS, ROWS); + wrefresh(gamew); session_write(mboard, COLS, ROWS, mbx, mby, "lost"); } diff --git a/assignment-1.5-arrays-pointers-files/minecurses/src/main.c b/assignment-1.5-arrays-pointers-files/minecurses/src/main.c @@ -3,14 +3,17 @@ int main(int argc, char **argv) { - main_win(); - options_menu(); - int ymax, xmax; - WINDOW *menuw = menu_win(&ymax, &xmax); - int COLS = set_cols(menuw, xmax); - int ROWS = set_rows(menuw, ymax); - int NMINES = set_nmines(menuw, COLS*ROWS); + init_curses(); + getmaxyx(stdscr, ymax, xmax); + echo(); + int COLS = set_cols(ymax, xmax); + int ROWS = set_rows(ymax); + int NMINES = set_nmines(ymax, COLS*ROWS); + noecho(); + options_menu(); + erase(); + refresh(); WINDOW *gamew = game_win(COLS, ROWS, NMINES); char **dboard = init_dboard(gamew, COLS, ROWS); diff --git a/assignment-1.5-arrays-pointers-files/minecurses/src/minesweeper.c b/assignment-1.5-arrays-pointers-files/minecurses/src/minesweeper.c @@ -21,7 +21,6 @@ char **init_dboard(WINDOW *gamew, int COLS, int ROWS) void fill_dboard(char **dboard, int COLS, int ROWS) { int i, j; - for (i = 0; i < ROWS; i++) for (j = 0; j < COLS; j++) dboard[i][j] = BLANK; @@ -36,7 +35,7 @@ char **init_mboard(WINDOW *gamew, int COLS, int ROWS, int NMINES) if (mboard == NULL) { - mvprintw(1, 1, "Error, not enough memory, exiting..."); + mvprintw(0, 0, "Error, not enough memory, exiting..."); refresh(); exit(EXIT_FAILURE); } @@ -53,9 +52,7 @@ char **init_mboard(WINDOW *gamew, int COLS, int ROWS, int NMINES) void place_mines(char **mboard, int COLS, int ROWS, int NMINES) { int i, wrand, hrand; - srand(time(NULL)); - for (i = 0; i < NMINES; i++) { wrand = rand() % ROWS; @@ -67,24 +64,23 @@ void place_mines(char **mboard, int COLS, int ROWS, int NMINES) void add_adj(char **mboard, int COLS, int ROWS) { int i, j; - for (i = 0; i < ROWS; i++) for (j = 0; j < COLS; j++) if (!is_mine(mboard, i, j)) mboard[i][j] = adj_mines(mboard, i, j, COLS, ROWS) + '0'; } -bool is_mine(char **mboard, int row, int col) +int is_mine(char **mboard, int row, int col) { - return (mboard[row][col] == MINE) ? true : false; + return (mboard[row][col] == MINE) ? TRUE : FALSE; } -bool outof_bounds(int row, int col, int COLS, int ROWS) +int outof_bounds(int row, int col, int COLS, int ROWS) { - return (row < 0 || row > ROWS-1 || col < 0 || col > COLS-1) ? true : false; + return (row < 0 || row > ROWS-1 || col < 0 || col > COLS-1) ? TRUE : FALSE; } -int8_t adj_mines(char **mboard, int row, int col, int COLS, int ROWS) +uint8_t adj_mines(char **mboard, int row, int col, int COLS, int ROWS) { int8_t nadj = 0; @@ -103,7 +99,6 @@ int8_t adj_mines(char **mboard, int row, int col, int COLS, int ROWS) void fill_spaces(char **mboard, int COLS, int ROWS, int NMINES) { int i, j; - for (i = 0; i < ROWS; i++) for (j = 0; j < COLS; j++) if (mboard[i][j] != MINE && mboard[i][j] == '0') diff --git a/assignment-1.5-arrays-pointers-files/minecurses/src/navigation.c b/assignment-1.5-arrays-pointers-files/minecurses/src/navigation.c @@ -5,11 +5,9 @@ void navigate(WINDOW *gamew, char **mboard, char *move, int *mbx, int *mby) int ymax, xmax; static int y = 1, x = 2; getmaxyx(gamew, ymax, xmax); - update_curs(gamew, y, x); *mbx = (x-2)/3; *mby = y-1; - mvprintw(1, 1, "Current position: (%d, %d) ", *mbx, *mby); refresh(); getmv(gamew, move, &y, &x, ymax, xmax); } diff --git a/assignment-1.5-arrays-pointers-files/minecurses/src/outputs.c b/assignment-1.5-arrays-pointers-files/minecurses/src/outputs.c @@ -3,39 +3,40 @@ void print_board(WINDOW *gamew, char **dboard, int COLS, int ROWS) { int i, j, x, y = 1; - print_grid(gamew, ROWS, COLS); - + wattron(gamew, A_BOLD); for (i = 0; i < ROWS; i++) { x = 2; for (j = 0; j < COLS; j++) { - wattron(gamew, A_BOLD); mvwaddch(gamew, y, x, dboard[i][j]); x += 3; } y++; } - - wrefresh(gamew); - wattron(gamew, A_BOLD); } void print_grid(WINDOW *gamew, int ROWS, int COLS) { int i, j; - + wattroff(gamew, A_BOLD); for (i = 1; i <= ROWS; i++) { wmove(gamew, i, 1); for (j = 0; j < COLS; j++) wprintw(gamew, "[ ]"); } - wrefresh(gamew); } +void session_info(int mbx, int mby, int xmax, int xmid, 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"); +} + void session_write(char **mboard, int COLS, int ROWS, int hitrow, int hitcol, const char *status) { int i, j; @@ -43,7 +44,7 @@ void session_write(char **mboard, int COLS, int ROWS, int hitrow, int hitcol, co if (mnsout == NULL) { - mvprintw(1, 1, "Error opening file, exiting..."); + mvprintw(0, 0, "Error opening file, exiting..."); refresh(); exit(EXIT_FAILURE); } @@ -61,7 +62,7 @@ void session_write(char **mboard, int COLS, int ROWS, int hitrow, int hitcol, co fprintf(mnsout, "\n"); } - mvprintw(1, 1, "Session written to file "); + mvprintw(0, 0, "Session written to file "); refresh(); getchar(); } @@ -78,7 +79,7 @@ void score_write(int ndefused, int COLS, int ROWS) if (scorelog == NULL) { - mvprintw(1, 1, "Error opening file, exiting..."); + mvprintw(0, 0, "Error opening file, exiting..."); refresh(); exit(EXIT_FAILURE); } @@ -86,7 +87,7 @@ void score_write(int ndefused, int COLS, int ROWS) { fprintf(scorelog, "\n%s\t\t\t\t%d\t\t\t\t\t%dx%d", playername, ndefused, COLS, ROWS); sort_scorelog(scorelog); // pending - mvprintw(1, 1, "New score written to score log"); + mvprintw(0, 0, "New score written to score log"); refresh(); getchar(); } @@ -100,7 +101,7 @@ char *get_pname(void) char buffer[20]; char *playername; - move(1, 1); + move(0, 0); echo(); clrtoeol(); printw("Your name: "); @@ -122,22 +123,22 @@ void game_won(WINDOW *gamew, int ymid, int xmid) { wclear(gamew); wrefresh(gamew); - wattron(stdscr, A_BOLD); - mvwprintw(stdscr, ymid-2, xmid-11, "You defused all the mines!"); - mvwprintw(stdscr, ymid-1, xmid-3, "You won :)"); - mvwprintw(stdscr, ymid, xmid-11, "Press any key to continue"); + 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"); refresh(); - wattroff(stdscr, A_BOLD); + attroff(A_BOLD); } void game_over(WINDOW *gamew, char **mboard, int ymid, int xmid) { wclear(gamew); wrefresh(gamew); - wattron(stdscr, A_BOLD); - mvwprintw(stdscr, ymid-2, xmid-24, "You hit a mine! (or tried to defuse the wrong cell)"); - mvwprintw(stdscr, ymid-1, xmid-4, "Game over :("); - mvwprintw(stdscr, ymid, xmid-11, "Press any key to continue"); + 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"); refresh(); - wattroff(stdscr, A_BOLD); + attroff(A_BOLD); } 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,49 +1,40 @@ #include "settings.h" -int set_cols(WINDOW *menuw, int xmax) +int set_cols(int ymax, int xmax) { int COLS; - do { - mvwprintw(menuw, 1, 1, "Columns (Min = 5, Max = %d): ", (xmax-2)/3-2); - wrefresh(menuw); + mvprintw(ymax-4, 1, "Columns (Min = 5, Max = %d): ", (xmax-2)/3-2); + refresh(); scanw("%d", &COLS); - mvwprintw(menuw, 1, COLS_CHAR_LENGTH, "%d", COLS); - wrefresh(menuw); + refresh(); } while (COLS < 5 || COLS > (xmax-2)/3-2); - return COLS; } -int set_rows(WINDOW *menuw, int ymax) +int set_rows(int ymax) { int ROWS; - do { - mvwprintw(menuw, 2, 1, "Rows (Min = 5, Max = %d): ", ymax-14); - wrefresh(menuw); + mvprintw(ymax-3, 1, "Rows (Min = 5, Max = %d): ", ymax-4); + refresh(); scanw("%d", &ROWS); - mvwprintw(menuw, 2, ROWS_CHAR_LENGTH, "%d", ROWS); - wrefresh(menuw); - } while (ROWS < 5 || ROWS > ymax - 14); - + refresh(); + } while (ROWS < 5 || ROWS > ymax-4); return ROWS; } -int set_nmines(WINDOW *menuw, int DIMENSIONS) +int set_nmines(int ymax, int DIMENSIONS) { int NMINES; - do { - mvwprintw(menuw, 3, 1, "Mines (Max = %d): ", DIMENSIONS-15); // -10 so the player has a chance to win - wrefresh(menuw); + mvprintw(ymax-2, 1, "Mines (Max = %d): ", DIMENSIONS-15); // -10 so the player has a chance to win + refresh(); scanw("%d", &NMINES); - mvwprintw(menuw, 3, NMINES_CHAR_LENGTH, "%d", NMINES); - wrefresh(menuw); + refresh(); } while (NMINES < 1 || NMINES > DIMENSIONS-15); - return NMINES; } 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,47 +1,58 @@ #include "wins.h" -void main_win(void) +void init_curses(void) { initscr(); noecho(); cbreak(); - - WINDOW *mainw = newwin(0, 0, 0, 0); - wattron(mainw, A_BOLD); - box(mainw, 0, 0); - refresh(); - wrefresh(mainw); - wattroff(mainw, A_BOLD); -} - -WINDOW *menu_win(int *ymax, int *xmax) -{ - int nsettings = 3; - getmaxyx(stdscr, *ymax, *xmax); - WINDOW *menuw = newwin(nsettings+2, *xmax-8, *ymax-8, 4); - wattron(menuw, A_BOLD); - box(menuw, 0, 0); - wrefresh(menuw); - wattroff(menuw, A_BOLD); - return menuw; } WINDOW *game_win(int COLS, int ROWS, int NMINES) { + int ymax, xmax; + getmaxyx(stdscr, ymax, xmax); int wrows = ROWS+2; int wcols = COLS*3+2; - WINDOW *gameWin = newwin(wrows, wcols, 2, 4); - wattron(gameWin, A_BOLD); - box(gameWin, 0, 0); - wrefresh(gameWin); - wattroff(gameWin, A_BOLD); - return gameWin; + 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; } void options_menu(void) { - int ymax = getmaxy(stdscr); - mvprintw(ymax-3, 5, "q Quit w/k Move up s/j Move down a/h Move Left d/l Move Right [ENTER]/o Open cell"); - mvprintw(ymax-2, 5, "f Flag cell g Defuse (if flagged only) p Pause music + Volume up - Volume down"); - refresh(); + int ymax, xmax; + getmaxyx(stdscr, ymax, xmax); + int w = 33, h = 15; + int wy = ymax/2 - h/2; + int wx = xmax/2 - w/2; + WINDOW *opts = newwin(h, w, wy, wx); + werase(opts); + box(opts, 0, 0); + fill_menu(opts); + wrefresh(opts); + wgetch(opts); + werase(opts); + wrefresh(opts); + delwin(opts); +} + +void fill_menu(WINDOW *opts) +{ + mvwprintw(opts, 1, 1, "q Quit"); + mvwprintw(opts, 2, 1, "w/k Move up"); + mvwprintw(opts, 3, 1, "s/j Move down"); + mvwprintw(opts, 4, 1, "a/h Move left"); + mvwprintw(opts, 5, 1, "d/l Move right"); + mvwprintw(opts, 6, 1, "f Flag cell"); + mvwprintw(opts, 7, 1, "g Defuse (if flagged only)"); + mvwprintw(opts, 8, 1, "p Pause music"); + mvwprintw(opts, 9, 1, "+ Volume up"); + mvwprintw(opts, 10, 1, "- Volume down"); + mvwprintw(opts, 11, 1, "[ENTER]/o Open cell"); + mvwprintw(opts, 13, 1, "Press any key to quit the menu"); } diff --git a/assignment-1.5-arrays-pointers-files/minecurses/txt/mnsout.txt b/assignment-1.5-arrays-pointers-files/minecurses/txt/mnsout.txt @@ -1,9 +1,44 @@ -Mine hit at position (2, 3) +Mine hit at position (16, 29) Board overview -- - - - - -1 1 1 - - -1 * 1 - - -1 1 1 - - -- - - - - +- - - - - - - - - - 1 * 1 - - - - - - - - - - - - - - - - - - - - - - - - 1 * 1 +- - - - - - - - - - 1 1 1 - - - - - - - - - - - - - - - - - 1 1 1 - - - - 1 1 1 +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1 * 1 - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1 1 1 - - - - - - - +- - - - 1 1 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - 1 * 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - 1 1 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - 1 1 1 - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - 1 D 1 - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - 1 1 1 - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - 1 1 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - 1 * 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - 1 1 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - 1 1 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - 1 * 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - 1 1 1 - - - - 1 1 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - 1 * 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - 1 1 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - 1 1 1 - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - 1 * 1 - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - 1 1 1 - - - - - - - - - - - - - - - - - - 1 1 1 - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1 * 1 - - +- - - - - 1 1 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - 1 1 1 - - +- - - - - 1 * 1 - 1 1 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - 1 1 1 - 1 * 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - 1 1 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - 1 1 1 - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - 1 * 2 1 1 - - - - - - - - - - - - +- - - - - - - - - - - 1 1 1 - - - - - - - - - 1 1 2 * 1 - - - - - - - - - - - - +- - - - - - - - - - - 1 * 1 - - - - - - - - - - - 1 1 1 - - - - - - - - - - - - +- - - - - - - - - - - 1 1 1 - - - - - - - - - - - - - - - - - - - - - - - - - -