Untitled
unknown
c_cpp
2 years ago
1.4 kB
5
Indexable
#include <cstdlib> #include "../state/state.hpp" #include "./minimax.hpp" #define INF 1061109567 /** * @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(dpth == 0){ 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; Move maxMove, minMove; auto actions = state->legal_actions; for(auto it : actions){ int val = minimax(state, depth, state->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 }
Editor is loading...