五子棋(Gomoku)博弈程序

这是一个使用C++实现的五子棋AI玩家程序,包括搜索算法、评估函数和回溯法。程序能够根据对手的走法进行智能决策,寻找最佳落子位置。
摘要由CSDN通过智能技术生成
// Five AI Player
// Author: coolypf

// #define TEST_PERF

#ifdef TEST_PERF
#include <Windows.h>
#endif

#include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
#include <memory.h>
#include <time.h>
using namespace std;

const int infinite = 10000000;

const int s[4][3][2] = {
	{
  {100000, 0},
	{15000, -200000},
	{10, 0}},
	{
  {15000, -50000},
	{100, -200},
	{5, 0}},
	{
  {50, -50},
	{10, -5},
	{2, 0}},
	{
  {10, -5},
	{2, -1},
	{1, 0}}
};

const int range = 15;
const int black = 1;
const int white = 2;
int self, enemy;
int rnd;

clock_t start_time;
int tle;

int board[range][range];

struct Combo {
	int x;
	int y;
	int score;
	bool operator < (const Combo &ano) const { return (score > ano.score); }
};

void AI(int, int);
int backtrace(int, int, int[][range], int);
int evalutatestep(const int[][range], int, int, int);
int evaluate(const int[][range], int);

inline int onboard(int x, int y)
{
	return (x>=0 && y>=0 && x<range && y<range) ? 1 : 0;
}

int main()
{
	int x, y;
	string cmd;

	rnd = 0;
	memset(board, 0, sizeof(board));

	cin >> cmd;
	cin >> self;
	enemy = black + white - self;

	while(1){
		rnd ++;
		cin >> cmd;
		cin >> x >> y;
		if(onboard(x, y))
			board[x][y] = enemy;
		start_time = clock();
		tle = 0;
		AI(x, y);
	}

	return 0;
}

void AI(int px, int py)
{
	int x = 0, y = 0;
	int decide = 0;
	int guess = 0;
	static int ppx, ppy, myx, myy, cx, cy;
#ifdef TEST_PERF
	LARGE_INTEGER pf_li;
	double pf_freq, pf_st, pf_ed, pf_t;
	static double pf_maxtime = 0.0;
	QueryPerformanceFrequency(&pf_li);
	pf_freq=(double)pf_li.QuadPart;
	QueryPerformanceCounter(&pf_li);
	pf_st=(double)pf_li.QuadPart;
	int break_at = 0;
#endif

	if(self == black && rnd == 1) {
		decide = 1;
		x = 7;
		y = 7;
	}

	if(self == white && rnd == 1) {
		int mindelta = infinite;
		if((px - 7)*(px - 7) + (py - 7)*(py - 7) > 32) {
			x = 7;
			y = 7;
			decide = 1;
		} else for(int i=3; i<=11; ++i)
			for(int j=3; j<=11; ++j) {
				int d1 = (i - px)*(i - px) + (j - py)*(j - py);
				int d2 = (i - 7)*(i - 7) + (j - 7)*(j - 7);
				if(d1 == 2 && d2 < mindelta) {
					mindelta = d2;
					x = i;
					y = j;
					decide = 1;
					cx = px;
					cy = py;
				}
			}
	}

	if(!decide) {
		static Combo vc[range*range];
		static int mark[range][range];
		int cur = 0;
		memset(mark, 0, sizeof(mark));

		for(int i=0; i<range; ++i) {
			for(int j=0; j<range; ++j) {
				if(board[i][j] == 0 && (
					onboard(i, j-1) && board[i][j-1]
				||  onboard(i, j-2) && board[i][j-2]
				||  onboard(i, j+1) && board[i][j+1]
				||  onboard(i, j+2) && board[i][j+2]
				||  onboard(i-1, j) && board[i-1][j]
				||  onboard(i-2, j) && board[i-2][j]
				||  onboard(i+1, j) && board[i+1][j]
				||  onboard(i+2, j) && board[i+2][j]
				||  onboard(i-1, j-1) && board[i-1][j-1]
				||  onboard(i-2, j-2) && board[i-2][j-2]
				||  onboard(i+1, j+1) && board[i+1][j+1]
				||  onboard(i+2, j+2) && board[i+2][j+2]
				||  onboard(i-1, j+1) && board[i-1][j+1]
				||  onboard(i-2, j+2) && board[i-2][j+2]
				||  onboard(i+1, j-1) && board[i+1][j-1]
				||  onboard(i+2, j-2) && board[i+2][j-2]
				)) {
					vc[cur].x = i;
					vc[cur].y = j;
					vc[cur].score = evalutatestep(board, i, j, self);
					cur ++;
					mark[i][j] = 1;
				}
			}
		}

		sort(vc, vc+cur);

		guess = cur;
		cur = min(cur, 22);

		if(vc[0].score < infinite && rnd < 107) {
			int maxs = -infinite-100;

			for(int i=0; i<cur; ++i) {
				int nx = vc[i].x, ny = vc[i].y;
				int bakx[25], baky[25], bakc = 0;

				board[nx][ny] =
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值