Minimax

mail@pastecode.io avatar
unknown
c_cpp
a year ago
1.6 kB
3
Indexable
Never
#include <cstdlib>

#include "../state/state.hpp"
#include "./minimax.hpp"
#define INF 1e9


/**
 * @brief get a legal action by minimax
 * S
 * @param state Now state
 * @param depth You may need this for other policy
 * @return Move 
 */
int Minimax::minimax(State *state, int dpth, int player){
  if(player == 0 && state->game_state == WIN)
    return INF;
  if(player == 1 && state->game_state == WIN)
    return -INF;
  if(dpth == 0 || state->game_state == DRAW){
    return state->evaluate();
  }
  if(player == 0){
    int value = -INF;
    auto actions = state->legal_actions;
    for(auto it : actions){
      value = std::max(value, minimax(state->next_state(it), dpth - 1, 1));
    }
    return value;
  }
  else{
    int value = INF;
    auto actions = state->legal_actions;
    for(auto it : actions){
      value = std::min(value, minimax(state->next_state(it), dpth - 1, 0));
    }
    return value;
  }
}

Move Minimax::get_move(State *state, int depth){
  if(!state->legal_actions.size())
    state->get_legal_actions();
  int maxVal = -INF;
  int minVal = INF;
  auto actions = state->legal_actions;
  Move maxMove = actions[0], minMove = actions[0];
  for(auto it : actions){
    int val = minimax(state->next_state(it), depth, state->next_state(it)->player);
    if(val > maxVal){
        maxVal = val;
        maxMove = it;
    }
    if(val < minVal){
        minVal = val;
        minMove = it;
    }
  }
  if(state->player == 0) // white plays first
    return maxMove;
  else
    return minMove; // Black
}