void minmax(game & cur) { if (cur.finished()) { cout << "game is done" << endl; return; } forward_list<game> moves = cur.getMoves(); int best = -10000; game best_move; int count = 0; while(!moves.empty()) { debug(++count); game top = moves.front(); moves.pop_front(); int score = calcvalue(top, true); if (score > best) { best_move = top; best = score; } } cout << "best move is \n" << best_move; }
int calcvalue(game& cur, bool isMax) { if (cur.finished()) { if (isMax) return cur.getValue(); return -cur.getValue(); } forward_list<game> moves = cur.getMoves(); int best = isMax? -10000 : 10000; while(!moves.empty()) { game top = moves.front(); moves.pop_front(); int score = calcvalue(top, !isMax); if (isMax) { best = max(best, score); } else best = min(best, score); } return best; }