uni

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

commit 89b027a834d074b903ffbe4616b447da4c2072a1
parent f9a49e6b1d429b688ee4d4b9532ca19607c7eee0
Author: Christos Margiolis <christos@margiolis.net>
Date:   Fri, 17 Jan 2020 12:45:43 +0200

fixed minesweeper bug, pending gameover fix

Diffstat:
Aassignment-1.5-arrays-pointers-files/minesweeper/include/gamelogic.h | 20++++++++++++++++++++
Massignment-1.5-arrays-pointers-files/minesweeper/include/minesweeper.h | 11+++--------
Massignment-1.5-arrays-pointers-files/minesweeper/mnsout.txt | 34++++++++++++++++++++++++++++++++++
Aassignment-1.5-arrays-pointers-files/minesweeper/src/gamelogic.c | 56++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Massignment-1.5-arrays-pointers-files/minesweeper/src/minesweeper.c | 39+++++++++++++++++++++++----------------
5 files changed, 136 insertions(+), 24 deletions(-)

diff --git a/assignment-1.5-arrays-pointers-files/minesweeper/include/gamelogic.h b/assignment-1.5-arrays-pointers-files/minesweeper/include/gamelogic.h @@ -0,0 +1,19 @@ +#ifndef GAMELOGIC_H +#define GAMELOGIC_H + +#if defined linux || defined __unix__ +#include <ncurses.h> +#elif defined _WIN32 || defined _WIN64 +#include <pdcurses.h> +#include <stdint.h> +#endif + +#include <stdbool.h> + +// Game logic +void selection(struct _win_st*, char **, char **, int, int); +bool transfer(char **, char **, int, int); +void reveal(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 @@ -12,6 +12,7 @@ #define HIDDEN '#' #define MINE '*' +#define CLEAR " " void main_win(); void start(); @@ -22,22 +23,16 @@ int set_height(struct _win_st*, int); int set_nmines(struct _win_st*, int); void game_win(int, int, int); -void init_dispboard(struct _win_st*, int, int); +char **init_dispboard(struct _win_st*, int, int); void fill_dispboard(char **, int, int); -void init_mineboard(struct _win_st*, int, 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); -void selection(); -void transfer(char **, char **, int, int); -void reveal(struct _win_st*, char **, char **, int, int); -void game_over(struct _win_st*); - void print(struct _win_st*, char **, int, int); void filewrite(char **, int, int); diff --git a/assignment-1.5-arrays-pointers-files/minesweeper/mnsout.txt b/assignment-1.5-arrays-pointers-files/minesweeper/mnsout.txt @@ -0,0 +1,34 @@ +Mine hit at position (1, 2) + +Board overview + +- - - - - - - - 1 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 2 1 1 - - - 1 * 1 +- - - - - - - 1 * 1 - - - - - - - - - - - 2 * 2 - - - 1 1 1 +- - - - - - - 1 1 1 - - - - - - - - - - - 2 * 3 1 1 - - - - +- - - 1 1 2 1 1 - - - - - - - - - - - - - 1 1 2 * 1 - - - - +- - - 1 * 2 * 1 - - - - - - - - - - 1 1 1 - - 1 1 1 1 1 1 - +- - - 1 1 2 1 1 - - - - - - - - - - 1 * 1 - - - 1 1 2 * 1 - +- - - - - - - - - - - - - - - - - - 1 2 2 1 - - 1 * 2 1 1 - +- - - - - - - - - - - - - - - - - 1 1 3 * 2 - - 1 1 1 - - - +- - - - - - - - - 1 1 1 - - - - - 1 * 3 * 2 - - - - - - - - +- - - - - - - - - 1 * 1 - - - - - 1 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 * 2 1 - - - - - - - +- - - - - - - - - - - 1 * 1 - - - - - 2 3 * 1 - - - - - - - +- - - - - - - - - - - 1 1 1 - 1 1 1 - 1 * 2 1 - - - - - - - +- - - - - - - - - - - - - - - 1 * 1 - 1 1 1 - - - - - - - - +- - - - - - - - - - - - - - - 1 1 1 - - - - - - - - - - - - diff --git a/assignment-1.5-arrays-pointers-files/minesweeper/src/gamelogic.c b/assignment-1.5-arrays-pointers-files/minesweeper/src/gamelogic.c @@ -0,0 +1,55 @@ +#include <string.h> +#include "minesweeper.h" +#include "gamelogic.h" + +void selection(WINDOW *gameWin, char **dispboard, char **mineboard, int WIDTH, int HEIGHT) +{ + int chrow, chcol; + bool gameOver = true; + int yMax, xMax, yMiddle, xMiddle; + getmaxyx(gameWin, yMax, xMax); + yMiddle = yMax / 2; + xMiddle = xMax / 2; + + 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); + refresh(); + mvprintw(1, 1, CLEAR); + transfer(dispboard, mineboard, chrow, chcol); + reveal(gameWin, dispboard, chrow, chcol); + getchar(); + } while ((chrow < 0 || chrow > WIDTH-1 || chcol < 0 || chcol > HEIGHT-1) && !gameOver); + + if (gameOver) + { + game_over(gameWin, mineboard, yMiddle, xMiddle); + print(gameWin, mineboard, WIDTH, HEIGHT); + filewrite(mineboard, WIDTH, HEIGHT); + } +} + + +bool transfer(char **dispboard, char **mineboard, int chrow, int chcol) +{ + dispboard[chrow][chcol] = mineboard[chrow][chcol]; + return (dispboard[chrow][chcol] == MINE) ? true : false; +} + + +void reveal(WINDOW *gameWin, char **dispboard, int chrow, int chcol) +{ + mvwaddch(gameWin, chrow, chcol, dispboard[chrow][chcol]); + wrefresh(gameWin); +} + +void game_over(WINDOW *gameWin, char **mineboard, int yMiddle, int xMiddle) +{ + wclear(gameWin); + mvwprintw(gameWin, yMiddle, xMiddle, "Game over :("); + wrefresh(gameWin); +}+ \ No newline at end of file diff --git a/assignment-1.5-arrays-pointers-files/minesweeper/src/minesweeper.c b/assignment-1.5-arrays-pointers-files/minesweeper/src/minesweeper.c @@ -2,6 +2,7 @@ #include <string.h> #include <time.h> #include "minesweeper.h" +#include "gamelogic.h" void main_win() @@ -30,7 +31,7 @@ void start() wrefresh(menuWin); keypad(menuWin, true); - //set_mode(menuWin); + set_mode(menuWin); int WIDTH = set_width(menuWin, xMax); int HEIGHT = set_height(menuWin, yMax); @@ -49,8 +50,10 @@ void set_mode(WINDOW *menuWin) // later 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) // clear contents first + switch (mode) { case 'k': case 'K': @@ -108,12 +111,12 @@ int set_nmines(WINDOW *menuWin, int DIMENSIONS) do { - mvwprintw(menuWin, 3, 1, "Mines (Max = %d): ", DIMENSIONS-600); // -500 so the player has a chance to win + 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-500); + } while (NMINES < 1 || NMINES > DIMENSIONS-10); return NMINES; } @@ -130,12 +133,18 @@ void game_win(int WIDTH, int HEIGHT, int NMINES) wrefresh(gameWin); keypad(gameWin, true); - init_dispboard(gameWin, WIDTH, HEIGHT); - init_mineboard(gameWin, WIDTH, HEIGHT, NMINES); + char **dispboard = init_dispboard(gameWin, WIDTH, HEIGHT); + char **mineboard = init_mineboard(gameWin, WIDTH, HEIGHT, NMINES); + + selection(gameWin, dispboard, mineboard, WIDTH, HEIGHT); + filewrite(mineboard, WIDTH, HEIGHT); + + free(dispboard); + free(mineboard); } -void init_dispboard(WINDOW *gameWin, int WIDTH, int HEIGHT) +char **init_dispboard(WINDOW *gameWin, int WIDTH, int HEIGHT) { int i; char **dispboard = (char **)malloc(WIDTH * sizeof(char *)); @@ -154,7 +163,7 @@ void init_dispboard(WINDOW *gameWin, int WIDTH, int HEIGHT) getchar(); } - free(dispboard); + return dispboard; } void fill_dispboard(char **dispboard, int WIDTH, int HEIGHT) @@ -167,7 +176,7 @@ void fill_dispboard(char **dispboard, int WIDTH, int HEIGHT) } -void init_mineboard(WINDOW *gameWin, int WIDTH, int HEIGHT, int NMINES) +char **init_mineboard(WINDOW *gameWin, int WIDTH, int HEIGHT, int NMINES) { int i; char **mineboard = (char **)malloc(WIDTH * sizeof(char *)); @@ -181,15 +190,12 @@ void init_mineboard(WINDOW *gameWin, int WIDTH, int HEIGHT, int NMINES) } else { - fill_spaces(mineboard, WIDTH, HEIGHT, NMINES); place_mines(mineboard, WIDTH, HEIGHT, NMINES); add_adj(mineboard, WIDTH, HEIGHT); - - print(gameWin, mineboard, WIDTH, HEIGHT); - filewrite(mineboard, WIDTH, HEIGHT); + fill_spaces(mineboard, WIDTH, HEIGHT, NMINES); } - free(mineboard); + return mineboard; } @@ -226,10 +232,11 @@ bool is_mine(char **mineboard, int row, int col) bool outof_bounds(int row, int col, int WIDTH, int HEIGHT) { - return (row < 0 || row > WIDTH || col < 0 || col > HEIGHT) ? true : false; + return (row < 0 || row > WIDTH-1 || col < 0 || col > HEIGHT-1) ? true : false; } + int8_t adj_mines(char **mineboard, int row, int col, int WIDTH, int HEIGHT) { int8_t numAdj = 0; @@ -253,7 +260,7 @@ void fill_spaces(char **mineboard, int WIDTH, int HEIGHT, int NMINES) for (i = 0; i < WIDTH; i++) for (j = 0; j < HEIGHT; j++) - if (mineboard[i][j] != MINE) + if (mineboard[i][j] != MINE && mineboard[i][j] == '0') mineboard[i][j] = '-'; }