标签:quit 游戏 efi string boa void ase include 扫雷
游戏设计核心代码区
头文件
#ifndef GAMEH
#define GAMEH
#include<stdio.h>
#include<string.h>
#include<time.h>
#include<time.h>
#define ROW 12
#define COL 12
void menu();
void GAME();
#endif
game.c
#include "game.h"
void menu()
{
printf("###############################\n");
printf("### 1 PLAY ######### 2 EXIT ###\n");
printf("###############################\n");
printf("请输入你的选项:");
}
void show_board(char board[][12],int row,int col)
{
for (int i = 1; i < row-1; i++)
{
for (int j = 1; j < col-1; j++)
{
printf(" [%c] ", board[i][j]);
}
printf("\n");
printf("\n");
}
}
void set_mine(char mine_board[][12], int row, int col)
{
int i = 0;
while (i < 20)
{
int x = rand() % 10 + 1;
int y = rand() % 10 + 1;
if (mine_board[x][y] == ‘0‘)
{
mine_board[x][y] = ‘1‘;
i++;
}
}
}
int get_mines(char mine_board[][12],int row,int col,int x,int y)
{
return mine_board[x - 1][y - 1] + mine_board[x - 1][y] + mine_board[x - 1][y + 1] + \
mine_board[x][y - 1] + mine_board[x][y + 1] + \
mine_board[x + 1][y - 1] + mine_board[x + 1][y] + mine_board[x + 1][y + 1] - 8 ‘0‘;
}
void move(char my_board[][12],char mine_board[][12], int row, int col)
{
int count = 80;
int x = 0;
int y = 0;
do {
show_board(my_board, ROW, COL);
printf("请输入坐标:");
scanf_s("%d%d", &x, &y);
if (x < 1 || x > 10 || y < 1 || y > 10)
{
printf("输入坐标错误");
continue;
}
if (my_board[x][y] != ‘‘)
{
printf("输入坐标重复");
continue;
}
if (mine_board[x][y] == ‘1‘)
{
break;
}
int num = get_mines(mine_board, ROW, COL, x, y);
my_board[x][y] = num + ‘0‘;
count--;
} while (count > 0);
if (count > 0)
{
printf("你被炸死了!\n");
}
else
{
printf("你赢了!\n");
}
printf("下面是雷区的排布!\n");
show_board( mine_board, ROW, COL);
}
void GAME()
{
srand((unsigned long)time(NULL));
char my_board[ROW][COL];
char mine_board[ROW][COL];
memset(my_board, ‘*‘, sizeof(my_board));
memset(mine_board, ‘0‘, sizeof(mine_board));
set_mine(mine_board, ROW, COL);
move(my_board,mine_board, ROW, COL);
}
main.c
#include "game.h"
int main()
{
int select = 0;
int quit = 1;
while (quit)
{
menu();
scanf_s("%d", &select);
switch (select)
{
case 1:
GAME();
break;
case 2:
quit = 0;
break;
default:
printf("输入有误\n");
break;
}
}
printf("拜拜\n");
return 0;
}
??
标签:quit 游戏 efi string boa void ase include 扫雷
原文地址:https://blog.51cto.com/14946385/2545739