From 51866120f2f5ba172f31bbe1189a826dce2bbbd8 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sun, 2 Jan 2022 13:26:00 +0100 Subject: [PATCH] we now have custom grid in config --- QUESTIONS.md | 7 +++ config.yml | 7 ++- headers/utils.h | 7 +-- src/configManagement.cpp | 105 ++++++++++++++++++++++++++++++++------- src/gameBasics.cpp | 6 +-- 5 files changed, 107 insertions(+), 25 deletions(-) create mode 100644 QUESTIONS.md diff --git a/QUESTIONS.md b/QUESTIONS.md new file mode 100644 index 0000000..e3368e0 --- /dev/null +++ b/QUESTIONS.md @@ -0,0 +1,7 @@ +Questions que je (Thomas Rubini) voudrait poser au prof +c'est un fichier perso, après si vous voulez ajouter des trucs allez-y + +- Est-ce que vous préférez l'implémentation orienté objet ou orienté macro pour lire la config ? +- Est-ce que vouloir faire des structures optimisées (pas de redondance de mémoire) est une bonne chose, ou osef ? +- Est-ce que traduire les chars A B et C (identifiants des types d'aliens) tirés de la config en valeurs d'enum est une bonne chose, osef, ou contre-productif ? +- Est-ce que mon implémentation du réseau est bone ? \ No newline at end of file diff --git a/config.yml b/config.yml index c6800ba..aa62e50 100644 --- a/config.yml +++ b/config.yml @@ -38,4 +38,9 @@ projectiles: points: invader3: 100 invader2: 250 - invader1: 600 \ No newline at end of file + invader1: 600 + +grid: + - AAAAAAAAAA + - BBBB BBBBB + - CCCCCCCCCC \ No newline at end of file diff --git a/headers/utils.h b/headers/utils.h index 51074e5..f71d725 100644 --- a/headers/utils.h +++ b/headers/utils.h @@ -17,9 +17,10 @@ enum class WinValue{ using namespace std; enum class Invader { - TypeA, - TypeB, - TypeC, + TYPEA, + TYPEB, + TYPEC, + NONE, }; typedef vector InvadersColumn; typedef vector InvadersGrid; diff --git a/src/configManagement.cpp b/src/configManagement.cpp index 196dda9..8b38874 100644 --- a/src/configManagement.cpp +++ b/src/configManagement.cpp @@ -13,7 +13,7 @@ private: char getChar(const configKey& key); int getInt(const configKey& key); nsGraphics::RGBAcolor getColor(const configKey& key); - + void getList(const configKey& key, vector&); }; void ConfigBuilder::dumpInternalValues(){ @@ -26,11 +26,16 @@ void trimSpaces(string& str){ str.erase(0, str.find_first_not_of(' ')); } +/* + * WARNING : This implementation of YAML is not meant to detect and report errors in a non YAML-compliant file + */ + void ConfigBuilder::parseFile(const string& fname) { ifstream file(fname); if(!file.is_open())throw runtime_error("Error while opening config.yml. Check file location ?"); vector keyParts; + unsigned listIndex; while (!file.eof()) { string line; getline(file, line); @@ -41,23 +46,40 @@ void ConfigBuilder::parseFile(const string& fname) { unsigned currentIndent = 0; while (line[currentIndent] == ' ')++currentIndent; - match = line.find(':'); - if (match == string::npos)throw runtime_error("Invalid line : " + line); - string key = line.substr(0, match); - string value = line.substr(match + 1); - trimSpaces(key); - trimSpaces(value); - if (value.empty()) { - keyParts.resize(currentIndent); - keyParts.push_back(key); - } else { + if(line[currentIndent]=='-'){ + string value = line.substr(currentIndent+1); + trimSpaces(value); + string fullKey; for (unsigned i = 0; i < currentIndent; ++i) { fullKey.append(keyParts[i]); fullKey.append("."); } - fullKey.append(key); + // lists are just treated as sections with key 0,1,2... + fullKey.append(to_string((listIndex))); + ++listIndex; internalValues[fullKey] = value; + + }else{ + match = line.find(':'); + if (match == string::npos)throw runtime_error("Invalid line : " + line); + string key = line.substr(0, match); + string value = line.substr(match + 1); + trimSpaces(key); + trimSpaces(value); + if (value.empty()) { + keyParts.resize(currentIndent); + keyParts.push_back(key); + listIndex = 0; + } else { + string fullKey; + for (unsigned i = 0; i < currentIndent; ++i) { + fullKey.append(keyParts[i]); + fullKey.append("."); + } + fullKey.append(key); + internalValues[fullKey] = value; + } } } @@ -65,12 +87,47 @@ void ConfigBuilder::parseFile(const string& fname) { } void ConfigBuilder::readConfig() { - // TODO PUT THAT IN CONFIG ?? -#define S 48 - collectedData.grid.resize(S); - for(unsigned i=0;i(j); + + vector tmp; + getList("grid", tmp); + + // we are essentially going to translate a line-oriented config to a column-oriented grid + + unsigned maxSize = 0; + for(string& s : tmp){ + if(s.size()>maxSize)maxSize = s.size(); + } + collectedData.grid.resize(maxSize); + + for(string& s : tmp){ + unsigned i=0; + for(;i& toPopulate) { + size_t i=0; + string fullKey = key+".0"; + if(!internalValues.contains(fullKey))throw runtime_error("Non-existent list key requested : "+key); + + do{ + toPopulate.push_back(internalValues.at(fullKey)); + ++i; + fullKey = key+"."+to_string(i); + }while(internalValues.contains(key+"."+to_string(i))); +} + nsGraphics::RGBAcolor ConfigBuilder::getColor(const configKey& key) { // switch do not work with strings, and I don't want to implement a constexpr hash function string colorStr = getString(key); diff --git a/src/gameBasics.cpp b/src/gameBasics.cpp index e676736..0d31add 100644 --- a/src/gameBasics.cpp +++ b/src/gameBasics.cpp @@ -119,15 +119,15 @@ void Game::display() { basePos.getY() + j * confData.invadersSize + j * confData.invadersDistance ); switch(grid[i][j]){ - case Invader::TypeA:{ + case Invader::TYPEA:{ pm.drawInvader1(vec, confData.invadersSize); break; } - case Invader::TypeB:{ + case Invader::TYPEB:{ pm.drawInvader2(vec, confData.invadersSize); break; } - case Invader::TypeC:{ + case Invader::TYPEC:{ pm.drawInvader3(vec, confData.invadersSize); break; }