uni

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

commit 9889701c8a5668c403ccae8868841e250889bd9f
parent 86dadc5457a6385bac49418a93ada45fd198383d
Author: Christos Margiolis <christos@margiolis.net>
Date:   Tue, 21 Jan 2020 21:18:17 +0200

added draft mns for test

Diffstat:
Aassignment-1.5-arrays-pointers-files/kcombinations/bin/kcombs | 0
Massignment-1.5-arrays-pointers-files/kcombinations/include/kcombinations.h | 9++++-----
Massignment-1.5-arrays-pointers-files/kcombinations/obj/arrhandler.o | 0
Massignment-1.5-arrays-pointers-files/kcombinations/obj/kcombinations.o | 0
Massignment-1.5-arrays-pointers-files/kcombinations/src/arrhandler.c | 15+++++++++++++--
Massignment-1.5-arrays-pointers-files/kcombinations/src/kcombinations.c | 42++++++++++++++++++++++++++++++------------
Aassignment-1.5-arrays-pointers-files/mns/Makefile | 37+++++++++++++++++++++++++++++++++++++
Aassignment-1.5-arrays-pointers-files/mns/bin/minesweeper | 0
Aassignment-1.5-arrays-pointers-files/mns/include/gameplay.h | 14++++++++++++++
Aassignment-1.5-arrays-pointers-files/mns/include/minesweeper.h | 36++++++++++++++++++++++++++++++++++++
Aassignment-1.5-arrays-pointers-files/mns/include/navigation.h | 9+++++++++
Aassignment-1.5-arrays-pointers-files/mns/include/outputs.h | 14++++++++++++++
Aassignment-1.5-arrays-pointers-files/mns/include/settings.h | 11+++++++++++
Aassignment-1.5-arrays-pointers-files/mns/include/wins.h | 10++++++++++
Aassignment-1.5-arrays-pointers-files/mns/mnsout.txt | 14++++++++++++++
Aassignment-1.5-arrays-pointers-files/mns/obj/gameplay.o | 0
Aassignment-1.5-arrays-pointers-files/mns/obj/main.o | 0
Aassignment-1.5-arrays-pointers-files/mns/obj/minesweeper.o | 0
Aassignment-1.5-arrays-pointers-files/mns/obj/navigation.o | 0
Aassignment-1.5-arrays-pointers-files/mns/obj/outputs.o | 0
Aassignment-1.5-arrays-pointers-files/mns/obj/settings.o | 0
Aassignment-1.5-arrays-pointers-files/mns/obj/wins.o | 0
Aassignment-1.5-arrays-pointers-files/mns/src/gameplay.c | 70++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aassignment-1.5-arrays-pointers-files/mns/src/main.c | 10++++++++++
Aassignment-1.5-arrays-pointers-files/mns/src/minesweeper.c | 121+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aassignment-1.5-arrays-pointers-files/mns/src/navigation.c | 47+++++++++++++++++++++++++++++++++++++++++++++++
Aassignment-1.5-arrays-pointers-files/mns/src/outputs.c | 81+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aassignment-1.5-arrays-pointers-files/mns/src/settings.c | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
Aassignment-1.5-arrays-pointers-files/mns/src/wins.c | 58++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aassignment-1.5-arrays-pointers-files/mns/todo.md | 22++++++++++++++++++++++
30 files changed, 653 insertions(+), 19 deletions(-)

