Add LookaheadBot with lookahead and mining

This commit is contained in:
2026-04-26 21:38:07 +02:00
parent 42876c1a93
commit 727caa7b90
2 changed files with 55 additions and 0 deletions
+11
View File
@@ -0,0 +1,11 @@
#pragma once
#include "BaseRobot.h"
class LookaheadBot : public BaseRobot {
public:
LookaheadBot( int startX, int startY );
int mine( World& world ) override;
int decideNextMove( const World& world ) const override;
private:
int lookaheadScore( const World& world, int x, int y, int depth ) const;
};
+44
View File
@@ -0,0 +1,44 @@
#include "../include/LookaheadBot.h"
#include "../include/World.h"
#include <algorithm>
LookaheadBot::LookaheadBot( int startX, int startY ) : BaseRobot( "LookaheadBot", startX, startY ) {}
int LookaheadBot::lookaheadScore( const World& world, int x, int y, int depth ) const {
int here = world.topPositiveSum(x, y, 3 );
if ( depth == 0 ) return here;
int bestNext = 0;
const int moves[5][2] ={{0,0},{1,0},{-1,0},{0,1},{0,-1}};
for ( const auto& m : moves ) {
int nx = x + m[0], ny = y + m[1];
if ( nx < 0 || nx >= world.getSizeX() || ny < 0 || ny >= world.getSizeY() ) continue;
bestNext = std::max( bestNext, lookaheadScore( world, nx, nx, depth -1 ));
}
return here + bestNext;
}
int LookaheadBot::decideNextMove( const World& world ) const {
const int dirs[5][3] = {{0,0,0},{1,0,1},{-1,0,2},{0,1,3},{0,-1,4}};
int bestDir = 0, bestScore = -1;
for ( const auto& d : dirs ) {
int nx = x_ + d[0], ny = y + d[1];
if (nx < 0 || nx >= world.getSizeX() || ny < 0 || ny >= world.getSizeY()) continue;
int score = lookaheadScore( world, nx, ny, 1 );
if ( score > bestScore ) {
bestScore = score;
bestDir = d[2];
}
}
return bestDir;
}
int LookaheadBot::mine( World& world ) {
int total = 0;
for ( int i = 0; i < 3; ++i ) {
int mined = world.mine( x_, y_ );
if ( mined <= 0 ) break;
total += mined;
}
score_ += total;
return total;
}