From af20e4df32d6a16ede920d877b02193859c2adc7 Mon Sep 17 00:00:00 2001 From: r4tss <68610647+r4tss@users.noreply.github.com> Date: Tue, 25 Jan 2022 18:44:43 +0100 Subject: Added new projects --- nsweeper.c | 182 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 182 insertions(+) create mode 100644 nsweeper.c (limited to 'nsweeper.c') diff --git a/nsweeper.c b/nsweeper.c new file mode 100644 index 0000000..9db1cc4 --- /dev/null +++ b/nsweeper.c @@ -0,0 +1,182 @@ +// Minesweeper game + +#include +#include +#include +#include + +#define len(array) sizeof(array)/sizeof(*array) + +struct tile +{ + int y; + int x; + int n; // Number of mines around tile + bool d; // Display or not + bool m; // Mine or not +}; + +void cac(struct tile *t, int tS, int w, int y, int x, bool init, bool *o) +{ + int mines = 0; + bool no = false; + for(int i = 0;i 0) && (i - w - 1 + cx + (cy * w)) < tS) + mines++; + else + cac(t, tS, w, cy, cx, false, &no); + clear(); + mvprintw(0, 0, "%i | %i, %i | %i, %i", t[i - 1 - w + (w * cy) + cx].m, cy, cx, i, i - 1 - w + cx + (cy * w)); + refresh(); + getch(); + } + } + if(init || (!init && mines == 0)) + t[i].d = false; + t[i].n = mines; + } + } + } +} + +int main() +{ + srand(time(NULL)); + + // Most of these shouldn't be initialized, rather given a value before gameplay starts + // wh is offset by +2 and ww is offset by +1 when it comes to logic but actual size wise ww is offset by wh * 2 + 1 + int phase = 1, wh = 8, ww = wh * 2, cy = 0, cx = 0, ty, tx, tc, ti; + bool gO = false, t; + + initscr(); + noecho(); + refresh(); + + switch(phase) + { + case 0: + //WINDOW * m = newwin(5, 10, ); + break; + case 1: + // Move window init to after phase 0 later + WINDOW * w = newwin(wh + 2, ww + 1, 0, 0); + + struct tile map[wh * wh]; + for(int i = 0, x = 0, y = 0;i= ww) + { + x = 0; + y++; + } + } + + // Loop to get locations of mines in array mL, this will be moved into phase 0 later + tc = 0; + while(tc < wh * wh / 4) + { + t = false; + // (upper - lower + 1) + lower + ty = (rand() % (wh - 1)) + 1; + tx = (rand() % (ww - 1)) + 1; + + if(tx % 2 != 0) + tx++; + + for(int i = 0;i 1) + cx-=2; + break; + case 100: + if(cx < ww - 3) + cx+=2; + break; + case 119: + if(cy >= 1) + cy--; + break; + case 115: + if(cy < wh - 1) + cy++; + break; + case 10: + cac(map, len(map), ww / 2, cy, cx, true, &gO); + break; + } + } + mvwprintw(w, wh / 2, ww / 3, "Game Over"); + curs_set(0); + wrefresh(w); + getch(); + break; + } + + endwin(); + return 0; +} -- cgit v1.2.3