From 27806972359126c98c3a102cda94cb1e3df03ad7 Mon Sep 17 00:00:00 2001 From: Florian Egger Date: Wed, 15 Apr 2026 14:08:52 +0200 Subject: [PATCH] Implement World rearrange and add RandomBot and main Make World::checkEffects non-const and provide its implementation. Add RandomBot implementation and declare a private randomNumber helper. Add a simple main() entrypoint. --- include/RandomBot.h | 2 ++ include/World.h | 2 +- main.cpp | 14 ++++++++++++++ src/RandomBot.cpp | 29 +++++++++++++++++++++++++++++ src/World.cpp | 40 +++++++++++++++++++++++++++++++++++++--- 5 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 main.cpp create mode 100644 src/RandomBot.cpp diff --git a/include/RandomBot.h b/include/RandomBot.h index 39370e4..db4ff37 100644 --- a/include/RandomBot.h +++ b/include/RandomBot.h @@ -9,4 +9,6 @@ class RandomBot : public BaseRobot { public: RandomBot ( const int startX, const int startY ); int mine ( World& world ) override; + private: + int randomNumber(); }; diff --git a/include/World.h b/include/World.h index 5a0a707..e8e07d5 100644 --- a/include/World.h +++ b/include/World.h @@ -24,7 +24,7 @@ class World { void rearrange (); // stage 3 - int checkEffects ( int x, int y ) const; + int checkEffects ( int x, int y ); private: int _sizeX, _sizeY, _sizeZ; diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..68d6410 --- /dev/null +++ b/main.cpp @@ -0,0 +1,14 @@ +#include +#include "include/Game.h" + +int main () { + try { + Game game; + game.run (); + } catch ( const std::exception& e ) { + std::cerr << "Fatal Error: " << e.what() << "\n"; + return 1; + } + return 0; + +} diff --git a/src/RandomBot.cpp b/src/RandomBot.cpp new file mode 100644 index 0000000..2bb5b4b --- /dev/null +++ b/src/RandomBot.cpp @@ -0,0 +1,29 @@ +#include "../include/RandomBot.h" +#include "../include/World.h" + +#include + +RandomBot::RandomBot ( const int startX, const int startY ): +BaseRobot ( "RandomBot", startX, startY ) {} + +int RandomBot::mine ( World& world ) { + int total = 0; + int grabbed = 0; + + for ( int z = world.getSizeZ() -1; z >= 0 && grabbed <= randomNumber(); --z ) { + int value = world.getValue ( x_, y_, z ); + if ( value > 0 ) { + total += value; + world.setValue ( x_, y_, z, 0 ); + ++grabbed; + } + } + score_ += total; + return total; +} + +int randomNumber () { + static std ::mt19937 rng ( std::random_device{}()); + std::uniform_int_distribution < int > distr ( 0, 9 ); + return distr ( rng ); +}; diff --git a/src/World.cpp b/src/World.cpp index 5190f78..8bc2153 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -3,8 +3,8 @@ #include #include -// #include -// #include +#include +#include World::World ( int x, int y, int z ) : _sizeX ( x ), _sizeY ( y ), _sizeZ ( z ), @@ -79,5 +79,39 @@ void World::display () const { } // TODO: -// void World::rearrange () // int World::checkEffects ( int x, int y ) const +void World::rearrange() { + std::mt19937 rng(std::random_device{}()); + std::uniform_int_distribution opDist(0, 2); + + for (int x = 0; x < _sizeX; ++x) { + for (int y = 0; y < _sizeY; ++y) { + std::vector values; + std::vector positions; + for (int z = 0; z < _sizeZ; ++z) { + if (_grid[x][y][z] != 0) { + values.push_back(_grid[x][y][z]); + positions.push_back(z); + } + } + + switch (opDist(rng)) { + case 0: std::shuffle(values.begin(), values.end(), rng); break; + case 1: std::sort(values.begin(), values.end()); break; + case 2: std::sort(values.begin(), values.end(), std::greater()); break; + } + + for (std::size_t i = 0; i < positions.size(); ++i) + _grid[x][y][positions[i]] = values[i]; + } + } +} + +int World::checkEffects ( int x, int y ) { + auto& col = _grid[x][y]; + auto it = std::find_if(col.begin(), col.end(), [](int v){ return v < 0; }); + if (it == col.end()) return 0; + int effect = *it; + *it = 0; // consume: the effect fires exactly once + return effect; +}