uni

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

commit 65762ec7ae187d50a8e5c95be154fe8063cb2f6b
parent ec304a1cd62337ba25cb14f2e7aaac100a2434f6
Author: Christos Margiolis <christos@margiolis.net>
Date:   Fri, 24 Apr 2020 06:00:10 +0300

updated submodule

Diffstat:
Massignment-1.5-arrays-pointers-files/minecurses/README.md | 3++-
Massignment-1.5-arrays-pointers-files/minecurses/bin/minecurses | 0
Massignment-1.5-arrays-pointers-files/minecurses/log/scorelog.csv | 1+
Massignment-1.5-arrays-pointers-files/minecurses/log/session.txt | 8++++----
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/navigation.o | 0
Massignment-1.5-arrays-pointers-files/minecurses/obj/outputs.o | 0
Massignment-1.5-arrays-pointers-files/minecurses/obj/wins.o | 0
Massignment-1.5-arrays-pointers-files/minecurses/src/audio.c | 42+++++++++++++++++++++---------------------
Massignment-1.5-arrays-pointers-files/minecurses/src/audio.h | 1-
Massignment-1.5-arrays-pointers-files/minecurses/src/gameplay.c | 108++++++++++++++++++++++++++++++++++++++++----------------------------------------
Massignment-1.5-arrays-pointers-files/minecurses/src/gameplay.h | 42+++++++++++++++++++++++++++++++-----------
Massignment-1.5-arrays-pointers-files/minecurses/src/main.c | 36+++++++++++++++++++++++++++---------
Aassignment-1.5-arrays-pointers-files/minecurses/src/main.h | 20++++++++++++++++++++
Massignment-1.5-arrays-pointers-files/minecurses/src/minesweeper.c | 110++++++++++++++++++++++++++++++++++++++++----------------------------------------
Massignment-1.5-arrays-pointers-files/minecurses/src/minesweeper.h | 42++++++++----------------------------------
Massignment-1.5-arrays-pointers-files/minecurses/src/navigation.c | 72++++++++++++++++++++++++++++++++++++------------------------------------
Massignment-1.5-arrays-pointers-files/minecurses/src/navigation.h | 2+-
Massignment-1.5-arrays-pointers-files/minecurses/src/outputs.c | 192++++++++++++++++++++++++++++++++++++++++----------------------------------------
Massignment-1.5-arrays-pointers-files/minecurses/src/outputs.h | 15++++++++-------
Massignment-1.5-arrays-pointers-files/minecurses/src/settings.c | 32++++++++++++++++----------------
Massignment-1.5-arrays-pointers-files/minecurses/src/settings.h | 6+++---
Massignment-1.5-arrays-pointers-files/minecurses/src/wins.c | 16++++++++--------
Massignment-1.5-arrays-pointers-files/minecurses/src/wins.h | 4++--
26 files changed, 393 insertions(+), 359 deletions(-)

