您的位置:首页 > 其它

简易版扫雷

2016-09-16 21:16 302 查看
(1)头文件


pragma once

ifndef MINE_H

define MINE_H

include

include

include

define CRT_SECURE_NO_WARNINGS

pragma warning(disable:4996)

define CLOS 11

define ROWS 11

define NUM 10

void menu();

void play_game();

void init(char mine_board[ROWS][CLOS], char show_board[ROWS][CLOS]);

void player(char mine_board[ROWS][CLOS], char show_board[ROWS][CLOS]);

void display(char show_board[ROWS][CLOS]);

endif

(2)函数的实现

include “mine.h”

void menu()

{

printf(“[b]*************************[/b]\n”);

printf(“* 1.play *\n”);

printf(“* 2.exit *\n”);

printf(“[b]*************************[/b]\n”);

}
void init(char mine_board[ROWS][CLOS], char show_board[ROWS][CLOS])

{

int i = 0;

int j = 0;

int x = 0;

int y = 0;

int n = NUM;

for (i = 0; i < ROWS; i++)

{

for (j = 0; j < CLOS; j++)

{

show_board[i][j] = ‘*’;

mine_board[i][j] = ‘0’;

}
}
srand((unsigned)time(NULL));

while (n)

{

x = rand() % 9 + 1;

y = rand() % 9 + 1;

mine_board[x][y] = ‘1’;

n–;

}


}
void display(char show_board[ROWS][CLOS])

{

int i = 0;

int j = 0;

printf(“\n”);

for (i = 1; i < ROWS - 1; i++)

{

for (j = 1; j < CLOS - 1; j++)

{

printf(” %c “,show_board[i][j]);

}
printf(“\n”);

}
printf(“\n”);

}
void player(char mine_board[ROWS][CLOS], char show_board[ROWS][CLOS])

{

int x = 0;

int y = 0;

int count = 0;

do

{

display(show_board);

display(mine_board);

printf(“请输入坐标>:”);

scanf(“%d %d”, &x, &y);

if ((x < 1) || (x > ROWS - 2) || (y < 1) || (y > CLOS - 2))

{

printf(“坐标不合法!”);

}
else if (mine_board[x ][y ] == ‘1’)

{

printf(“踩雷了!游戏结束!\n”);

break;

}
else

{

if (mine_board[x - 1][y - 1] == ‘1’)

count++;

if (mine_board[x ][y] == ‘1’)

count++;

if (mine_board[x + 1][y - 1] == ‘1’)

count++;

if (mine_board[x - 1][y] == ‘1’)

count++;

if (mine_board[x + 1][y] == ‘1’)

count++;

if (mine_board[x - 1][y +1] == ‘1’)

count++;

if (mine_board[x ][y +1] == ‘1’)

count++;

if (mine_board[x + 1][y+1 ] == ‘1’)

count++;

show_board[x ][y ] = count+’0’;

}
display(show_board);

} while (1);

}
void play_game()

{

int x = 0;

int y = 0;

char mine_board[ROWS][CLOS] = { 0 };

char show_board[ROWS][CLOS] = { 0 };

init(mine_board, show_board);

player(mine_board, show_board);

}
(3)主函数

include “mine.h”

int main()

{

int input = 1;

do
{
menu();
printf("请选择1或2>:");
scanf("%d", &input);
switch (input)
{
case 1:
play_game();
break;
case 2:
input = 0;
break;
}} while (input);

system("pause");
return 0;


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: