#include "hard.h" hard::hard(QWidget *parent) : Widget(parent) {
QPushButton *autoMap=new QPushButton("系统生成",this); QPushButton *customMap=new QPushButton("用户输入",this); QPushButton *_return=new QPushButton("返回",this);
QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); autoMap->setSizePolicy(sizePolicy); autoMap->setMinimumSize(QSize(60, 20)); autoMap->setMaximumSize(QSize(240, 80)); customMap->setSizePolicy(sizePolicy); customMap->setMinimumSize(QSize(60, 20)); customMap->setMaximumSize(QSize(240, 80)); _return->setSizePolicy(sizePolicy); _return->setMinimumSize(QSize(120, 50)); _return->setMaximumSize(QSize(440, 130));
QHBoxLayout *mode=new QHBoxLayout(this); QVBoxLayout *frame=new QVBoxLayout(this);
mode->addWidget(autoMap); mode->addWidget(customMap); mode->addWidget(_return);
frame->addLayout(mode);
connect(autoMap,&QPushButton::clicked,this,&hard::sysCreate); connect(_return,&QPushButton::clicked,this,&hard::clickback); connect(customMap,&QPushButton::clicked,this,&hard::humCreate);
}; hard::~hard() { }
void hard::clickback() { emit repage(); }
void hard::sysCreate() { emit Cretomap(); }
void hard::humCreate() { emit HumtoMap(); }
void hard::createMap() {
number = { {0, 1, 2, 6, 7, 8, 12, 13, 14}, {0, 1, 2, 3, 6, 7, 8, 9, 12, 13, 14, 15, 18, 19, 20, 21}, {0, 1, 2, 3, 6, 7, 8, 9, 12, 13, 14, 15, 18, 19, 20, 21}, {0, 1, 2, 3, 4, 6, 7, 8, 9, 10, 12, 13, 14, 15, 16, 18, 19, 20, 21, 22, 24, 25, 26, 27, 28}, {0, 1, 2, 3, 4, 6, 7, 8, 9, 10, 12, 13, 14, 15, 16, 18, 19, 20, 21, 22, 24, 25, 26, 27, 28}, {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35}, {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35} };
int pos4 = 0; int autopost[5] = {}; int autoposi[8] = {}; int onerand = 0; for (;;) { pos4 = rand() % 36 + 1;
for (int i = 1; i <= 4; i++) { for (;;) { onerand = rand() % 7 + 1; if (checkFour(autopost, onerand)) break; } autopost[i] = onerand; autoposi[autopost[i]]++; } choiceSys(autopost, pos4); for (int i = 1; i <= 7; i++) { if (autoposi[i] != 1) { int erand = rand() % (number[i - 1].size()); solution[i-1]=number[i-1][erand];
} }
if(endMap()) break;
} }
void hard::choiceSys(int* autopost, int pos4) {
for (int i = 1; i <= 4; i++)
{ int findmax = findFour(number[autopost[i] - 1].data(), number[autopost[i] - 1].size() - 1, pos4); int findmin = 0; int erand = 0; switch (autopost[i]) { case 7: case 6: erand = pos4; solution[autopost[i]-1]=erand; break; case 5: case 4: findmin = findmax - 6; if (findmin < 0) findmin = 0; erand = rand() % (findmax - findmin) + findmin; solution[autopost[i]-1]=number[autopost[i] - 1][erand]; break; case 3: case 2: findmin = findmax - 4; if (findmin < 0) findmin = 0; erand = rand() % (findmax - findmin) + findmin; solution[autopost[i]-1]=number[autopost[i] - 1][erand]; break; case 1: findmin = findmax - 4; if (findmin < 0) findmin = 0; erand = rand() % (findmax - findmin) + findmin; solution[autopost[i]-1]=number[autopost[i] - 1][erand]; break; } } }
int hard::findFour(int* a, int end, int c) { int str = 0; int empt = 0; for (; str <= end;) { int min = (str + end) / 2; if (a[min] <= c) { str = min + 1; empt = min; } else end = min - 1; } return empt; }
bool hard::checkFour(int* post,int n) { for (int i = 1; i <= 4; i++) { if (post[i]==n) { return false; } } return true; }
bool hard::endMap() { int work_map[36]={}; for (int i = 0; i < 7; i++) { int empt = solution[i]; switch (i+1) { case 7: case 6: work_map[empt]++; break; case 5: case 4: work_map[empt]++; work_map[empt + 1]++; work_map[empt + 6]++; work_map[empt + 7]++; break; case 3: case 2: for (int i = 0; i < 3; i++) { work_map[empt + i]++; work_map[empt + 6 + i]++; work_map[empt + 12 + i]++; } break; case 1: for (int i = 0; i < 4; i++) { work_map[empt + i]++; work_map[empt + 6 + i]++; work_map[empt + 12 + i]++; work_map[empt + 18 + i]++; } break; } } int count = 0; for (int i = 0; i < 36; i++) { if (work_map[i] > 4) { return false; } else if (work_map[i] == 4) count++; } if (count != 1) { return false; } else {
for(int i=0;i<36;i++) { int empt = work_map[i]; switch (empt) { case 4: workmap[i]=4; break;
case 3: workmap[i]=3; break;
case 1: workmap[i]=3; break;
case 2: workmap[i]=2; break;
case 0: workmap[i]=2; break; } }
QFile file("hardMap.bin"); if (!file.open(QIODevice::Append)) { qDebug() << "无法打开文件"; return 0; } QDataStream in(&file);
for(int i=0;i<36;i++) { in<<workmap[i]; } for(int i=0;i<7;i++) { in<<solution[i]; } file.close();
return true; } }
int* hard::reMap() { return workmap; }
int* hard::reSolution() { return solution; }
|