黑白棋.docx

上传人:b****5 文档编号:5701085 上传时间:2022-12-31 格式:DOCX 页数:32 大小:24.62KB
下载 相关 举报
黑白棋.docx_第1页
第1页 / 共32页
黑白棋.docx_第2页
第2页 / 共32页
黑白棋.docx_第3页
第3页 / 共32页
黑白棋.docx_第4页
第4页 / 共32页
黑白棋.docx_第5页
第5页 / 共32页
点击查看更多>>
下载资源
资源描述

黑白棋.docx

《黑白棋.docx》由会员分享,可在线阅读,更多相关《黑白棋.docx(32页珍藏版)》请在冰豆网上搜索。

黑白棋.docx

黑白棋

C语言:

某黑白棋代码

//othellobyvilinov

#include

#include

#include

#include

#defineNONE0

#defineBLACK1

#defineWHITE2

#defineDUMMY3

#definef(x,y)(x)*9+y

voiddraw();

voidMoveGenerator(intdeep);//*

intgetnum();

voidturn(intdeep,intnum);//*

voidback(intdeep);//*

intAI();

voidhelp();

intcheck(intnum);

intCheckWay(intnum);

intSetLevel();

voidEndGenerator(intdeep);

intHappyEnding(intdeep);

intBadEnding(intdeep,intalpha,intbeta);

intEnding(intdeep,intalpha,intbeta);

intsearch(intdeep);

intAlphaBeta(intdeep,intalpha,intbeta);

intPVS(intdeep,intalpha,intbeta);

intMAX_DEPTH=10;

intEND_DEPTH=14;

intcolour=BLACK;

intcomputer=WHITE;

intblack=2,white=2;

intnote;

charboard[91]={

3,3,3,3,3,3,3,3,3,

3,0,0,0,0,0,0,0,0,

3,0,0,0,0,0,0,0,0,

3,0,0,0,0,0,0,0,0,

3,0,0,0,2,1,0,0,0,

3,0,0,0,1,2,0,0,0,

3,0,0,0,0,0,0,0,0,

3,0,0,0,0,0,0,0,0,

3,0,0,0,0,0,0,0,0,

3,3,3,3,3,3,3,3,3,3};

intdirection[8]={-10,-9,-8,-1,1,8,9,10};

intvalue[91]={

0,0,0,0,0,0,0,0,0,

0,40,-8,4,3,3,4,-8,40,

0,-8,-10,2,1,1,2,-10,-8,

0,4,2,3,1,1,3,2,4,

0,3,1,1,2,2,1,1,3,

0,3,1,1,2,2,1,1,3,

0,4,2,3,1,1,3,2,4,

0,-8,-10,2,1,1,2,-10,-8,

0,40,-8,4,3,3,4,-8,40,

0,0,0,0,0,0,0,0,0};

intMoveList[20][30];

intTurnList[20][10];

//0-7各方向上的吃子个数。

8num。

9总吃子个数

intspace[20];//ENDingsearch

intflag[20];

intBestScore;

intBestMove;

intmove[61]={0,//这个零是“捣乱”的。

说笑,为了一点点的效率提升……

/*C4*/31,32,58,59,39,42,48,51,

/*C3*/30,33,57,60,

/*A1*/10,17,73,80,

/*A3*/12,15,75,78,28,35,55,62,

/*A4*/13,14,76,77,37,44,46,53,

/*B3*/21,24,66,69,29,34,56,61,

/*B4*/22,23,67,68,38,43,47,52,

/*A2*/11,16,74,79,19,26,64,71,

/*B2*/20,25,65,70};

intway;//check()&CheckWay()&turn()&back()

intnow;//CheckWay()

intcount;//CheckWay()

inttemp;//MoveGenerator()&turn()&back()&AI()&G()

clock_ttime1,time2;

voidNEW(){

colour=BLACK;

computer=WHITE;

black=2,white=2;

intx,y;

for(x=0;x<91;x++)

board[x]=3;

for(x=1;x<9;x++)

for(y=1;y<9;y++)

board[f(x,y)]=0;

board[f(4,4)]=2;

board[f(5,5)]=2;

board[f(4,5)]=1;

board[f(5,4)]=1;

}

