主函数:
#define _CRT_SECURE_NO_WARNINGS 1
#include "gamefounction.h"
void game()
{
while (1)
{
//打印菜单
menu();
int choice = 0;
printf("请输入:");
scanf("%d", &choice);
switch (choice)
{
case 1:
gameplay();
printf("是否继续游戏?\n");
break;
case 0:
break;
default:
printf("输入不合法,请重新输入!\n");
break;
}
if (0 == choice)
{
break;
}
}
}
int main()
{
srand(time(NULL));
game();
}
gameplay()函数为扫雷功能的实现函数:
其头文件:
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 19
#define COL 19
#define ROWS ROW+2
#define COLS COL+2
#define Count 5
void menu();
void gameplay();
//初始化
void InitBoard(char arr[ROWS][COLS], int rows, int cols,char set);
//设置雷的位置
void SetLocation(char location[ROWS][COLS], int row, int col);
//打印界面
void DisplayBoard(char board[ROWS][COLS], int row, int col);
//玩家选择位置
void UserMove(char location[ROWS][COLS], char board[ROWS][COLS], int row, int col);
//判断是否结束
int Count_geshu(char location[ROWS][COLS], char board[ROWS][COLS], int x, int y, int row, int col);
?源文件:
#define _CRT_SECURE_NO_WARNINGS 1
#include "gamefounction.h"
void menu()
{
printf("********************************\n");
printf("******** 1.进行游戏 ********\n");
printf("******** 0.退出游戏 ********\n");
printf("********************************\n");
}
void gameplay()
{
char location[ROWS][COLS] = {0};
char board[ROWS][COLS] = {0};
//初始化雷的位置数组与棋盘数组
InitBoard(location, ROWS, COLS, '0');
InitBoard(board, ROWS, COLS, '*');
//设置雷的位置
SetLocation(location, ROW, COL);
//打印棋盘
DisplayBoard(location, ROW, COL);
DisplayBoard(board, ROW, COL);
//开始扫雷
UserMove(location, board, ROW, COL);
}
void InitBoard(char arr[ROWS][COLS], int rows, int cols, char set)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
arr[i][j] = set;
}
}
}
void SetLocation(char location[ROWS][COLS], int row, int col)
{
int cou = Count;
while (cou)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if ('0' == location[x][y])
{
location[x][y] = '1';
cou--;
}
}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
printf(" ");
for (int i = 1; i <= row; i++)
{
printf(" %2d", i);
}
printf("\n");
printf(" ");
for (int i = 1; i <= row; i++)
{
printf("___");
}
printf("\n");
for (int i = 1; i <= row; i++)
{
printf("%2d|", i);
for (int j = 1; j <= col; j++)
{
printf(" %c", board[i][j]);
}
printf("\n");
if (i < row)
printf(" |\n");
else
printf("\n");
}
}
void UserMove(char location[ROWS][COLS], char board[ROWS][COLS], int row, int col)
{
int count = 0;
int x = 0;
int y = 0;
while (1)
{
printf("请输入所选择的位置:");
scanf("%d%d", &x, &y);
if (1 <= x && x <= row && 1 <= y && y <= col)
{
if ('1' == location[x][y])
{
printf("你踩到雷了,gg\n");
DisplayBoard(location, ROW, COL);
break;
}
else
{
count += Count_geshu(location, board, x, y, row, col);
//printf("count = %d\n", count);
if (count == ROW * COL - Count)
{
printf("恭喜你扫雷成功!\n");
DisplayBoard(board, ROW, COL);
/*printf("\n地雷位置如下:\n");
DisplayBoard(location, ROW, COL);*/
break;
}
DisplayBoard(board, ROW, COL);
}
}
else
{
printf("输入不合法,请重新输入:");
}
}
}
int Count_geshu(char location[ROWS][COLS], char board[ROWS][COLS], int x, int y, int row, int col)
{
if (1 <= x && x <= row && 1 <= y && y <= col)
{
if ('*' == board[x][y])
{
int shuliang = 0;
/*int shuliang =
location[x - 1][y] +
location[x - 1][y + 1] +
location[x][y + 1] +
location[x + 1][y + 1] +
location[x + 1][y] +
location[x + 1][y - 1] +
location[x][y - 1] +
location[x - 1][y - 1] - 8 * '0';
board[x][y] = shuliang + '0';*/
for (int i = x - 1; i <= x + 1; i++)
{
for (int j = y - 1; j <= y + 1; j++)
{
shuliang += (location[i][j] - '0');
}
}
board[x][y] = shuliang + '0';
if ('0' == board[x][y])
{
board[x][y] = ' ';
return 1 + Count_geshu(location, board, x-1, y, row, col) + Count_geshu(location, board, x-1, y+1, row, col)
+ Count_geshu(location, board, x, y+1, row, col) + Count_geshu(location, board, x+1, y+1, row, col)
+ Count_geshu(location, board, x+1, y, row, col) + Count_geshu(location, board, x+1, y-1, row, col)
+ Count_geshu(location, board, x, y-1, row, col) + Count_geshu(location, board, x-1, y-1, row, col);
}
return 1;
}
return 0;
}
return 0;
}
????????其中Count_geshu函数为扫雷中实现用户操作并且对指定位置周围一圈雷的个数进行计算,如果计算得出周围存在雷,则将雷的个数赋予棋盘,如果没有雷,则将‘ ’赋予棋盘,同时递归,对其周围一圈棋盘上的雷个数进行计算。
? ? ? ? 首先判定这个坐标的合法性,然后在确定位置是否被计算,如果没被计算,则进行计算。
|