// gcc main.c -o minesweeper #include #include #include int width, height, mines; char *field, *flags; void flood_fill(int x, int y) { char* tile = &field[x + y * width]; if (*tile != '#') return; // Count the number of mines around the tile int neighbour_mines = 0; for (int x_offset = -1; x_offset <= 1; x_offset++) { for (int y_offset = -1; y_offset <= 1; y_offset++) { if (tile[x_offset + y_offset * width] == 'M') neighbour_mines++; } } // If we don't find any mines around the tile // We mark the tile as empty and continue the search if (neighbour_mines == 0) { *tile = '_'; flood_fill(x, y-1); flood_fill(x-1, y); flood_fill(x+1, y); flood_fill(x, y+1); return; } // If we do find mines around the tile // We write the number of mines found *tile = neighbour_mines + '0'; } int main(int argc, char** argv) { if (argc != 4) { printf("Run ./minesweeper [width] [height] [mines]\n"); return 1; } // Setup // Converting the 3 parameters to integers width = atoi(argv[1]) + 2; height = atoi(argv[2]) + 2; mines = atoi(argv[3]); int cursor_x = 1, cursor_y = 1; // Prevent the need for pressing enter struct termios termconf; tcgetattr(0, &termconf); termconf.c_lflag &= ~(ICANON); tcsetattr(0, TCSANOW, &termconf); // Allocate field and clear field = (char*)malloc(width*height); flags = (char*)malloc(width*height); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int is_border = (x == 0 || y == 0 || x == width-1 || y == height-1); field[x+y*width] = is_border ? '+' : '#'; flags[x+y*width] = 0; } } // Place mines randomly while (mines > 0) { unsigned int tile_index = rand() % (width*height); if (field[tile_index] == '#') { field[tile_index] = 'M'; mines--; } } int empty_tiles_left = -1; while (1) { // Clear terminal printf("\033c"); // Print field for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { char c = field[x+y*width]; // If the tile is a bomb - print it as a regular tile if (c == 'M') c = '#'; // If the tile has flag on it show it. if (c == '#' && flags[x+y*width] == 1) c = '.'; // Print the cursor instead of the tile if (x == cursor_x && y == cursor_y) c = 'X'; printf("%c", c); } printf("\n"); } printf("Movement: Arrowkeys\nSelect tile: Spacebar\nFlag tile: f\n"); if (empty_tiles_left == 0) { printf("Success!"); return 0; } // Get input char input = getchar(); // Handle movement cursor_x += (input == 'D') ? -1 : (input == 'C') ? 1 : 0; cursor_y += (input == 'A') ? -1 : (input == 'B') ? 1 : 0; // Clamping cursor_x and cursor_y to within the field cursor_x = (cursor_x < 0) ? 0 : (cursor_x >= width) ? width-1 : cursor_x; cursor_y = (cursor_y < 0) ? 0 : (cursor_y >= height) ? height-1 : cursor_y; char* cursor_tile = &field[cursor_x+cursor_y*width]; char* flag_tile = &flags[cursor_x+cursor_y*width]; // Handle placing flag if (input == 'f') *flag_tile = !*flag_tile; // Handle "digging" else if (input == ' ' && !*flag_tile) { // Hit a mine if (*cursor_tile == 'M') break; // Hit an empty tile if (*cursor_tile == '#') { flood_fill(cursor_x, cursor_y); empty_tiles_left = 0; for (int i = 0; i < width*height; i++) { if (field[i] == '#') empty_tiles_left++; } } } } printf("Boom!"); return 0; }