commit e39f4824efd9cf11deaaaf82b5da27f400c01f14 parent 2b23453861a4b6f5209deee9659489871de7a03e Author: Christos Margiolis <christos@margiolis.net> Date: Sat, 4 Apr 2020 02:07:12 +0300 updated submodule Diffstat:
12 files changed, 117 insertions(+), 115 deletions(-)
diff --git a/assignment-1.5-arrays-pointers-files/minecurses/README.md b/assignment-1.5-arrays-pointers-files/minecurses/README.md @@ -1,6 +1,6 @@ # minescurses -A simplistic minesweeper game using the `ncurses` and `SDL2` libraries. The game was originally made as a university assignment of mine, but I decided to take it a bit further afterwards. +A minesweeper game using the `ncurses` and `SDL2` libraries. The game was originally made as a university assignment of mine, but I decided to take it a bit further afterwards. ## How to play @@ -45,7 +45,7 @@ Additional controls * `SDL2_mixer` ```shell -$ cd path_to/minesweeper +$ cd path_to/minecurses $ make $ make run $ make clean # optional 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/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/src/gameplay.c b/assignment-1.5-arrays-pointers-files/minecurses/src/gameplay.c @@ -5,11 +5,11 @@ void play_minesweeper(WINDOW *gameWin, char **dispboard, char **mineboard, int C int mbx = 0, mby = 0; bool gameOver = false; int numDefused = 0; - int yMax, xMax, yMiddle, xMiddle; + int ymax, xmax, ymid, xmid; char move; - getmaxyx(stdscr, yMax, xMax); - yMiddle = yMax/2; - xMiddle = xMax/2; + getmaxyx(stdscr, ymax, xmax); + ymid = ymax/2; + xmid = xmax/2; print_board(gameWin, dispboard, COLS, ROWS); @@ -33,15 +33,15 @@ void play_minesweeper(WINDOW *gameWin, char **dispboard, char **mineboard, int C else if (move == PAUSE_AUDIO) pause_audio(); // handle audio else if (move == VOLUME_UP || move == VOLUME_DOWN) volume(move); - mvprintw(1, xMiddle-8, "Defused mines: %d/%d", numDefused, NMINES); + mvprintw(1, xmid-8, "Defused mines: %d/%d", numDefused, NMINES); } while (((mby >= 0 && mby < ROWS) && (mbx >= 0 && mbx < COLS)) && numDefused < NMINES && !gameOver && move != QUIT); if (gameOver == true) - handle_gameover(gameWin, mineboard, yMiddle, xMiddle, COLS, ROWS, mby, mbx); + handle_gameover(gameWin, mineboard, ymid, xmid, COLS, ROWS, mby, mbx); if (numDefused == NMINES) - handle_win(gameWin, mineboard, yMiddle, xMiddle, COLS, ROWS, mby, mbx, numDefused); + handle_win(gameWin, mineboard, ymid, xmid, COLS, ROWS, mby, mbx, numDefused); } bool open_cell(WINDOW *gameWin, char **dispboard, char **mineboard, int mby, int mbx, bool gameOver) @@ -83,17 +83,17 @@ bool is_defused(char **dispboard, char **mineboard, int mby, int mbx) return ((dispboard[mby][mbx] == DEFUSED)) ? true : false; } -void handle_gameover(WINDOW *gameWin, char **mineboard, int yMiddle, int xMiddle, int COLS, int ROWS, int mby, int mbx) +void handle_gameover(WINDOW *gameWin, char **mineboard, int ymid, int xmid, int COLS, int ROWS, int mby, int mbx) { - game_over(gameWin, mineboard, yMiddle, xMiddle); + game_over(gameWin, mineboard, ymid, xmid); getchar(); print_board(gameWin, mineboard, COLS, ROWS); session_write(mineboard, COLS, ROWS, mbx, mby, "lost"); } -void handle_win(WINDOW *gameWin, char **mineboard, int yMiddle, int xMiddle, int COLS, int ROWS, int mby, int mbx, int numDefused) +void handle_win(WINDOW *gameWin, char **mineboard, int ymid, int xmid, int COLS, int ROWS, int mby, int mbx, int numDefused) { - game_won(gameWin, yMiddle, xMiddle); + game_won(gameWin, ymid, xmid); getchar(); session_write(mineboard, COLS, ROWS, mbx, mby, "won"); score_write(numDefused, COLS, ROWS); 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,20 +6,20 @@ int main(int argc, char **argv) main_win(); options_menu(); - int yMax, xMax; - WINDOW *menuWin = menu_win(&yMax, &xMax); - int COLS = set_cols(menuWin, xMax); - int ROWS = set_rows(menuWin, yMax); - int NMINES = set_nmines(menuWin, COLS*ROWS); + 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); - WINDOW *gameWin = game_win(COLS, ROWS, NMINES); - char **dispboard = init_dispboard(gameWin, COLS, ROWS); - char **mineboard = init_mineboard(gameWin, COLS, ROWS, NMINES); + WINDOW *gamew = game_win(COLS, ROWS, NMINES); + char **dispboard = init_dispboard(gamew, COLS, ROWS); + char **mineboard = init_mineboard(gamew, COLS, ROWS, NMINES); pthread_t audioThread; long threadID = 1; pthread_create(&audioThread, NULL, play_audio, (void *)threadID); - play_minesweeper(gameWin, dispboard, mineboard, COLS, ROWS, NMINES); + play_minesweeper(gamew, dispboard, mineboard, COLS, ROWS, NMINES); pthread_cancel(audioThread); free(dispboard); 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,6 +1,6 @@ #include "minesweeper.h" -char **init_dispboard(WINDOW *gameWin, int COLS, int ROWS) +char **init_dispboard(WINDOW *gamew, int COLS, int ROWS) { int i; char **dispboard = (char **)malloc(ROWS * sizeof(char *)); @@ -27,7 +27,7 @@ void fill_dispboard(char **dispboard, int COLS, int ROWS) dispboard[i][j] = BLANK; } -char **init_mineboard(WINDOW *gameWin, int COLS, int ROWS, int NMINES) +char **init_mineboard(WINDOW *gamew, int COLS, int ROWS, int NMINES) { int i; char **mineboard = (char **)malloc(ROWS * sizeof(char *)); 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,68 +1,68 @@ #include "navigation.h" -void navigate(WINDOW *gameWin, char **mineboard, char *move, int *mboardXLoc, int *mboardYLoc) +void navigate(WINDOW *gamew, char **mineboard, char *move, int *mboardx, int *mboardy) { - int yMax, xMax; - static int yLoc = 1, xLoc = 2; - getmaxyx(gameWin, yMax, xMax); + int ymax, xmax; + static int y = 1, x = 2; + getmaxyx(gamew, ymax, xmax); - update_curs(gameWin, yLoc, xLoc); - *mboardXLoc = (xLoc-2)/3; - *mboardYLoc = yLoc-1; - mvprintw(1, 1, "Current position: (%d, %d) ", *mboardXLoc, *mboardYLoc); + update_curs(gamew, y, x); + *mboardx = (x-2)/3; + *mboardy = y-1; + mvprintw(1, 1, "Current position: (%d, %d) ", *mboardx, *mboardy); refresh(); - getmv(gameWin, move, &yLoc, &xLoc, yMax, xMax); + getmv(gamew, move, &y, &x, ymax, xmax); } -void getmv(WINDOW *gameWin, char *move, int *yLoc, int *xLoc, int yMax, int xMax) +void getmv(WINDOW *gamew, char *move, int *y, int *x, int ymax, int xmax) { - *move = wgetch(gameWin); + *move = wgetch(gamew); switch (*move) // vim keys support!! { case 'k': case 'K': case 'w': case 'W': - mvup(yLoc); + mvup(y); break; case 'j': case 'J': case 's': case 'S': - mvdown(yLoc, yMax); + mvdown(y, ymax); break; case 'h': case 'H': case 'a': case 'A': - mvleft(xLoc); + mvleft(x); break; case 'l': case 'L': case 'd': case 'D': - mvright(xLoc, xMax); + mvright(x, xmax); break; } } -void mvup(int *yLoc) +void mvup(int *y) { - (*yLoc)--; - if (*yLoc < 1) *yLoc = 1; + (*y)--; + if (*y < 1) *y = 1; } -void mvdown(int *yLoc, int yMax) +void mvdown(int *y, int ymax) { - (*yLoc)++; - if (*yLoc > yMax-2) *yLoc = yMax-2; + (*y)++; + if (*y > ymax-2) *y = ymax-2; } -void mvleft(int *xLoc) +void mvleft(int *x) { - *xLoc -= 3; - if (*xLoc < 2) *xLoc = 2; + *x -= 3; + if (*x < 2) *x = 2; } -void mvright(int *xLoc, int xMax) +void mvright(int *x, int xmax) { - *xLoc += 3; - if (*xLoc > xMax-3) *xLoc = xMax-3; + *x += 3; + if (*x > xmax-3) *x = xmax-3; } -void update_curs(WINDOW *gameWin, int yLoc, int xLoc) +void update_curs(WINDOW *gamew, int y, int x) { - wmove(gameWin, yLoc, xLoc); + wmove(gamew, y, x); } 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,39 +1,39 @@ #include "outputs.h" -void print_board(WINDOW *gameWin, char **board, int COLS, int ROWS) +void print_board(WINDOW *gamew, char **board, int COLS, int ROWS) { int i, j, x, y = 1; - print_grid(gameWin, ROWS, COLS); + print_grid(gamew, ROWS, COLS); for (i = 0; i < ROWS; i++) { x = 2; for (j = 0; j < COLS; j++) { - wattron(gameWin, A_BOLD); - mvwaddch(gameWin, y, x, board[i][j]); + wattron(gamew, A_BOLD); + mvwaddch(gamew, y, x, board[i][j]); x += 3; } y++; } - wrefresh(gameWin); - wattron(gameWin, A_BOLD); + wrefresh(gamew); + wattron(gamew, A_BOLD); } -void print_grid(WINDOW *gameWin, int ROWS, int COLS) +void print_grid(WINDOW *gamew, int ROWS, int COLS) { int i, j; for (i = 1; i <= ROWS; i++) { - wmove(gameWin, i, 1); + wmove(gamew, i, 1); for (j = 0; j < COLS; j++) - wprintw(gameWin, "[ ]"); + wprintw(gamew, "[ ]"); } - wrefresh(gameWin); + wrefresh(gamew); } void session_write(char **mineboard, int COLS, int ROWS, int hitRow, int hitCol, const char *status) @@ -118,26 +118,26 @@ void sort_scorelog(FILE *scoreLog) } -void game_won(WINDOW *gameWin, int yMiddle, int xMiddle) +void game_won(WINDOW *gamew, int ymid, int xmid) { - wclear(gameWin); - wrefresh(gameWin); + wclear(gamew); + wrefresh(gamew); wattron(stdscr, A_BOLD); - mvwprintw(stdscr, yMiddle-2, xMiddle-11, "You defused all the mines!"); - mvwprintw(stdscr, yMiddle-1, xMiddle-3, "You won :)"); - mvwprintw(stdscr, yMiddle, xMiddle-11, "Press any key to continue"); + 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"); refresh(); wattroff(stdscr, A_BOLD); } -void game_over(WINDOW *gameWin, char **mineboard, int yMiddle, int xMiddle) +void game_over(WINDOW *gamew, char **mineboard, int ymid, int xmid) { - wclear(gameWin); - wrefresh(gameWin); + wclear(gamew); + wrefresh(gamew); wattron(stdscr, A_BOLD); - mvwprintw(stdscr, yMiddle-2, xMiddle-24, "You hit a mine! (or tried to defuse the wrong cell)"); - mvwprintw(stdscr, yMiddle-1, xMiddle-4, "Game over :("); - mvwprintw(stdscr, yMiddle, xMiddle-11, "Press any key to continue"); + 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"); refresh(); wattroff(stdscr, 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,48 +1,48 @@ #include "settings.h" -int set_cols(WINDOW *menuWin, int xMax) +int set_cols(WINDOW *menuw, int xmax) { int COLS; do { - mvwprintw(menuWin, 1, 1, "Columns (Min = 5, Max = %d): ", (xMax-2)/3-2); - wrefresh(menuWin); + mvwprintw(menuw, 1, 1, "Columns (Min = 5, Max = %d): ", (xmax-2)/3-2); + wrefresh(menuw); scanw("%d", &COLS); - mvwprintw(menuWin, 1, COLS_CHAR_LENGTH, "%d", COLS); - wrefresh(menuWin); - } while (COLS < 5 || COLS > (xMax-2)/3-2); + mvwprintw(menuw, 1, COLS_CHAR_LENGTH, "%d", COLS); + wrefresh(menuw); + } while (COLS < 5 || COLS > (xmax-2)/3-2); return COLS; } -int set_rows(WINDOW *menuWin, int yMax) +int set_rows(WINDOW *menuw, int ymax) { int ROWS; do { - mvwprintw(menuWin, 2, 1, "Rows (Min = 5, Max = %d): ", yMax-14); - wrefresh(menuWin); + mvwprintw(menuw, 2, 1, "Rows (Min = 5, Max = %d): ", ymax-14); + wrefresh(menuw); scanw("%d", &ROWS); - mvwprintw(menuWin, 2, ROWS_CHAR_LENGTH, "%d", ROWS); - wrefresh(menuWin); - } while (ROWS < 5 || ROWS > yMax - 14); + mvwprintw(menuw, 2, ROWS_CHAR_LENGTH, "%d", ROWS); + wrefresh(menuw); + } while (ROWS < 5 || ROWS > ymax - 14); return ROWS; } -int set_nmines(WINDOW *menuWin, int DIMENSIONS) +int set_nmines(WINDOW *menuw, int DIMENSIONS) { int NMINES; do { - mvwprintw(menuWin, 3, 1, "Mines (Max = %d): ", DIMENSIONS-15); // -10 so the player has a chance to win - wrefresh(menuWin); + mvwprintw(menuw, 3, 1, "Mines (Max = %d): ", DIMENSIONS-15); // -10 so the player has a chance to win + wrefresh(menuw); scanw("%d", &NMINES); - mvwprintw(menuWin, 3, NMINES_CHAR_LENGTH, "%d", NMINES); - wrefresh(menuWin); + mvwprintw(menuw, 3, NMINES_CHAR_LENGTH, "%d", NMINES); + wrefresh(menuw); } 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 @@ -6,31 +6,31 @@ void main_win() noecho(); cbreak(); - WINDOW *mainWin = newwin(0, 0, 0, 0); - wattron(mainWin, A_BOLD); - box(mainWin, 0, 0); + WINDOW *mainw = newwin(0, 0, 0, 0); + wattron(mainw, A_BOLD); + box(mainw, 0, 0); refresh(); - wrefresh(mainWin); - wattroff(mainWin, A_BOLD); + wrefresh(mainw); + wattroff(mainw, A_BOLD); } -WINDOW *menu_win(int *yMax, int *xMax) +WINDOW *menu_win(int *ymax, int *xmax) { int numSettings = 3; - getmaxyx(stdscr, *yMax, *xMax); - WINDOW *menuWin = newwin(numSettings+2, *xMax-8, *yMax-8, 4); - wattron(menuWin, A_BOLD); - box(menuWin, 0, 0); - wrefresh(menuWin); - wattroff(menuWin, A_BOLD); - return menuWin; + getmaxyx(stdscr, *ymax, *xmax); + WINDOW *menuw = newwin(numSettings+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 winRows = ROWS+2; - int winCols = COLS*3+2; - WINDOW *gameWin = newwin(winRows, winCols, 2, 4); + 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); @@ -40,8 +40,8 @@ WINDOW *game_win(int COLS, int ROWS, int NMINES) void options_menu() { - 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"); + 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(); } 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,9 @@ -Mine hit at position (1, 1) +Mine hit at position (2, 3) Board overview -* 1 - - - -1 1 - - - -- - - - - - - - - - +1 1 1 - - +1 * 1 - - +1 1 1 - - - - - - - diff --git a/assignment-1.5-arrays-pointers-files/minecurses/txt/scorelog.txt b/assignment-1.5-arrays-pointers-files/minecurses/txt/scorelog.txt @@ -7,3 +7,5 @@ Player Mines Defused Dimensions ------------------------------------------------------ christos 1 5x5 christos 1 5x5 + +christos 1 5x5