对于扫雷我们并不陌生,在初中和高中上计算机课上,我们可能就会玩上几把,那么如何去写一个基本扫雷的程序呢?
第一步,我们应该先有一个扫雷的基本图形,这里我们用字符’ 1‘表示雷,用’*‘来表示未扫雷时的情景。废话不多说直接上代码。
void My_Board(char arr[ROWS][COLS], int row, int col)
{
cout << "-----------扫雷--------------" << endl;
for (int k = 0; k <= ROW; k++)
{
cout << k << " ";
}
cout << endl;
for (int i = 1; i <= row; i++)
{
cout << i <<" ";
for (int j = 1; j <= col; j++)
{
cout << arr[i][j] <<" ";
}
cout << endl;
}
cout << "-----------扫雷--------------" << endl;
}
其次,我们还需要对其进行一个初始化:
void Init_Board(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;
}
}
}
set是传来的字符,我们通过set可以来对雷的形状以及非雷的形状进行改变。
第二步,我们应该对雷的位置和个数进行安排。
对于类的个数我们用:
#define mine_count 10
对于类的位置我们需要用一个函数来实现,
void set_mine(char mine[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int count = mine_count;
while (count)
{
x = rand() % row + 1;
y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
第三步,我们需要进行扫雷,那么应该怎么扫除呢,我们是用二维数组实现的所以可以用二维坐标来确定玩家想要扫除的位置,同时对于扫雷的结果有三种,就是第一次就扫到雷就结束了,还有第一次没扫到,得到了一个数字,这个数字是它一圈所含有雷的个数,我们在代码中应该将其加在一起。第三种结果是全部的雷都被找到,那么此时游戏也应该结束。
void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int win =0;
int x = 0;
int y = 0;
while (win<row*col-mine_count)
{
cout << "请输入您想要扫雷坐标:" << endl;
cin >> x >> y;
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
system("cls");
cout << "很遗憾,失败了,再接再厉吧!" << endl;
My_Board(mine, ROW, COL);
break;
}
else
{
int count = get_mine(mine, x, y);
show[x][y] = count + '0';
system("cls");
My_Board(show, ROW, COL);
win++;
}
}
else
{
cout << "您的坐标有误,请重新输入!" << endl;
}
}
if (win == row * col - mine_count)
{
cout << " 棒 棒 棒 " << endl;
cout << "恭喜您,扫除所有雷!" << endl;
}
system("pause");
system("cls");
}
对于全部的代码,如下:
首先是头文件game.h的:
#pragma once
#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define mine_count 10
void Init_Board(char arr[ROWS][COLS], int rows, int cols, char set);
void My_Board(char arr[ROWS][COLS], int row, int col);
void set_mine(char arr[ROWS][COLS], int row, int col);
void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
int get_mine(char mine[ROWS][COLS], int x, int y);
接着是game.cpp的:
#include"game.h"
void Init_Board(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 My_Board(char arr[ROWS][COLS], int row, int col)
{
cout << "-----------扫雷--------------" << endl;
for (int k = 0; k <= ROW; k++)
{
cout << k << " ";
}
cout << endl;
for (int i = 1; i <= row; i++)
{
cout << i <<" ";
for (int j = 1; j <= col; j++)
{
cout << arr[i][j] <<" ";
}
cout << endl;
}
cout << "-----------扫雷--------------" << endl;
}
void set_mine(char mine[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int count = mine_count;
while (count)
{
x = rand() % row + 1;
y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
int get_mine(char mine[ROWS][COLS],int x,int y)
{
return mine[x - 1][y] +
mine[x - 1][y - 1] +
mine[x ][y - 1] +
mine[x + 1][y - 1] +
mine[x + 1][y] +
mine[x + 1][y + 1] +
mine[x][y + 1] +
mine[x - 1][y + 1] - 8 * '0';
}
void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int win =0;
int x = 0;aaaa
int y = 0;
while (win<row*col-mine_count)
{
cout << "请输入您想要扫雷坐标:" << endl;
cin >> x >> y;
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
system("cls");
cout << "很遗憾,失败了,再接再厉吧!" << endl;
My_Board(mine, ROW, COL);
break;
}
else
{
int count = get_mine(mine, x, y);
show[x][y] = count + '0';
system("cls");
My_Board(show, ROW, COL);
win++;
}
}
else
{
cout << "您的坐标有误,请重新输入!" << endl;
}
}
if (win == row * col - mine_count)
{
cout << " 棒 棒 棒 " << endl;
cout << "恭喜您,扫除所有雷!" << endl;
}
system("pause");
system("cls");
}
最后是test.cpp的:
#include"game.h"
void menu()
{
cout << "☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆" << endl;
cout << "☆☆☆☆☆1.0 play game ☆☆☆☆" << endl;
cout << "☆☆☆☆☆0.0 exit ☆☆☆☆☆☆" << endl;
cout << "☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆" << endl;
}
void game()
{
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
Init_Board(mine, ROWS, COLS, '0');
Init_Board(show, ROWS, COLS, '*');
/*My_Board(mine,ROW,COL);*/
/*My_Board(show, ROW, COL);*/
set_mine(mine, ROW, COL);
My_Board(show, ROW, COL);
find_mine(mine, show,ROW, COL);
}
int main()
{
srand((unsigned int)time(NULL));
int choice = 0;
while (true)
{
menu();
cout << "请输入您的选择:" << endl;
cin >> choice;
switch (choice)
{
case 1:
game();
break;
case 0:
exit(0);
break;
default:
cout << "您的输入有误请重新输入:" << endl;
break;
}
}
return 0;
}