commit 928b2e9312359acd7d0fb7cad5f0a09aa301ad40
parent 66ad94eb1c8d51a2802983477bfa949ef4ee90e4
Author: Christos Margiolis <christos@margiolis.net>
Date: Sat, 11 Jan 2020 16:57:38 +0200
pending minesweeper bug fix
Diffstat:
4 files changed, 140 insertions(+), 60 deletions(-)
diff --git a/assignment-1.5-arrays-pointers-files/minesweeper/include/minesweeper.h b/assignment-1.5-arrays-pointers-files/minesweeper/include/minesweeper.h
@@ -5,10 +5,12 @@
#include <ncurses.h>
#elif defined _WIN32 || defined _WIN64
#include <pdcurses.h>
+#include <stdint.h>
#endif
#include <stdbool.h>
+#define HIDDEN '#'
#define MINE '*'
void main_win();
@@ -24,16 +26,18 @@ void init_dispboard(struct _win_st*, int, int);
void fill_dispboard(char **, int, int);
void init_mineboard(struct _win_st*, int, int, int);
void place_mines(char **, int, int, int);
-int adj_mines(char **, int, int);
+void add_adj(char **, int, int);
+bool is_mine(char **, int, int);
+uint8_t adj_mines(char **, int, int); // uint8_t so it has the same range as char
void fill_spaces(char **, int, int, int);
void selection();
void transfer(char **, char **, int, int);
void reveal(struct _win_st*, char **, char **, int, int);
-bool is_mine(char **, int, int);
void game_over(struct _win_st*);
void print(struct _win_st*, char **, int, int);
+void filewrite(char **, int, 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
@@ -0,0 +1,34 @@
+Mine hit at position (1, 2)
+
+Board overview
+
+- - - - - - - - - - - * - - - - - - - - - - - - - - - - - -
+- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+- - - - - - * - - - - - - - - - - - - - - - - - - - - - - -
+- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+* - - - - - - - - - - - - - - - - - - - - * - - - - - - - -
+- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+- - - - - - - - - * - - - - - - - - - - - - - - - - - - - -
+- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+- - - - - - - - - - - - - - - - - - - - - - - - - - * - - -
+- - - - - - - - - - - - - - - * - - - - - - - - - - - * - -
+- - - - - - - - - - - - - - - - - - - - - * - - - - - - - -
+- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+- - - - - - * - - - - - - - - - - - - - - - - - - - - - - -
+- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+- - - - - - * - - - - - - - - - - - - - - - - - - - - - - -
+- - - - - - - - - - - - - - - - - - - - - - - - * - - - - -
+- - - - - - - - - - - - - - - - - - - - - - - * - - - - - -
+- * - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+- - - - - - - - - - - - - - - - - - - - - - - - - - * - - -
diff --git a/assignment-1.5-arrays-pointers-files/minesweeper/src/main.c b/assignment-1.5-arrays-pointers-files/minesweeper/src/main.c
@@ -4,24 +4,13 @@
#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);
-
+ main_win();
start();
-
endwin();
return 0;
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 <ncurses.h>
#elif defined _WIN32 || defined _WIN64
#include <pdcurses.h>
+#include <stdint.h>
#endif
#include <stdlib.h>
@@ -11,6 +12,20 @@
#include "minesweeper.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;
@@ -117,7 +132,7 @@ void game_win(int WIDTH, int HEIGHT, int NMINES)
int yMax, xMax;
getmaxyx(stdscr, yMax, xMax);
- WINDOW *gameWin = newwin(43, xMax-10, (yMax/2) - 25, 5);
+ WINDOW *gameWin = newwin(43, xMax-10, (yMax/2) - 24, 5);
box(gameWin, 0, 0);
refresh();
wrefresh(gameWin);
@@ -135,9 +150,17 @@ void init_dispboard(WINDOW *gameWin, int WIDTH, int HEIGHT)
for (i = 0; i < WIDTH; i++)
*(dispboard + i) = (char *)malloc(HEIGHT);
- fill_dispboard(dispboard, WIDTH, HEIGHT);
- print(gameWin, dispboard, WIDTH, HEIGHT);
- getchar();
+ if (dispboard == NULL)
+ {
+ mvprintw(1, 1, "Error, not enough memory, exiting...");
+ exit(EXIT_FAILURE);
+ }
+ else
+ {
+ fill_dispboard(dispboard, WIDTH, HEIGHT);
+ print(gameWin, dispboard, WIDTH, HEIGHT);
+ getchar();
+ }
free(dispboard);
}
@@ -148,7 +171,7 @@ void fill_dispboard(char **dispboard, int WIDTH, int HEIGHT)
for (i = 0; i < WIDTH; i++)
for (j = 0; j < HEIGHT; j++)
- *(*(dispboard + i) + j) = '#';
+ *(*(dispboard + i) + j) = HIDDEN;
}
@@ -159,10 +182,20 @@ void init_mineboard(WINDOW *gameWin, int WIDTH, int HEIGHT, int NMINES)
for (i = 0; i < WIDTH; i++)
*(mineboard + i) = (char *)malloc(HEIGHT);
- fill_spaces(mineboard, WIDTH, HEIGHT, NMINES);
- place_mines(mineboard, WIDTH, HEIGHT, NMINES);
+ if (mineboard == NULL)
+ {
+ mvprintw(1, 1, "Error, not enough memory, exiting...");
+ exit(EXIT_FAILURE);
+ }
+ else
+ {
+ fill_spaces(mineboard, WIDTH, HEIGHT, NMINES);
+ place_mines(mineboard, WIDTH, HEIGHT, NMINES);
+ //add_adj(mineboard, WIDTH, HEIGHT);
- print(gameWin, mineboard, WIDTH, HEIGHT);
+ print(gameWin, mineboard, WIDTH, HEIGHT);
+ filewrite(mineboard, WIDTH, HEIGHT);
+ }
free(mineboard);
}
@@ -182,46 +215,39 @@ void place_mines(char **mineboard, int WIDTH, int HEIGHT, int NMINES)
}
}
- /*
- 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]
- */
-
-int adj_mines(char **mineboard, int WIDTH, int HEIGHT)
+
+void add_adj(char **mineboard, int WIDTH, int HEIGHT)
{
- int row, col, numAdj = 0;
+ int i, j;
- for (row = 1; row <= WIDTH; row++)
- {
- for (col = 1; col <= HEIGHT; col++)
- {
- if
- (
- (*(*(mineboard + (row - 1)) + col) != MINE) || // North
- (*(*(mineboard + (row + 1)) + col) != MINE) || // South
- (*(*(mineboard + (row)) + col + 1) != MINE) || // East
- (*(*(mineboard + (row)) + col - 1) != MINE) || // West
- (*(*(mineboard + (row - 1)) + col + 1) != MINE) || // North-East
- (*(*(mineboard + (row - 1)) + col - 1) != MINE) || // North-West
- (*(*(mineboard + (row + 1)) + col + 1) != MINE) || // South-East
- (*(*(mineboard + (row - 1)) + col - 1) != MINE) // South-West
- )
- numAdj++;
- }
- }
+ for (i = 0; i < WIDTH; i++)
+ for (j = 0; j < HEIGHT; j++)
+ if (!is_mine(mineboard, i, j))
+ *(*(mineboard + i) + j) = adj_mines(mineboard, i, j) + '0';
+}
+
+
+bool is_mine(char **mineboard, int row, int col)
+{
+ if (*(*(mineboard + row) + col) == MINE)
+ return true;
+ else
+ return false;
+}
+
+
+uint8_t adj_mines(char **mineboard, int row, int col)
+{
+ uint8_t numAdj = 0;
+
+ if ((*(*(mineboard + row) + col - 1) != MINE)) numAdj++; // North
+ if ((*(*(mineboard + row) + col + 1) != MINE)) numAdj++; // South
+ if ((*(*(mineboard + (row + 1)) + col) != MINE)) numAdj++; // East
+ if ((*(*(mineboard + (row - 1)) + col) != MINE)) numAdj++; // West
+ if ((*(*(mineboard + (row + 1)) + col - 1) != MINE)) numAdj++; // North-East
+ if ((*(*(mineboard + (row - 1)) + col - 1) != MINE)) numAdj++; // North-West
+ if ((*(*(mineboard + (row + 1)) + col + 1) != MINE)) numAdj++; // South-East
+ if ((*(*(mineboard + (row - 1)) + col + 1) != MINE)) numAdj++; // South-West
return numAdj;
}
@@ -254,4 +280,31 @@ void print(WINDOW *gameWin, char **mineboard, int WIDTH, int HEIGHT)
wrefresh(gameWin);
}
}
+}
+
+
+void filewrite(char **mineboard, int WIDTH, int HEIGHT)
+{
+ FILE *mnsOut = fopen("mnsout.txt", "w");
+ int i, j;
+
+ if (mnsOut == NULL)
+ {
+ mvprintw(1, 1, "Error opening file, exiting...");
+ exit(EXIT_FAILURE);
+ }
+ else
+ {
+ fprintf(mnsOut, "Mine hit at position (%d, %d)\n\n", 1, 2);
+
+ fprintf(mnsOut, "Board overview\n\n");
+ for (i = 0; i < WIDTH; i++)
+ {
+ for (j = 0; j < HEIGHT; j++)
+ fprintf(mnsOut, "%c ", *(*(mineboard + i) + j));
+ fprintf(mnsOut, "\n");
+ }
+ mvprintw(1, 1, "Session written to file");
+ refresh();
+ }
}
\ No newline at end of file