diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..30cf57e --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,10 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Ignored default folder with query files +/queries/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/include/SortBot.h b/include/SortBot.h index d90127f..c4c7a89 100644 --- a/include/SortBot.h +++ b/include/SortBot.h @@ -7,3 +7,4 @@ class SortBot : public BaseRobot { SortBot ( int startX, int startY ); int mine ( World &world ) override; }; + diff --git a/src/SortBot.cpp b/src/SortBot.cpp index 585480d..31cfa97 100644 --- a/src/SortBot.cpp +++ b/src/SortBot.cpp @@ -4,4 +4,29 @@ #include #include -SortBot::SortBot ( int startX, int startY ) : BaseRobot ( "SortBot", startX, startY ) {} +SortBot::SortBot ( const int startX, const int startY ) : BaseRobot ( "SortBot", startX, startY ) {} + +int SortBot::mine ( World &world ) { + // create ( value, z ) pairs for positive values ( exclude mined blocks and effects ) + std::vector> values; + for ( int z = 0; z < world.getSizeZ(); ++z ) { + int v = world.getValue(x_, y_, z ); + if ( v > 0 ) values.emplace_back(v, z ); + } + if ( values.empty()) return 0; + // sort descending + std::sort ( values.begin(), values.end(), [] ( const auto& a, const auto& b ) { return a.first > b.first;} ); + +for ( auto& [x, z] : values ) { + world.setValue ( x_, y_, z, 0); +} + for ( std::size_t i = 0; i < values.size(); ++i ) { + world.setValue( x_, y_, static_cast(i), values[i].first ); + } + int surface = world.getSurfaceLevel(x_, y_); + if ( surface < 0 ) return 0; + int mined = world.getValue ( x_, y_, surface ); + world.setValue ( x_, y_, surface, 0 ); + score_ += mined; + return mined; +}