第二个小游戏,扫雷

    xiaoxiao2022-07-05  156

    game.h #pragma once #define _CRT_SECURE_NO_WARNINGS #include<stdlib.h> #include<stdio.h> #include<time.h> #define ROW 9 #define COL 9 #define ROWS ROW+2 #define COLS COL+2 void InitBoard(char board[ROWS][COLS], int row, int col, char ret); void DisplayBoard(char board[ROWS][COLS], int row, int col); void SetMine(char board[ROWS][COLS], int row, int col); char GetMineCount(char mine[ROWS][COLS], int x, int y); void SweepMine(char mine[ROWS][COLS], char show[ROWS][COLS]); void RmoveMine(char mine[ROWS][COLS], int x, int y); void Expand(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y); #define EASY_COUNT 10 game.c #include"game.h" void InitMine(char mine[ROWS][COLS], int row, int col) { int i = 0; int j = 0; /*char set; scanf("%c",&set);*/ for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { mine[i][j] = '0'; } } } void InitShow(char show[ROWS][COLS], int row, int col) { int i = 0; int j = 0; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { show[i][j] = '*'; } } } void InitBoard(char board[ROWS][COLS], int rows, int cols, char ret) { int x = 0,y=0; for (x = 0; x < rows; x++) { for (y = 0; y < cols; y++) { board[x][y] = ret; } } } void DisplayBoard(char board[ROWS][COLS], int row, int col) { int i = 0, j = 0; printf(" "); for (i = 0; i < row - 1; i++) { printf(" %d", i); } printf("\n"); for (i = 0; i < row - 1; i++) { printf("- ", i); for (j = 0; j < col - 1; j++) { printf("%c ", board[i][j]); } printf("\n"); } } void SetMine(char mine[ROWS][COLS], int row, int col) { int x = 0; int y = 0; int minecount = EASY_COUNT; srand((unsigned)time(NULL)); while (minecount) { x = rand() % (ROW)+1; y = rand() % (COL)+1; if (mine[x][y] == '0') { mine[x][y] = '1'; minecount--; } } } char GetMineCount(char mine[ROWS][COLS], int x, int y) //统计周围雷的个数函数 { int minecount = 0; if (mine[x - 1][y] == '1') minecount++; if (mine[x - 1][y + 1] == '1') minecount++; if (mine[x - 1][y + 1] == '1') minecount++; if (mine[x][y + 1] == '1') minecount++; if (mine[x + 1][y + 1] == '1') minecount++; if (mine[x + 1][y] == '1') minecount++; if (mine[x + 1][y - 1] == '1') minecount++; if (mine[x][y - 1] == '1') minecount++; if (mine[x - 1][y - 1] == '1') minecount++; return minecount; } void SweepMine(char mine[ROWS][COLS], char show[ROWS][COLS]) //排雷函数 { int x = 0; int y = 0; int clear = 0; //已经排雷个数 int minecount = 0; //周围雷数 while (clear < (ROW)*(COL)-EASY_COUNT) { printf("开始排雷,请输入坐标:\n"); scanf("%d%d", &x, &y); if (mine[x][y] == '1') { if (clear == 0) //第一次踩雷移走 { RmoveMine(mine, x, y); DisplayBoard(mine, ROWS, COLS); printf("\n"); minecount = GetMineCount(mine, x, y); if (minecount == 0) { show[x][y] = ' '; clear++; Expand(mine, show, x, y); DisplayBoard(show, ROWS, COLS); } else { show[x][y] = minecount + '0'; DisplayBoard(show, ROWS, COLS); } } else { printf("很遗憾,你被炸死了!\n"); DisplayBoard(mine, ROWS, COLS); break; } } else if (mine[x][y] != '1') { minecount = GetMineCount(mine, x, y); if (minecount == 0) { show[x][y] = ' '; } else { show[x][y] = minecount + '0'; } clear++; Expand(mine, show, x, y); DisplayBoard(show, ROWS, COLS); } if (clear == (ROW)*(COL)-EASY_COUNT) { printf("扫雷成功\n"); } } } void RmoveMine(char mine[ROWS][COLS], int x, int y)//第一次踩雷 移走 { mine[x][y] = '0'; GetMineCount(mine, x, y); while (1) { int x1 = rand() % (ROW)+1; int y1 = rand() % (COL)+1; if (mine[x1][y1] != '1' && ((x1 != x) && (y1 != y))) { mine[x1][y1] = '1'; break; } } } void Expand(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)//展开函数 { if ((x >= 1) && (y >= 1) && (x <= ROW) && (y <= COL)) { if (GetMineCount(mine, x, y) == 0) //周围没有雷 { show[x][y] = ' '; //置为空白 if (show[x - 1][y - 1] == '*') { Expand(mine, show, x - 1, y - 1); } if (show[x - 1][y] == '*') { Expand(mine, show, x - 1, y); } if (show[x - 1][y + 1] == '*') { Expand(mine, show, x - 1, y + 1); } if (show[x][y + 1] == '*') { Expand(mine, show, x, y + 1); } if (show[x + 1][y + 1] == '*') { Expand(mine, show, x + 1, y + 1); } if (show[x + 1][y] == '*') { Expand(mine, show, x + 1, y); } if (show[x + 1][y - 1] == '*') { Expand(mine, show, x + 1, y - 1); } if (show[x][y - 1] == '*') { Expand(mine, show, x, y - 1); } } } } text.c #include"game.h" void menu() { printf(" 《扫雷游戏》 \n"); printf(" 1 . play game \n"); printf(" 0 . exit game \n"); printf("请选择\n"); } void game() { int x = 0; int y = 0; int count = 0; char mine[ROWS][COLS] = { 0 }; char show[ROWS][COLS] = { 0 }; InitBoard(show, ROWS, COLS, '*'); DisplayBoard(show, ROWS, COLS); printf("\n"); InitBoard(mine, ROWS, COLS, '0'); /*DisplayBoard(mine,ROWS,COLS);*/ SetMine(mine, ROW, COL); /*DisplayBoard(mine,ROWS,COLS);*/ printf("\n"); SweepMine(mine ,show); } int main() { int input = 0; do { menu(); scanf("%d", &input); switch (input) { case 1: game(); break; case 0: printf("退出游戏\n"); break; default: printf("输入有误,请重新输入\n"); break; } } while (input); system("pause"); return 0; }
    最新回复(0)