void Minmax(int low, int high) { int mid; int a1, b1, a2, b2; if(high - low == 1) { if(A[low] < A[high]) { x = A[low]; y = A[high]; } else { x = A[high]; y = A[low]; } } else { mid = (low + high) / 2; Minmax(low, mid); a1 = x; b1 = y; Minmax(mid + 1, high); a2 = x; b2 = y; x = (a1 < a2)? a1: a2; y = (b1 > b2)? b1: b2; } }
Node Janggi::Minmax(Node node, int depth, Turn turn) { if (depth==0 || //terminal node #if WIN32 abs(node.GetValue()) >= (INT_MAX / 2) // win or lose ) { #else fabs(node.GetValue()) >= (INT_MAX / 2) // win or lose ) { #endif node.SetLeafValue(node.GetValue()); return node; //only one child. herself. } vector<Node> children = node.GetChildren(turn); int best_value; Node best_node; if (turn == TURN_CHO) { //maximizing player best_value = INT_MIN; for (Node n : children) { int v = Minmax(n, depth-1, TURN_HAN).GetLeafValue(); if(v > best_value) { best_value = v; n.SetLeafValue(v); best_node = n; } } } else { //TRUN_HAN . minizing player best_value = INT_MAX; for (Node n : children) { int v = Minmax(n, depth-1, TURN_CHO).GetLeafValue(); if (v < best_value) { best_value = v; n.SetLeafValue(v); best_node = n; } } } //debug /* if ( depth == MINMAX_DEPTH) { int i=0; for (Node n: children) { Board b= n.board; printf("%d) %d,%d -> %d,%d\n", i++, n.GetAction().prev.x, n.GetAction().prev.y, n.GetAction().next.x, n.GetAction().next.y); } printf("best : %d,%d -> %d,%d. best_score = %d\n", best_node.GetAction().prev.x, best_node.GetAction().prev.y, best_node.GetAction().next.x, best_node.GetAction().next.y, best_value); } */ return best_node; }
int main() { Minmax(1, N); printf("%d %d", x, y); return 0; }
double Janggi::Simulation(Node curNode, Turn turn) { Node s = Minmax(curNode, MCTS_SIMULATION_DEPTH, turn); curNode.Init(); return s.GetValue(); }