老程序员教你一天时间完成C语言扫雷游戏
目录
- 扫雷游戏
- 1.菜单
- 2.初始化棋盘
- 3.打印棋盘
- 4.布置雷
- 5.排查雷
- 6.整体效果展示
- 7.代码分享
- 7.1 test.c
- 7.2 game.c
- 7.3 game.h
- 总结
扫雷游戏
今天我们就要设计一款这样的游戏,首先需要设置棋盘,然后对棋盘进行随机布雷,最后在排雷。
1.菜单
首先制作一个简易菜单
int main() { int input; srand((unsigned int)time(NULL)); do { menu(); printf("请选择:\n"); scanf("%d", &input); switch (input) { case 1: game(); break; case 0: printf("退出游戏\n"); break; default: printf("选择错误\n"); break; } } while(input); return 0; }
效果如下:
2.初始化棋盘
首先需要设置两个二维数组,分别存放雷的信息和排查出雷的信息
char mine[ROWS][COLS] = { 0 };//存放雷的信息 char show[ROWS][COLS] = { 0 };//存放排查出雷的信息
然后对其进行初始化
//初始化棋盘 void InitBoard(char board[ROWS][COLS], int rows, int cols,char set) { for (int i = 0;i < rows;i++) { for (int j = 0;j < cols;j++) { board[i][j] = set; } } }
3.打印棋盘
棋盘的打印可以直接遍历二维数组,并加上一些分割线
void PrintBoard(char board[ROWS][COLS], int row, int col) { printf("--------扫雷--------\n"); for (int i = 0;i <= 9;i++) { printf("%d ", i); } printf("\n"); for (int i = 1;i < row+1 ;i++) { printf("%d ", i); for (int j = 1;j < col+1;j++) { printf("%c ", board[i][j]); } printf("\n"); } printf("--------------------\n"); }
打印出来的棋盘如下
4.布置雷
棋盘打印出来了,接下来就是在棋盘上随机布雷
void SetMine(char board[ROWS][COLS], int row, int col) { int count = EASY_COUNT; while (count) { int x = rand() % 9 + 1; int y = rand() % 9 + 1; if (board[x][y] != '1') { board[x][y] = '1'; count--; } } }
5.排查雷
排查雷采用玩家输入坐标,对比观察该坐标是否有雷并给出提示,提示显示该坐标周围有几个雷或者是否被炸死
//排查雷 int GetMineCount(char board[ROWS][COLS],int x,int y) { int count=0; for (int i = x - 1;i <= x + 1;i++) { for (int j = y - 1;j <= y + 1;j++) { count += (board[i][j]-'0'); } } return count; } void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) { int x, y,win=0; while (win<row*col-EASY_COUNT) { printf("请输入坐标\n"); scanf("%d %d", &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col) { if (mine[x][y] != '1') { int count = GetMineCount(mine,x,y); show[x][y] = count + '0'; PrintBoard(show, ROW, COL); win++; } else { printf("很遗憾,你被炸死了\n"); PrintBoard(mine, ROW, COL); break; } } else { printf("坐标非法,请重新输入\n"); } } if (win == row * col - EASY_COUNT) { printf("恭喜你,排雷成功\n"); PrintBoard(mine, ROW, COL); } }
6.整体效果展示
7.代码分享
7.1 test.c
#define _CRT_SECURE_NO_WARNINGS 1 #include"game.h" void menu() { printf("*********************************\n"); printf("********* 1. play **********\n"); printf("********* 0. exit **********\n"); printf("*********************************\n"); } void game() { char mine[ROWS][COLS] = { 0 };//存放雷的信息 char show[ROWS][COLS] = { 0 };//存放排查出雷的信息 //初始化棋盘 InitBoard(mine, ROWS, COLS,'0'); InitBoard(show, ROWS, COLS,'*'); //打印棋盘 //PrintBoard(mine, ROW, COL); PrintBoard(show, ROW, COL); //布置雷 SetMine(mine, ROW, COL); //PrintBoard(mine, ROW, COL); //排查雷 FindMine(mine, show, ROW, COL); } int main() { int input; srand((unsigned int)time(NULL)); do { menu(); printf("请选择:\n"); scanf("%d", &input); switch (input) { case 1: game(); break; case 0: printf("退出游戏\n"); break; default: printf("选择错误\n"); break; } } while(input); return 0; }
7.2 game.c
#define _CRT_SECURE_NO_WARNINGS 1 #include"game.h" //初始化棋盘 void InitBoard(char board[ROWS][COLS], int rows, int cols,char set) { for (int i = 0;i < rows;i++) { for (int j = 0;j < cols;j++) { board[i][j] = set; } } } //打印棋盘 void PrintBoard(char board[ROWS][COLS], int row, int col) { printf("--------扫雷--------\n"); for (int i = 0;i <= 9;i++) { printf("%d ", i); } printf("\n"); for (int i = 1;i < row+1 ;i++) { printf("%d ", i); for (int j = 1;j < col+1;j++) { printf("%c ", board[i][j]); } printf("\n"); } printf("--------------------\n"); } //设置雷 void SetMine(char board[ROWS][COLS], int row, int col) { int count = EASY_COUNT; while (count) { int x = rand() % 9 + 1; int y = rand() % 9 + 1; if (board[x][y] != '1') { board[x][y] = '1'; count--; } } } //排查雷 int GetMineCount(char board[ROWS][COLS],int x,int y) { int count=0; for (int i = x - 1;i <= x + 1;i++) { for (int j = y - 1;j <= y + 1;j++) { count += (board[i][j]-'0'); } } return count; } void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) { int x, y,win=0; while (win<row*col-EASY_COUNT) { printf("请输入坐标\n"); scanf("%d %d", &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col) { if (mine[x][y] != '1') { int count = GetMineCount(mine,x,y); show[x][y] = count + '0'; PrintBoard(show, ROW, COL); win++; } else { printf("很遗憾,你被炸死了\n"); PrintBoard(mine, ROW, COL); break; } } else { printf("坐标非法,请重新输入\n"); } } if (win == row * col - EASY_COUNT) { printf("恭喜你,排雷成功\n"); PrintBoard(mine, ROW, COL); } }
7.3 game.h
#pragma once #include<stdio.h> #include<time.h> #include<stdlib.h> #define ROW 9 #define COL 9 #define ROWS ROW+2 #define COLS COL+2 #define EASY_COUNT 10 //初始化棋盘 void InitBoard(char board[ROWS][COLS], int rows, int cols,char set); //打印棋盘 void PrintBoard(char board[ROWS][COLS], int row, int col); //设置雷 void SetMine(char board[ROWS][COLS], int row, int col); //排查雷 void FindMine(char board1[ROWS][COLS], char board2[ROWS][COLS], int row, int col);
总结
本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注海外IDC网的更多内容!
【本文由:湖北阿里云代理商 http://www.558idc.com/aliyun.html提供,感恩】