扫雷
本帖最后由 人造人 于 2018-2-8 18:34 编辑灵感来自 http://bbs.fishc.com/thread-104918-1-1.html
我目前不会写自动翻开的那部分
如果我点了一个,这个和这个周围没有雷,那就把这些翻开,这部分我目前不会写
源码
**** Hidden Message *****
2018-02-08更新在13楼
最后一张图是亮点
@风过无痕丶
{:10_256:} 666 人造人 发表于 2018-2-8 01:39
@风过无痕丶
我觉得需要一个边框! {:10_277:}我觉得自动翻开那部分 可以写个类似数组的自动增长因子的东西~
翻开那部分由翻的次数 来增长~
举例说:
翻第一次, 检索周围 两步的范围有没雷, 如果有雷 就只开当前范围, 如果没雷就打开增长范围的未知区域。。。
翻第二次 检索周围两步的次方
。。。。。
我大概的思维就是这样~ 还有~昨晚的那个随机数我好像找到解决方法了。。
如果不要种子,就只有一个值~
把种子放在 主调函数就可以了。。。 {:10_245:}自动是自动开了~我怎么感觉我写成外挂了~ 把雷区就不开着 给我剩在那了~ 6 风过无痕丶 发表于 2018-2-8 11:03
我觉得需要一个边框!
确实需要一个边框
^_^ 风过无痕丶 发表于 2018-2-8 11:20
我觉得自动翻开那部分 可以写个类似数组的自动增长因子的东西~
翻开那部分由翻的次数 来增长~ ...
还是不明白,这部分我不会写
^_^
// 检测附近有没雷,如果没雷,打开n的位置
//如果有雷 打开当前位置
void Prompt_arr(void){
// 先取得当前位置!
int x,y;
gps_saolei(&x,&y);
// 来吧!开始数雷!
int count_A = 0;
int count_B = 0;
int count_C = 0;
int count_D = 0;
int n = count_saolei; // count_saolei 为全局变量,按下开格子之后会自增~ 检测到有雷,值会缩减~
// 总觉得还差点什么~
// 检测一下 是否在边界,如果是在边界的话,就不打开那么宽了
for (int i = 0; i < n--; i++){
if (x > 15){
;
}
else {
if (juzhen_tnt == 8){ // 8为雷标识符
count_A++; // 有雷的话,就数一下有几个雷
}
else {
juzhen = 3; // 没雷的话,就打开格子
}
}
}
n = count_saolei;
for (int i = 0; i < n--; i++){
if (x == 0){
;
}
else {
if (juzhen_tnt == 8){
count_B++;
}
else {
juzhen = 3;
}
}
}
n = count_saolei;
for (int i = 0; i < n--; i++){
if (y > 15){
;
}
else {
if (juzhen_tnt == 8){
count_C++;
}
else {
juzhen = 3;
}
}
}
n = count_saolei;
for (int i = 0; i < n--; i++){
if (y == 0){
if (juzhen_tnt == 8){
count_D++;
}
else {
juzhen = 3;
}
}
}
}
};
功能是可以实现~但是有点毛病~遇到雷了 它会绕开~就剩个光秃秃的雷区~可能还要修改一下算法和全局变量的衰减力度
风过无痕丶 发表于 2018-2-8 16:29
功能是可以实现~但是有点毛病~遇到雷了 它会绕开~就剩个光秃秃的雷区~可能还要修改一下算法和全局变量 ...
谢谢,我研究一下
^_^ 本帖最后由 人造人 于 2018-2-8 18:36 编辑
2018-02-08 更新
就差自动翻开的部分了吧
#include <iostream>
#include <string>
#include <ctime>
#include <windows.h>
#include <conio.h>
struct MapElem
{
bool status;
bool is_mine;
bool flag;
intcount;
};
const int MAP_X = 16;
const int MAP_Y = 16;
MapElem map;
void GlobalInit(void)
{
for(int i = 0; i < MAP_X; ++i)
{
for(int j = 0; j < MAP_Y; ++j)
{
map.is_mine = false;
map.status = false;
map.flag = false;
map.count = 0;
}
}
}
// 隐藏CMD窗口光标
void HideCursor(void)
{
CONSOLE_CURSOR_INFO cci;
cci.bVisible = FALSE;
cci.dwSize = sizeof(cci);
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorInfo(handle, &cci);
}
static void GotoXY(int x, int y)
{
COORD c;
c.X = x * 2;
c.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}
static void DrawString(const char *str, int color)
{
static const WORD attr[] = {
FOREGROUND_RED | FOREGROUND_INTENSITY, //红色
FOREGROUND_GREEN | FOREGROUND_INTENSITY, //绿色
FOREGROUND_BLUE | FOREGROUND_INTENSITY, //蓝色
FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY //白色
};
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD NumberOfAttrsWritten;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
printf(str);
FillConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), attr, strlen(str), csbi.dwCursorPosition, &NumberOfAttrsWritten);
}
void SetMine(int num)
{
for(int i = 0; i < num; ++i)
{
int x, y;
do
{
x = rand() % MAP_X;
y = rand() % MAP_Y;
}
while(map.is_mine);
map.is_mine = true;
}
}
void ShowCount(int x, int y, int color)
{
if(!map.count)
{
DrawString("∷", color);
return;
}
char buf = "①";
buf += map.count - 1;
DrawString(buf, color);
}
const int MAP_OFFSET_X = 20;
const int MAP_OFFSET_Y = 10;
void DrawMap(void)
{
int color;
for(int i = 0; i < MAP_X; ++i)
{
for(int j = 0; j < MAP_Y; ++j)
{
if(map.flag)
color = 1;
else
color = 3;
GotoXY(i + MAP_OFFSET_X, j + MAP_OFFSET_Y);
if(map.flag)
{
DrawString("◎", color);
continue;
}
if(map.status)
{
if(map.is_mine)
DrawString("★", color);
else
ShowCount(i, j, color);
}
else
DrawString("■", color);
}
}
}
void UpdateCursor(POINT po)
{
DrawMap();
GotoXY(po.x + MAP_OFFSET_X, po.y + MAP_OFFSET_Y);
DrawString("□", 3);
}
bool SelectTarget(POINT cur)
{
if(map.status)
return false;
map.status = true;
DrawMap();
if(map.is_mine)
{
GotoXY(cur.x + MAP_OFFSET_X, cur.y + MAP_OFFSET_Y);
DrawString("★", 0);
return true;
}
return false;
}
void ShowMap(void)
{
int color;
for(int i = 0; i < MAP_X; ++i)
{
for(int j = 0; j < MAP_Y; ++j)
{
if(!map.status)
{
if(map.flag)
color = 1;
else
color = 3;
GotoXY(i + MAP_OFFSET_X, j + MAP_OFFSET_Y);
if(map.is_mine)
DrawString("★", color);
else
ShowCount(i, j, color);
}
}
}
}
int GetCount(int x, int y)
{
int count = 0;
// 左上角 -> 右上角
for(int i = 0; i < 3; ++i)
if((x - 1 + i >= 0) && (y - 1 >= 0))
if(map.is_mine)
++count;
// 左下角 -> 右下角
for(int i = 0; i < 3; ++i)
if((x - 1 + i >= 0) && (y + 1 < MAP_Y))
if(map.is_mine)
++count;
// 左边
if(x - 1 >= 0) // 有左边吗?
if(map.is_mine)
++count;
// 右边
if(x + 1 < MAP_X) // 有右边吗?
if(map.is_mine)
++count;
return count;
}
void GenerateCount(void)
{
for(int i = 0; i < MAP_X; ++i)
{
for(int j = 0; j < MAP_Y; ++j)
{
if(!map.is_mine)
map.count = GetCount(i, j);
}
}
}
int main(void)
{
srand((unsigned int)time(nullptr));
HideCursor();
GlobalInit();
SetMine(30);
GenerateCount();
DrawMap();
POINT cur = {MAP_X / 2, MAP_Y / 2};
UpdateCursor(cur);
char command;
while(1)
{
command = _getch();
switch(command)
{
case 'q':
case 'Q':
goto EXIT;
case 'w':
case 'W':
if(cur.y > 0)
{
--cur.y;
UpdateCursor(cur);
}
break;
case 's':
case 'S':
if(cur.y < MAP_Y - 1)
{
++cur.y;
UpdateCursor(cur);
}
break;
case 'a':
case 'A':
if(cur.x > 0)
{
--cur.x;
UpdateCursor(cur);
}
break;
case 'd':
case 'D':
if(cur.x < MAP_X - 1)
{
++cur.x;
UpdateCursor(cur);
}
break;
case 'f':
case 'F':
if(!map.status)
{
map.flag = !map.flag;
DrawMap();
}
break;
case ' ':
if(!map.flag)
if(SelectTarget(cur))
goto EXIT;
break;
}
}
EXIT:
ShowMap();
GotoXY(0, 34);
return 0;
}
1 楼主,还有在写吗,我写了一下自动打开的,好像有些bug#include<bits/stdc++.h>
#include<windows.h>
#include <conio.h>
using namespace std;
const int map_x = 16;
const int map_y = 16;
struct Mapelem{
bool status = false;
bool is_mine = false;
int course = 0;
}Map;
void Hidecursor()
{
CONSOLE_CURSOR_INFO cci;
cci.bVisible = FALSE;
cci.dwSize = sizeof(cci);
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorInfo(handle, &cci);
}
void gotoxy(int x,int y){
COORD cod;
cod.X = x * 2;
cod.Y = y;
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), cod );
}
void print_course(int x,int y){
if(!Map.course){
printf(" ");
return;
}
printf("%d",Map.course);
}
void drawmap(){
for(int i=0; i<map_x; i++){
for(int j=0; j<map_y; j++){
gotoxy(i,j);
if(Map.status){
if(Map.is_mine)
printf("║О");
else
print_course(i,j);
}
else
printf("║Ж");
}
}
}
void UpdateCursor(POINT po)
{
drawmap();
gotoxy(po.x, po.y);
printf("║У");
}
void setmine(int num){
int x,y;
for(int i=0; i<num; i++){
do{
x = rand()%map_x;
y = rand()%map_y;
}while(Map.is_mine);
Map.is_mine = true;
}
}
int GetCount(int x, int y)
{
int count = 0;
// вСио╫г -> срио╫г
for(int i = 0; i < 3; ++i)
if((x - 1 + i >= 0) && (y - 1 >= 0))
if(Map.is_mine)
++count;
// вСоб╫г -> сроб╫г
for(int i = 0; i < 3; ++i)
if((x - 1 + i >= 0) && (y + 1 < map_y))
if(Map.is_mine)
++count;
// вС╠ъ
if(x - 1 >= 0) // спвС╠ъбП?
if(Map.is_mine)
++count;
// ср╠ъ
if(x + 1 < map_x) // спср╠ъбП?
if(Map.is_mine)
++count;
return count;
}
void GenerateCount(void)
{
for(int i = 0; i < map_x; ++i)
{
for(int j = 0; j < map_y; ++j)
{
if(!Map.is_mine)
Map.course = GetCount(i, j);
}
}
}
void show_mine(POINT p ){
if (Map.status)
return;
else if (Map.course>0){
Map.status = true;
return;
}
else{
Map.status = true;
POINT k;
int i;
for(i = 0; i < 3; ++i)
if((p.x - 1 + i >= 0) && (p.y - 1 >= 0))
{
k.x = p.x - 1 +i;
k.y = p.y - 1;
show_mine(k);
}
// вСоб╫г -> сроб╫г
for(i = 0; i < 3; ++i)
if((p.x - 1 + i >= 0) && (p.y + 1 < map_y))
{
k.x = p.x - 1 + i;
k.y = p.y + 1;
show_mine(k);
}
// вС╠ъ
if(p.x - 1 >= 0) // спвС╠ъбП?
{
k.x = p.x - 1;
k.y = k.y;
show_mine(k);
}
// ср╠ъ
if(p.x + 1 < map_x) // спср╠ъбП?
{
k.x = p.x + 1;
k.y = p.y;
show_mine(k);
}
}
}
bool SelectTarget(POINT p){
if(Map.status)
return false;
if(Map.is_mine)
return true;
//print_course(p.x,p.y);
show_mine(p);
drawmap();
return false;
}
void showmap(){
for(int i=0; i<map_x; i++){
for(int j=0; j<map_y; j++){
if(!Map.status){
gotoxy(i,j);
if(Map.is_mine)
printf("║О");
else
print_course(i,j);
}
}
}
}
int main(){
srand((unsigned int)time(0));
Hidecursor();
drawmap();
setmine(30);
GenerateCount();
POINT p = {0,0};
UpdateCursor(p);
while(1){
char c = _getch();
switch(c){
case 'q':
case 'Q':
goto EXIT;
break;
case 'a':
case 'A':
if(p.x>0){
p.x--;
UpdateCursor(p);
}break;
case 's':
case 'S':
if(p.y<15){
p.y++;
UpdateCursor(p);
}break;
case 'W':
case 'w':
if(p.y>0){
p.y--;
UpdateCursor(p);
}break;
case 'd':
case 'D':
if(p.x<15){
p.x++;
UpdateCursor(p);
}break;
case ' ':
if(SelectTarget(p))
goto EXIT;
break;
}
}
EXIT:
showmap();
return 0;
}
我觉得应该再加一个游戏通过的函数
ps:编码问题中文和一些字符无法显示。。。。 mintaka 发表于 2018-3-26 22:48
我觉得应该再加一个游戏通过的函数
ps:编码问题中文和一些字符无法显示。。。。
这是编码问题吗?
谢谢楼主
人造人 发表于 2018-3-26 23:15
这是编码问题吗?
是我电脑的编译器设置问题,原本的星号,方块会变成你这样子,虽然我运行时还是正常,你应该要改一下{:10_243:}