|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #define SIZE 9
- #define MINES 10
- char board[SIZE][SIZE];
- char visible[SIZE][SIZE];
- void initializeBoard() {
- int i,j;
- for (i = 0; i < SIZE; i++) {
- for (j = 0; j < SIZE; j++) {
- board[i][j] = ' ';
- visible[i][j] = '-';
- }
- }
- }
- void printBoard() {
- printf(" 1 2 3 4 5 6 7 8 9\n");
- int i,j;
- for (i = 0; i < SIZE; i++) {
- printf("%d ", i + 1);
- for (j = 0; j < SIZE; j++) {
- printf("%c ", visible[i][j]);
- }
- printf("\n");
- }
- }
- void placeMines() {
- srand(time(NULL));
- int count = 0;
- while (count < MINES) {
- int x = rand() % SIZE;
- int y = rand() % SIZE;
- if (board[x][y] != '*') {
- board[x][y] = '*';
- count++;
- }
- }
- }
- int countAdjacentMines(int x, int y) {
- int count = 0,i,j;
- for (i = -1; i <= 1; i++) {
- for (j = -1; j <= 1; j++) {
- int newX = x + i;
- int newY = y + j;
- if (newX >= 0 && newX < SIZE && newY >= 0 && newY < SIZE && board[newX][newY] == '*') {
- count++;
- }
- }
- }
- return count;
- }
- void reveal(int x, int y) {
- if (x < 0 || x >= SIZE || y < 0 || y >= SIZE || visible[x][y] != '-') {
- return;
- }
- visible[x][y] = countAdjacentMines(x, y) + '0';
- if (visible[x][y] == '0') {
- int i,j;
- for (i = -1; i <= 1; i++) {
- for (j = -1; j <= 1; j++) {
- int newX = x + i;
- int newY = y + j;
- reveal(newX, newY);
- }
- }
- }
- }
- int main() {
- initializeBoard();
- placeMines();
- int gameOver = 0;
- while (!gameOver) {
- printBoard();
- printf("Enter row and column (e.g., 3 4): ");
- int row, col;
- scanf("%d %d", &row, &col);
- if (row < 1 || row > SIZE || col < 1 || col > SIZE) {
- printf("Invalid input. Please try again.\n");
- continue;
- }
- row--;
- col--;
- if (board[row][col] == '*') {
- printf("Game over! You hit a mine!\n");
- gameOver = 1;
- } else {
- reveal(row, col);
- int remaining = SIZE * SIZE - MINES;
- int revealed = 0;
- int i,j;
- for (i = 0; i < SIZE; i++) {
- for (j = 0; j < SIZE; j++) {
- if (visible[i][j] != '-') {
- revealed++;
- }
- }
- }
- if (revealed == remaining) {
- printf("Congratulations! You win!\n");
- gameOver = 1;
- }
- }
- }
- return 0;
- }
复制代码
花了两三天空闲时间写写,对大家来说应该就是很普通的东西,没写注释了,太像八皇后了这东西
|
评分
-
查看全部评分
|