逻辑代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu()
{
printf(" 游戏菜单\n");
printf("1. play\n");
printf("2. exit\n");
}
void ruler()
{
printf("\n");
printf(" 扫雷游戏规则\n");
printf("\n");
printf("输入严格按照以下要求:\n");
printf("1. 输入完记得按下回车键才起效果!\n");
printf("2. 坐标中间加空格!\n");
printf(" 例如 想输入坐标55\n");
printf(" 应该输入5 5(中间有空格)\n");
printf(" 输入完再按下回车即可\n");
printf("\n");
printf("如果明白了,那按照以上规则输入完坐标55以后进入游戏菜单吧!\n");
int i = 0;
int j = 0;
scanf("%d %d", &i, &j);
system("cls");
}
void game()
{
char mine[ROWS][COLS] = { 0 };//存放雷的信息
char show[ROWS][COLS] = { 0 };//存放排查好雷的信息
//初始化函数
Initboard(mine, ROWS, COLS, '0');//初始化mine数组
Initboard(show, ROWS, COLS, '*');//初始化show数组
//布置雷的信息
Setmine(mine, ROW, COL);
//打印棋盘
Displayboard(show, ROW, COL);
//Displayboard(mine, ROW, COL);
//排雷
Finemine(mine, show, ROW, COL);
}
void test()
{
int input = 0;
int i = 0;
ruler();
menu();
scanf("%d", &input);
here:
system("cls");
switch (input)
{
case 1:
game();
printf(" 是否继续游戏?\n");
printf("1. continue 2. exit \n");
int i = 0;
scanf("%d", &i);
system("cls");
if (i == 1)
{
goto here;
}
else
{
goto there;
}
break;
case 2:
there:
printf("game over!\n");
break;
default:
printf("please input again!\n");
break;
}
}
int main()
{
srand((unsigned int)time(NULL));
test();
return 0;
}
游戏需要的头文件:
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
//真正操作对象
#define ROW 9
#define COL 9
//用这个来防止越界问题
#define ROWS ROW+2
#define COLS COL+2
#define MINE_MAX 10
//初始化雷的信息
void Initboard(char arr[ROWS][COLS], int rows, int cols, char set);
//打印棋盘
void Displayboard(char arr[ROWS][COLS], int row, int col);
//布置雷的信息
void Setmine(char mine[ROWS][COLS], int row, int col);
//排雷
void Finemine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
游戏本体代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void Initboard(char arr[ROWS][COLS], int rows, int cols, char set)
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
arr[i][j] = set;
}
}
}
void Displayboard(char arr[ROWS][COLS], int row, int col)
{
int i = 0;
for (i = 0; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
int j = 0;
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", arr[i][j]);
}
printf("\n");
}
}
void Setmine(char mine[ROWS][COLS], int row, int col)
{
int count = MINE_MAX;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
//为了不让其他地方用到这个函数
static int Get_mine_count(char mine[ROWS][COLS], int x, int y)
{
return mine[x - 1][y] +
mine[x - 1][y - 1] +
mine[x - 1][y + 1] +
mine[x][y - 1] +
mine[x][y + 1] +
mine[x + 1][y] +
mine[x + 1][y - 1] +
mine[x + 1][y] - 8 * '0';
}
void Finemine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int win = 0;
while (win < row * col - MINE_MAX)
{
printf("请输入你想要排雷的坐标:\n");
scanf("%d %d", &x, &y);
system("cls");
printf("\n");
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
printf("You are die!\n");
Sleep(3000);
system("cls");
printf("\n");
printf("雷的分布如下:");
printf("(0代表安全的位置,1代表雷的位置)\n");
Displayboard(mine, ROW, COL);
Sleep(3000);
break;
}
else
{
//计算附近有多少个雷
int n = Get_mine_count(mine, x, y);
show[x][y] = n + '0';
Displayboard(show, row, row);
win++;
}
}
else
{
printf("Plese input again!\n");
printf("\n");
Displayboard(show, row, row);
}
}
if (win == row * col - MINE_MAX)
{
Sleep(2000);
system("cls");
printf("You are win!\n");
Sleep(2000);
system("cls");
}
}
|