aboutsummaryrefslogtreecommitdiff
path: root/game/ai/engine.h
blob: 584cb11d2b5b4ddf16083a5b319d39479f28dc75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#ifndef ENGINE_H
#define ENGINE_H

#include <iostream>

#include <string>
#include <vector>
#include <limits>
#include <cmath>

#include "../ground.h"
#include "../gst.h"

#include "action.h"
#include "tactic.h"
#include "generator.h"
#include "evaluator.h"
#include "performer.h"

namespace ai {

class engine {
    public:
    engine (Gst &gst) : init(gst) {}
    Gst &init;
    
    tactic get_best () {
        tactic t { search(init, 4) };
        std::cout << t.to_string();
        return t;
    }
    
    tactic search (Gst &gst, int depth) {
        generator gen { gst };
        std::vector<tactic> tactics = gen.tactics();
        tactic best; best.eval = std::numeric_limits<float>::lowest();
        for (tactic t : tactics) {
            performer perf { gst };
            Gst next { perf.apply(t) };
            t.eval = negamax(next, depth, gst.turn);
            std::cout << "depth " << depth << " eval " << t.eval << "\n";
            if (t.eval > best.eval) best = t;
        }
        return best;
    }
    
    float negamax (Gst gst, int depth, int player) {
        //for (int i=0; i<3-depth; i++) std::cout << " "; std::cout << depth << "\n";
        if (depth == 0) {
            evaluator eval { gst };
            return eval.eval(player);
        }
        float value = std::numeric_limits<float>::lowest();
        generator gen { gst };
        auto tactics = gen.tactics();
        for (tactic t : tactics) {
            performer perf { gst };
            Gst next { perf.apply(t) };
            value = fmax(value, negamax(next, depth-1, -player));
        }
        return -value;
    }
};

}
#endif