diff --git a/assignment-1.5-arrays-pointers-files/kcombinations/bin/kcombs b/assignment-1.5-arrays-pointers-files/kcombinations/bin/kcombs Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/kcombinations/include/kcombinations.h b/assignment-1.5-arrays-pointers-files/kcombinations/include/kcombinations.h @@ -12,14 +12,14 @@ void x_pair(int *, int *); void y_pair(int *, int *); void print_combs(int *, int, int, int, int, int, int); -void combinations(int *, int *, int, int, int, int, int *, int *, int *, int, int, int, int); +void combinations(int *, int *, int *, int, int, int, int, int, int *, int *, int *, int, int, int, int); bool even_calc(int *, int, int, int); bool sum_comb_calc(int *, int, int, int); -int frequency(); +int frequency(int *, int *, int *, int); long int combinations_count(int, int); long double factorial(int); -void print_other(int, int, int, int, int); // add freq +void print_other(int, int, int, int, int, int *, int *); -#endif- \ No newline at end of file +#endif diff --git a/assignment-1.5-arrays-pointers-files/kcombinations/obj/arrhandler.o b/assignment-1.5-arrays-pointers-files/kcombinations/obj/arrhandler.o Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/kcombinations/obj/kcombinations.o b/assignment-1.5-arrays-pointers-files/kcombinations/obj/kcombinations.o Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/kcombinations/src/arrhandler.c b/assignment-1.5-arrays-pointers-files/kcombinations/src/arrhandler.c @@ -74,4 +74,16 @@ void swap(int *a, int *b) int temp = *a; *a = *b; *b = temp; -}- \ No newline at end of file +} + + +int find_pos(int *arr, int numIter, int val) +{ + int pos, i; + + for (i = 0; i < numIter; i++) + if (val == *(arr + i)) + pos = i; + + return pos; +} diff --git a/assignment-1.5-arrays-pointers-files/kcombinations/src/kcombinations.c b/assignment-1.5-arrays-pointers-files/kcombinations/src/kcombinations.c @@ -58,7 +58,8 @@ void y_pair(int *y1, int *y2) void print_combs(int *arr, int N, int K, int x1, int x2, int y1, int y2) { int *currComb = (int *)malloc(N * sizeof(int)); - int unFrstCond = 0, unScndCondOnly = 0, printed = 0; + int *freqArr = (int *)malloc(N * sizeof(int)); + int unFrstCond = 0, unScndCondOnly = 0, printed = 0; if (currComb == NULL) { @@ -69,15 +70,16 @@ void print_combs(int *arr, int N, int K, int x1, int x2, int y1, int y2) } else { - combinations(arr, currComb, 0, N-1, 0, K, &printed, &unFrstCond, &unScndCondOnly, x1, x2, y1, y2); - print_other(N, K, unFrstCond, unScndCondOnly, printed); + combinations(arr, currComb, freqArr, 0, N-1, 0, N, K, &printed, &unFrstCond, &unScndCondOnly, x1, x2, y1, y2); + print_other(N, K, unFrstCond, unScndCondOnly, printed, arr, freqArr); } free(currComb); + free(freqArr); } -void combinations(int *arr, int *currComb, int start, int end, int index, int K, int *printed, int *unFrstCond, int *unScndCondOnly, int x1, int x2, int y1, int y2) +void combinations(int *arr, int *currComb, int *freqArr, int start, int end, int index, int N, int K, int *printed, int *unFrstCond, int *unScndCondOnly, int x1, int x2, int y1, int y2) { int i, j; @@ -88,8 +90,13 @@ void combinations(int *arr, int *currComb, int start, int end, int index, int K, if (even_calc(currComb, K, x1, x2) && sum_comb_calc(currComb, K, y1, y2)) { printf("%d ", *(currComb + j)); - if (j == K - 1) { (*printed)++; printf("\n"); } - } // add freq + if (j == K - 1) + { + frequency(freqArr, currComb, arr, N); + (*printed)++; + printf("\n"); + } + } } if (!even_calc(currComb, K, x1, x2) && sum_comb_calc(currComb, K, y1, y2)) (*unFrstCond)++; if (!sum_comb_calc(currComb, K, y1, y2)) (*unScndCondOnly)++; @@ -99,7 +106,7 @@ void combinations(int *arr, int *currComb, int start, int end, int index, int K, for (i = start; i <= end && end-i+1 >= K-index; i++) { *(currComb + index) = *(arr + i); - combinations(arr, currComb, i+1, end, index+1, K, printed, unFrstCond, unScndCondOnly, x1, x2, y1, y2); + combinations(arr, currComb, freqArr, i+1, end, index+1, N, K, printed, unFrstCond, unScndCondOnly, x1, x2, y1, y2); } } @@ -126,13 +133,19 @@ bool sum_comb_calc(int *arr, int K, int y1, int y2) } -int frequency() +int frequency(int *freqArr, int *currComb, int *arr, int N) { + int pos, i; + for (i = 0; i < N; i++) + { + pos = find_pos(arr, N, *(currComb + i)); + (*(freqArr + pos))++; + } } -long int combinations_count(int N, int K) // wtf ??????? +long int combinations_count(int N, int K) { return (factorial(N) / (factorial(K) * factorial(N - K))); } @@ -148,10 +161,15 @@ long double factorial(int num) } -void print_other(int N, int K, int unFrstCond, int unScndCondOnly, int printed) +void print_other(int N, int K, int unFrstCond, int unScndCondOnly, int printed, int *arr, int *freqArr) { - printf("\nTotal number of combinations %d to %d: %ld\n", N, K, combinations_count(N, K)); + int i; + + printf("\nTotal number of combinations %d to %d: %ld\n", N, K, combinations_count(N, K)); printf("Number of combinations not satisfying the first condition: %d\n", unFrstCond); printf("Number of combinations not satisfying the second condition only: %d\n", unScndCondOnly); - printf("Printed combinations: %d\n", printed); + printf("Printed combinations: %d\n\n", printed); + + for (i = 0; i < N; i++) + printf("%d appeared %d times\n", *(arr + i), *(freqArr + i)); } diff --git a/assignment-1.5-arrays-pointers-files/mns/Makefile b/assignment-1.5-arrays-pointers-files/mns/Makefile @@ -0,0 +1,36 @@ +TARGET = minesweeper + +SRC_DIR = src +OBJ_DIR = obj +BIN_DIR = bin + +SRC = $(wildcard $(SRC_DIR)/*.c) +OBJ = $(SRC:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o) + +MOVE = mv +MKDIR_P = mkdir -p + +CC = gcc +CPPFLAGS += -Iinclude +CFLAGS += -Wall +LDFLAGS += -Llib +LDLIBS += -lm -lncurses + +.PHONY: all clean + +all: $(TARGET) + +$(TARGET): $(OBJ) + $(MKDIR_P) $(BIN_DIR) + $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@ + $(MOVE) $(TARGET) $(BIN_DIR) + +$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c + $(MKDIR_P) $(OBJ_DIR) + $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@ + +run: + ./$(BIN_DIR)/$(TARGET) + +clean: + $(RM) $(OBJ) $(BIN_DIR)/$(TARGET)+ \ No newline at end of file diff --git a/assignment-1.5-arrays-pointers-files/mns/bin/minesweeper b/assignment-1.5-arrays-pointers-files/mns/bin/minesweeper Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/mns/include/gameplay.h b/assignment-1.5-arrays-pointers-files/mns/include/gameplay.h @@ -0,0 +1,13 @@ +#ifndef GAMEPLAY_H +#define GAMEPLAY_H + +#include "minesweeper.h" + +#define DEFUSEKEY 'd' + +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 *); + +#endif+ \ No newline at end of file diff --git a/assignment-1.5-arrays-pointers-files/mns/include/minesweeper.h b/assignment-1.5-arrays-pointers-files/mns/include/minesweeper.h @@ -0,0 +1,36 @@ +#ifndef MINESWEEPER_H +#define MINESWEEPER_H + +#if defined linux || defined __unix__ +#include <ncurses.h> +#elif defined _WIN32 || defined _WIN64 +#include <pdcurses.h> +#include <stdint.h> +#endif + +#include <stdlib.h> +#include <string.h> +#include <time.h> + +#include "settings.h" +#include "gameplay.h" +#include "navigation.h" +#include "outputs.h" +#include "wins.h" + +#define HIDDEN '#' +#define MINE '*' +#define CLEAR " " + +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); +bool outof_bounds(int, int, int, int); +int8_t adj_mines(char **, int, int, int, int); +void fill_spaces(char **, int, int, int); + +#endif diff --git a/assignment-1.5-arrays-pointers-files/mns/include/navigation.h b/assignment-1.5-arrays-pointers-files/mns/include/navigation.h @@ -0,0 +1,8 @@ +#ifndef NAVIGATION_H +#define NAVIGATION_H + +#include "minesweeper.h" + +void navigate(struct _win_st*, int, int); + +#endif+ \ No newline at end of file diff --git a/assignment-1.5-arrays-pointers-files/mns/include/outputs.h b/assignment-1.5-arrays-pointers-files/mns/include/outputs.h @@ -0,0 +1,13 @@ +#ifndef OUTPUTS_H +#define OUTPUTS_H + +#include "minesweeper.h" + +void print_grid(struct _win_st*, int, int); +void game_won(struct _win_st*, char **, int, int); +void game_over(struct _win_st*, char **, int, int); + +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/mns/include/settings.h b/assignment-1.5-arrays-pointers-files/mns/include/settings.h @@ -0,0 +1,10 @@ +#ifndef SETTINGS_H +#define SETTINGS_H + +#include "minesweeper.h" + +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/mns/include/wins.h b/assignment-1.5-arrays-pointers-files/mns/include/wins.h @@ -0,0 +1,10 @@ +#ifndef WINS_H +#define WINS_H + +#include "minesweeper.h" + +void main_win(); +void start(); +void game_win(int, int, int); + +#endif diff --git a/assignment-1.5-arrays-pointers-files/mns/mnsout.txt b/assignment-1.5-arrays-pointers-files/mns/mnsout.txt @@ -0,0 +1,14 @@ +Mine hit at position (3, 2) + +Board overview + +- - - - 1 * 1 - 1 * +- - 1 1 3 2 2 - 1 1 +- - 1 * 2 * 1 - - - +- - 1 1 2 1 1 - - - +- - 1 1 1 - - - - - +- - 1 * 1 - - - 1 1 +- - 1 1 1 - 1 1 2 * +- 1 1 2 1 1 1 * 2 1 +- 1 * 2 * 2 2 2 1 - +- 1 1 2 1 2 * 1 - - diff --git a/assignment-1.5-arrays-pointers-files/mns/obj/gameplay.o b/assignment-1.5-arrays-pointers-files/mns/obj/gameplay.o Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/mns/obj/main.o b/assignment-1.5-arrays-pointers-files/mns/obj/main.o Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/mns/obj/minesweeper.o b/assignment-1.5-arrays-pointers-files/mns/obj/minesweeper.o Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/mns/obj/navigation.o b/assignment-1.5-arrays-pointers-files/mns/obj/navigation.o Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/mns/obj/outputs.o b/assignment-1.5-arrays-pointers-files/mns/obj/outputs.o Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/mns/obj/settings.o b/assignment-1.5-arrays-pointers-files/mns/obj/settings.o Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/mns/obj/wins.o b/assignment-1.5-arrays-pointers-files/mns/obj/wins.o Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/mns/src/gameplay.c b/assignment-1.5-arrays-pointers-files/mns/src/gameplay.c @@ -0,0 +1,70 @@ +#include "gameplay.h" + +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; + xMiddle = xMax / 2; + + do + { + 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, chCol, chRow, NMINES, &numDefused); + mvprintw(1, xMiddle-9, "Defused mines: %d/%d", numDefused, NMINES); + reveal(gameWin, dispboard, chCol, chRow); + getchar(); + } 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, 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; + } +} + + +void reveal(WINDOW *gameWin, char **dispboard, int chCol, int chRow) +{ + mvwaddch(gameWin, chRow+1, chCol+1, dispboard[chCol][chRow]); + wrefresh(gameWin); +} diff --git a/assignment-1.5-arrays-pointers-files/mns/src/main.c b/assignment-1.5-arrays-pointers-files/mns/src/main.c @@ -0,0 +1,10 @@ +#include "minesweeper.h" + +int main(int argc, char **argv) +{ + main_win(); + start(); + endwin(); + + return 0; +} diff --git a/assignment-1.5-arrays-pointers-files/mns/src/minesweeper.c b/assignment-1.5-arrays-pointers-files/mns/src/minesweeper.c @@ -0,0 +1,121 @@ +#include "minesweeper.h" + +char **init_dispboard(WINDOW *gameWin, int COLS, int ROWS) +{ + int i; + char **dispboard = (char **)malloc(COLS * sizeof(char *)); + for (i = 0; i < COLS; i++) + dispboard[i] = (char *)malloc(ROWS); + + if (dispboard == NULL) + { + mvprintw(1, 1, "Error, not enough memory, exiting..."); + exit(EXIT_FAILURE); + } + else fill_dispboard(dispboard, COLS, ROWS); + + return dispboard; +} + + +void fill_dispboard(char **dispboard, int COLS, int ROWS) +{ + int i, j; + + for (i = 0; i < COLS; i++) + for (j = 0; j < ROWS; j++) + dispboard[i][j] = HIDDEN; +} + + +char **init_mineboard(WINDOW *gameWin, int COLS, int ROWS, int NMINES) +{ + int i; + char **mineboard = (char **)malloc(COLS * sizeof(char *)); + for (i = 0; i < COLS; i++) + mineboard[i] = (char *)malloc(ROWS); + + if (mineboard == NULL) + { + mvprintw(1, 1, "Error, not enough memory, exiting..."); + exit(EXIT_FAILURE); + } + else + { + 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 COLS, int ROWS, int NMINES) +{ + int i, wRand, hRand; + + srand(time(NULL)); + + for (i = 0; i < NMINES; i++) + { + wRand = rand() % COLS; + hRand = rand() % ROWS; + mineboard[wRand][hRand] = MINE; + } +} + + +void add_adj(char **mineboard, int COLS, int ROWS) +{ + int i, 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, COLS, ROWS) + '0'; +} + + +bool is_mine(char **mineboard, int row, int col) +{ + return (mineboard[row][col] == MINE) ? true : false; +} + +bool outof_bounds(int row, int col, int COLS, int ROWS) +{ + return (row < 0 || row > COLS-1 || col < 0 || col > ROWS-1) ? true : false; +} + + + +int8_t adj_mines(char **mineboard, int row, int col, int COLS, int ROWS) +{ + int8_t numAdj = 0; + + 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 COLS, int ROWS, int NMINES) +{ + int i, 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] = '-'; +} diff --git a/assignment-1.5-arrays-pointers-files/mns/src/navigation.c b/assignment-1.5-arrays-pointers-files/mns/src/navigation.c @@ -0,0 +1,46 @@ +#include "navigation.h" + +void navigate(WINDOW *gameWin, int COLS, int ROWS) +{ + keypad(gameWin, true); + int move, i, j; + int highlight = 0; + + + for (i = 0; i < ROWS*COLS; i++) + { + if (i == highlight) + wattron(gameWin, A_REVERSE); + wattroff(gameWin, A_REVERSE); + } + + switch (move) + { + case 'k': + case 'K': + case KEY_UP: + highlight--; + if (highlight == -1) highlight = 0; + break; + case 'j': + case 'J': + case KEY_DOWN: + highlight++; + if (highlight == COLS) highlight = COLS-1; + break; + case 'h': + case 'H': + case KEY_LEFT: + highlight--; + if (highlight == -1) highlight = 0; + break; + case 'l': + case 'L': + case KEY_RIGHT: + highlight++; + if (highlight == ROWS) highlight = ROWS-1; + break; + + //if (move == 10) reveal(gameWin, dispboard, chCol, chRow); + } +}+ \ No newline at end of file diff --git a/assignment-1.5-arrays-pointers-files/mns/src/outputs.c b/assignment-1.5-arrays-pointers-files/mns/src/outputs.c @@ -0,0 +1,80 @@ +#include "outputs.h" + +void print_grid(WINDOW *gameWin, int ROWS, int COLS) +{ + int i, j; + for (j = 1; j <= ROWS; j++) + { + wmove(gameWin, j, 1); + for (i = 0; i < COLS; i++) + wprintw(gameWin, "[ ]"); + } + wrefresh(gameWin); +} + + +void game_won(WINDOW *gameWin, char **mineboard, int yMiddle, int xMiddle) +{ + 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); + mvwprintw(gameWin, yMiddle-11, xMiddle-11, "You hit a mine!"); + mvwprintw(gameWin, yMiddle-10, xMiddle-10, "Game over :("); + wrefresh(gameWin); + wclear(gameWin); +} + + +void print_board(WINDOW *gameWin, char **board, int COLS, int ROWS) +{ + int i, j, x, y; + + for (i = 0; i < ROWS; i++) + { + for (j = 0; j < COLS; j++) + { + mvwaddch(gameWin, j+1, i+1, board[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/mns/src/settings.c b/assignment-1.5-arrays-pointers-files/mns/src/settings.c @@ -0,0 +1,51 @@ +#include "settings.h" + +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 diff --git a/assignment-1.5-arrays-pointers-files/mns/src/wins.c b/assignment-1.5-arrays-pointers-files/mns/src/wins.c @@ -0,0 +1,58 @@ +#include "wins.h" + +void main_win() +{ + initscr(); + noecho(); + cbreak(); + + WINDOW *mainWin = newwin(0, 0, 0, 0); + box(mainWin, 0, 0); + refresh(); + wrefresh(mainWin); + keypad(mainWin, true); +} + + +void start() +{ + int yMax, xMax; + int numSettings = 3; + getmaxyx(stdscr, yMax, xMax); + + WINDOW *menuWin = newwin(numSettings+2, xMax-10, yMax-7, 5); + box(menuWin, 0, 0); + refresh(); + wrefresh(menuWin); + keypad(menuWin, true); + + int COLS = set_cols(menuWin, xMax); + int ROWS = set_rows(menuWin, yMax); + int NMINES = set_nmines(menuWin, COLS * ROWS); + + delwin(menuWin); + game_win(COLS, ROWS, NMINES); + getchar(); +} + + +void game_win(int COLS, int ROWS, int NMINES) +{ + int yMax, xMax; + getmaxyx(stdscr, yMax, xMax); + + WINDOW *gameWin = newwin(COLS, ROWS, 2, 3); + + box(gameWin, 0, 0); + //mvwin(gameWin, (yMax/2) - 12, xMax/2); + wrefresh(gameWin); + refresh(); + + char **dispboard = init_dispboard(gameWin, COLS, ROWS); + char **mineboard = init_mineboard(gameWin, COLS, ROWS, NMINES); + print_grid(gameWin, ROWS, COLS); + selection(gameWin, dispboard, mineboard, COLS, ROWS, NMINES); + + free(dispboard); + free(mineboard); +} diff --git a/assignment-1.5-arrays-pointers-files/mns/todo.md b/assignment-1.5-arrays-pointers-files/mns/todo.md @@ -0,0 +1,21 @@ +* 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