From bb16c32bde58cba70e4877aa2d3ebd04332eb575 Mon Sep 17 00:00:00 2001 From: jacopograndi Date: Tue, 4 Jan 2022 13:35:02 +0100 Subject: linux compile and imgs --- game/ai/engine.h | 130 +++++++++++++++++++++++++++---------------------------- 1 file changed, 65 insertions(+), 65 deletions(-) (limited to 'game/ai/engine.h') diff --git a/game/ai/engine.h b/game/ai/engine.h index d3755e1..584cb11 100644 --- a/game/ai/engine.h +++ b/game/ai/engine.h @@ -1,66 +1,66 @@ -#ifndef ENGINE_H -#define ENGINE_H - -#include - -#include -#include -#include -#include - -#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 tactics = gen.tactics(); - tactic best; best.eval = std::numeric_limits::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::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; - } -}; - -} +#ifndef ENGINE_H +#define ENGINE_H + +#include + +#include +#include +#include +#include + +#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 tactics = gen.tactics(); + tactic best; best.eval = std::numeric_limits::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::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 \ No newline at end of file -- cgit v1.2.3-54-g00ecf