扫雷游戏

    xiaoxiao2021-12-14  21

    算法思想:首先得需要两个数组,一个用来布雷(这个玩游戏的人是看不到的),另一个是玩游戏时显示的数组,再随机布好雷(个数自己决定),然后将当玩家随机输入某个坐标后,统计出这个点周围8个点雷的个数并且显示到数组中,以此类推。

    声明文件:game.h

    #ifndef __GAME1_H__ #define __GAME1_H__ #define ROWS 10 //游戏区域大小 #define COLS 10 #define DEFAULT_COUNT 20 //雷个数 void display(char arr[ROWS+2][COLS+2], int rows, int cols); //显示界面 void set_mine(char arr[ROWS+2][COLS+2], int rows, int cols); //布雷 int get_mine_num(char arr[ROWS+2][COLS+2], int x,int y); //统计某个点周围雷的个数 #endif //__GAME1_H__实现功能文件:game.c

    #include <stdio.h> #include "game2.h" #include <stdlib.h> void display(char arr[ROWS+2][COLS+2], int rows, int cols) 显示界面 { int i=0,j=0; printf(" "); for(i=1;i<=rows;i++) //输出列号 { printf("%-2d",i); } printf("\n"); for(i=1;i<=ROWS;i++) //从1开始,说明输出10*10的规矩矩阵 { printf("- ",i); //输出行号 for(j=1;j<=COLS;j++) { printf("%c ",arr[i][j]); } printf("\n"); } } int get_rand_num() //产生1-10之间的随机数 { return rand()+1; } void set_mine(char arr[ROWS+2][COLS+2], int rows, int cols) //布置雷 { int count =DEFAULT_COUNT; while(count) { int x=get_rand_num(); int y=get_rand_num(); if(arr[x][y]== '0') { arr[x][y]='1'; count--; } } } int get_mine_num(char arr[ROWS+2][COLS+2], int x,int y) //统计某个点周围8个点雷的个数 { return (arr[x-1][y-1]-'0'+ arr[x][y-1]-'0' + arr[x+1][y-1]-'0' + arr[x+1][y]-'0' + arr[x+1][y+1]-'0' + arr[x][y+1]-'0' + arr[x-1][y+1]-'0' + arr[x-1][y]-'0' ); }测试文件:test.c

    #include <stdio.h> #include "game2.h" #include <string.h> #include <time.h> #include <stdlib.h> void menu() { printf("**********************\n"); //每一行加\n printf("* 欢迎来到扫雷游戏 *\n"); printf("***** 1.play *******\n"); printf("***** 0.exit *******\n"); } enum Option //枚举类型,后面不加() { EXIT, PLAY }; void game() { int x=0,y=0,count=0; int win=0; char mine[ROWS+2][COLS+2]={0}; char show[ROWS+2][COLS+2]={0}; srand((unsigned)time(NULL)); memset (mine, '0',sizeof(char)*(ROWS+2)*(COLS+2)); //调用memset函数,初始化数组,参数3单位为字节,用sizeof关键字 memset (show, '*',sizeof(char)*(ROWS+2)*(COLS+2)); //display(mine,ROWS,COLS); //调用在屏幕上显示数组情况函数 printf("\n"); display(show,ROWS,COLS); set_mine(mine ,ROWS, COLS); //display(mine,ROWS,COLS); while(win<ROWS*COLS-DEFAULT_COUNT) //表示没有雷的位置还未完,游戏继续 { printf("请输入坐标:"); scanf("%d%d",&x,&y); if(mine[x][y]=='1') { printf("此处有雷,您被炸死"); printf("\n"); break; } else { win++; //没排一个空格,累计一次 count=get_mine_num(mine, x, y); //计算此位置周围雷个数 show[x][y]=count+'0'; //将雷个数转为字符形式,显示到展示数组中 count=get_mine_num(mine, x-1, y-1); //再以以上周围8个点为中心,统计出它们周围8个点的雷的个数情况 show[x-1][y-1]=count+'0'; count=get_mine_num(mine, x, y-1); //以下类似 show[x][y-1]=count+'0'; count=get_mine_num(mine, x+1, y-1); show[x+1][y-1]=count+'0'; count=get_mine_num(mine, x+1, y); show[x+1][y]=count+'0'; count=get_mine_num(mine, x+1, y+1); show[x+1][y+1]=count+'0'; count=get_mine_num(mine, x, y+1); show[x][y+1]=count+'0'; count=get_mine_num(mine, x-1, y+1); show[x-1][y+1]=count+'0'; count=get_mine_num(mine, x-1, y); show[x-1][y]=count+'0'; display(show,ROWS,COLS); } //display(show,ROWS,COLS); } if(win>=ROWS*COLS-DEFAULT_COUNT) //满足它,说明循环是从while中跳出来而不是break出来,排雷成功 { printf("排雷成功!"); } } int main() { int input=0; do { menu(); printf("请选择:"); //菜单出来之后选择机会 scanf("%d",&input); switch(input) { case PLAY: { game(); break; } case EXIT: break; default: { printf("输入有误,请重新输入:"); break; } } }while(input); //只要进来下一次必定打印菜单 return 0; }

    转载请注明原文地址: https://ju.6miu.com/read-969013.html

    最新回复(0)