本文实例为大家分享了C语言实现扫雷小游戏的具体代码,供大家参考,具体内容如下
主页面:

游戏页面:

虽然页面比较low,但我已经尽力了
具体代码实现:
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
#include<stdio.h>
#include<windows.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
int n;//记录已经探索的区域数
int x,y;//光标的横纵坐标
int T;//判断游戏是否失败,T=1为失败
int b[9][9];//区分每个位置的状态,0为未探索,1为已探索,2为插旗状态
int a[9][9];//随机生成的扫雷地图,-1为雷
void setColor(unsigned short ForeColor=7,unsigned short BackGroundColor=0) {//控制局部区域的字体颜色和背景颜色
HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle,ForeColor+BackGroundColor*0x10);
}
void gotoxy(int x, int y) {
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle, pos);
}
void add(int p,int q) {//雷的一周数值+1
for(int i=-1; i<2; i++)
for(int j=-1; j<2; j++)
if(p+i>=0&&p+i<9&&q+j>=0&&q+j<9&&a[p+i][q+j]>=0)
a[p+i][q+j]++;
}
void start() {//初始化函数
srand(time(0));
for(int i=0; i<9; i++)
for(int j=0; j<9; j++) {
a[i][j]=0;
b[i][j]=0;
}
x=0;
y=0;
T=0;
n=10;
while(n) {//由p和q随机数产生10个坐标,生成10个雷
int p,q;
p=rand()%9;
q=rand()%9;
if(!a[p][q]) {
n--;
a[p][q]=-1;
add(p,q);
}
}
}
void show() {//显示图
gotoxy(0,0);
for(int i=0; i<9; i++) {
if(i==0)
printf("┌───┬───┬───┬───┬───┬───┬───┬───┬───┐\n");
else
printf("├───┼───┼───┼───┼───┼───┼───┼───┼───┤\n");
printf("│");
for(int j=0; j<9; j++) {
printf(" ");
if(!b[i][j]) {
setColor(0,7);
if(i==x&&j==y)
setColor(0,12);
printf(" ");
} else {
if(i==x&&j==y)
setColor(7,4);
if(b[i][j]==-1)
printf("│>");
else if(a[i][j]>=0) {
if(a[i][j])
printf("%2d",a[i][j]);
else
printf(" ");
} else
printf("**");
}
setColor(7,0);
printf("│");
}
printf("\n");
if(i==8)
printf("└───┴───┴───┴───┴───┴───┴───┴───┴───┘\n");
}
printf("1.探索 2.插旗\n");
}
void ss(int x1,int y1) {//探索函数
if(b[x1][y1])
return ;
b[x1][y1]=1;
n++;
if(a[x1][y1]>0)
return;
if(a[x1][y1]<0) {
T=1;
return;
}
for(int i=-1; i<2; i++)
for(int j=-1; j<2; j++)
if(x1+i>=0&&x1+i<9&&y1+j>=0&&y1+j<9)
ss(x1+i,y1+j);
}
void doing() {//游戏进行流程函数
start();
while(n<71) {
show();
int t;
t=_getch();
switch(t) {
case 72: {
if(x)
x--;
break;
}
case 75: {
if(y)
y--;
break;
}
case 77: {
if(y<8)
y++;
break;
}
case 80: {
if(x<8)
x++;
break;
}
case '1': {
if(!b[x][y])
ss(x,y);
break;
}
case '2': {
if(!b[x][y])
b[x][y]=-1;
else if(b[x][y]==-1)
b[x][y]=0;
break;
}
}
if(T) {
for(int i=0; i<9; i++)
for(int j=0; j<9; j++)
b[i][j]=1;
break;
}
}
show();
if(T)
printf("游戏失败!\n");
if(!T)
printf("挑战成功!\n");
printf("请按'0'键返回主页!\n");
int o=1;
while(o!='0'){
o=_getch();
}
}
int main() {
int m=1;
while(m) {
system("cls");
printf("┌───────────┐\n");
printf("│ 扫雷 │\n");
printf("├───────────┤\n");
printf("│ 1.开始游戏│\n");
printf("├───────────┤\n");
printf("│ 2.退出游戏│\n");
printf("└───────────┘\n");
int t;
t=_getch();
if(t=='1')
doing();
if(t=='2')
break;
}
return 0;
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_43891765/article/details/104174321








发表评论
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。