C语言实现扫雷程序
程序员文章站
2022-04-07 15:37:54
...
Mine_Clear.h
#ifndef _MINE_CLEAR_H_
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<time.h>
#define _MINE_CLEAR_H_
#define ROW 9
int get_num(char mines[][ROW], int x, int y);
void expend(int x,int y,char mines[][ROW],char block[][ROW]);
void openOne(int x,int y,char mines[][ROW],char block[][ROW]);
void printblock(char mines[][ROW],char block[][ROW]);
void init(char mines[ROW][ROW], char block[ROW][ROW], int n);
int is_T_F(char block[][ROW],char mines[][ROW]);
int run(char block[][ROW],char mines[][ROW]);
#endif
Mine_Clear.c
#include"Mine_Clear.h"
int get_num(char mines[][ROW], int x, int y)
{
int count = 0;
if (mines[x - 1][y - 1] == '1' && x-1>= 0 && x-1<ROW && y-1>=0 && y-1<ROW )//左上方
{
count++;
}
if (mines[x - 1][y] == '1' && x-1>= 0 && x-1<ROW && y>=0 && y<ROW)//左边
{
count++;
}
if (mines[x - 1][y + 1] == '1' && x-1>= 0 && x-1<ROW && y+1>=0 && y+1<ROW)//左下方
{
count++;
}
if (mines[x][y - 1] == '1' && x>= 0 && x<ROW && y-1>=0 && y-1<ROW)//上方
{
count++;
}
if (mines[x][y + 1] == '1' && x>= 0 && x<ROW && y+1>=0 && y+1<ROW)//下方
{
count++;
}
if (mines[x + 1][y - 1] == '1' && x+1>= 0 && x+1<ROW && y-1>=0 && y-1<ROW)//右上方
{
count++;
}
if (mines[x + 1][y] == '1' && x+1>= 0 && x+1<ROW && y>=0 && y<ROW)//右方
{
count++;
}
if (mines[x + 1][y + 1] == '1' && x+1>= 0 && x+1<ROW && y+1>=0 && y+1<ROW)//右下方
{
count++;
}
return count;
}
void expend(int x,int y,char mines[][ROW],char block[][ROW]){
if(block[x][y]=='*'&&mines[x][y]!=1){
if(get_num(mines, x, y)==0){
block[x][y]='0';
if(x-1>=0 && x-1<ROW && y-1>=0 && y-1<ROW) {
expend(x-1,y-1,mines,block);
}
if(x-1>=0&&x-1<ROW&&y>=0&&y<ROW){
expend(x-1,y,mines,block);
}
if(x-1>=0&&x-1<ROW&&y+1>=0&&y+1<ROW){
expend(x-1,y+1,mines,block);
}
if(x>=0&&x<ROW&&y-1>=0&&y-1<ROW){
expend(x,y-1,mines,block);
}
if(x>=0&&x<ROW&&y+1>=0&&y+1<ROW){
expend(x,y+1,mines,block);
}
if(x+1>=0&&x+1<ROW&&y-1>=0&&y-1<ROW){
expend(x+1,y-1,mines,block);
}
if(x+1>=0&&x+1<ROW&&y>=0&&y<ROW){
expend(x+1,y,mines,block);
}
if(x+1>=0&&x+1<ROW&&y+1>=0&&y+1<ROW){
expend(x+1,y+1,mines,block);
}
}
else if(get_num(mines, x, y)!=0){
block[x][y]=('0'+get_num(mines, x, y));
}
}
}
void openOne(int x,int y,char mines[][ROW],char block[][ROW]){
if(x>=0&&y>=0&&x<=ROW-1&&y<=ROW-1){
if(mines[x][y]=='1'){
block[x][y]='X';
}
else{
expend(x,y,mines,block);
}
}
}
void printblock(char mines[][ROW],char block[][ROW]){
int i,j;
printf(" 0 ");
for(i=1;i<=ROW;i++){
printf("%d ",i);
}
printf("\n ");
for(i=1;i<=2*ROW;i++){
printf("-");
}
printf("\n");
for(i=0;i<ROW;i++){
printf(" %d | ",i+1);
for(j=0;j<ROW;j++){
printf("%c ",block[i][j]);
}
printf("\n");
}
printf("\n\n");
}
void init(char mines[ROW][ROW], char block[ROW][ROW], int n){
int i,j;
srand((unsigned int)time(NULL));
for(i=0;i<n;i++){
for(j=0;j<n;j++){
mines[i][j]='0'+((double)rand()/RAND_MAX+0.15);
block[i][j]='*';
}
}
}
int is_T_F(char block[][ROW],char mines[][ROW]){
int i,j;
for(i=0;i<ROW;i++){
for(j=0;j<ROW;j++){
if(block[i][j]=='*'&&mines[i][j]!='1'){
return 0;
}
}
}
return 1;
}
int run(char block[][ROW],char mines[][ROW]){
int x,y;
init(mines,block,ROW);
printblock(mines,block);
printf("请输入一个坐标:");
scanf("%d %d",&x,&y);
x-=1;
y-=1;
while(mines[x][y]=='1'){
init(mines,block,ROW);
}
system("cls");
openOne(x,y,mines,block);
printblock(mines,block);
while(mines[x][y]!='1'){
printf("请输入一个坐标:");
scanf("%d %d",&x,&y);
x-=1;
y-=1;
system("cls");
openOne(x,y,mines,block);
printblock(mines,block);
if(0==is_T_F(block,mines)){
continue;
}
else if(1==is_T_F(block,mines)){
printf("赢了!\n");
return 1;
}
}
printf("输了!\n");
return 0;
}
main.c
#include"Mine_Clear.h"
int main(){
int flag=1;
char mines[ROW][ROW];
char block[ROW][ROW];
while(flag) {
run(block,mines);
printf("还继续吗? 1.继续, 0.退出\n");
scanf("%d",&flag);
system("cls");
}
printf("谢谢使用\n");
system("pause");
return 0;
}
鉴于这游戏太烧脑了,所以测试了简单难度的
上一篇: 实现黑客帝国的代码雨