diff --git a/assignment-1.5-arrays-pointers-files/minecurses/README.md b/assignment-1.5-arrays-pointers-files/minecurses/README.md @@ -34,7 +34,8 @@ Additional controls * Add colors * Add UTF-8 support * Fix wrong mine spawning bug -* (Perhaps) pack board info into a `struct` +* ~~Pack board info into a `struct`~~ +* Fix `delwin(gamew)` bug ## 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/scorelog.csv b/assignment-1.5-arrays-pointers-files/minecurses/log/scorelog.csv @@ -17,3 +17,4 @@ christos,1,5x5 christos,1,5x5 christos,1,5x5 christos,1,5x5 +christos,1,5x5 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, 1) +Last mine defused at position (2, 2) Board overview -- - 1 D 1 -- - 1 1 1 -- - - - - +1 1 1 - - +1 D 1 - - +1 1 1 - - - - - - - - - - - - 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/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/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 @@ -2,33 +2,33 @@ void *play_audio(void *threadid) { - /*int tid = (long)threadid;*/ - Mix_Music *music = NULL; - SDL_Init(SDL_INIT_AUDIO); - Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 1, 4096); - music = Mix_LoadMUS(AUDIO_PATH); + /*int tid = (long)threadid;*/ + Mix_Music *music = NULL; + SDL_Init(SDL_INIT_AUDIO); + Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 1, 4096); + music = Mix_LoadMUS(AUDIO_PATH); - Mix_PlayMusic(music, -1); - while (Mix_PlayingMusic()) ; - Mix_FreeMusic(music); - Mix_CloseAudio(); + Mix_PlayMusic(music, -1); + while (Mix_PlayingMusic()) ; + Mix_FreeMusic(music); + Mix_CloseAudio(); } void volume(char option) { - static int volume = MIX_MAX_VOLUME; + static int volume = MIX_MAX_VOLUME; - switch (option) - { - case '+': - if (volume == MIX_MAX_VOLUME) break; - else Mix_VolumeMusic(volume += 10); - break; - case '-': - if (volume == 0) break; - else Mix_VolumeMusic(volume -= 10); - break; - } + switch (option) + { + case '+': + if (volume == MIX_MAX_VOLUME) break; + else Mix_VolumeMusic(volume += 10); + break; + case '-': + if (volume == 0) break; + else Mix_VolumeMusic(volume -= 10); + break; + } } void pause_audio(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 @@ -3,7 +3,6 @@ #include <SDL2/SDL.h> #include <SDL2/SDL_mixer.h> -#include "minesweeper.h" #define AUDIO_PATH "res/detective-8bit.wav" 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,39 +1,39 @@ #include "gameplay.h" -void play_minesweeper(WINDOW *gamew, char **dboard, char **mboard, int COLS, int ROWS, int NMINES) +void play_minesweeper(WINDOW *gamew, Board *brd) { - int mbx = 0, mby = 0; - int gameover = FALSE; - int ndefused = 0; - int ymid, xmid; - char move; - ymid = YMAX/2; - xmid = XMAX/2; + int mbx = 0, mby = 0; + int gameover = FALSE; + int ndefused = 0; + int ymid, xmid; + char move; + ymid = YMAX/2; + xmid = XMAX/2; - do - { + do + { erase(); delwin(gamew); refresh(); - gamew = game_win(COLS, ROWS, NMINES); - print_board(gamew, dboard, COLS, ROWS); - session_info(mbx, mby, xmid, ndefused, NMINES); - navigate(gamew, dboard, &move, &mbx, &mby); + gamew = game_win(brd->rows, brd->cols); + print_board(gamew, brd); + session_info(mbx, mby, xmid, ndefused, brd->nmines); + navigate(gamew, &move, &mbx, &mby); switch (move) { case ENTER: case OPEN_LOWER: case OPEN_UPPER: - gameover = open_cell(gamew, dboard, mboard, mby, mbx, gameover); + gameover = open_cell(gamew, brd, mby, mbx, gameover); break; case FLAG_LOWER: case FLAG_UPPER: - handle_flags(gamew, dboard, mboard, mby, mbx); + handle_flags(gamew, brd, mby, mbx); break; case DEFUSE_LOWER: case DEFUSE_UPPER: - if (dboard[mby][mbx] == FLAG && mboard[mby][mbx] == MINE) + if (brd->db[mby][mbx] == FLAG && brd->mb[mby][mbx] == MINE) { ndefused++; - defuse_mine(gamew, dboard, mboard, mby, mbx); + defuse_mine(gamew, brd, mby, mbx); } - else if (dboard[mby][mbx] == FLAG && mboard[mby][mbx] != MINE) gameover = TRUE; + else if (brd->db[mby][mbx] == FLAG && brd->mb[mby][mbx] != MINE) gameover = TRUE; break; case PAUSE_AUDIO: pause_audio(); @@ -42,81 +42,81 @@ void play_minesweeper(WINDOW *gamew, char **dboard, char **mboard, int COLS, int volume(move); break; case 'm': - handle_menu(gamew, dboard, COLS, ROWS); + handle_menu(gamew, brd); break; default: break; } - } while (((mby >= 0 && mby < ROWS) && (mbx >= 0 && mbx < COLS)) && - ndefused < NMINES && !gameover && move != QUIT); + } while (((mby >= 0 && mby < brd->rows) && (mbx >= 0 && mbx < brd->cols)) && + ndefused < brd->nmines && !gameover && move != QUIT); - 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); + if (gameover) + handle_gameover(gamew, brd, ymid, xmid, mby, mbx); + if (ndefused == brd->nmines) + handle_win(gamew, brd, ymid, xmid, mby, mbx, ndefused); } -int open_cell(WINDOW *gamew, char **dboard, char **mboard, int mby, int mbx, int gameover) +int open_cell(WINDOW *gamew, Board *brd, 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; + transfer(brd, mby, mbx); + reveal(gamew, brd, mby, mbx, mby+1, 3*mbx+2); + if (brd->db[mby][mbx] == MINE) gameover = TRUE; return gameover; } -void handle_flags(WINDOW *gamew, char **dboard, char **mboard, int mby, int mbx) +void handle_flags(WINDOW *gamew, Board *brd, int mby, int mbx) { - 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); + 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); } -void defuse_mine(WINDOW *gamew, char **dboard, char **mboard, int mby, int mbx) +void defuse_mine(WINDOW *gamew, Board *brd, int mby, int mbx) { refresh(); - dboard[mby][mbx] = mboard[mby][mbx] = DEFUSED; - reveal(gamew, dboard, mby, mbx, mby+1, 3*mbx+2); + brd->db[mby][mbx] = brd->mb[mby][mbx] = DEFUSED; + reveal(gamew, brd, mby, mbx, mby+1, 3*mbx+2); } -void transfer(char **dboard, char **mboard, int mby, int mbx) +void transfer(Board *brd, int mby, int mbx) { - dboard[mby][mbx] = mboard[mby][mbx]; + brd->db[mby][mbx] = brd->mb[mby][mbx]; } -void reveal(WINDOW *gamew, char **dboard, int mby, int mbx, int y, int x) +void reveal(WINDOW *gamew, Board *brd, int mby, int mbx, int y, int x) { - mvwaddch(gamew, y, x, dboard[mby][mbx]); - wrefresh(gamew); + mvwaddch(gamew, y, x, brd->db[mby][mbx]); + wrefresh(gamew); } -int is_defused(char **dboard, char **mboard, int mby, int mbx) +int is_defused(Board *brd, int mby, int mbx) { - return ((dboard[mby][mbx] == DEFUSED)) ? TRUE : FALSE; + return (brd->db[mby][mbx] == DEFUSED) ? TRUE : FALSE; } -void handle_menu(WINDOW *gamew, char **dboard, int COLS, int ROWS) +void handle_menu(WINDOW *gamew, Board *brd) { options_menu(); box(gamew, 0, 0); - print_board(gamew, dboard, COLS, ROWS); + print_board(gamew, brd); } -void handle_gameover(WINDOW *gamew, char **mboard, int ymid, int xmid, int COLS, int ROWS, int mby, int mbx) +void handle_gameover(WINDOW *gamew, Board *brd, int ymid, int xmid, int mby, int mbx) { - game_over(gamew, mboard, ymid, xmid); + game_over(gamew, ymid, xmid); getchar(); erase(); refresh(); box(gamew, 0, 0); - print_board(gamew, mboard, COLS, ROWS); + print_board(gamew, brd); wrefresh(gamew); - session_write(mboard, COLS, ROWS, mbx, mby, "lost"); + session_write(brd, mbx, mby, "lost"); } -void handle_win(WINDOW *gamew, char **mboard, int ymid, int xmid, int COLS, int ROWS, int mby, int mbx, int ndefused) +void handle_win(WINDOW *gamew, Board *brd, int ymid, int xmid, int mby, int mbx, int ndefused) { game_won(gamew, ymid, xmid); getchar(); - session_write(mboard, COLS, ROWS, mbx, mby, "won"); - score_write(ndefused, COLS, ROWS); + session_write(brd, mbx, mby, "won"); + score_write(ndefused, brd->rows, brd->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 @@ -1,17 +1,37 @@ #ifndef GAMEPLAY_H #define GAMEPLAY_H -#include "minesweeper.h" +#include "audio.h" +#include "main.h" +#include "navigation.h" +#include "outputs.h" +#include "wins.h" -void play_minesweeper (struct _win_st* gamew, char **dboard, char **mboard, int COLS, int ROWS, int NMINES); -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); -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); +#define BLANK ' ' +#define MINE '*' +#define ENTER '\n' +#define OPEN_LOWER 'o' +#define OPEN_UPPER 'O' +#define FLAG 'F' +#define FLAG_LOWER 'f' +#define FLAG_UPPER 'F' +#define DEFUSE_LOWER 'g' +#define DEFUSE_UPPER 'G' +#define DEFUSED 'D' +#define PAUSE_AUDIO 'p' +#define VOLUME_UP '+' +#define VOLUME_DOWN '-' +#define QUIT 'q' + +void play_minesweeper (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); #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 @@ -1,30 +1,48 @@ +#include "audio.h" +#include "gameplay.h" +#include "main.h" #include "minesweeper.h" +#include "settings.h" +#include "wins.h" #include <pthread.h> +static void clear_board(Board *brd) +{ + int i; + for (i = 0; i < brd->rows; i++) + { + free(brd->db[i]); + free(brd->mb[i]); + } + free(brd->db); + free(brd->mb); +} + int main(int argc, char **argv) { + Board brd; init_curses(); echo(); - int COLS = set_cols(); - int ROWS = set_rows(); - int NMINES = set_nmines(COLS*ROWS); + brd.cols = set_cols(); + brd.rows = set_rows(); + brd.nmines = set_nmines(brd.rows * brd.cols); noecho(); options_menu(); erase(); refresh(); - WINDOW *gamew = game_win(COLS, ROWS, NMINES); - char **dboard = init_dboard(gamew, COLS, ROWS); - char **mboard = init_mboard(gamew, COLS, ROWS, NMINES); + WINDOW *gamew = game_win(brd.rows, brd.cols); + brd.db = init_dboard(gamew, brd.cols, brd.rows); + brd.mb = init_mboard(gamew, brd.cols, brd.rows, brd.nmines); pthread_t audiothread; long threadid = 1; pthread_create(&audiothread, NULL, play_audio, (void *)threadid); - play_minesweeper(gamew, dboard, mboard, COLS, ROWS, NMINES); + play_minesweeper(gamew, &brd); pthread_cancel(audiothread); - free(dboard); - free(mboard); + clear_board(&brd); + delwin(gamew); endwin(); return 0; diff --git a/assignment-1.5-arrays-pointers-files/minecurses/src/main.h b/assignment-1.5-arrays-pointers-files/minecurses/src/main.h @@ -0,0 +1,20 @@ +#ifndef MAIN_H +#define MAIN_H + +#if defined linux || defined __unix__ || defined __APPLE__ +#include <ncurses.h> +#endif + +#include <stdlib.h> + +#define YMAX getmaxy(stdscr) +#define XMAX getmaxx(stdscr) + +typedef struct +{ + char **db; + char **mb; + int rows, cols, nmines; +} Board; + +#endif 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,11 +1,11 @@ #include "minesweeper.h" -char **init_dboard(WINDOW *gamew, int COLS, int ROWS) +char **init_dboard(WINDOW *gamew, int cols, int rows) { int i; - char **dboard = (char **)malloc(ROWS * sizeof(char *)); - for (i = 0; i < ROWS; i++) - dboard[i] = (char *)malloc(COLS); + char **dboard = (char **)malloc(rows * sizeof(char *)); + for (i = 0; i < rows; i++) + dboard[i] = (char *)malloc(cols); if (dboard == NULL) { @@ -13,25 +13,25 @@ char **init_dboard(WINDOW *gamew, int COLS, int ROWS) refresh(); exit(EXIT_FAILURE); } - else fill_dboard(dboard, COLS, ROWS); + else fill_dboard(dboard, cols, rows); return dboard; } -void fill_dboard(char **dboard, 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; + int i, j; + for (i = 0; i < rows; i++) + for (j = 0; j < cols; j++) + dboard[i][j] = BLANK; } -char **init_mboard(WINDOW *gamew, int COLS, int ROWS, int NMINES) +char **init_mboard(WINDOW *gamew, int cols, int rows, int nmines) { int i; - char **mboard = (char **)malloc(ROWS * sizeof(char *)); - for (i = 0; i < ROWS; i++) - mboard[i] = (char *)malloc(COLS); + char **mboard = (char **)malloc(rows * sizeof(char *)); + for (i = 0; i < rows; i++) + mboard[i] = (char *)malloc(cols); if (mboard == NULL) { @@ -39,68 +39,68 @@ char **init_mboard(WINDOW *gamew, int COLS, int ROWS, int NMINES) refresh(); exit(EXIT_FAILURE); } - else - { - place_mines(mboard, COLS, ROWS, NMINES); - add_adj(mboard, COLS, ROWS); - fill_spaces(mboard, COLS, ROWS, NMINES); - } + else + { + place_mines(mboard, cols, rows, nmines); + add_adj(mboard, cols, rows); + fill_spaces(mboard, cols, rows, nmines); + } return mboard; } -void place_mines(char **mboard, 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; - hrand = rand() % COLS; - mboard[wrand][hrand] = MINE; - } + int i, wrand, hrand; + srand(time(NULL)); + for (i = 0; i < nmines; i++) + { + wrand = rand() % rows; + hrand = rand() % cols; + mboard[wrand][hrand] = MINE; + } } -void add_adj(char **mboard, int COLS, int ROWS) +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'; + 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'; } int is_mine(char **mboard, int row, int col) { - return (mboard[row][col] == MINE) ? TRUE : FALSE; + return (mboard[row][col] == MINE) ? true : false; } -int 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; } -uint8_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; + int8_t nadj = 0; - if (!outof_bounds(row, col-1, COLS, ROWS) && mboard[row][col-1] == MINE) nadj++; // North - if (!outof_bounds(row, col+1, COLS, ROWS) && mboard[row][col+1] == MINE) nadj++; // South - if (!outof_bounds(row+1, col, COLS, ROWS) && mboard[row+1][col] == MINE) nadj++; // East - if (!outof_bounds(row-1, col, COLS, ROWS) && mboard[row-1][col] == MINE) nadj++; // West - if (!outof_bounds(row+1, col-1, COLS, ROWS) && mboard[row+1][col-1] == MINE) nadj++; // North-East - if (!outof_bounds(row-1, col-1, COLS, ROWS) && mboard[row-1][col-1] == MINE) nadj++; // North-West - if (!outof_bounds(row+1, col+1, COLS, ROWS) && mboard[row+1][col+1] == MINE) nadj++; // South-East - if (!outof_bounds(row-1, col+1, COLS, ROWS) && mboard[row-1][col+1] == MINE) nadj++; // South-West + if (!outof_bounds(row, col-1, cols, rows) && mboard[row][col-1] == MINE) nadj++; // north + if (!outof_bounds(row, col+1, cols, rows) && mboard[row][col+1] == MINE) nadj++; // south + if (!outof_bounds(row+1, col, cols, rows) && mboard[row+1][col] == MINE) nadj++; // east + if (!outof_bounds(row-1, col, cols, rows) && mboard[row-1][col] == MINE) nadj++; // west + if (!outof_bounds(row+1, col-1, cols, rows) && mboard[row+1][col-1] == MINE) nadj++; // north-east + if (!outof_bounds(row-1, col-1, cols, rows) && mboard[row-1][col-1] == MINE) nadj++; // north-west + if (!outof_bounds(row+1, col+1, cols, rows) && mboard[row+1][col+1] == MINE) nadj++; // south-east + if (!outof_bounds(row-1, col+1, cols, rows) && mboard[row-1][col+1] == MINE) nadj++; // south-west - return nadj; + return nadj; } -void fill_spaces(char **mboard, int COLS, int ROWS, int NMINES) +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') - mboard[i][j] = '-'; + int i, j; + for (i = 0; i < rows; i++) + for (j = 0; j < cols; j++) + if (mboard[i][j] != MINE && mboard[i][j] == '0') + mboard[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 @@ -1,49 +1,23 @@ #ifndef MINESWEEPER_H #define MINESWEEPER_H -#if defined linux || defined __unix__ || defined __APPLE__ -#include <ncurses.h> -#endif - #include <stdlib.h> #include <string.h> #include <time.h> -#include "audio.h" -#include "gameplay.h" -#include "navigation.h" -#include "outputs.h" -#include "settings.h" #include "wins.h" #define BLANK ' ' #define MINE '*' -#define CLEAR " " -#define ENTER '\n' -#define OPEN_LOWER 'o' -#define OPEN_UPPER 'O' -#define FLAG 'F' -#define FLAG_LOWER 'f' -#define FLAG_UPPER 'F' -#define DEFUSE_LOWER 'g' -#define DEFUSE_UPPER 'G' -#define DEFUSED 'D' -#define PAUSE_AUDIO 'p' -#define VOLUME_UP '+' -#define VOLUME_DOWN '-' -#define QUIT 'q' - -#define YMAX getmaxy(stdscr) -#define XMAX getmaxx(stdscr) -char **init_dboard (struct _win_st* gamew, int COLS, int ROWS); -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); +char **init_dboard (struct _win_st* gamew, int cols, int rows); +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); 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); +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/src/navigation.c b/assignment-1.5-arrays-pointers-files/minecurses/src/navigation.c @@ -1,66 +1,66 @@ #include "navigation.h" -void navigate(WINDOW *gamew, char **mboard, char *move, int *mbx, int *mby) +void navigate(WINDOW *gamew, char *move, int *mbx, int *mby) { int ymax, xmax; getmaxyx(gamew, ymax, xmax); - static int y = 1, x = 2; - update_curs(gamew, y, x); - *mbx = (x-2)/3; - *mby = y-1; - refresh(); - getmv(gamew, move, &y, &x, ymax, xmax); + static int y = 1, x = 2; + update_curs(gamew, y, x); + *mbx = (x-2)/3; + *mby = y-1; + refresh(); + getmv(gamew, move, &y, &x, ymax, xmax); } void getmv(WINDOW *gamew, char *move, int *y, int *x, int ymax, int xmax) { - *move = wgetch(gamew); - switch (*move) // vim keys support!! - { - case 'k': case 'K': - case 'w': case 'W': - mvup(y); - break; - case 'j': case 'J': - case 's': case 'S': - mvdown(y, ymax); - break; - case 'h': case 'H': - case 'a': case 'A': - mvleft(x); - break; - case 'l': case 'L': - case 'd': case 'D': - mvright(x, xmax); - break; - } + *move = wgetch(gamew); + switch (*move) // vim keys support!! + { + case 'k': case 'K': + case 'w': case 'W': + mvup(y); + break; + case 'j': case 'J': + case 's': case 'S': + mvdown(y, ymax); + break; + case 'h': case 'H': + case 'a': case 'A': + mvleft(x); + break; + case 'l': case 'L': + case 'd': case 'D': + mvright(x, xmax); + break; + } } void mvup(int *y) { - (*y)--; - if (*y < 1) *y = 1; + (*y)--; + if (*y < 1) *y = 1; } void mvdown(int *y, int ymax) { - (*y)++; - if (*y > ymax-2) *y = ymax-2; + (*y)++; + if (*y > ymax-2) *y = ymax-2; } void mvleft(int *x) { - *x -= 3; - if (*x < 2) *x = 2; + *x -= 3; + if (*x < 2) *x = 2; } void mvright(int *x, int xmax) { - *x += 3; - if (*x > xmax-3) *x = xmax-3; + *x += 3; + if (*x > xmax-3) *x = xmax-3; } void update_curs(WINDOW *gamew, int y, int x) { - wmove(gamew, y, x); + wmove(gamew, 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 @@ -3,7 +3,7 @@ #include "minesweeper.h" -void navigate (struct _win_st* gamew, char **mboard, char *move, int *mbx, int *mby); +void navigate (struct _win_st* gamew, char *move, int *mbx, int *mby); void getmv (struct _win_st* gamew, char *move, int *y, int *x, int ymax, int xmax); void mvup (int *y); void mvdown (int *y, int ymax); 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,106 +1,106 @@ #include "outputs.h" -void print_board(WINDOW *gamew, char **dboard, int COLS, int ROWS) +void print_board(WINDOW *gamew, Board *brd) { - int i, j, x, y = 1; - print_grid(gamew, ROWS, COLS); + int i, j, x, y = 1; + print_grid(gamew, brd->rows, brd->cols); wattron(gamew, A_BOLD); - for (i = 0; i < ROWS; i++) - { - x = 2; - for (j = 0; j < COLS; j++) - { - mvwaddch(gamew, y, x, dboard[i][j]); - x += 3; - } - y++; - } + for (i = 0; i < brd->rows; i++) + { + x = 2; + for (j = 0; j < brd->cols; j++) + { + mvwaddch(gamew, y, x, brd->db[i][j]); + x += 3; + } + y++; + } } -void print_grid(WINDOW *gamew, int ROWS, int COLS) +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); + 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 xmid, int ndefused, int NMINES) +void session_info(int mbx, int mby, 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, 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) +void session_write(Board *brd, int hitrow, int hitcol, const char *status) { - int i, j; - FILE *fsession = fopen(SESSION_PATH, "w"); - if (fsession == NULL) - { - mvprintw(0, 0, "Error opening file, exiting..."); - refresh(); - exit(EXIT_FAILURE); - } - else - { - strcmp(status, "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, "Board overview\n\n"); - for (i = 0; i < ROWS; i++) - { - for (j = 0; j < COLS; j++) - fprintf(fsession, "%c ", mboard[i][j]); - fprintf(fsession, "\n"); - } - } - fclose(fsession); + int i, j; + FILE *fsession = fopen(SESSION_PATH, "w"); + if (fsession == NULL) + { + mvprintw(0, 0, "Error opening file, exiting..."); + refresh(); + exit(EXIT_FAILURE); + } + else + { + strcmp(status, "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, "Board overview\n\n"); + for (i = 0; i < brd->rows; i++) + { + for (j = 0; j < brd->cols; j++) + fprintf(fsession, "%c ", brd->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(); - if (scorelog == NULL) - { - mvprintw(0, 0, "Error opening file, exiting..."); - refresh(); - exit(EXIT_FAILURE); - } - else - { - fprintf(scorelog, "%s,%d,%dx%d\n", playername, ndefused, COLS, ROWS); - sort_scorelog(scorelog); // pending + FILE *scorelog = fopen(SCORE_LOG_PATH, "a"); + char *playername = get_pname(); + if (scorelog == NULL) + { + mvprintw(0, 0, "Error opening file, exiting..."); + refresh(); + exit(EXIT_FAILURE); + } + else + { + fprintf(scorelog, "%s,%d,%dx%d\n", playername, ndefused, cols, rows); + sort_scorelog(scorelog); // pending clrtoeol(); show_scorelog(scorelog); - mvprintw(0, 0, "New score written to score log"); - refresh(); - getchar(); - } - fclose(scorelog); - free(playername); + mvprintw(0, 0, "New score written to score log"); + refresh(); + getchar(); + } + fclose(scorelog); + free(playername); } char *get_pname(void) { - char buffer[20]; - char *playername; - move(0, 0); - echo(); - clrtoeol(); - printw("Your name: "); - refresh(); - scanw("%s", buffer); - noecho(); - refresh(); - playername = (char *)malloc(strlen(buffer) + 1); - return (strcpy(playername, buffer)); + char buffer[20]; + char *playername; + move(0, 0); + echo(); + clrtoeol(); + printw("Your name: "); + refresh(); + scanw("%s", buffer); + noecho(); + refresh(); + playername = (char *)malloc(strlen(buffer) + 1); + return (strcpy(playername, buffer)); } void sort_scorelog(FILE *scorelog) @@ -120,24 +120,24 @@ void parse_data(FILE *scorelog) void game_won(WINDOW *gamew, int ymid, int xmid) { - wclear(gamew); - wrefresh(gamew); - 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(); - attroff(A_BOLD); + wclear(gamew); + wrefresh(gamew); + 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(); + attroff(A_BOLD); } -void game_over(WINDOW *gamew, char **mboard, int ymid, int xmid) +void game_over(WINDOW *gamew, int ymid, int xmid) { - wclear(gamew); - wrefresh(gamew); - 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(); - attroff(A_BOLD); + wclear(gamew); + wrefresh(gamew); + 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(); + attroff(A_BOLD); } diff --git a/assignment-1.5-arrays-pointers-files/minecurses/src/outputs.h b/assignment-1.5-arrays-pointers-files/minecurses/src/outputs.h @@ -1,21 +1,22 @@ #ifndef OUTPUTS_H #define OUTPUTS_H -#include "minesweeper.h" +#include "main.h" +#include <string.h> #define SESSION_PATH "log/session.txt" #define SCORE_LOG_PATH "log/scorelog.csv" -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 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); +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, const char *status); +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, char **mboard, int ymid, int xmid); +void game_over (struct _win_st* gamew, int ymid, int xmid); #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,40 +1,40 @@ #include "settings.h" -int set_cols() +int set_cols(void) { - int COLS; + int cols; do { mvprintw(YMAX-4, 1, "Columns (Min = 5, Max = %d): ", (XMAX-2)/3-2); refresh(); - scanw("%d", &COLS); + scanw("%d", &cols); refresh(); - } while (COLS < 5 || COLS > (XMAX-2)/3-2); - return COLS; + } while (cols < 5 || cols > (XMAX-2)/3-2); + return cols; } -int set_rows() +int set_rows(void) { - int ROWS; + int rows; do { mvprintw(YMAX-3, 1, "Rows (Min = 5, Max = %d): ", YMAX-4); refresh(); - scanw("%d", &ROWS); + scanw("%d", &rows); refresh(); - } while (ROWS < 5 || ROWS > YMAX-4); - return ROWS; + } while (rows < 5 || rows > YMAX-4); + return rows; } -int set_nmines(int DIMENSIONS) +int set_nmines(int dimensions) { - int NMINES; + int nmines; do { - mvprintw(YMAX-2, 1, "Mines (Max = %d): ", DIMENSIONS-15); // -10 so the player has a chance to win + mvprintw(YMAX-2, 1, "Mines (Max = %d): ", dimensions-15); // -10 so the player has a chance to win refresh(); - scanw("%d", &NMINES); + scanw("%d", &nmines); refresh(); - } while (NMINES < 1 || NMINES > DIMENSIONS-15); - return NMINES; + } while (nmines < 1 || nmines > dimensions-15); + return nmines; } 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 (); -int set_rows (); -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 @@ -7,18 +7,18 @@ void init_curses(void) cbreak(); } -WINDOW *game_win(int COLS, int ROWS, int NMINES) +WINDOW *game_win(int rows, int cols) { - int wrows = ROWS+2; - int wcols = COLS*3+2; + 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); + WINDOW *gamew = newwin(wrows, wcols, wy, wx); wattron(gamew, A_BOLD); - box(gamew, 0, 0); - wrefresh(gamew); - wattroff(gamew, A_BOLD); - return gamew; + box(gamew, 0, 0); + wrefresh(gamew); + wattroff(gamew, A_BOLD); + return gamew; } void options_menu(void) diff --git a/assignment-1.5-arrays-pointers-files/minecurses/src/wins.h b/assignment-1.5-arrays-pointers-files/minecurses/src/wins.h @@ -1,10 +1,10 @@ #ifndef WINS_H #define WINS_H -#include "minesweeper.h" +#include "main.h" void init_curses (void); -WINDOW *game_win (int COLS, int ROWS, int NMINES); +WINDOW *game_win (int rows, int cols); void options_menu (void); void fill_menu (struct _win_st*);