implement mining functionality in SortBot and add .gitignore

This commit is contained in:
2026-04-15 11:38:15 +02:00
parent b43f266973
commit 4a38215e31
3 changed files with 37 additions and 1 deletions
+10
View File
@@ -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
+1
View File
@@ -7,3 +7,4 @@ class SortBot : public BaseRobot {
SortBot ( int startX, int startY );
int mine ( World &world ) override;
};
+26 -1
View File
@@ -4,4 +4,29 @@
#include <algorithm>
#include <vector>
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<std::pair<int, int>> 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<int>(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;
}