Hanoi-Towers/source/backEnd.cpp

66 lines
1.1 KiB
C++
Raw Normal View History

2018-04-11 20:27:22 +03:00
#include "backEnd.h"
#include <cmath>
2018-04-13 22:24:52 +03:00
#include <QDataStream>
2018-01-20 17:44:28 +03:00
2018-04-11 20:27:22 +03:00
BackEnd::BackEnd():
2018-01-20 17:44:28 +03:00
QObject()
{
2018-04-11 22:13:34 +03:00
isFirstStart = true;
lvl = 1;
2018-04-13 22:24:52 +03:00
2018-04-11 22:13:34 +03:00
readCnfig();
2018-04-11 20:27:22 +03:00
}
2018-04-11 22:13:34 +03:00
void BackEnd::writeConfig() const{
2018-01-20 17:44:28 +03:00
QFile f(SAVE);
if(f.open(QIODevice::WriteOnly|QIODevice::Truncate)){
2018-04-13 22:24:52 +03:00
QDataStream stream(&f);
stream << lvl;
stream << isFirstStart;
2018-01-20 17:44:28 +03:00
f.close();
}
}
2018-04-11 20:27:22 +03:00
2018-04-11 22:13:34 +03:00
void BackEnd::readCnfig() {
2018-01-20 17:44:28 +03:00
QFile f(SAVE);
2018-04-13 22:24:52 +03:00
if(f.exists() && f.open(QIODevice::ReadOnly)){
QDataStream stream(&f);
stream >> lvl;
stream >> isFirstStart;
2018-01-20 17:44:28 +03:00
f.close();
2018-04-12 23:18:39 +03:00
if(lvl < 1 || lvl > 99) {
lvl = 1;
}
2018-04-11 22:13:34 +03:00
emit isFirstChanged();
2018-01-20 17:44:28 +03:00
}
2018-04-11 22:13:34 +03:00
}
unsigned short BackEnd::getMinSteps(const unsigned short lvl) const{
return pow(2, lvl) - 1;
}
void BackEnd::save(const short &lvl){
this->lvl = lvl;
writeConfig();
}
bool BackEnd::isFirst()const{
return isFirstStart;
}
void BackEnd::setShowHelp(bool state){
isFirstStart = state;
emit isFirstChanged();
writeConfig();
}
short BackEnd::read()const{
return lvl;
}
BackEnd::~BackEnd(){
writeConfig();
2018-01-20 17:44:28 +03:00
}