• 全部
• 问答

# 请教如何设计五子棋的人工智能？

zzhouj 2004-07-15 11:08:43

...全文
225 点赞 收藏 10

10 条回复

thirdapple 2004-07-19

sos http://community.csdn.net/Expert/topic/3188/3188484.xml?temp=.419552

zzhouj 2004-07-19

/**
* Find best position for next step of computer player, and return the
* step score.
*/
static SINT16 FCMainFindBestPosition(SINT8 * s8SelPosX, SINT8 * s8SelPosY,
UINT8 u8ComputerPlayer,
UINT8 u8PeoplePlayer)
{
int i; /* for loop to count 5 power of u8LimitNum */

UINT8 u8PosX, u8PosY; /* for loop hole chess board */

UINT8 u8LimitNum = 0; /* for loop from five chess lint to two */
/* chess lint */

SINT16 s16Temp = 1; /* power value of 5 power of u8LimitNum */

SINT32 s32NumPosSel = 0; /* for count power of every position which */
/* is FC_NO_PALYER */

SINT32 s32MaxPosSel = 0; /* record max power of all position */

SINT32 s32Temp = 0; /* power value of */
/*FCMainFindConnectLinesByPoint() */

SINT16 s16StepScore = 0; /* return of step score */

/* initial input parameter */
*s8SelPosX = -1;
*s8SelPosY = -1;

/* from five chess lint to two chess lint */
for (u8LimitNum = 5; u8LimitNum >= 2; u8LimitNum--) {
/* loop hole chess board */
for (u8PosX = 0; u8PosX < FC_BOARD_WIDTH; u8PosX++) {
for (u8PosY = 0; u8PosY < FC_BOARD_HEIGHT; u8PosY++) {
/* only count position which is FC_NO_PALYER */
if (nFiveChessBoard[u8PosX][u8PosY] == FC_NO_PALYER) {
/* count 5 power of u8LimitNum */
s16Temp = 1;
for (i = 0; i < 5; i++) {
s16Temp = s16Temp * u8LimitNum;
}

/* first count computer player's power value */
s32Temp =
FCMainFindConnectLinesByPoint(u8PosX, u8PosY,
(UINT8)
nComputerPlayer,
u8LimitNum);
/* count power of the position */
s32NumPosSel =
(s32Temp * s16Temp + 1) * 10 +
FCMainPositionPower(u8PosX, u8PosY);
if (s32NumPosSel > s32MaxPosSel) {
s32MaxPosSel = s32NumPosSel;
*s8SelPosX = u8PosX;
*s8SelPosY = u8PosY;
} else if (s32NumPosSel == s32MaxPosSel) {
if ((FCPalRand() & 0x01) == 1) {
*s8SelPosX = u8PosX;
*s8SelPosY = u8PosY;
}
}

/* the count people player's power value */
s32Temp =
FCMainFindConnectLinesByPoint(u8PosX, u8PosY,
(UINT8)
nPeoplePlayer,
u8LimitNum);
s32NumPosSel =
(s32Temp * s16Temp) * 10 +
FCMainPositionPower(u8PosX, u8PosY);
if (s32NumPosSel == s32MaxPosSel) {
if ((FCPalRand() & 0x01) == 1) {
*s8SelPosX = u8PosX;
*s8SelPosY = u8PosY;
}
} else if (s32NumPosSel > s32MaxPosSel) {
s32MaxPosSel = s32NumPosSel;
*s8SelPosX = u8PosX;
*s8SelPosY = u8PosY;
}
} /* if */
} /* for */
} /* for */
} /* for */

s16StepScore = (SINT16) s32MaxPosSel;
FCPalSprintf(szGameLog, "(x, y) := (%d, %d)", *s8SelPosX, *s8SelPosY);
FCPalLogStr(szGameLog);
return s16StepScore;
}

zzhouj 2004-07-19

