コード例 #1
0
int max_value(board b, int d, int player, int pos)
{
	int alpha = -9999;
	bool turn = b.board_update(player, pos);
	if (!turn && (d == depth))
	{
		if (b.terminal_check()) b.end_game(player);
		print_minimax(player, pos, d, evaluation(b));
	}
	if (turn)
	{
		int beta = 9999;
		print_minimax(player, pos, d, beta);
		if (b.terminal_check())
		{
			b.end_game(player);
			beta = evaluation(b);
			print_minimax(player, pos, d, beta);
			return beta;
		}
		for (int i = 2; i <= b.b_size / 2; i++)
		{
			if (b.board_value(player, i) == 0) {}
			else
			{
				beta = min(beta, max_value(b, d, player, i));
				print_minimax(player, pos, d, beta);
			}
		}
		return beta;
	}
	else if (d != depth)
	{
		print_minimax(player, pos, d, alpha);
		if (b.terminal_check())
		{
			b.end_game(player);
			alpha = evaluation(b);
			print_minimax(player, pos, d, alpha);
			return alpha;
		}
		vector<int> a(1);
		for (int i = 2; i <= b.b_size / 2; i++)
		{
			if (b.board_value((player + 1) % 2, i) == 0) {}
			else
			{
				alpha = max(alpha, min_value(b, d + 1, (player + 1) % 2, i, false, 0, a));
				print_minimax(player, pos, d, alpha);
			}
		}
	}
	else alpha = evaluation(b);
	return alpha;
}
コード例 #2
0
int mini_max_purn(board b, int player)
{
	int d = 0;
	int alpha = -9999;
	int beta = 9999;
	int v;
	int temp, max_child = 1;
	vector<int> par;
	par.insert(par.end(), 0);
	print_minimax(player, 0, 0, alpha, alpha, beta);
	if (b.terminal_check())
	{
		b.end_game(player);
		v = evaluation(b);
		print_minimax(player, 0, d, v, alpha, beta);
		return v;
	}
	for (int i = 2; i <= b.b_size / 2; i++)
	{
		if (b.board_value(player, i) == 0) {}
		else
		{
			temp = min_value_purn(b, d + 1, player, i, alpha, beta, true, 1, par);
			if (temp > alpha)
			{
				alpha = temp;
				max_child = i;
			}
			print_minimax(player, 0, d, alpha, alpha, beta);
		}
	}
	struct ele node;
	node.cur = 1;
	node.level = 0;
	node.next = max_child;
	node.parents = par;
	turns.insert(turns.end(), node);
	return max_child;
}
コード例 #3
0
int min_value_purn(board b, int d, int player, int pos, int alpha, int beta, bool state, int level, vector<int> par)
{
	int v = 9999;
	bool turn = b.board_update(player, pos);
	if (!turn && (d == depth))
	{
		if (b.terminal_check()) b.end_game(player);
		print_minimax(player, pos, d, evaluation(b), alpha, beta);
	}
	if (turn)
	{
		int v = -9999;
		int temp, m = -1;
		print_minimax(player, pos, d, v, alpha, beta);
		if (b.terminal_check())
		{
			b.end_game(player);
			v = evaluation(b);
			print_minimax(player, pos, d, v, alpha, beta);
			return v;
		}
		par.insert(par.end(), pos);
		for (int i = 2; i <= b.b_size / 2; i++)
		{
			if (b.board_value(player, i) == 0) {}
			else
			{
				temp = min_value_purn(b, d, player, i, alpha, beta, state, (level + 1), par);
				if (temp > v)
				{
					m = i;
					v = temp;
				}
				if (v >= beta)
				{
					print_minimax(player, pos, d, v, alpha, beta);
					if (state == true)
					{
						struct ele node;
						node.cur = pos;
						node.level = level;
						node.next = m;
						node.parents = par;
						turns.insert(turns.end(), node);
					}
					return v;
				}
				alpha = max(alpha, v);
				print_minimax(player, pos, d, v, alpha, beta);
			}
		}
		if (state == true)
		{
			struct ele node;
			node.cur = pos;
			node.level = level;
			node.next = m;
			node.parents = par;
			turns.insert(turns.end(), node);
		}
		return v;
	}
	else if (d != depth)
	{
		state = false;
		print_minimax(player, pos, d, v, alpha, beta);
		if (b.terminal_check())
		{
			b.end_game(player);
			v = evaluation(b);
			print_minimax(player, pos, d, v, alpha, beta);
			return v;
		}
		for (int i = 2; i <= b.b_size / 2; i++)
		{
			if (b.board_value((player + 1) % 2, i) == 0) {}
			else
			{
				v = min(v, max_value_purn(b, d + 1, (player + 1) % 2, i, alpha, beta));
				if (alpha >= v)
				{
					print_minimax(player, pos, d, v, alpha, beta);
					return v;
				}
				beta = min(beta, v);
				print_minimax(player, pos, d, v, alpha, beta);

			}
		}
	}
	else
	{
		v = evaluation(b);
	}
	return v;
}
コード例 #4
0
int max_value_purn(board b, int d, int player, int pos, int alpha, int beta)
{
	int v = -9999;
	bool turn = b.board_update(player, pos);
	if (!turn && (d == depth))
	{
		if (b.terminal_check()) b.end_game(player);
		print_minimax(player, pos, d, evaluation(b), alpha, beta);
	}
	if (turn)
	{
		int v = 9999;
		print_minimax(player, pos, d, v, alpha, beta);
		if (b.terminal_check())
		{
			b.end_game(player);
			v = evaluation(b);
			print_minimax(player, pos, d, v, alpha, beta);
			return v;
		}
		for (int i = 2; i <= b.b_size / 2; i++)
		{
			if (b.board_value(player, i) == 0) {}
			else
			{
				v = min(v, max_value_purn(b, d, player, i, alpha, beta));
				if (alpha >= v)
				{
					print_minimax(player, pos, d, v, alpha, beta);
					return v;
				}
				beta = min(beta, v);
				print_minimax(player, pos, d, v, alpha, beta);
			}
		}
		return v;
	}
	else if (d != depth)
	{
		print_minimax(player, pos, d, v, alpha, beta);
		if (b.terminal_check())
		{
			b.end_game(player);
			v = evaluation(b);
			print_minimax(player, pos, d, v, alpha, beta);
			return v;
		}
		vector<int> a(1);
		for (int i = 2; i <= b.b_size / 2; i++)
		{
			if (b.board_value((player + 1) % 2, i) == 0) {}
			else
			{
				v = max(v, min_value_purn(b, d + 1, (player + 1) % 2, i, alpha, beta, false, 0, a));
				if (v >= beta)
				{
					print_minimax(player, pos, d, v, alpha, beta);
					return v;
				}
				alpha = max(alpha, v);
				print_minimax(player, pos, d, v, alpha, beta);
			}
		}
	}
	else
	{
		v = evaluation(b);
	}
	return v;
}
コード例 #5
0
int min_value(board b, int d, int player, int pos, bool state, int level, vector<int> par)
{
	int alpha = 9999;
	bool turn = b.board_update(player, pos);
	if (!turn && (d == depth))
	{
		if (b.terminal_check()) b.end_game(player);
		print_minimax(player, pos, d, evaluation(b));
	}
	if (turn)
	{
		int beta = -9999;
		int temp, m = -1;
		print_minimax(player, pos, d, beta);
		if (b.terminal_check())
		{
			b.end_game(player);
			beta = evaluation(b);
			print_minimax(player, pos, d, beta);
			return beta;
		}
		par.insert(par.end(), pos);
		for (int i = 2; i <= ((b.b_size) / 2); i++)
		{
			if (b.board_value(player, i) == 0) {}
			else
			{
				temp = min_value(b, d, player, i, state, (level + 1), par);
				if (temp > beta)
				{
					m = i;
					beta = temp;
				}
				print_minimax(player, pos, d, beta);
			}
		}
		if (state == true && m != -1)
		{
			struct ele node;
			node.cur = pos;
			node.level = level;
			node.next = m;
			node.parents = par;
			turns.insert(turns.end(), node);
		}

		return beta;
	}
	else if (d != depth)
	{
		state = false;
		level = 0;
		print_minimax(player, pos, d, alpha);
		if (b.terminal_check())
		{
			b.end_game(player);
			alpha = evaluation(b);
			print_minimax(player, pos, d, alpha);
			return alpha;
		}
		for (int i = 2; i <= b.b_size / 2; i++)
		{
			if (b.board_value((player + 1) % 2, i) == 0) {}
			else
			{
				alpha = min(alpha, max_value(b, d + 1, (player + 1) % 2, i));
				print_minimax(player, pos, d, alpha);
			}
		}
	}
	else alpha = evaluation(b);
	return alpha;
}