头文件
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
#define ROW 8
#define COL 8
#define STYLE '?'
#define NUM 20//想埋雷的个数
extern void Game();
主要函数
#include"clear_mine.h"
static void SetMines(char board[][COL],int row,int col){
int count = NUM;
while (count){
int x = rand() % (row - 2) + 1;
int y = rand() % (col - 2) + 1;
if (board[x][y] == '0'){
board[x][y] = '1';
count--;
}
}
}
static void ShowLine(int col){//控制格线的函数
for (int i = 0; i <= (col - 2); i++){
printf("----");
}
printf("\n");
}
ShowBoard(char board[][COL], int row, int col){
printf(" ");
for (int i = 1; i <= (col - 2); i++){
printf("%d ", i);
}
printf("\n");
ShowLine(col);
for (int i = 1; i <= (row - 2); i++){
printf("%-3d|", i);
for (int j = 1; j <= (col - 2); j++){
printf(" %c |", board[i][j]);
}
printf("\n");
ShowLine(col);
}
}
static char CountMines(char board[][COL], int x, int y){
return board[x - 1][y - 1] + board[x - 1][y] + board[x - 1][y + 1] + board[x][y + 1] + \
board[x + 1][y + 1] + board[x + 1][y] + board[x + 1][y - 1] + board[x][y - 1] - 7 * '0';
//'0'---> 0 字符到数字的转化
}
void Game(){
srand((unsigned long)time(NULL));
//定义两个数组,一个用于埋雷,一个用于显示玩的界面
char show_board[ROW][COL];
char mine_board[ROW][COL];
memset(show_board, STYLE, sizeof(show_board));//memset用于初始化数组;
memset(mine_board, '0', sizeof(mine_board));
SetMines(mine_board, ROW, COL);//埋雷:随机位置,固定个数
int count = (ROW - 2)*(COL - 2) - NUM;//排完所有的雷应该循环的次数
while (count){
system("cls");
ShowBoard(show_board, ROW, COL);
printf("Please Enter Your Postion<x,y>#");
int x = 0;
int y = 0;
scanf("%d %d", &x, &y);
if (x<1 || x>10 || y<1 || y>10){
printf("Postion Error!\n");
continue;
}
if (show_board[x][y] != STYLE){//判断是否扫过雷
printf("Postion Is Not *\n");
continue;
}
if (mine_board[x][y] == '1'){//被炸死
printf("game over!\n");
ShowBoard(mine_board, ROW, COL);
break;
}
show_board[x][y] = CountMines(mine_board, x, y);//['0','8']
count--;
}
}
主函数
#include"clear_mine.h"
//定义两个数组,一个用于埋雷,一个用于显示玩的界面
static void Menu(){
printf("##############################\n");
printf("# 1.Play 0.Exit #\n");
printf("##############################\n");
}
int main(){
int select = 0;
int quit = 0;
while (!quit){
Menu();
printf("Please Enter#");
scanf("%d", &select);
switch (select){
case 1:
Game();
break;
case 0:
quit = 1;
break;
default:
printf("Postion Error,Try Again!\n");
break;
}
}
printf("byebye!\n");
system("pause");
return 0;
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/110985.html