// 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;
		五子棋(Gomoku)博弈程序
最新推荐文章于 2024-10-27 19:10:09 发布
          
          
       
          
       
       这是一个使用C++实现的五子棋AI玩家程序,包括搜索算法、评估函数和回溯法。程序能够根据对手的走法进行智能决策,寻找最佳落子位置。
这是一个使用C++实现的五子棋AI玩家程序,包括搜索算法、评估函数和回溯法。程序能够根据对手的走法进行智能决策,寻找最佳落子位置。
          
 
                   
                   
                   
                   最低0.47元/天 解锁文章
最低0.47元/天 解锁文章
                           
                       
       
           
                 
                 
                 
                 
                 
                
               
                 
                 
                 
                 
                
               
                 
                 扫一扫
扫一扫
                     
              
             
                   3103
					3103
					
 被折叠的  条评论
		 为什么被折叠?
被折叠的  条评论
		 为什么被折叠?
		 
		  到【灌水乐园】发言
到【灌水乐园】发言                                
		 
		 
    
   
    
   
             
            


 
            