uni

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

commit 88f0f76181d45ecd30ad14c80418ba5a699b5e66
parent 5498eb5bb195dfc4e541b6f5b8cd093a93b68bca
Author: Christos Margiolis <christos@margiolis.net>
Date:   Sat, 11 Jan 2020 00:08:29 +0200

started minesweeper

Diffstat:
Massignment-1.5-arrays-pointers-files/minesweeper/Makefile | 2+-
Dassignment-1.5-arrays-pointers-files/minesweeper/include/ccolors.h | 32--------------------------------
Massignment-1.5-arrays-pointers-files/minesweeper/include/minesweeper.h | 28++++++++++++++++++++++++++++
Massignment-1.5-arrays-pointers-files/minesweeper/src/main.c | 19++++++++++++++++++-
Massignment-1.5-arrays-pointers-files/minesweeper/src/minesweeper.c | 212+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
Massignment-1.5-arrays-pointers-files/minesweeper/todo.md | 10++++++++--
6 files changed, 264 insertions(+), 39 deletions(-)

diff --git a/assignment-1.5-arrays-pointers-files/minesweeper/Makefile b/assignment-1.5-arrays-pointers-files/minesweeper/Makefile @@ -11,7 +11,7 @@ MKDIR_P = mkdir -p CPPFLAGS += -Iinclude CFLAGS += -Wall LDFLAGS += -Llib -LDLIBS += -lm +LDLIBS += -lm -lncurses .PHONY: all clean diff --git a/assignment-1.5-arrays-pointers-files/minesweeper/include/ccolors.h b/assignment-1.5-arrays-pointers-files/minesweeper/include/ccolors.h @@ -1,31 +0,0 @@ -#ifndef C_COLORS_H -#define C_COLORS_H - -#include <stdio.h> - -enum options {RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, BOLD_RED, BOLD_GREEN, BOLD_YELLOW, BOLD_BLUE, BOLD_MAGENTA, BOLD_CYAN, STANDARD}; -enum options color; - -void set_color(enum options color) -{ - switch (color) - { - case RED: printf("\033[0;31m"); break; - case GREEN: printf("\033[0;32m"); break; - case YELLOW: printf("\033[0;33m"); break; - case BLUE: printf("\033[0;34m"); break; - case MAGENTA: printf("\033[0;35m"); break; - case CYAN: printf("\033[0;36m"); break; - - case BOLD_RED: printf("\033[1;31m"); break; - case BOLD_GREEN: printf("\033[1;32m"); break; - case BOLD_YELLOW: printf("\033[1;33m"); break; - case BOLD_BLUE: printf("\033[1;34m"); break; - case BOLD_MAGENTA: printf("\033[1;35m"); break; - case BOLD_CYAN: printf("\033[1;36m"); break; - - case STANDARD: printf("\033[0m"); break; - } -} - -#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 @@ -1,8 +1,36 @@ #ifndef MINESWEEPER_H #define MINESWEEPER_H +#if defined linux || defined __unix__ +#include <ncurses.h> +#elif defined _WIN32 || defined _WIN64 +#include <pdcurses.h> +#endif + #include <stdbool.h> +void main_win(); +void start(); + +int set_width(struct _win_st*, int); +int set_height(struct _win_st*, int); +int set_minesnum(struct _win_st*, int); + +void game_win(int, int, int); +void init_dispboard(struct _win_st*, int, int); +void fill_dispboard(char **, int, int); +void init_mineboard(struct _win_st*, int, int, int); +void fill_mineboard(char **, int, int, int); + +void adj_mines(char **, int, int; + +void fill_spaces(char **, int, int, int); + +bool is_mine(char **, int, int); +void transfer(char **, char **, int, int); +void reveal(struct _win_st*, char **, int, int); +void game_over(struct _win_st*); +void print(struct _win_st*, char **, int, int); #endif \ No newline at end of file diff --git a/assignment-1.5-arrays-pointers-files/minesweeper/src/main.c b/assignment-1.5-arrays-pointers-files/minesweeper/src/main.c @@ -1,11 +1,28 @@ -#include <stdio.h> +#if defined linux || defined __unix__ +#include <ncurses.h> +#elif defined _WIN32 || defined _WIN64 +#include <pdcurses.h> +#endif + #include <stdbool.h> #include "minesweeper.h" int main(int argc, char **argv) { + initscr(); + noecho(); + cbreak(); + WINDOW *mainWin = newwin(0, 0, 0, 0); + box(mainWin, 0, 0); + refresh(); + wrefresh(mainWin); + keypad(mainWin, true); + + start(); + + endwin(); return 0; } \ 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 @@ -1,6 +1,212 @@ -#include <stdio.h> +#if defined linux || defined __unix__ +#include <ncurses.h> +#elif defined _WIN32 || defined _WIN64 +#include <pdcurses.h> +#endif + #include <stdlib.h> #include <stdbool.h> +#include <string.h> #include <time.h> #include "minesweeper.h" -#include "ccolors.h"- \ No newline at end of file + + +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 WIDTH = set_width(menuWin, xMax); + int HEIGHT = set_height(menuWin, yMax); + int MINES = set_minesnum(menuWin, WIDTH * HEIGHT); + + game_win(WIDTH, HEIGHT, MINES); + getchar(); +} + + +int set_width(WINDOW *menuWin, int xMax) +{ + int WIDTH; + + do + { + mvwprintw(menuWin, 1, 1, "Width: "); + wrefresh(menuWin); + scanw("%d", &WIDTH); + mvwprintw(menuWin, 1, strlen("Width: ") + 1, "%d", WIDTH); + wrefresh(menuWin); + } while (WIDTH < 5 || WIDTH > 114 /*WIDTH > xMax*/); + + return WIDTH; +} + + +int set_height(WINDOW *menuWin, int yMax) +{ + int HEIGHT; + + do + { + mvwprintw(menuWin, 2, 1, "Height: "); + wrefresh(menuWin); + scanw("%d", &HEIGHT); + mvwprintw(menuWin, 2, strlen("Height: ") + 1, "%d", HEIGHT); + wrefresh(menuWin); + } while (HEIGHT < 5 || HEIGHT > 41 /*HEIGHT > yMax*/); + + return HEIGHT; +} + + +int set_minesnum(WINDOW *menuWin, int DIMENSIONS) +{ + int MINES; + + do + { + mvwprintw(menuWin, 3, 1, "Mines: "); + wrefresh(menuWin); + scanw("%d", &MINES); + mvwprintw(menuWin, 3, strlen("Mines: ") + 1, "%d", MINES); + wrefresh(menuWin); + } while (MINES < 1 || MINES > DIMENSIONS); + + return MINES; +} + + +void game_win(int WIDTH, int HEIGHT, int MINES) +{ + int yMax, xMax; + getmaxyx(stdscr, yMax, xMax); + + WINDOW *gameWin = newwin(43, xMax-10, (yMax/2) - 25, 5); + box(gameWin, 0, 0); + refresh(); + wrefresh(gameWin); + keypad(gameWin, true); + + init_dispboard(gameWin, WIDTH, HEIGHT); + init_mineboard(gameWin, WIDTH, HEIGHT, MINES); +} + + +void init_dispboard(WINDOW *gameWin, int WIDTH, int HEIGHT) +{ + int i; + char **dispboard = (char **)malloc(WIDTH * sizeof(char *)); + for (i = 0; i < WIDTH; i++) + *(dispboard + i) = (char *)malloc(HEIGHT); + + fill_dispboard(dispboard, WIDTH, HEIGHT); + print(gameWin, dispboard, WIDTH, HEIGHT); + + free(dispboard); +} + +void fill_dispboard(char **dispboard, int WIDTH, int HEIGHT) +{ + int i, j; + + for (i = 0; i < WIDTH; i++) + for (j = 0; j < HEIGHT; j++) + *(*(dispboard + i) + j) = '#'; +} + + +void init_mineboard(WINDOW *gameWin, int WIDTH, int HEIGHT, int MINES) +{ + int i; + char **mineboard = (char **)malloc(WIDTH * sizeof(char *)); + for (i = 0; i < WIDTH; i++) + *(mineboard + i) = (char *)malloc(HEIGHT); + + fill_spaces(mineboard, WIDTH, HEIGHT, MINES); + fill_mineboard(mineboard, WIDTH, HEIGHT, MINES); + + print(gameWin, mineboard, WIDTH, HEIGHT); + + free(mineboard); +} + + +void fill_mineboard(char **mineboard, int WIDTH, int HEIGHT, int MINES) +{ + int i, wRand, hRand; + + srand(time(NULL)); + + for (i = 0; i < MINES; i++) + { + for (int j = 0; j < MINES; j++) + { + wRand = rand() % WIDTH; + hRand = rand() % HEIGHT; + *(*(mineboard + wRand) + hRand) = '*'; + //adj_mines(mineboard, i, j); + } + } +} + + /* + Cell-->Current Cell (row, col) + N --> North (row-1, col) + S --> South (row+1, col) + E --> East (row, col+1) + W --> West (row, col-1) + N.E--> North-East (row-1, col+1) + N.W--> North-West (row-1, col-1) + S.E--> South-East (row+1, col+1) + S.W--> South-West (row+1, col-1) + */ + + /* + [x-1, y-1][x, y-1][x+1, y-1] + [x-1, y ][x, y ][x+1, y ] + [x-1, y+1][x, y+1][x+1, y+1] + */ + +void adj_mines(char **mineboard, int row, int col) +{ + int numAdj = 0; + + if (*(*(mineboard + (row - 1)) + col) != '*'); +} + + +void fill_spaces(char **mineboard, int WIDTH, int HEIGHT, int MINES) +{ + int i, j; + + for (i = 0; i < WIDTH; i++) + { + for (j = 0; j < HEIGHT; j++) + { + if ((*(*mineboard + i) + j) != '*') + *(*(mineboard + i) + j) = '-'; + } + } +} + + +void print(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); + } + } +}+ \ No newline at end of file diff --git a/assignment-1.5-arrays-pointers-files/minesweeper/todo.md b/assignment-1.5-arrays-pointers-files/minesweeper/todo.md @@ -29,4 +29,10 @@ // Get file name // Write to file -// free()- \ No newline at end of file +// free() + +```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