資源簡介
利用Minimax算法或α-β剪枝算法實現井字棋游戲。

代碼片段和文件信息
#include
#include
int?chess[3][3];
int?xycount;
int?IsWin()
{
int?i;
for(i=0;i<3;i++)
{
if(chess[i][0]==1?&&?chess[i][1]==1?&&?chess[i][2]==1)?return?1;
if(chess[i][0]==-1?&&?chess[i][1]==-1?&&?chess[i][2]==-1)?return?-1;
}
for(i=0;i<3;i++)
{
if(chess[0][i]==1?&&?chess[1][i]==1?&&?chess[2][i]==1)?return?1;
if(chess[0][i]==-1?&&?chess[1][i]==-1?&&?chess[2][i]==-1)?return?-1;
}
if((chess[0][0]==1&&chess[1][1]==1&&chess[2][2]==1)||(chess[2][0]==1&&chess[1][1]==1&&chess[0][2]==1))?return?1;
????if((chess[0][0]==-1&&chess[1][1]==-1&&chess[2][2]==-1)||(chess[2][0]==-1&&chess[1][1]==-1&&chess[0][2]==-1))?return?-1;
return?0;
}
int?Evaluation()
{
int?ijtemp[3][3];
x=y=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(chess[i][j]==0)?temp[i][j]=1;
else?temp[i][j]=chess[i][j];
}
}
?
for(i=0;i<3;i++)
{
x+=(temp[i][0]+temp[i][1]+temp[i][2])/3;
}
for(i=0;i<3;i++)
{
x+=(temp[0][i]+temp[1][i]+temp[2][i])/3;
}
x+=(temp[0][0]+temp[1][1]+temp[2][2])/3;
x+=(temp[2][0]+temp[1][1]+temp[0][2])/3;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(chess[i][j]==0)?temp[i][j]=-1;
else?temp[i][j]=chess[i][j];
}
}
for(i=0;i<3;i++)
{
y+=(temp[i][0]+temp[i][1]+temp[i][2])/3;
}
for(i=0;i<3;i++)
{
y+=(temp[0][i]+temp[1][i]+temp[2][i])/3;
}
y+=(temp[0][0]+temp[1][1]+temp[2][2])/3;
y+=(temp[2][0]+temp[1][1]+temp[0][2])/3;
return?x+y;
}
int?AlphaBeta(int?&valueint?deepbool?MAX)
{
bool?prune=false;
int?ijflagtemp;????????
?????
????if(deep==3||deep+count==9)
????{
????????return?Evaluation();
????}
????????????????
????if(IsWin()==1)
????{
????????value=10000;
????????return?0;
????}
????if(MAX)??????????????????
????????flag=10000;
????else
????????flag=-10000;
????for(i=0;i<3?&&?!prune;i++)
????{
????????for(j=0;j<3?&&?!prune;j++)
????????{
????????????if(chess[i][j]==0)
????????????{
????????????????if(MAX)
????????????????{
????????????????????chess[i][j]=-1;
????????????????????????
????????????????????if(IsWin()==-1)
????????????????????????temp=-10000;
????????????????????else
????????????????????????temp=AlphaBeta(flagdeep+1!MAX);
????????????????????if(temp ????????????????????if(flag<=value)?prune=true;??????????????????????
????????????????}
????????????????else
????????????????{
????????????????????chess[i][j]=1;
????????????????????
????????????????????if(IsWin()==1)
????????????????????????temp=10000;
????????????????????else
????????????????????????temp=AlphaBeta(flagdeep+1!MAX);
????????????????????if(temp>flag)?flag=temp;?????????????????????
????????????????????if(flag>=value)?prune=true;
???????????????????????
????????????????}
????????????????chess[i][j]=0;
????????????}
????????}
????}
????if(MAX)
????{
????????if(flag>value)
????????????value=flag;
????}
????else
????{
??
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????6430??2010-01-10?09:03??Chess.cpp
????I.A....????299311??2010-08-17?22:33??報告?.doc
-----------?---------??----------?-----??----
???????????????305741????????????????????2
- 上一篇:activity 生命周期 demo
- 下一篇:servlet實現 網上商品信息查詢
評論
共有 條評論