<commit_before>/*
 * minimax.hh
 * <NAME> <<EMAIL>>
 */

#ifndef __MINIMAX__HH
#define __MINIMAX__HH

#include "arena.hh"

#include <algorithm>

const int minimax_depth = 10;

class MinimaxAgent : public Agent {
private:
	Arena* game;
	int playerSign;

	int minimax(int depth, bool doMax) {
		if (depth <= 0) {
			return game->eval(playerSign);
		}
		vector<Action*> moves = game->generateMoves();
		if (!moves.size() || game->doEarlyStop()) {
			return game->eval(playerSign);
		}

		game->switchPlayer();
		int alpha = doMax ? -INF : INF;
		for (size_t i=0; i < moves.size(); ++i) {
			Action* move = moves[i];
			game->apply(move);
			int score = minimax(depth - 1, !doMax);
			game->revert(move);
			if (doMax) {
				alpha = max(alpha, score);
			} else {
				alpha = min(alpha, score);
			}
		}
		deleteAllExcept(moves, NULL);
		return alpha;
	}

public:
	MinimaxAgent(string algo)
		: Agent(algo)
	{}

	Action* getAction(Arena* state, vector<Action*> moves) {
		game = state;
		Action* best = NULL;
		int best_score = -INF;
		playerSign = state->curPlayerSign();
		game->switchPlayer(); 
		for (size_t i=0; i < moves.size(); ++i) {
			Action* move = moves[i];
			state->apply(move);
			int score = minimax(minimax_depth, false);
			state->revert(move);
			if (score > best_score) {
				best = move;
				best_score = score;
			}
		}
		return best;
	}
};

#endif<commit_msg>O_O, search tree was missing switchPlayer() calls<commit_after>/*
 * minimax.hh
 * <NAME> <<EMAIL>>
 */

#ifndef __MINIMAX__HH
#define __MINIMAX__HH

#include "arena.hh"

#include <algorithm>

const int minimax_depth = 10;

class MinimaxAgent : public Agent {
private:
	Arena* game;
	int playerSign;

	int minimax(int depth, bool doMax) {
		if (depth <= 0) {
			return game->eval(playerSign);
		}
		vector<Action*> moves = game->generateMoves();
		if (!moves.size() || game->doEarlyStop()) {
			return game->eval(playerSign);
		}

		game->switchPlayer();
		int alpha = doMax ? -INF : INF;
		for (size_t i=0; i < moves.size(); ++i) {
			Action* move = moves[i];
			game->apply(move);
			int score = minimax(depth - 1, !doMax);
			game->revert(move);
			if (doMax) {
				alpha = max(alpha, score);
			} else {
				alpha = min(alpha, score);
			}
		}
		game->switchPlayer();
		deleteAllExcept(moves, NULL);
		return alpha;
	}

public:
	MinimaxAgent(string algo)
		: Agent(algo)
	{}

	Action* getAction(Arena* state, vector<Action*> moves) {
		game = state;
		Action* best = NULL;
		int best_score = -INF;
		playerSign = state->curPlayerSign();
		game->switchPlayer(); 
		for (size_t i=0; i < moves.size(); ++i) {
			Action* move = moves[i];
			state->apply(move);
			int score = minimax(minimax_depth, false);
			state->revert(move);
			if (score > best_score) {
				best = move;
				best_score = score;
			}
		}
		game->switchPlayer();
		return best;
	}
};

#endif
{'fraction_non_alphanumeric': 0.1637323943661972, 'fraction_numerical': 0.004225352112676056, 'mean_word_length': 3.4046511627906977, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1239080', 'n_tokens_mistral': 1212, 'n_tokens_neox': 1061, 'n_words': 253}