/**
* Find best position for next step of computer player, and return the step score.
*/
static SINT16 FCMainFindBestPosition(SINT8 * s8SelPosX, SINT8 * s8SelPosY, UINT8 u8ComputerPlayer, UINT8 u8PeoplePlayer)
{
int i; /* for loop to count 5 power of u8LimitNum */
UINT8 u8PosX, u8PosY; /* for loop hole chess board */
UINT8 u8LimitNum = 0; /* for loop from five chess lint to two chess lint */
SINT16 s16Temp = 1; /* power value of 5 power of u8LimitNum */

SINT32 s32NumPosSel = 0; /* for count power of every position which is FC_NO_PALYER */
SINT32 s32MaxPosSel = 0; /* record max power of all position */
SINT32 s32Temp = 0; /* power value of FCMainFindConnectLinesByPoint() */

SINT16 s16StepScore = 0; /* return of step score */

/* initial input parameter */
*s8SelPosX = -1;
*s8SelPosY = -1;

/* from five chess lint to two chess lint */
for (u8LimitNum = 5; u8LimitNum >= 2; u8LimitNum--) {
/* loop hole chess board */
for (u8PosX = 0; u8PosX < FC_BOARD_WIDTH; u8PosX++) {
for (u8PosY = 0; u8PosY < FC_BOARD_HEIGHT; u8PosY++) {
/* only count position which is FC_NO_PALYER */
if (nFiveChessBoard[u8PosX][u8PosY] == FC_NO_PALYER) {
/* count 5 power of u8LimitNum */
s16Temp = 1;
for (i = 0; i < 5; i++) {
s16Temp = s16Temp * u8LimitNum;
}

/* first count computer player's power value */
s32Temp = FCMainFindConnectLinesByPoint(u8PosX, u8PosY, (UINT8) nComputerPlayer, u8LimitNum);
/* count power of the position */
s32NumPosSel = (s32Temp * s16Temp + 1) * 10 + FCMainPositionPower(u8PosX, u8PosY);
if (s32NumPosSel > s32MaxPosSel) {
s32MaxPosSel = s32NumPosSel;
*s8SelPosX = u8PosX;
*s8SelPosY = u8PosY;
} else if (s32NumPosSel == s32MaxPosSel) {
if ((FCPalRand() & 0x01) == 1) {
*s8SelPosX = u8PosX;
*s8SelPosY = u8PosY;
}
}

/* the count people player's power value */
s32Temp = FCMainFindConnectLinesByPoint(u8PosX, u8PosY, (UINT8) nPeoplePlayer, u8LimitNum);
s32NumPosSel = (s32Temp * s16Temp) * 10 + FCMainPositionPower(u8PosX, u8PosY);
if (s32NumPosSel == s32MaxPosSel) {
if ((FCPalRand() & 0x01) == 1) {
*s8SelPosX = u8PosX;
*s8SelPosY = u8PosY;
}
} else if (s32NumPosSel > s32MaxPosSel) {
s32MaxPosSel = s32NumPosSel;
*s8SelPosX = u8PosX;
*s8SelPosY = u8PosY;
}
} /* if */
} /* for */
} /* for */
} /* for */

s16StepScore = (SINT16) s32MaxPosSel;
FCPalSprintf(szGameLog, "(x, y) := (%d, %d)", *s8SelPosX, *s8SelPosY);
FCPalLogStr(szGameLog);
return s16StepScore;
}

shines77 2004-07-17

shines77 2004-07-17

int alpha_bate(board, color, alpha, beta)
{
......
nMove = GetCaptureMoves(board, color, moveLists);
for(int i=0; i<nMove; i++)
{
......
alpha_bate(board, 1-color, alpha, beta);
}
......
return value;
}

chenzhengzhanglu 2004-07-17

shines77 2004-07-17

AI的关键就在评估函数这快，所以没有NowCan(((((( ★ ))))))想象的那么难，掌握了剪枝以后就是评估函数的设计了

NowCan 2004-07-16

chenzhengzhanglu 2004-07-16

3.2w+

2004-07-15 11:08