資源簡介
使用mcts算法的近似ai,能夠對戰ten(超級井字棋),并且能夠輸出勝率和調整計算難度。超級井字棋:9個九宮格,每個玩家落子的格子由另一個玩家的落子決定。 玩家在任意一個九宮格井字棋勝利則占領這個九宮格,最終在9個九宮格中完成井字棋獲勝。
代碼片段和文件信息
#include
#include
#include
#include
#include
#include
#include?
using?namespace?std;
int?result;
int?top;?
int?cot;?
int?first=0;
int?DEG=10000000;
int?n;
int?txty;
int?ans=0;
bool?w;
int?tm[10][9];
bool?tf[9];
bool?flag;
int?tp;
int?map[10][9];
int?line[8][3]={
123
456
789
147
258
369
159
357};
int?tem;
int?ttem[2][80];
bool?fin[9];
int?p;
int?xy;
int?res=0;?
int?print()
{
system(“cls“);
for?(int?i=1;i<=3;i++)
{
for?(int?j=1;j<=3;j++)
{
for?(int?k=1;k<=3;k++)
{
for(int?l=1;l<=3;l++)
{
switch?(map[line[i-1][k-1]][line[j-1][l-1]-1])
{
case?0:
{
cout?<“.?“;
break;
}
case?1:
{
cout?<“O?“;
break;
}
case?2:
{
cout?<“X?“;
break;
}
default?:?cout?<“error“;
}
}
cout?<“|“?<“?“;
}
cout?< }
cout?<“-----------------------“?< }
return?0;
}
int?start()
{
cot=0;
memset(map0sizeof(map));
memset(fin0sizeof(fin));
memset(ttem0sizeof(ttem));
srand((unsigned)time(NULL));
p=5;
cout?<“Powered?by?Li?Shiheng“?< cout?<“Algorithm?:?MCTS“< cout?< cout?<“----------------------------HELP--------------------------------“?< cout?<“In?fact?ten?is?a?super?Tic-Tac-Toe.It?has?9?blocks?each?of?them?has?9?places?to?play.?“?< cout?<“The?input?should?be?like?a?b?which?a?means?blockb?means?place.“< cout?<“The?rule?is:?you?must?play?in?the?block?that?the?place?which?another?one?plays?in?points?to.“?< cout?<“For?example?if?you?play?in?9?2?ten?must?play?in?the?block?2.“?< cout?<“If?in?one?block?you?winthe?block?is?yours.?You?will?play?until?finish?a?Tic-Tac-Toe?in?general“?< cout?<“If?the?block?you?should?play?in?has?finishedyou?can?play?everywhere.“< system(“pause“);
system(“cls“);
cout?<“type?in(0?to?play?first1?to?play?the?second):?“;
cin?>>?first;
char?ff;
cout?<“Would?you?set?degree?(Recommend?not)(Y/N):?“;
cin?>>?ff;
if?(ff==‘Y‘)
{
cout?<“Degree:(default:10000000)?“;
cin?>>?DEG;
}?
print();
return?0;
}
int?is_win(int?mint?aint?side)
{
int?cxd=0;
int?k=0;
int?l=0;
for?(int?i=1;i<=8;i++)
{
for?(int?j=1;j<=3;j++)
{
if?(m==1)
{
if(map[a][line[i-1][j-1]-1]==1)?k++;
if(map[a][line[i-1][j-1]-1]==2)?l++;
}
if?(m==2)
{
if(tm[a][line[i-1][j-1]-1]==1)?k++;
if(tm[a][line[i-1][j-1]-1]==2)?l++;
}
}
if?(side==1)
{
if(k>=cxd)?cxd?=?k;
????k=0;
}
else
{
if(l>=cxd)?cxd?=?l;
????l=0;
}
}
return?cxd;
}
int?mcts(int?uint?v)
{
top=(int)DEG/pow(81-cot2);
int?ran=0;
int?num=0;
int?ans=0;
for?(int?s=1;s<=top;s++)
{
n=v;
????w=0;
???? tp=u;
ty=v;?
???
- 上一篇:Sigar 使用詳解
- 下一篇:從放棄C語言到使用C刷算法的簡明教程v4.7.zip
評論
共有 條評論