voiddraw(){

intx,y;

printf("12345678");

for(x=1;x<9;x++){

printf("\n%c",(char)x-1+'a');

for(y=1;y<9;y++){

if(!

board[f(x,y)])printf(".");

if(board[f(x,y)]==WHITE)printf("O");

if(board[f(x,y)]==BLACK)printf("X");

}

}

printf("\n");

if(colour==computer){

printf("NOTE:

%dkn/s\n",note/(time2-time1));

printf("BestScore:

%d\n",BestScore);

}

return;

}

voidMoveGenerator(intdeep){

MoveList[deep][0]=0;

for(temp=60;temp>0;temp--)

if(check(move[temp]))

MoveList[deep][++MoveList[deep][0]]=move[temp];

return;

}

intcheck(intnum){

if(board[num])return0;

for(way=7;way>=0;way--)

if(CheckWay(num))

return1;

return0;

}

intCheckWay(intnum){

count=0;

for(now=num+direction[way];

board[now]&&board[now]!

=DUMMY;now+=direction[way]){

if(board[now]==colour)

returncount;

count++;

}

return0;

}

voidturn(intdeep,intnum){

TurnList[deep][9]=0;

for(way=7;way>=0;way--){

temp=CheckWay(num);

TurnList[deep][way]=temp;

TurnList[deep][9]+=temp;

for(now=num+direction[way];temp>0;temp--,now+=direction[way])

board[now]=colour;

}

TurnList[deep][8]=num;

board[num]=colour;

if(colour==WHITE){

white+=TurnList[deep][9]+1;

black-=TurnList[deep][9];

}else{

white-=TurnList[deep][9];

black+=TurnList[deep][9]+1;

}

return;

}

voidback(intdeep){

inttemp2=3-colour,num=TurnList[deep][8];

for(way=7;way>=0;way--)

for(temp=TurnList[deep][way],now=num+direction[way];temp>0;temp--,now+=direction[way])

board[now]=temp2;

board[num]=NONE;

if(colour==WHITE){

white-=TurnList[deep][9]+1;

black+=TurnList[deep][9];

}else{

white+=TurnList[deep][9];

black-=TurnList[deep][9]+1;

}

return;

}

intgetnum(){

chars[30];

intlenS;

intnum;

for(;;){

if(colour==computer)

returnAI();

printf(">>");

scanf("%s",s);

lenS=strlen(s);

if(lenS==2)

if(s[0]>='a'&&s[0]<='h'&&s[1]>='1'&&s[1]<='8'){

num=f(s[0]-'a'+1,s[1]-'1'+1);

if(check(num))

returnnum;

}

if(lenS==1){

if(s[0]=='q')

exit

(2);

if(s[0]=='h')

help();

if(s[0]=='c')

computer=3-computer;

if(s[0]=='s')

computer=1-computer;

if(s[0]=='d')

draw();

if(s[0]=='n')

return0;

if(s[0]=='l')

SetLevel();

}

if(lenS==4&&!

strcmp("exit",s))

exit

(2);

if(lenS==4&&!

strcmp("help",s))

help();

if(lenS==4&&!

strcmp("draw",s))

draw();

if(lenS==3&&!

strcmp("new",s))

return0;

if(lenS==5&&!

strcmp("level",s))

SetLevel();

}

}

intSetLevel(){

charc;

printf("当前中盘搜索深度%d层,终盘搜索深度%d层\n",MAX_DEPTH,END_DEPTH);

while

(1){

printf("设置中盘搜索深度(1-12):

");

scanf("%d",&MAX_DEPTH);

if(feof(stdin)||ferror(stdin)){

printf("\n输入错误\n");

exit

(2);

}

while((c=getchar())!

='\n'&&c!

=EOF);

if(MAX_DEPTH<1||MAX_DEPTH>12)

continue;

break;

};

while

(1){

printf("设置尾盘搜索深度(1-16):

");

scanf("%d",&END_DEPTH);

if(feof(stdin)||ferror(stdin)){

printf("\n输入错误\n");

exit

(2);

}

while((c=getchar())!

='\n'&&c!

=EOF);

if(END_DEPTH<1||END_DEPTH>16)

continue;

break;

};

return0;

}

voidhelp(){

printf("--------------HELP--------------\n");

printf("\"h\",\"help\"帮助\n");

printf("\"a1\"..\"h8\"棋子地点\n");

printf("\"q\",\"exit\"退出\n");

printf("\"d\",\"draw\"画出棋盘\n");

printf("\"n\",\"new\"重新开始\n");

printf("\"l\",\"level\"设置等级\n");

printf("\"c\"让步\n");

printf("\"s\"双人下棋(再按一次则返回人机)\n");

printf("--------------END--------------\n");

return;

}

intAI(){

space[0]=64-white-black;

memset(flag,0,sizeof(flag));

if(space[0]>END_DEPTH)

returnsearch(MAX_DEPTH);

space[0]=0;

for(temp=60;temp>0;temp--)

if(!

board[move[temp]])

space[++space[0]]=move[temp];

returnHappyEnding(space[0]);//o(∩_∩)o

}

intG(){

intval=0;

for(temp=10;temp<81;temp++)

if(board[temp]==WHITE)

val+=value[temp];

elseif(board[temp]==BLACK)

val-=value[temp];

//note++;

returnval;

}

///eNDing

voidEndGenerator(intdeep){

MoveList[deep][0]=0;

for(temp=space[0];temp>0;temp--)

if(check(space[temp]))

MoveList[deep][++MoveList[deep][0]]=space[temp];

return;

}

intHappyEnding(intdeep){

intscore;

note++;

EndGenerator(deep);

//thefirstnote

turn(deep,MoveList[deep][MoveList[deep][0]]);

BestScore=-Ending(deep-1,-65,65);

BestMove=MoveList[deep][MoveList[deep][0]];

back(deep);

MoveList[deep][0]--;

//firstnoteend

for(;MoveList[deep][0]>0;MoveList[deep][0]--){

turn(deep,MoveList[deep][MoveList[deep][0]]);

score=-BadEnding(deep-1,-BestScore-1,-BestScore);

if(score>BestScore){

BestScore=-Ending(deep-1,-65,-score);

BestMove=MoveList[deep][MoveList[deep][0]];

}

back(deep);

}

returnBestMove;

}

intBadEnding(intdeep,intalpha,intbeta){

intcurrent=-65,score;

note++;

if(!

deep)

if(colour==WHITE)

returnblack-white;

else

returnwhite-black;

colour=3-colour;

EndGenerator(deep);

if(!

MoveList[deep][0]){//nomove

if(flag[deep])//gameover

if(colour==WHITE){

colour=3-colour;

returnwhite-black;

}else{

colour=3-colour;

returnblack-white;

}

//让步……英语怎么说?

flag[deep]=1;

score=-BadEnding(deep,-beta,-alpha);

flag[deep]=0;

colour=3-colour;

returnscore;

}

for(;MoveList[deep][0]>0;MoveList[deep][0]--){

turn(deep,MoveList[deep][MoveList[deep][0]]);

score=-BadEnding(deep-1,-beta,-alpha);

back(deep);

if(score>current){

current=score;

if(score>=beta)

break;

}

}

colour=3-colour;

returncurrent;

}

intEnding(intdeep,intalpha,intbeta){

intbest,score;

note++;

if(!

deep)

if(colour==WHITE)

returnblack-white;

else

returnwhite-black;

colour=3-colour;

EndGenerator(deep);

if(!

MoveList[deep][0]){//nomove

if(flag[deep])//gameover

if(colour==WHITE){

colour=3-colour;

returnwhite-black;

}else{

colour=3-colour;

returnblack-white;

}

//让步

flag[deep]=1;

score=-Ending(deep,-beta,-alpha);

flag[deep]=0;

colour=3-colour;

returnscore;

}

//firstnote

turn(deep,MoveList[deep][MoveList[deep][0]]);

best=-Ending(deep-1,-beta,-alpha);

back(deep);

MoveList[deep][0]--;

//noteend

for(;MoveList[deep][0]>0;MoveList[deep][0]--){

if(best

if(best>alpha)

alpha=best;

turn(deep,MoveList[deep][MoveList[deep][0]]);

score=-BadEnding(deep-1,-alpha-1,-alpha);

if(score>alpha&&score

best=-Ending(deep-1,-65,-score);

elseif(score>best)

best=score;

back(deep);

}

}

colour=3-colour;

returnbest;

}

///eNDingeND

///search

intsearch(intdeep){

intscore;

note++;

MoveGenerator(deep);

//thefirstnote

turn(deep,MoveList[deep][MoveList[deep][0]]);

BestScore=-PVS(deep-1,-32767,32767);

BestMove=MoveList[deep][MoveList[deep][0]];

back(deep);

MoveList[deep][0]--;

//firstnoteend

for(;MoveList[deep][0]>0;MoveList[deep][0]--){

turn(deep,MoveList[deep][MoveList[deep][0]]);

score=-AlphaBeta(deep-1,-BestScore-1,-BestScore);

if(score>BestScore){

BestScore=-PVS(deep-1,-32767,-score);

BestMove=MoveList[deep][MoveList[deep][0]];

}

back(deep);

}

returnBestMove;

}

intAlphaBeta(intdeep,intalpha,intbeta){

intcurrent=-32767,score;

note++;

if(!

deep)

if(colour==WHITE)

return-G();

else

returnG();

colour=3-colour;

MoveGenerator(deep);

if(!

MoveList[deep][0]){//nomove

if(flag[deep])//gameover

if(colour==WHITE){

colour=3-colour;

returnG();

}else{

colour=3-colour;

return-G();

}

//让步……英语怎么说?

flag[deep]=1;

score=-AlphaBeta(deep,-beta,-alpha);

flag[deep]=0;

colour=3-colour;

returnscore;

}

for(;MoveList[deep][0]>0;MoveList[deep][0]--){

turn(deep,MoveList[deep][MoveList[deep][0]]);

score=-AlphaBeta(deep-1,-beta,-alpha);

back(deep);

if(score>current){

current=score;

if(score>=beta)

break;

}

}

colour=3-colour;

returncurrent;

}

intPVS(intdeep,intalpha,intbeta){

intbest,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 医药卫生 > 基础医学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1