????????C++初学者,学了一个月想检验一下自己的学习成果,做了一个简单的扫雷。
????????受所学知识限制,界面比较简陋,功能也不是很完善,不过还是可以玩的。
????????后续知识充分的时候回来把他完善一下的,废话不多说,上代码。
函数头文件:
#pragma once
//交换函数
void swap(int& a, int& b)
{
int t = a;
a = b;
b = t;
}
//循环判断数组
bool judge_array(int X[],int Y[], int x, int y, int num_cin)
{
for (size_t i = 0; i < num_cin; i++)
{
if (X[i] == x && Y[i] == y)
return true;
}
return false;
}
//判断位数
int judge_size(int m)
{
int n = 0;
if (m == 0)
return 1;
while (m > 0)
{
n++;
m /= 10.0;
}
return n;
}
//获取地雷
int get_landmain(int array[], int i, int j)
{
for (size_t a = 0; a < j; a++)
{
a < i ? array[a] = 1 : array[a] = 0;
}
return *array;
}
//地雷分配
int allocation_landmain(int* landmain_0, int** landmain, int num, int num_landmain)
{
//get_rand_number(landmain_0, num_landmain);
int for_num = 0;
for (size_t i = 0; i < num; i++)
{
for (size_t j = 0; j < num; j++)
{
landmain[i][j] = landmain_0[for_num];
for_num++;
}
}
return **landmain;
}
//计算周围雷数
int sum_landmain(int x, int y, int **landmain ,int length)
{
int sum = 0;
if (x - 1 >= 0 && x - 1 <= length && y - 1 >= 0 && y - 1 <= length)
if (landmain[x - 1][y - 1] == 1)
sum++;
if (x >= 0 && x <= length && y - 1 >= 0 && y - 1 <= length)
if (landmain[x][y - 1] == 1)
sum++;
if (x + 1 >= 0 && x + 1 <= length && y - 1 >= 0 && y - 1 <= length)
if (landmain[x + 1][y - 1] == 1)
sum++;
if (x - 1 >= 0 && x - 1 <= length && y >= 0 && y <= length)
if (landmain[x - 1][y] == 1)
sum++;
if (x + 1 >= 0 && x + 1 <= length && y >= 0 && y <= length)
if (landmain[x + 1][y] == 1)
sum++;
if (x - 1 >= 0 && x - 1 <= length && y + 1 >= 0 && y + 1 <= length)
if (landmain[x - 1][y + 1] == 1)
sum++;
if (x >= 0 && x <= length && y + 1 >= 0 && y + 1 <= length)
if (landmain[x][y + 1] == 1)
sum++;
if (x + 1 >= 0 && x + 1 <= length && y + 1 >= 0 && y + 1 <= length)
if (landmain[x + 1][y + 1] == 1)
sum++;
return sum;
}
//随机雷
void creatMine(int** landmain, int k) //k是棋盘边长-1
{
for (int i = 0; i < k * k / 8; i++)
landmain[i / (k + 1)][i % (k + 1)] = 1; //现在数组0——(k*k/8-1)的位置生成指定数量的地雷,k*k/8是棋盘地雷的总数;
int swapTime = 800; //设置交换次数
srand((unsigned)time(NULL)); //初始化随机种子
for (int i = 0; i < swapTime; i++)
{
int row1 = rand() % (k + 1);
int col1 = rand() % (k + 1);
int row2 = rand() % (k + 1);
int col2 = rand() % (k + 1); //随机生成两组位置
swap(landmain[row1][col1], landmain[row2][col2]); //将这两处的.x信息交换,实现更新数组,致使地雷逐渐分布开来
}
}
//表格顶部输出
void frame_up(int size, int length)
{
for (size_t a = 0; a <= size; a++)
{
std::cout << " ";
}
for (size_t b = 0; b < length; b++)
{
std::cout << b;
for (size_t c = 0; c <= size - judge_size(b); c++)
{
std::cout << " ";
}
}
std::cout << std::endl;
for (size_t a = 0; a < size; a++)
{
std::cout << " ";
}
std::cout << "┌";
for (size_t b = 1; b < length; b++)
{
std::cout << "──┬";
}
std::cout << "──┐" << std::endl;
}
//表格中部输出
void frame_middle(int size, int length)
{
for (size_t a = 0; a < size; a++)
{
std::cout << " ";
}
std::cout << "├";
for (size_t b = 1; b < length; b++)
{
std::cout << "──┼";
}
std::cout << "──┤" << std::endl;
}
//表格底部输出
void frame_down(int size, int length)
{
for (size_t a = 0; a < size; a++)
{
std::cout << " ";
}
std::cout << "└";
for (size_t b = 1; b < length; b++)
{
std::cout << "──┴";
}
std::cout << "──┘" << std::endl;
}
//表格竖线输出—1
void frame_long_string(int size, int length)
{
static int b = 0;
std::cout << b;
for (size_t c = 0; c < size - judge_size(b); c++)
{
std::cout << " ";
}
for (size_t d = 0; d <= length; d++)
{
std::cout << "│ " << " ";
}
std::cout << std::endl;
b++;
}
//表格竖线输出—2
void frame_long_string(int size, int length, int X[], int Y[], int num_cin, int **landmain, int k)
{
std::cout << k;
for (size_t c = 0; c < size - judge_size(k); c++)
{
std::cout << " ";
}
for (size_t d = 0; d <= length; d++)
{
std::cout << "│ " ;
if (d != length)
if (judge_array(X, Y, d, k, num_cin))
std::cout << sum_landmain(d, k, landmain, length);
else
std::cout << " ";
}
std::cout << std::endl;
}
主函数:
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include"Function.h"
using namespace std;
int main()
{
cout << "欢迎来到扫雷" << endl << "游戏规则简单介绍:" << endl << endl;
cout << "私人制作的小游戏,功能还不是很完善,以后会更新升级的。" << endl;
cout << "如果没有提示“游戏失败”则代表游戏继续进行;如果出现了“游戏失败”则代表游戏失败了!" << endl << endl;
cout << "游戏下面正式开始,祝你好运" << endl << endl;
//获取区块范围
int i ;
cout << "请输入一个50以内的数以确定范围" << endl;
cout << "软件界面最好放到最大,不同的屏幕适应的范围不同,如果出现乱码请重新开始并减小输入的数" << endl;
cin >> i;
//自定义地雷
int num_landmain;
cout << "请输入你的地雷数:";
cin >> num_landmain;
//判断位数
int size = judge_size(i);
frame_up(size, i);
for (size_t a = 0; a < i; a++)
{
frame_long_string(size, i);
if(a!=i-1)
frame_middle(size, i);
}
frame_down(size, i);
//获取地雷
int * landmain_0 = new int[i*i];
get_landmain(landmain_0, num_landmain, i * i);
//分配地雷
int** landmain = new int* [i];
for (int k = 0; k < i; ++k)
{
landmain[k] = new int[i];
memset(landmain[k], 0, sizeof(int) * i);
}
allocation_landmain(landmain_0, landmain, i, num_landmain);
creatMine(landmain, i - 1);
delete[] landmain_0;
cout << "横着的数字代表X轴,竖着的代表Y轴。" << endl << "格式:X坐标 空格 Y坐标 (例如:3 4)" << endl;
cout << endl;
//输入坐标,输出界面
int* X = new int[i*i];
int* Y = new int[i*i];
int num_cin = 0;
for (int a = 0; a < i*i; a++)
{
cout << "输入你想点击的坐标:" << endl;
cin >> X[a] >> Y[a];
num_cin++;//输入的次数
int k=0;
frame_up(size, i);
for (size_t a = 0; a < i; a++)
{
frame_long_string(size, i, X, Y, num_cin, landmain, k);
if (a != i - 1)
frame_middle(size, i);
k++;
}
frame_down(size, i);
cout << endl;
if (landmain[X[a]][Y[a]] == 1)
{
cout << "游戏失败!";
break;
}
if (num_cin == i*i-num_landmain)
{
cout << "游戏胜利!";
break;
}
}
delete[] X;
delete[] Y;
delete[] landmain;
cout << "OK了家人们,可以关了";
int j;
cin >> j;
}
|