commit 3daad01864d7fb395de14997afd0237b2d3c0176 parent 3ecf3e75f493c09cff5ad09ae4e8ac48758db59b Author: Christos Margiolis <christos@margiolis.net> Date: Wed, 22 Jan 2020 12:38:51 +0200 almost finished minesweeper Diffstat:
23 files changed, 79 insertions(+), 79 deletions(-)
diff --git a/assignment-1.5-arrays-pointers-files/README.md b/assignment-1.5-arrays-pointers-files/README.md @@ -9,6 +9,21 @@ * fcombinations * minesweeper +## Minesweeper + +![MinesweeperSCR](uni/uni-assignments/assignment-1.5-arrays-pointers-files/minesweeper/sample.png "MinesweeperSCR") + +### How to play + +* Enter number of columns +* Enter number of rows +* Enter number of mines +* Move with `w`/`s`/`a`/`d` or Vim keys (`k`/`j`/`h`/`l`) +* Open cell with `[ENTER]` or `o` +* Flag cell with `f` +* Defuse mine (if flagged) with `g` +* Quit with `q` + ## Execution * ```program_name``` = program's name diff --git a/assignment-1.5-arrays-pointers-files/combinations/bin/combs b/assignment-1.5-arrays-pointers-files/combinations/bin/combs Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/combinations/obj/arrhandler.o b/assignment-1.5-arrays-pointers-files/combinations/obj/arrhandler.o Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/combinations/obj/combinations.o b/assignment-1.5-arrays-pointers-files/combinations/obj/combinations.o Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/minesweeper/bin/minesweeper b/assignment-1.5-arrays-pointers-files/minesweeper/bin/minesweeper Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/minesweeper/include/gameplay.h b/assignment-1.5-arrays-pointers-files/minesweeper/include/gameplay.h @@ -4,7 +4,7 @@ #include "minesweeper.h" void play_minesweeper(struct _win_st*, char **, char **, int, int, int); -void transfer(char **, char **, int, int, int, int *); +void transfer(char **, char **, int, int); void reveal(struct _win_st*, char **, int, int, int, int); void flag_handler(); bool is_flagged(); diff --git a/assignment-1.5-arrays-pointers-files/minesweeper/include/outputs.h b/assignment-1.5-arrays-pointers-files/minesweeper/include/outputs.h @@ -5,7 +5,6 @@ void print_grid(struct _win_st*, int, int); void print_board(struct _win_st*, char **, int, int); - void game_won(struct _win_st*, int, int); void game_over(struct _win_st*, char **, int, int); void filewrite(char **, int, int, int, int, const char *); diff --git a/assignment-1.5-arrays-pointers-files/minesweeper/include/settings.h b/assignment-1.5-arrays-pointers-files/minesweeper/include/settings.h @@ -3,6 +3,9 @@ #include "minesweeper.h" +#define COLS_CHAR_LENGTH strlen("Columns (Max = XXX): ") + 1 +#define ROWS_CHAR_LENGTH strlen("Rows (Max = YYY): ") + 1 +#define NMINES_CHAR_LENGTH strlen("Mines (Max = MMMM): ") + 1 int set_cols(struct _win_st*, int); int set_rows(struct _win_st*, int); diff --git a/assignment-1.5-arrays-pointers-files/minesweeper/include/wins.h b/assignment-1.5-arrays-pointers-files/minesweeper/include/wins.h @@ -6,5 +6,7 @@ void main_win(); WINDOW *menu_win(int *, int *); WINDOW *game_win(int, int, int); +void options_menu(); +void options_win(); #endif diff --git a/assignment-1.5-arrays-pointers-files/minesweeper/man b/assignment-1.5-arrays-pointers-files/minesweeper/man diff --git a/assignment-1.5-arrays-pointers-files/minesweeper/mnsout.txt b/assignment-1.5-arrays-pointers-files/minesweeper/mnsout.txt @@ -1,34 +1,9 @@ -Mine hit at position (15, 24) +Last mine defused at position (1, 2) Board overview -- - - - - - - - - 1 1 1 - - - - - - 1 * * 1 - - - - - 1 1 1 -- - - - - - - - - 1 * 1 - 1 1 1 - - 1 2 2 1 - - - - - 1 * 1 -- - - - - - - - - 1 1 1 - 1 * 1 - - - - - - - - - 1 1 2 1 1 -- - - - - - - - - - - - - 1 1 1 - - - - - - - - - 1 * 1 - - -- - - - - - - - - - - - - - - - - - - - - 1 1 1 - 2 2 2 - - -- - - - - - - - - - - - - - - - - - - - - 1 * 1 - 1 * 1 - - -- - - - - - - - - - - - - - - - - - - - - 2 2 2 - 1 1 1 - - -- - - - - - - - - 1 1 1 - - - - - - - - - 1 * 1 - - 1 1 1 - -- 1 1 1 - - - - - 2 * 2 - - - - - - - - - 1 1 1 - - 1 * 1 - -- 1 * 1 - - - - - 2 * 2 - - - - - - - 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 2 1 - - - - - - - - - - - - - - - - - - - - - - - - -- - 1 * * 1 - 1 1 1 - - - - - - - - - - - - - - - - 1 1 1 - -- - 1 2 2 2 1 2 * 1 - - - - - - - - - - - - - - - - 1 * 1 - -- - - - - 1 * 2 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 - - +1 1 - - - +D 1 - - - +1 1 - - - +- - - - - +- - - - - diff --git a/assignment-1.5-arrays-pointers-files/minesweeper/obj/gameplay.o b/assignment-1.5-arrays-pointers-files/minesweeper/obj/gameplay.o Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/minesweeper/obj/navigation.o b/assignment-1.5-arrays-pointers-files/minesweeper/obj/navigation.o Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/minesweeper/obj/outputs.o b/assignment-1.5-arrays-pointers-files/minesweeper/obj/outputs.o Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/minesweeper/obj/settings.o b/assignment-1.5-arrays-pointers-files/minesweeper/obj/settings.o Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/minesweeper/obj/wins.o b/assignment-1.5-arrays-pointers-files/minesweeper/obj/wins.o Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/minesweeper/sample.png b/assignment-1.5-arrays-pointers-files/minesweeper/sample.png Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/minesweeper/src/gameplay.c b/assignment-1.5-arrays-pointers-files/minesweeper/src/gameplay.c @@ -3,7 +3,7 @@ void play_minesweeper(WINDOW *gameWin, char **dispboard, char **mineboard, int COLS, int ROWS, int NMINES) { int mboardXLoc = 0, mboardYLoc = 0; - bool gameOver = false; + bool gameOver = false, cantFlag = false; int numDefused = 0, numFlagged = 0; int yMax, xMax, yMiddle, xMiddle; char move; @@ -15,14 +15,39 @@ void play_minesweeper(WINDOW *gameWin, char **dispboard, char **mineboard, int C { navigate(gameWin, dispboard, &move, &mboardXLoc, &mboardYLoc); - if (move == '\n') + if (move == '\n' || move == 'o' || move == 'O') // handle cell opening { - transfer(dispboard, mineboard, mboardYLoc, mboardXLoc, NMINES, &numDefused); + transfer(dispboard, mineboard, mboardYLoc, mboardXLoc); reveal(gameWin, dispboard, mboardYLoc, mboardXLoc, mboardYLoc + 1, 3*mboardXLoc + 2); + //cantFlag = true; if (dispboard[mboardYLoc][mboardXLoc] == MINE) gameOver = true; } - } while (((mboardYLoc >= 0 && mboardYLoc < COLS) && (mboardXLoc >= 0 && mboardXLoc < ROWS)) - && numDefused < NMINES && !gameOver && move != 'q'); + else if (move == 'f' || move == 'F') // handle falgs + { + if (dispboard[mboardYLoc][mboardXLoc] == 'F') // undo flag + dispboard[mboardYLoc][mboardXLoc] = ' '; + else + dispboard[mboardYLoc][mboardXLoc] = 'F'; + // handle already defused mine + reveal(gameWin, dispboard, mboardYLoc, mboardXLoc, mboardYLoc + 1, 3*mboardXLoc + 2); + } + else if (move == 'g' || move == 'G') // check for defuse + { + if (dispboard[mboardYLoc][mboardXLoc] == 'F' && mineboard[mboardYLoc][mboardXLoc] == MINE) // is_mine replace + { + numDefused++; + refresh(); + dispboard[mboardYLoc][mboardXLoc] = mineboard[mboardYLoc][mboardXLoc] = 'D'; + reveal(gameWin, dispboard, mboardYLoc, mboardXLoc, mboardYLoc + 1, 3*mboardXLoc + 2); + } + // handle false defusal + // handle already defused mine + } + + mvprintw(1, xMiddle-8, "Defused mines: %d/%d", numDefused, NMINES); + + } while (((mboardYLoc >= 0 && mboardYLoc < COLS) && (mboardXLoc >= 0 && mboardXLoc < ROWS)) && + numDefused < NMINES && !gameOver && move != 'q'); if (gameOver == true) { @@ -41,7 +66,7 @@ void play_minesweeper(WINDOW *gameWin, char **dispboard, char **mineboard, int C } -void transfer(char **dispboard, char **mineboard, int mboardYLoc, int mboardXLoc, int NMINES, int *numDefused) +void transfer(char **dispboard, char **mineboard, int mboardYLoc, int mboardXLoc) { dispboard[mboardYLoc][mboardXLoc] = mineboard[mboardYLoc][mboardXLoc]; } diff --git a/assignment-1.5-arrays-pointers-files/minesweeper/src/navigation.c b/assignment-1.5-arrays-pointers-files/minesweeper/src/navigation.c @@ -37,10 +37,6 @@ void getmv(WINDOW *gameWin, char *move, int *yLoc, int *xLoc, int yMax, int xMax case 'd': case 'D': mvright(yLoc, xLoc, yMax, xMax); break; - case 'f': case 'F': - break; - case '\n': - break; default: break; } } diff --git a/assignment-1.5-arrays-pointers-files/minesweeper/src/outputs.c b/assignment-1.5-arrays-pointers-files/minesweeper/src/outputs.c @@ -11,6 +11,7 @@ void print_board(WINDOW *gameWin, char **board, int COLS, int ROWS) x = 2; for (j = 0; j < COLS; j++) { + wattron(gameWin, A_BOLD); mvwaddch(gameWin, y, x, board[i][j]); x += 3; } @@ -18,6 +19,7 @@ void print_board(WINDOW *gameWin, char **board, int COLS, int ROWS) } wrefresh(gameWin); + wattron(gameWin, A_BOLD); } @@ -35,7 +37,7 @@ void print_grid(WINDOW *gameWin, int ROWS, int COLS) wrefresh(gameWin); } -// fix + void filewrite(char **mineboard, int COLS, int ROWS, int hitRow, int hitCol, const char *status) { int i, j; @@ -73,10 +75,12 @@ void game_won(WINDOW *gameWin, int yMiddle, int xMiddle) { wclear(gameWin); wrefresh(gameWin); - mvwprintw(stdscr, yMiddle-2, xMiddle-7, "You defused all the mines!"); - mvwprintw(stdscr, yMiddle-1, xMiddle-6, "You won :)"); + 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"); refresh(); + wattroff(stdscr, A_BOLD); } @@ -84,8 +88,10 @@ void game_over(WINDOW *gameWin, char **mineboard, int yMiddle, int xMiddle) { wclear(gameWin); wrefresh(gameWin); + wattron(stdscr, A_BOLD); mvwprintw(stdscr, yMiddle-2, xMiddle-6, "You hit a mine!"); mvwprintw(stdscr, yMiddle-1, xMiddle-4, "Game over :("); mvwprintw(stdscr, yMiddle, xMiddle-11, "Press any key to continue"); refresh(); + wattroff(stdscr, A_BOLD); } \ No newline at end of file diff --git a/assignment-1.5-arrays-pointers-files/minesweeper/src/settings.c b/assignment-1.5-arrays-pointers-files/minesweeper/src/settings.c @@ -9,7 +9,7 @@ int set_cols(WINDOW *menuWin, int xMax) mvwprintw(menuWin, 1, 1, "Columns (Max = %d): ", (xMax-2)/3 - 2); wrefresh(menuWin); scanw("%d", &COLS); - mvwprintw(menuWin, 1, strlen("Columns (Max = XXX): ") + 1, "%d", COLS); + mvwprintw(menuWin, 1, COLS_CHAR_LENGTH, "%d", COLS); wrefresh(menuWin); } while (COLS < 5 || COLS > (xMax-2)/3 - 2); @@ -26,7 +26,7 @@ int set_rows(WINDOW *menuWin, int yMax) mvwprintw(menuWin, 2, 1, "Rows (Max = %d): ", yMax-14); wrefresh(menuWin); scanw("%d", &ROWS); - mvwprintw(menuWin, 2, strlen("Rows (Max = YYY): ") + 1, "%d", ROWS); + mvwprintw(menuWin, 2, ROWS_CHAR_LENGTH, "%d", ROWS); wrefresh(menuWin); } while (ROWS < 5 || ROWS > yMax - 14); @@ -43,15 +43,9 @@ int set_nmines(WINDOW *menuWin, int DIMENSIONS) mvwprintw(menuWin, 3, 1, "Mines (Max = %d): ", DIMENSIONS-10); // -10 so the player has a chance to win wrefresh(menuWin); scanw("%d", &NMINES); - mvwprintw(menuWin, 3, strlen("Mines (Max = MMMM): ") + 1, "%d", NMINES); + mvwprintw(menuWin, 3, NMINES_CHAR_LENGTH, "%d", NMINES); wrefresh(menuWin); } while (NMINES < 1 || NMINES > DIMENSIONS-10); return NMINES; -} - - -void init_colors() -{ - } \ No newline at end of file diff --git a/assignment-1.5-arrays-pointers-files/minesweeper/src/wins.c b/assignment-1.5-arrays-pointers-files/minesweeper/src/wins.c @@ -7,9 +7,11 @@ void main_win() cbreak(); WINDOW *mainWin = newwin(0, 0, 0, 0); + wattron(mainWin, A_BOLD); box(mainWin, 0, 0); refresh(); wrefresh(mainWin); + wattroff(mainWin, A_BOLD); } @@ -17,9 +19,11 @@ WINDOW *menu_win(int *yMax, int *xMax) { int numSettings = 3; getmaxyx(stdscr, *yMax, *xMax); - WINDOW *menuWin = newwin(numSettings+2, *xMax-8, *yMax-7, 4); + 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; } @@ -29,8 +33,10 @@ 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); + wattron(gameWin, A_BOLD); box(gameWin, 0, 0); wrefresh(gameWin); + wattroff(gameWin, A_BOLD); return gameWin; } @@ -38,6 +44,7 @@ WINDOW *game_win(int COLS, int ROWS, int NMINES) void options_menu() { int yMax = getmaxy(stdscr); - mvprintw(yMax-2, 1, "q Quit m Manual"); // bold letters + 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)"); refresh(); } \ No newline at end of file diff --git a/assignment-1.5-arrays-pointers-files/minesweeper/todo.md b/assignment-1.5-arrays-pointers-files/minesweeper/todo.md @@ -1,21 +0,0 @@ -* limits -* resizing -* ```selection()``` -* ```transfer()``` -* ```reveal()``` -* ```is_mine()``` -* ```game_over()``` -* Game over menu - * Press key to continue - * Dialog menu - -* Export to text - - * Get file name - * Write to file - -```c -pin = (char **)malloc(M*sizeof(char *)); -for (int i = 0; i < M; i++) - *(pin + i) = (char *)malloc(N); -```- \ No newline at end of file