minimax_ai.h 1.71 KB
#ifndef MINIMAX_AI_H
#define MINIMAX_AI_H

#include <player_interface.h>
#include <basic_types.h>
#include <utility.h>
#include <iostream>
#include <vector>
#include <array>
#include <execution>


namespace othello::minimax_ai
{

  namespace detail {

    // Data
    struct MiniMaxData {
        BitBoard board;
        int cost;
        BitPos move;
    };

    // Node
    struct MiniMaxNode {
      std::vector<std::unique_ptr<MiniMaxNode>> children; // children
      MiniMaxData                               data;     // board data

      MiniMaxNode( const BitBoard& board, const BitPos& move) : data{board,0,move} {}
    };

    // Tree
    struct MiniMaxTree {
        std::unique_ptr<MiniMaxNode> root {nullptr};
    };
  }

  void createTree(detail::MiniMaxTree& tree, const BitBoard& board,
                  const PlayerId& current_playerid, int depth);

  void createChildren(detail::MiniMaxNode& parent, const PlayerId& current_playerid,
                      int depth);




  class MiniMaxAI : public AIInterface {

    // Constructors
  public:
    MiniMaxAI(const PlayerId& player_id);

    // PlayerInterface interface
  public:
    void think(const BitBoard &board, const PlayerId &player_id,
               const std::chrono::seconds &max_time) override;

    BitPos bestMove() const override;

    void computeCost(detail::MiniMaxNode& parent, const PlayerId& current_playerid);

    void computeMiniMax(detail::MiniMaxNode& parent, const PlayerId& current_playerid, bool isMax);


  private:
    mutable BitPos     m_best_move;
    detail::MiniMaxTree m_minimax_tree;
    std::array<int, othello::detail::computeBoardSize()> m_costs;
    int max_depth;
  };

}   // namespace othello::minimax_ai
#endif   // MINIMAX_AI_H