“扫雷”C语言实现(含炸开效果)
game.h
#include#include #include enum Option { rows = 9, cols = 9, Rows = rows + 2, Cols = cols + 2, Difficulty = 10 }; void menu(); void InitBoard(char Board[Rows][Cols],int Row,int Col, char a); void SetMine(char Board[Rows][Cols],int Row,int Col); void Display(char Board[Rows][Cols], int Row, int Col); void Playgame(char Board[Rows][Cols], char ShowBoard[Rows][Cols]); int Countmine(char Board[Rows][Cols], char ShowBoard[Rows][Cols], int Row, int Col); void Openmine(char Board[Rows][Cols], char ShowBoard[Rows][Cols], int Row, int Col);
test.c
#include"game.h" void game() { //创建游戏棋盘 char MineBoard[Rows][Cols] = { 0 }; char ShowBoard[Rows][Cols] = { 0 }; InitBoard(MineBoard, Rows, Cols,'0'); InitBoard(ShowBoard, Rows, Cols, '*');//如何用mem实现 //展示棋盘 //Display(MineBoard, Rows, Cols); Display(ShowBoard, Rows, Cols); printf("n"); //随机放置炸d SetMine(MineBoard, Rows, Cols); //printf("n"); Display(MineBoard, Rows, Cols);//查看是否设置成功 //准备工作完成,玩家正式开始游戏 Playgame(MineBoard, ShowBoard); } void test() { menu(); int input; printf("请输入:>"); scanf_s("%d", &input); switch (input-1) { case 0: game(); break; case 1: printf("正在退出游戏"); break; default: printf("输入错误,请输入1或2"); break; } } int main() { srand((unsigned)time(NULL)); test(); return 0; }
game.c
#include"game.h" void menu() { printf("*************************n"); printf("*******1.play************n"); printf("*******2.exit************n"); printf("*************************n"); } void InitBoard(char Board[Rows][Cols], int Row, int Col,char a) // { int i , j ; for (i = 0;i < Row; i++) { for (j = 0;j < Col; j++) { Board[i][j] = a; } } } void SetMine(char Board[Rows][Cols], int Row, int Col)// { int flag = 0; while (flag< Difficulty) { int x = rand() % 9+1; int y = rand() % 9+1; if (Board[x][y] == '0') { Board[x][y] = '1'; flag++; } } } void Display(char Board[Rows][Cols], int Row, int Col) { int i = 0, j = 0; for (i = 0; i < Rows-1; i++) { printf("n"); for (j = 0; j < Cols-1; j++) { if (i == 0 && j != 0) { printf("%d", j); } else if (j == 0 && i != 0) { printf("%d", i); } else if (j == 0 && i == 0) { printf("0"); } else if (00 && ShowBoard[Row-1][Col] == '*') { Openmine(Board, ShowBoard, Row - 1, Col); } if (Row + 1 < Rows - 1 && ShowBoard[Row+1][Col] == '*') { Openmine(Board, ShowBoard, Row + 1, Col); } if (Col - 1 > 0 && ShowBoard[Row][Col-1] == '*') { Openmine(Board, ShowBoard, Row, Col - 1); } if (Col + 1 < Cols - 1 && ShowBoard[Row][Col+1] == '*') { Openmine(Board, ShowBoard, Row, Col + 1); } if (Row - 1 > 0 && Col - 1 > 0 && ShowBoard[Row-1][Col-1] == '*') { Openmine(Board, ShowBoard, Row - 1, Col - 1); } if (Row - 1 > 0 && Col + 1 < Cols - 1 && ShowBoard[Row-1][Col+1] == '*') { Openmine(Board, ShowBoard, Row - 1, Col+ 1); } if (Col - 1 > 0 && Row + 1 > 0 && ShowBoard[Row+1][Col-1] == '*') { Openmine(Board, ShowBoard, Row + 1, Col - 1); } if (Col + 1 n"); scanf_s("%d%d", &x, &y); if (x > 0 && x <= 9 && y <= 9 && y > 0) { if (Board[x][y] == '0') { Openmine(Board, ShowBoard, x, y);//利用递归实现炸开效果 Display(ShowBoard, Rows, Cols); //检查有没有胜利 int flag = 0; for (int i = 1; i < Rows - 1; i++) { for (int j = 1; j < Cols - 1; j++) { if (ShowBoard[i][j] == '*') { ++flag; } } } if (flag == Difficulty) { printf("你已找出所有炸弹,游戏胜利!"); break; } } else { printf("炸弹bomb!游戏结束n"); Display(Board, Rows, Cols); break; } } else { printf("输入错误,请重新输入1~9的数字"); } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)