uni

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

commit e97bb94164feb0742b327699e2a287914f63aebc
parent 8f0c18fa1f9fbdd4b1648ba0704cd2410dfcbd1d
Author: Christos Margiolis <christos@margiolis.net>
Date:   Mon, 20 Jan 2020 11:09:00 +0200

bug fixes, pending mode set

Diffstat:
Massignment-1.5-arrays-pointers-files/minesweeper/bin/minesweeper | 0
Massignment-1.5-arrays-pointers-files/minesweeper/include/gameplay.h | 15++++++---------
Massignment-1.5-arrays-pointers-files/minesweeper/include/minesweeper.h | 17++++++++---------
Aassignment-1.5-arrays-pointers-files/minesweeper/include/outputs.h | 10++++++++++
Aassignment-1.5-arrays-pointers-files/minesweeper/include/settings.h | 12++++++++++++
Massignment-1.5-arrays-pointers-files/minesweeper/mnsout.txt | 119++++---------------------------------------------------------------------------
Massignment-1.5-arrays-pointers-files/minesweeper/obj/gameplay.o | 0
Massignment-1.5-arrays-pointers-files/minesweeper/obj/minesweeper.o | 0
Aassignment-1.5-arrays-pointers-files/minesweeper/obj/outputs.o | 0
Aassignment-1.5-arrays-pointers-files/minesweeper/obj/settings.o | 0
Massignment-1.5-arrays-pointers-files/minesweeper/src/gameplay.c | 68++++++++++++++++++++++++++++++++++++++++++++++++++------------------
Massignment-1.5-arrays-pointers-files/minesweeper/src/minesweeper.c | 224++++++++++++++++++-------------------------------------------------------------
Aassignment-1.5-arrays-pointers-files/minesweeper/src/outputs.c | 48++++++++++++++++++++++++++++++++++++++++++++++++
Aassignment-1.5-arrays-pointers-files/minesweeper/src/settings.c | 81+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
14 files changed, 271 insertions(+), 323 deletions(-)

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 @@ -1,18 +1,15 @@ #ifndef GAMEPLAY_H #define GAMEPLAY_H -#if defined linux || defined __unix__ -#include <ncurses.h> -#elif defined _WIN32 || defined _WIN64 -#include <pdcurses.h> -#include <stdint.h> -#endif +#include "minesweeper.h" -#include <stdbool.h> +#define DEFUSEKEY 'd' -void selection(struct _win_st*, char **, char **, int, int); -bool transfer(char **, char **, int, int); +void selection(struct _win_st*, char **, char **, int, int, int); +bool transfer(char **, char **, int, int, int, int *); void reveal(struct _win_st*, char **, int, int); +bool defused(char **, char **, int, int, int, int *); +void game_won(struct _win_st*, char **, int, int); void game_over(struct _win_st*, char **, int, int); #endif \ No newline at end of file diff --git a/assignment-1.5-arrays-pointers-files/minesweeper/include/minesweeper.h b/assignment-1.5-arrays-pointers-files/minesweeper/include/minesweeper.h @@ -8,7 +8,13 @@ #include <stdint.h> #endif -#include <stdbool.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> + +#include "settings.h" +#include "gameplay.h" +#include "outputs.h" #define HIDDEN '#' #define MINE '*' @@ -16,16 +22,12 @@ void main_win(); void start(); -void set_mode(struct _win_st*); - -int set_width(struct _win_st*, int); -int set_height(struct _win_st*, int); -int set_nmines(struct _win_st*, int); void game_win(int, int, int); char **init_dispboard(struct _win_st*, int, int); void fill_dispboard(char **, int, int); char **init_mineboard(struct _win_st*, int, int, int); + void place_mines(char **, int, int, int); void add_adj(char **, int, int); bool is_mine(char **, int, int); @@ -33,7 +35,4 @@ bool outof_bounds(int, int, int, int); int8_t adj_mines(char **, int, int, int, int); void fill_spaces(char **, int, int, int); -void print_board(struct _win_st*, char **, int, int); -void filewrite(char **, int, int, int, int); - #endif \ No newline at end of file diff --git a/assignment-1.5-arrays-pointers-files/minesweeper/include/outputs.h b/assignment-1.5-arrays-pointers-files/minesweeper/include/outputs.h @@ -0,0 +1,9 @@ +#ifndef OUTPUTS_H +#define OUTPUTS_H + +#include "minesweeper.h" + +void print_board(struct _win_st*, char **, int, int); +void filewrite(char **, int, int, int, int, const char *); + +#endif+ \ No newline at end of file diff --git a/assignment-1.5-arrays-pointers-files/minesweeper/include/settings.h b/assignment-1.5-arrays-pointers-files/minesweeper/include/settings.h @@ -0,0 +1,11 @@ +#ifndef SETTINGS_H +#define SETTINGS_H + +#include "minesweeper.h" + +void set_mode(struct _win_st*); +int set_cols(struct _win_st*, int); +int set_rows(struct _win_st*, int); +int set_nmines(struct _win_st*, int); + +#endif+ \ No newline at end of file diff --git a/assignment-1.5-arrays-pointers-files/minesweeper/mnsout.txt b/assignment-1.5-arrays-pointers-files/minesweeper/mnsout.txt @@ -1,116 +1,9 @@ -Mine hit at position (23, 22) +Last mine defused at position (0, 1) 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 1 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - 1 1 1 - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - 1 * 1 - - 1 1 1 - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - 1 1 1 - - 2 * 2 - - - - - - - - - - - -- - - - - - - - - - - 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 2 2 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 * 1 - 1 1 1 -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1 1 1 - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - 1 1 1 - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - 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 - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - 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 - - - - - - - - - - - - - - - - - - - - - - - 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/minesweeper.o b/assignment-1.5-arrays-pointers-files/minesweeper/obj/minesweeper.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/src/gameplay.c b/assignment-1.5-arrays-pointers-files/minesweeper/src/gameplay.c @@ -1,11 +1,10 @@ -#include <string.h> -#include "minesweeper.h" #include "gameplay.h" -void selection(WINDOW *gameWin, char **dispboard, char **mineboard, int WIDTH, int HEIGHT) +void selection(WINDOW *gameWin, char **dispboard, char **mineboard, int COLS, int ROWS, int NMINES) { int chRow, chCol; bool gameOver; + int numDefused = 0; int yMax, xMax, yMiddle, xMiddle; getmaxyx(stdscr, yMax, xMax); yMiddle = yMax / 2; @@ -13,41 +12,74 @@ void selection(WINDOW *gameWin, char **dispboard, char **mineboard, int WIDTH, i do { - mvprintw(1, 1, "Choice (row, col): "); - scanw("%d %d", &chRow, &chCol); - chRow++; - chCol++; - mvprintw(1, strlen("Choice (row, col): ") + 1, "(%d,%d)", chRow-1, chCol-1); + mvprintw(1, 1, "Choice (col, row): "); + scanw("%d %d", &chCol, &chRow); + mvprintw(1, strlen("Choice (col, row): ") + 1, "(%d,%d)", chCol, chRow); refresh(); mvprintw(1, 1, CLEAR); - gameOver = transfer(dispboard, mineboard, chRow, chCol); - reveal(gameWin, dispboard, chRow, chCol); + gameOver = transfer(dispboard, mineboard, chCol, chRow, NMINES, &numDefused); + mvprintw(1, xMiddle-9, "Defused mines: %d/%d", numDefused, NMINES); + reveal(gameWin, dispboard, chCol, chRow); getchar(); - } while (chRow < 0 || chRow > WIDTH-1 || chCol < 0 || chCol > HEIGHT-1 || !gameOver); + } while (((chCol >= 0 && chCol < COLS) && (chRow >= 0 && chRow < ROWS)) && numDefused < NMINES && !gameOver); if (gameOver == true) { game_over(gameWin, mineboard, yMiddle, xMiddle); getchar(); - print_board(gameWin, mineboard, WIDTH, HEIGHT); - filewrite(mineboard, WIDTH, HEIGHT, chRow, chCol); + print_board(gameWin, mineboard, COLS, ROWS); + filewrite(mineboard, COLS, ROWS, chCol, chRow, "lost"); + } + + if (numDefused == NMINES) + { + game_won(gameWin, mineboard, yMiddle, xMiddle); + getchar(); + filewrite(mineboard, COLS, ROWS, chCol, chRow, "won"); + } +} + + +bool transfer(char **dispboard, char **mineboard, int chCol, int chRow, int NMINES, int *numDefused) +{ + dispboard[chCol][chRow] = mineboard[chCol][chRow]; + return (!defused(dispboard, mineboard, chCol, chRow, NMINES, numDefused)) ? true : false; +} + +bool defused(char **dispboard, char **mineboard, int chCol, int chRow, int NMINES, int *numDefused) +{ + char c = getchar(); + + if (dispboard[chCol][chRow] == MINE) + { + if (c == DEFUSEKEY) + { + dispboard[chCol][chRow] = mineboard[chCol][chRow] = 'D'; + (*numDefused)++; + return true; + } + else return false; } } -bool transfer(char **dispboard, char **mineboard, int chRow, int chCol) +void reveal(WINDOW *gameWin, char **dispboard, int chCol, int chRow) { - dispboard[chRow][chCol] = mineboard[chRow][chCol]; - return (dispboard[chRow][chCol] == MINE) ? true : false; + mvwaddch(gameWin, chRow+1, chCol+1, dispboard[chCol][chRow]); + wrefresh(gameWin); } -void reveal(WINDOW *gameWin, char **dispboard, int chRow, int chCol) +void game_won(WINDOW *gameWin, char **mineboard, int yMiddle, int xMiddle) { - mvwaddch(gameWin, chRow, chCol, dispboard[chRow][chCol]); + wclear(gameWin); + mvwprintw(gameWin, yMiddle-11, xMiddle-18, "You defused all the mines!"); + mvwprintw(gameWin, yMiddle-10, xMiddle-10, "You won :)"); wrefresh(gameWin); + wclear(gameWin); } + void game_over(WINDOW *gameWin, char **mineboard, int yMiddle, int xMiddle) { wclear(gameWin); diff --git a/assignment-1.5-arrays-pointers-files/minesweeper/src/minesweeper.c b/assignment-1.5-arrays-pointers-files/minesweeper/src/minesweeper.c @@ -1,8 +1,4 @@ -#include <stdlib.h> -#include <string.h> -#include <time.h> #include "minesweeper.h" -#include "gameplay.h" void main_win() { @@ -32,96 +28,16 @@ void start() set_mode(menuWin); - int WIDTH = set_width(menuWin, xMax); - int HEIGHT = set_height(menuWin, yMax); - int NMINES = set_nmines(menuWin, WIDTH * HEIGHT); + int COLS = set_cols(menuWin, xMax); + int ROWS = set_rows(menuWin, yMax); + int NMINES = set_nmines(menuWin, COLS * ROWS); - game_win(WIDTH, HEIGHT, NMINES); + game_win(COLS, ROWS, NMINES); getchar(); } -void set_mode(WINDOW *menuWin) // loop -{ - char mode; - mvwprintw(menuWin, 1, 1, "Keyboard or text mode (k/t): "); - wrefresh(menuWin); - scanw("%c", &mode); - mvwprintw(menuWin, 1, strlen("Keyboard or text mode (k/t): ") + 1, "%c", mode); - wrefresh(menuWin); - mvwprintw(menuWin, 1, 1, CLEAR); // thanks stefastra && spyrosROUM!!!! :-DDDD - wrefresh(menuWin); - - switch (mode) - { - case 'k': - case 'K': - mvwprintw(menuWin, 2, 1, "Keyboard mode"); - wrefresh(menuWin); - break; - case 't': - case 'T': - mvwprintw(menuWin, 2, 1, "Text mode"); - wrefresh(menuWin); - break; - default: - break; - } -} - - -int set_width(WINDOW *menuWin, int xMax) -{ - int WIDTH; - - do - { - mvwprintw(menuWin, 1, 1, "Width (Max = %d): ", xMax-12); - wrefresh(menuWin); - scanw("%d", &WIDTH); - mvwprintw(menuWin, 1, strlen("Width (Max = XXX): ") + 1, "%d", WIDTH); - wrefresh(menuWin); - } while (WIDTH < 5 || WIDTH > xMax - 12); - - return WIDTH; -} - - -int set_height(WINDOW *menuWin, int yMax) -{ - int HEIGHT; - - do - { - mvwprintw(menuWin, 2, 1, "Height (Max = %d): ", yMax-12); - wrefresh(menuWin); - scanw("%d", &HEIGHT); - mvwprintw(menuWin, 2, strlen("Height (Max = YYY): ") + 1, "%d", HEIGHT); - wrefresh(menuWin); - } while (HEIGHT < 5 || HEIGHT > yMax - 12); - - return HEIGHT; -} - - -int set_nmines(WINDOW *menuWin, int DIMENSIONS) -{ - int NMINES; - - do - { - 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); - wrefresh(menuWin); - } while (NMINES < 1 || NMINES > DIMENSIONS-10); - - return NMINES; -} - - -void game_win(int WIDTH, int HEIGHT, int NMINES) +void game_win(int COLS, int ROWS, int NMINES) { int yMax, xMax; getmaxyx(stdscr, yMax, xMax); @@ -132,22 +48,22 @@ void game_win(int WIDTH, int HEIGHT, int NMINES) wrefresh(gameWin); keypad(gameWin, true); - char **dispboard = init_dispboard(gameWin, WIDTH, HEIGHT); - char **mineboard = init_mineboard(gameWin, WIDTH, HEIGHT, NMINES); + char **dispboard = init_dispboard(gameWin, COLS, ROWS); + char **mineboard = init_mineboard(gameWin, COLS, ROWS, NMINES); - selection(gameWin, dispboard, mineboard, WIDTH, HEIGHT); + selection(gameWin, dispboard, mineboard, COLS, ROWS, NMINES); free(dispboard); free(mineboard); } -char **init_dispboard(WINDOW *gameWin, int WIDTH, int HEIGHT) +char **init_dispboard(WINDOW *gameWin, int COLS, int ROWS) { int i; - char **dispboard = (char **)malloc(WIDTH * sizeof(char *)); - for (i = 0; i < WIDTH; i++) - dispboard[i] = (char *)malloc(HEIGHT); + char **dispboard = (char **)malloc(COLS * sizeof(char *)); + for (i = 0; i < COLS; i++) + dispboard[i] = (char *)malloc(ROWS); if (dispboard == NULL) { @@ -156,30 +72,31 @@ char **init_dispboard(WINDOW *gameWin, int WIDTH, int HEIGHT) } else { - fill_dispboard(dispboard, WIDTH, HEIGHT); - print_board(gameWin, dispboard, WIDTH, HEIGHT); + fill_dispboard(dispboard, COLS, ROWS); + print_board(gameWin, dispboard, COLS, ROWS); getchar(); } return dispboard; } -void fill_dispboard(char **dispboard, int WIDTH, int HEIGHT) + +void fill_dispboard(char **dispboard, int COLS, int ROWS) { int i, j; - for (i = 0; i < WIDTH; i++) - for (j = 0; j < HEIGHT; j++) + for (i = 0; i < COLS; i++) + for (j = 0; j < ROWS; j++) dispboard[i][j] = HIDDEN; } -char **init_mineboard(WINDOW *gameWin, int WIDTH, int HEIGHT, int NMINES) +char **init_mineboard(WINDOW *gameWin, int COLS, int ROWS, int NMINES) { int i; - char **mineboard = (char **)malloc(WIDTH * sizeof(char *)); - for (i = 0; i < WIDTH; i++) - mineboard[i] = (char *)malloc(HEIGHT); + char **mineboard = (char **)malloc(COLS * sizeof(char *)); + for (i = 0; i < COLS; i++) + mineboard[i] = (char *)malloc(ROWS); if (mineboard == NULL) { @@ -188,16 +105,20 @@ char **init_mineboard(WINDOW *gameWin, int WIDTH, int HEIGHT, int NMINES) } else { - place_mines(mineboard, WIDTH, HEIGHT, NMINES); - add_adj(mineboard, WIDTH, HEIGHT); - fill_spaces(mineboard, WIDTH, HEIGHT, NMINES); + place_mines(mineboard, COLS, ROWS, NMINES); + add_adj(mineboard, COLS, ROWS); + fill_spaces(mineboard, COLS, ROWS, NMINES); + + // tests + //print_board(gameWin, mineboard, COLS, ROWS); + //filewrite(mineboard, COLS, ROWS, 1, 2); } return mineboard; } -void place_mines(char **mineboard, int WIDTH, int HEIGHT, int NMINES) +void place_mines(char **mineboard, int COLS, int ROWS, int NMINES) { int i, wRand, hRand; @@ -205,21 +126,21 @@ void place_mines(char **mineboard, int WIDTH, int HEIGHT, int NMINES) for (i = 0; i < NMINES; i++) { - wRand = rand() % WIDTH; - hRand = rand() % HEIGHT; + wRand = rand() % COLS; + hRand = rand() % ROWS; mineboard[wRand][hRand] = MINE; } } -void add_adj(char **mineboard, int WIDTH, int HEIGHT) +void add_adj(char **mineboard, int COLS, int ROWS) { int i, j; - for (i = 0; i < WIDTH; i++) - for (j = 0; j < HEIGHT; j++) + for (i = 0; i < COLS; i++) + for (j = 0; j < ROWS; j++) if (!is_mine(mineboard, i, j)) - mineboard[i][j] = adj_mines(mineboard, i, j, WIDTH, HEIGHT) + '0'; + mineboard[i][j] = adj_mines(mineboard, i, j, COLS, ROWS) + '0'; } @@ -228,81 +149,36 @@ bool is_mine(char **mineboard, int row, int col) return (mineboard[row][col] == MINE) ? true : false; } -bool outof_bounds(int row, int col, int WIDTH, int HEIGHT) +bool outof_bounds(int row, int col, int COLS, int ROWS) { - return (row < 0 || row > WIDTH-1 || col < 0 || col > HEIGHT-1) ? true : false; + return (row < 0 || row > COLS-1 || col < 0 || col > ROWS-1) ? true : false; } -int8_t adj_mines(char **mineboard, int row, int col, int WIDTH, int HEIGHT) +int8_t adj_mines(char **mineboard, int row, int col, int COLS, int ROWS) { int8_t numAdj = 0; - if (!outof_bounds(row, col - 1, WIDTH, HEIGHT) && mineboard[row][col-1] == MINE) numAdj++; // North - if (!outof_bounds(row, col + 1, WIDTH, HEIGHT) && mineboard[row][col+1] == MINE) numAdj++; // South - if (!outof_bounds(row + 1, col, WIDTH, HEIGHT) && mineboard[row+1][col] == MINE) numAdj++; // East - if (!outof_bounds(row - 1, col, WIDTH, HEIGHT) && mineboard[row-1][col] == MINE) numAdj++; // West - if (!outof_bounds(row + 1, col - 1, WIDTH, HEIGHT) && mineboard[row+1][col-1] == MINE) numAdj++; // North-East - if (!outof_bounds(row - 1, col - 1, WIDTH, HEIGHT) && mineboard[row-1][col-1] == MINE) numAdj++; // North-West - if (!outof_bounds(row + 1, col + 1, WIDTH, HEIGHT) && mineboard[row+1][col+1] == MINE) numAdj++; // South-East - if (!outof_bounds(row - 1, col + 1, WIDTH, HEIGHT) && mineboard[row-1][col+1] == MINE) numAdj++; // South-West + if (!outof_bounds(row, col - 1, COLS, ROWS) && mineboard[row][col-1] == MINE) numAdj++; // North + if (!outof_bounds(row, col + 1, COLS, ROWS) && mineboard[row][col+1] == MINE) numAdj++; // South + if (!outof_bounds(row + 1, col, COLS, ROWS) && mineboard[row+1][col] == MINE) numAdj++; // East + if (!outof_bounds(row - 1, col, COLS, ROWS) && mineboard[row-1][col] == MINE) numAdj++; // West + if (!outof_bounds(row + 1, col - 1, COLS, ROWS) && mineboard[row+1][col-1] == MINE) numAdj++; // North-East + if (!outof_bounds(row - 1, col - 1, COLS, ROWS) && mineboard[row-1][col-1] == MINE) numAdj++; // North-West + if (!outof_bounds(row + 1, col + 1, COLS, ROWS) && mineboard[row+1][col+1] == MINE) numAdj++; // South-East + if (!outof_bounds(row - 1, col + 1, COLS, ROWS) && mineboard[row-1][col+1] == MINE) numAdj++; // South-West return numAdj; } -void fill_spaces(char **mineboard, int WIDTH, int HEIGHT, int NMINES) +void fill_spaces(char **mineboard, int COLS, int ROWS, int NMINES) { int i, j; - for (i = 0; i < WIDTH; i++) - for (j = 0; j < HEIGHT; j++) + for (i = 0; i < COLS; i++) + for (j = 0; j < ROWS; j++) if (mineboard[i][j] != MINE && mineboard[i][j] == '0') mineboard[i][j] = '-'; -} - - -void print_board(WINDOW *gameWin, char **mineboard, int WIDTH, int HEIGHT) -{ - int i, j; - - for (i = 0; i < WIDTH; i++) - { - for (j = 0; j < HEIGHT; j++) - { - mvwaddch(gameWin, j + 1, i + 1, mineboard[i][j]); - wrefresh(gameWin); - } - } -} - - -void filewrite(char **mineboard, int WIDTH, int HEIGHT, int hitRow, int hitCol) -{ - int i, j; - FILE *mnsOut = fopen("mnsout.txt", "w"); - - if (mnsOut == NULL) - { - mvprintw(1, 1, "Error opening file, exiting..."); - exit(EXIT_FAILURE); - } - else - { - fprintf(mnsOut, "Mine hit at position (%d, %d)\n\n", hitRow, hitCol); - fprintf(mnsOut, "Board overview\n\n"); - - for (i = 0; i < WIDTH; i++) // fix inversion - { - for (j = 0; j < HEIGHT; j++) - fprintf(mnsOut, "%c ", mineboard[i][j]); - fprintf(mnsOut, "\n"); - } - - mvprintw(1, 1, "Session written to file"); - refresh(); - } - - fclose(mnsOut); } \ No newline at end of file diff --git a/assignment-1.5-arrays-pointers-files/minesweeper/src/outputs.c b/assignment-1.5-arrays-pointers-files/minesweeper/src/outputs.c @@ -0,0 +1,47 @@ +#include "outputs.h" + +void print_board(WINDOW *gameWin, char **mineboard, int COLS, int ROWS) +{ + int i, j; + + for (i = 0; i < ROWS; i++) + { + for (j = 0; j < COLS; j++) + { + mvwaddch(gameWin, j+1, i+1, mineboard[i][j]); + wrefresh(gameWin); + } + } +} + + +void filewrite(char **mineboard, int COLS, int ROWS, int hitCol, int hitRow, const char *status) +{ + int i, j; + FILE *mnsOut = fopen("mnsout.txt", "w"); + + if (mnsOut == NULL) + { + mvprintw(1, 1, "Error opening file, exiting..."); + exit(EXIT_FAILURE); + } + else + { + strcmp(status, "won") + ? fprintf(mnsOut, "Mine hit at position (%d, %d)\n\n", hitCol, hitRow) + : fprintf(mnsOut, "Last mine defused at position (%d, %d)\n\n", hitCol, hitRow); + fprintf(mnsOut, "Board overview\n\n"); + + for (i = 0; i < ROWS; i++) + { + for (j = 0; j < COLS; j++) + fprintf(mnsOut, "%c ", mineboard[j][i]); + fprintf(mnsOut, "\n"); + } + + mvprintw(1, 1, "Session written to file"); + refresh(); + } + + fclose(mnsOut); +}+ \ 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 @@ -0,0 +1,80 @@ +#include "settings.h" + +void set_mode(WINDOW *menuWin) // loop +{ + char mode; + mvwprintw(menuWin, 1, 1, "Keyboard or text mode (k/t): "); + wrefresh(menuWin); + scanw("%c", &mode); + mvwprintw(menuWin, 1, strlen("Keyboard or text mode (k/t): ") + 1, "%c", mode); + wrefresh(menuWin); + mvwprintw(menuWin, 1, 1, CLEAR); // thanks stefastra && spyrosROUM!!!! :-DDDD + wrefresh(menuWin); + + switch (mode) + { + case 'k': + case 'K': + mvwprintw(menuWin, 2, 1, "Keyboard mode"); + wrefresh(menuWin); + break; + case 't': + case 'T': + mvwprintw(menuWin, 2, 1, "Text mode"); + wrefresh(menuWin); + break; + default: + break; + } +} + + +int set_cols(WINDOW *menuWin, int xMax) +{ + int COLS; + + do + { + mvwprintw(menuWin, 1, 1, "Columns (Max = %d): ", xMax-12); + wrefresh(menuWin); + scanw("%d", &COLS); + mvwprintw(menuWin, 1, strlen("Columns (Max = XXX): ") + 1, "%d", COLS); + wrefresh(menuWin); + } while (COLS < 5 || COLS > xMax - 12); + + return COLS; +} + + +int set_rows(WINDOW *menuWin, int yMax) +{ + int ROWS; + + do + { + mvwprintw(menuWin, 2, 1, "Rows (Max = %d): ", yMax-12); + wrefresh(menuWin); + scanw("%d", &ROWS); + mvwprintw(menuWin, 2, strlen("Rows (Max = YYY): ") + 1, "%d", ROWS); + wrefresh(menuWin); + } while (ROWS < 5 || ROWS > yMax - 12); + + return ROWS; +} + + +int set_nmines(WINDOW *menuWin, int DIMENSIONS) +{ + int NMINES; + + do + { + 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); + wrefresh(menuWin); + } while (NMINES < 1 || NMINES > DIMENSIONS-10); + + return NMINES; +}+ \ No newline at end of file