Ich arbeite derzeit an der Programmierung des Spiels von Minesweeper für ein Schulprojekt, aber ich bin auf der Seite fest, wo wir die Mine, die der Benutzer will enthüllen und wenn es eine 0 ist, decken alle Minen um es herum und so weiter. Ich habe mein Spielbrett um 2 Zeilen und Spalten größer als die tatsächliche benutzerdefinierte Spielbrettgröße gemacht, so dass ich einen Rand darum habe, um die Anzahl der Minen um Randfälle zu zählen. (Num_rows und NUM_COLS sind die Dimensionen, die das Board eigentlich sein sollte)Rekursive Funktion in Minesweeper
x x x x x x x Quick illustration of what my board looks like.
x . . . . . x '.' in this picture represent tiles of the actual game
x . . . . . x board while 'x' is the border around it
x . . . . . x
x . . . . . x
x x x x x x x
Der Code, den ich unten geschrieben immer in Segmentierung Fehler führt. Ich denke, dass es ein Problem ist, wenn ich in die Grenze renne. Hat jemand irgendwelche Vorschläge? Entschuldigung, wenn der Code wirklich schlecht/schwer zu lesen ist. Ich bin ein Anfänger für C und Programmierung im Allgemeinen. Danke im Voraus!
typedef struct Tile_struct { //board struct definitions
int visibility;
int num_mines_around;
} Tile;
typedef struct Board_struct {
int num_rows;
int num_cols;
int num_mines;
Tile** the_board;
} Board;
//This is how I allocated memory for the board
void CreateBoard(char** argv, Board* board) {
int row, col;
board->num_rows = atoi(argv[1]);
board->num_cols = atoi(argv[2]);
board->num_mines = atoi(argv[3]);
board->the_board = (Tile**)malloc(board->num_rows * sizeof(Tile*));
for (row = 0; row < (board->num_rows) + 2; row++) {
board->the_board[row] = (Tile*)malloc(board->num_cols *
sizeof(Tile));
for (col = 0; col < (board->num_cols) + 2; col++) {
board->the_board[row][col].visibility = 0; //hide all tiles
}
}
}
void RevealTiles(Board* board, int row, int col) {
int i, j;
if (row <= 0 || row >= board->num_rows + 1 || col <= 0 || col >=
board->num_cols + 1) {
return;
}
else if (board->the_board[row][col].num_mines_around != 0) {
board->the_board[row][col].visibility = 4; //reveal that tile
}
else {
board->the_board[row][col].visibility = 4;
for (i = row - 1; i <= row + 1; i++) {
for (j = col - 1; j <= col + 1; j++) {
if (i == row && j == col) {
continue;
}
RevealTiles(board, i, j);
}
}
}
}
Bitte lesen [mcve]. – Stargateur
Können Sie uns bitte zeigen, wie Sie den Vorstand erklärt (und initialisiert) haben? –
Bearbeitet, danke! – mc2017