#include <stdio.h> #include <stdlib.h> #include <time.h> using namespace std; #define COUNT 10 #define ROW 9 #define COL 9 #define ROWS ROW+2 #define COLS COL+2 void game(); int Get(char Board[ROWS][COLS], int x, int y); void Find(char Board[ROWS][COLS], char Shows[ROWS][COLS]); void InitBoard(char Board[ROWS][COLS],char ch); void Display(char Board[ROWS][COLS]); void Blank(char Board[ROWS][COLS], char Shows[ROWS][COLS], int x, int y); void Set(char Board[ROWS][COLS]); void menu(); int main() { int input = 0; do { menu(); scanf("%d", &input); switch (input) { case 1: game(); break; case 0: printf("程序退出!\n"); break; default: break; } } while (input); return 0; } void menu() { printf("**************************\n"); printf("****** 1.play ******\n"); printf("****** 0.exit ******\n"); printf("**************************\n"); } void game() { srand((unsigned int)time(NULL)); char Board[ROWS][COLS]; char Shows[ROWS][COLS]; InitBoard(Board, '0'); InitBoard(Shows, '*'); Display(Shows); Set(Board); #if 0 printf("*********************\n"); printf("*********************\n"); Display(Board); #endif Find(Board, Shows); } void InitBoard(char Board[ROWS][COLS], char ch) { for (int i = 0; i < ROWS; i++) { for (int j = 0; j < ROWS; j++) { Board[i][j] = ch; } } } void Display(char Shows[ROWS][COLS]) { for (int i = 0; i <= ROW; i++) { printf("%d | ", i); for (int j = 1; j <= ROW; j++) { if (i == 0) { printf("%d ", j); } if (i >= 1) { if (Shows[i][j] == '0') { Shows[i][j] = ' '; } printf("%c ", Shows[i][j]); } } printf("\n"); if (i == 0) printf("0-|------------------\n"); } } void Set(char Board[ROWS][COLS]) { int x = 1; int y = 1; int count = COUNT; while (count) { x = rand() % ROW + 1; y = rand() % COL + 1; if (Board[x][y] == '0') { Board[x][y] = '1'; count--; } } } int g_win = 0; void Find(char Board[ROWS][COLS], char Shows[ROWS][COLS]) { int x = 0; int y = 0; g_win = ROW * COL - COUNT; while (g_win > 0) { printf("输入坐标->\n"); printf("x: "); scanf("%d", &x); printf("y: "); scanf("%d", &y); if (x >= 1 && x <= 9 && y >= 1 && y <= 9) { if (Board[x][y] == '1') { printf("恭喜你,你被炸死了~\n"); Display(Board); printf("告诉你死法如何?\n"); break; } else { Blank(Board, Shows, x, y); Display(Shows); g_win--; } } else { printf("输入坐标错误,重新输入吧~\n"); } } if (g_win == ROW * COL - COUNT) printf("通关!\n"); } int Get(char Board[ROWS][COLS], int x, int y) { return Board[x - 1][y - 1] + Board[x - 1][y] + Board[x - 1][y + 1] + Board[x][y - 1] + Board[x][y + 1] + Board[x + 1][y - 1] + Board[x + 1][y] + Board[x + 1][y + 1] - 8 * '0'; } void Blank(char Board[ROWS][COLS], char Shows[ROWS][COLS], int x, int y) { if (x >= 1 && x <= ROW && y >= 1 && y <= COL) { int count = Get(Board, x, y); if (count == 0 && Board[x][y] != '1'&& Shows[x][y] == '*') { Shows[x][y] = ' '; for (int i = x - 1; i <= x + 1; i++) { for (int j = y - 1; j <= y + 1; j++) { Blank(Board, Shows, i, j); } } } else { Shows[x][y] = count + '0'; } } }
一起扫雷吧~