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.
This commit is contained in:
2026-04-15 14:08:52 +02:00
parent 22e8ea8d46
commit 2780697235
5 changed files with 83 additions and 4 deletions
+2
View File
@@ -9,4 +9,6 @@ class RandomBot : public BaseRobot {
public:
RandomBot ( const int startX, const int startY );
int mine ( World& world ) override;
private:
int randomNumber();
};
+1 -1
View File
@@ -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;
+14
View File
@@ -0,0 +1,14 @@
#include <iostream>
#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;
}
+29
View File
@@ -0,0 +1,29 @@
#include "../include/RandomBot.h"
#include "../include/World.h"
#include <random>
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 );
};
+37 -3
View File
@@ -3,8 +3,8 @@
#include <iostream>
#include <random>
// #include <algorithm>
// #include <atomic>
#include <algorithm>
#include <atomic>
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<int> opDist(0, 2);
for (int x = 0; x < _sizeX; ++x) {
for (int y = 0; y < _sizeY; ++y) {
std::vector<int> values;
std::vector<int> 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<int>()); 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;
}